Codeforces Round #287 (Div. 2)A(排序)

来源:互联网 发布:js获取页面高度赋给div 编辑:程序博客网 时间:2024/06/05 16:58

题目链接:http://codeforces.com/contest/507/problem/A


解题思路:

排个序就出来了。


完整代码:

#include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#include <string>#include <cmath>using namespace std;typedef long long LL;const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const double EPS = 1e-9;const double PI = acos(-1.0); //M_PI;struct node{    int w , id;}k[10001];bool cmp(node a , node b){    return a.w < b.w;}int res[10001];int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    std::ios::sync_with_stdio(false);    std::cin.tie(0);    int n , key;    while(cin >> n >> key)    {        for(int i = 1 ; i <= n ; i ++)        {            cin >> k[i].w;            k[i].id = i;        }        memset(res , 0 , sizeof(res));        sort(k + 1 , k + n + 1 , cmp);        int cnt = 0;        for(int i = 1 ; i <= n ; i ++)        {            if(key >= k[i].w)            {                key -= k[i].w;                res[cnt++] = k[i].id;            }            if(key <= 0)                break;        }        cout << cnt << endl;        if(cnt == 0)            continue;        for(int i = 0 ; i < cnt - 1 ; i ++)            cout << res[i] << " ";        cout << res[cnt - 1] << endl;    }}


0 0
原创粉丝点击