ACM 之 B - Super Mobile Charger

Description

While HIT ACM Group finished their contest in Shanghai and is heading back Harbin, their train was delayed due to the heavy snow. Their mobile phones are all running out of battery very quick. Luckily, zb has a super mobile charger that can charge all phones.
There are N people on the train, and the i-th phone has p[i] percentage of power, the super mobile charger can charge at most M percentage of power.
Now zb wants to know, at most how many phones can be charged to full power. (100 percent means full power.)

Input

The first line contains an integer T, meaning the number of the cases (1 <= T <= 50.).
For each test case, the first line contains two integers N, M(1 <= N <= 100,0 <= M <= 10000) , the second line contains N integers p[i](0 <= p[i] <= 100) meaning the percentage of power of the i-th phone.

Output

For each test case, output the answer of the question.

Sample Input

2
3 10
100 99 90
3 1000
0 0 0

Sample Output

2
3

理解:

你有一个移动电源,现在看你最多能给多少人的手机充满电。
简单排序就好。

代码部分

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int t,n,m,a[101],sum,all;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    cin>>t;
    while(t--){
        sum=0;
        memset(a,0,sizeof(a));
        cin>>n>>m;
        all=m;
        for(int i=0;i<n;i++)
            cin>>a[i];
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++){
            if(a[i]==100)
            {sum++;continue;}
            if(a[i]<100){
                if(all>=(100-a[i])){
                    all-=(100-a[i]);
                    a[i]=100;
                    sum++;
                    continue;
                }
                if(all<(100-a[i])){
                    break;
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

意见反馈 || 任何建议

联系我(新浪)
邮箱:qianlizhihao@gmail.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容