ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#215265 | #2639. 省钱 | ThySecret | 100 | 504ms | 3760kb | C++11 | 1.8kb | 2024-11-27 21:15:07 | 2024-11-27 23:39:22 |
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define DEBUG
#define x first
#define y second
#define File(a) freopen(a".in", "r", stdin); freopen(a".out", "w", stdout)
typedef long long ll;
typedef pair<int, int> PII;
const int N = 50005;
const int INF = 0x3f3f3f3f;
struct Node
{
int idx, val;
bool operator < (const Node &u) const { return val > u.val; };
};
int n, k, m, sum;
PII a[N];
bool vis[N];
priority_queue<int, vector<int>, greater<int>> heap;
priority_queue<Node> cost, disc;
bool cmp(PII a, PII b) { return a.y < b.y; }
signed main()
{
// ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k >> m;
for (int i = 1; i <= n; i ++)
cin >> a[i].x >> a[i].y;
sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= k; i ++)
{
sum += a[i].y;
if (sum > m)
return !printf("%d\n", i - 1);
// vis[i] = true;
heap.push(a[i].x - a[i].y);
#ifdef DEBUG
cout << sum << ' ';
#endif
}
for (int i = k + 1; i <= n; i ++)
{
cost.push({i, a[i].x});
disc.push({i, a[i].y});
}
for (int i = k + 1; i <= n; i ++)
{
#ifdef DEBUG
cout << sum << ' ';
#endif
while (!cost.empty() && vis[cost.top().idx]) cost.pop();
while (!disc.empty() && vis[disc.top().idx]) disc.pop();
int costidx = INF, discidx = INF, par1 = INF, par2 = INF;
if (!cost.empty()) costidx = cost.top().idx, par1 = cost.top().val;
if (!disc.empty()) discidx = disc.top().idx, par2 = disc.top().val + heap.top();
if (par1 > par2)
{
sum += par2;
disc.pop(); heap.pop();
heap.push({a[discidx].x - a[discidx].y});
vis[discidx] = true;
}
else
{
sum += par1;
cost.pop();
vis[costidx] = true;
}
if (sum > m)
return !printf("%d\n", i - 1);
}
cout << n << '\n';
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 48ms
memory: 2240kb
input:
50000 30828 852557364841 682084050 257603011 870868024 517458094 732267860 201407488 777566656 55879...
output:
17903
result:
ok single line: '17903'
Test #2:
score: 10
Accepted
time: 52ms
memory: 3584kb
input:
50000 10508 8982273367520 34111224 12372852 549875017 525549262 357107918 219952140 644308048 222008...
output:
37621
result:
ok single line: '37621'
Test #3:
score: 10
Accepted
time: 37ms
memory: 2148kb
input:
50000 23114 535861686266 359271294 298114231 605400720 491693949 755566780 539381575 155586610 92962...
output:
14723
result:
ok single line: '14723'
Test #4:
score: 10
Accepted
time: 43ms
memory: 3604kb
input:
50000 13490 4616703243118 286358449 133228996 162995754 17235506 661390160 561824344 282751480 15433...
output:
30961
result:
ok single line: '30961'
Test #5:
score: 10
Accepted
time: 49ms
memory: 3204kb
input:
50000 26352 8630976119100 70133466 32927792 90392510 89764542 307782646 75889114 123168574 66039130 ...
output:
42944
result:
ok single line: '42944'
Test #6:
score: 10
Accepted
time: 44ms
memory: 2148kb
input:
50000 11800 213255455323 405512104 311547645 122797690 35257030 782246460 533338866 416860264 504733...
output:
9869
result:
ok single line: '9869'
Test #7:
score: 10
Accepted
time: 49ms
memory: 3168kb
input:
50000 19734 4267681411347 732638120 327229436 361949068 274173372 539440696 285784669 94445920 84513...
output:
32498
result:
ok single line: '32498'
Test #8:
score: 10
Accepted
time: 47ms
memory: 3752kb
input:
50000 254 18445304121 375481124 36148026 388507104 183081259 261838134 179691990 485282800 209534680...
output:
1564
result:
ok single line: '1564'
Test #9:
score: 10
Accepted
time: 52ms
memory: 3760kb
input:
50000 3260 4076050769242 210627773 8756794 68253913 5333287 812306900 176444281 561388618 94960450 6...
output:
23173
result:
ok single line: '23173'
Test #10:
score: 10
Accepted
time: 83ms
memory: 2624kb
input:
50000 44485 12129791734731 590854222 262410600 992399148 641692708 219274382 56485932 730651726 4088...
output:
49537
result:
ok single line: '49537'