AGC027 A - Candy Distribution Again
Posted sssy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AGC027 A - Candy Distribution Again相关的知识,希望对你有一定的参考价值。
目录
题目链接
AGC027 A - Candy Distribution Again
题解
贪心即可
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#define gc getchar()
#define pc putchar
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1; char c = getchar();}
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-');
x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0');
}
const int maxn = 100007;
int a[maxn];
int main() {
int n = read(),k = read();
for(int i = 1;i <= n;++ i) a[i] = read();
std::sort(a + 1, a + n + 1);
int ans = 0;
for(int i = 1;i <= n;++ i) {
if(i < n) { if(a[i] <= k ) k -= a[i],ans ++;}
else if(i == n && a[i] == k) ans ++;
else break;
}
print(ans);
pc('
');
}
以上是关于AGC027 A - Candy Distribution Again的主要内容,如果未能解决你的问题,请参考以下文章
[AtCoder AGC27A]Candy Distribution Again