QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#141271 | #6525. New Houses | cy1999 | TL | 0ms | 0kb | C++14 | 2.3kb | 2023-08-17 10:08:32 | 2023-08-17 10:08:35 |
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read() {
ll x = 0,f = 1;
char ch = getchar();
while(ch < '0' ||ch > '9') {
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ '0');
ch = getchar();
}
return x * f;
}
const int N = 5e5 + 10;
int t, n, m;
struct person{
ll x, y; bool flag;
}a[N], b[N];
bool cmp(person c, person d) {
return c.y - c.x > d.y - d.x;
}
//!!!!!!!!!!注意,前面已经统计了答案了
int main() {
t = read();
while (t--) {
n = read(), m = read(); ll ans = 0, arr = 0;
for (int i = 1; i <= n; i++) {
a[i].x = read(), a[i].y = read();
if (a[i].x >= a[i].y) {
ans += a[i].x, arr++, a[i].flag = 0;
}
else a[i].flag = 1;
}
if (arr >= 2) {
int tot = 0;
for (int i = 1; i <= n; i++) {
if (a[i].flag) b[++tot] = a[i];
}
sort(b + 1, b + tot + 1, cmp);
//int las = m - arr; int k = n - arr;
int sum = m - n;//las - k
for (int i = 1; i <= tot ; i++) {
if (sum > 0) {
sum--; ans += b[i].y;
} else {
ans += b[i].x;
}
}
printf("%lld\n",ans);
} else if (arr == 0) {//!!!!!!!!!!!!!!!!!!!!!!!test
int tot = 0;
for (int i = 1; i <= n; i++) {
if (a[i].flag) b[++tot] = a[i];
}
sort(b + 1, b + tot + 1, cmp);
//int las = m - arr; int k = n - arr;
int sum = m - n;//las - k
if (sum == n - 1) sum++;
for (int i = 1; i <= tot ; i++) {
if (sum > 0) {
sum--; ans += b[i].y;
} else {
ans += b[i].x;
}
}
printf("%lld\n",ans);
} else {
while(1);
ans = 0;
int tot = 0;
for (int i = 1; i <= n; i++) {
b[++tot] = a[i];
}
sort(b + 1, b + tot + 1, cmp);
//int las = m - arr; int k = n - arr;
int sum = m - n;//las - k
if (sum == n - 1) sum++;
for (int i = 1; i <= tot ; i++) {
if (sum > 0) {
sum--; ans += b[i].y;
} else {
ans += b[i].x;
}
} ll anss = ans;
ans = b[tot].x + b[tot-1].x; tot -= 2;
sum = m - n;
for (int i = 1; i <= tot ; i++) {
if (sum > 0) {
sum--; ans += b[i].y;
} else {
ans += b[i].x;
}
} anss = max(anss, ans);
printf("%lld\n",anss);
}
}
return 0;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
3 4 5 1 100 100 1 100 1 100 1 2 2 1 10 1 10 2 3 100 50 1 1000