题目
链接:https://ac.nowcoder.com/acm/contest/28886/1004
来源:牛客网时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld题目描述
在一个游戏中,tokitsukaze需要在n个士兵中选出一些士兵组成一个团去打副本。
第i个士兵的战力为v[i],团的战力是团内所有士兵的战力之和。
但是这些士兵有特殊的要求:如果选了第i个士兵,这个士兵希望团的人数不超过s[i]。(如果不选第i个士兵,就没有这个限制。)
tokitsukaze想知道,团的战力最大为多少。输入描述:
第一行包含一个正整数n(1≤n≤10^5)。 接下来n行,每行包括2个正整数v,s(1≤v≤10^9,1≤s≤n)。
输出描述:
输出一个正整数,表示团的最大战力。
示例1
输入
2 1 2 2 2
输出
3
示例2
输入
3 1 3 2 3 100 1
输出
100
题解
失误之处小分析1:
重大失误:从来不看数据范围
ans的数据范围应该是long long
代码
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
priority_queue<int,vector<int> ,greater<int> >q;
struct _p
{
int v;//战斗力
int s;//s[i]
};
_p p[100010];
bool cmp(_p x, _p y)
{
return x.s > y.s;
}
int main()
{
long long ans = 0;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int t1;
int t2;
cin >> t1 >> t2;
p[i].v = t1;
p[i].s = t2;
}
sort(p, p + n, cmp);
long long tmp = 0;
for (int i = 0; i < n; i++)
{
int min = p[i].s;
while (!q.empty() && q.size() > min)
{
tmp = tmp - q.top();
q.pop();
}
if (q.size() < min)
{
q.push(p[i].v);
tmp = tmp + p[i].v;
}
else if (p[i].v > q.top())
{
tmp = tmp - q.top();
q.pop();
q.push(p[i].v);
tmp = tmp + p[i].v;
}
ans = max(ans, tmp);
}
cout << ans;
return 0;
}