由于本题的数据范围非常大,因此本题用到了二分答案。
直接上代码解释了
#include <iostream>
#include <queue>
using namespace std;
deque<int>q;
const int N = 1e5 + 5;
long long n, k, a, b, c, d, x, y, z, sum, l, r, mid, ans, p[N];
int check(long long x)判断是否能执行这么多轮循环
{
long long sum = 0;
for (int i = 1; i <= n; i++)
sum += min(p[i], x);
if (sum > k)
return 1;
else
return 0;
}
int main()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> p[i];
sum += p[i];
}
if (sum < k)
cout << "-1" << '\n';
else if (sum == k)
cout << '\n';
else
{
l = 0, r = 1e14;
while (l <= r)//二分答案
{
mid = (l + r) / 2;
if (check(mid))
{
r = mid - 1;
}
else
{
l = mid + 1;
ans = mid;
}
}
sum = 0;
for (int i = 1; i <= n; i++)
{
sum += min(p[i], ans);
p[i] -= min(p[i], ans);
}
k -= sum;//减掉sum后只需执行一轮循环
for (int i = 1; i <= n; i++)//这里使用队列来判断先后顺序
{
if (p[i] > 0)
q.push_back(i);
}
while (k--)
{
p[q.front()]--;
if (p[q.front()] > 0)
q.push_back(q.front());
q.pop_front();
}
while (!q.empty())
{
cout << q.front() << " ";
q.pop_front();
}
}
return 0;
}