Sagheer and Nubian Market

本题不使用二分查找必然TLE。 注意二分查找的使用!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
using namespace std;
long long pri[100005], now[100005];
int main()
{
long long n, mon;
cin >> n >> mon;
for (int i = 0; i < n; i++)
cin >> pri[i];
long long l = 1, h = n, res = 0, s = 0, mid;
while (l <= h)
{
mid = (l + h) / 2;
long long sum = 0;
for (int j = 0; j < n; j++)
{
now[j] = pri[j] + (j + 1) * mid;
}
sort(now, now + n);
for (int j = 0; j < mid; sum += now[j++]);
if (sum <= mon)
{
res = mid;
s = sum;
l = mid + 1;
}
else
{
h = mid - 1;
}
}
cout << res << ' ' << s << endl;
return 0;
}