杂题专项:UVa 10670

来源:互联网 发布:正道集团 知乎 编辑:程序博客网 时间:2024/05/21 06:01

一道贪心,即在花费小于减的情况下尽量除。注意最后剩下exactly m,不能小于m。另外因为输出的限制,所以建立一个结构体并用algorithm里的sort可以方便的满足要求。

#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;#define M 110struct agency{    char name[20];    int cost;    bool operator<(const agency& tmp) const    {        return cost<tmp.cost||cost==tmp.cost&&strcmp(name,tmp.name)<0;    }} a[M];int n,m,l;int main(){    freopen("in.txt","r",stdin);    int T;    int kase=1;    cin>>T;    while(T--)    {        cin>>n>>m>>l;        int A,B;        for(int i=0;i<l;i++)        {            char c;            int len=0;            while(c=getchar())            {                if(c==':') break;                if(!isupper(c)) continue;                a[i].name[len++]=c;            }            a[i].name[len]='\0';            scanf("%d,%d",&A,&B);            int s=n;            a[i].cost=0;            while(s/2>=m&&(s-s/2)*A>=B)            {                a[i].cost+=B;                s/=2;            }            a[i].cost+=(s-m)*A;        }        sort(a,a+l);        cout<<"Case "<<kase++<<endl;        for(int i=0;i<l;i++)        {            cout<<a[i].name<<" "<<a[i].cost<<endl;        }    }    return 0;}


原创粉丝点击