hdu5131 贪心

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<map>
#include<iostream>
using namespace std;
struct node
{
    char s[60];
    int v;
    int flag;
    int cou;
}a[300];
bool cmp(node fa,node fb)
{
    if(fa.v!=fb.v)
        return fa.v>fb.v;
    else return strcmp(fa.s,fb.s)<0;
}
int main()
{
    int n,m,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        map<string,int>mp;
        if(!n)break;
        for(i=0;i<n;i++)
            scanf("%s %d",a[i].s,&a[i].v);
        sort(a,a+n,cmp);
        int count=0,index=0;
        for(i=0;i<n;i++)
        {
            string ss="";
            int len=strlen(a[i].s);
            for(j=0;j<len;j++)
                ss+=a[i].s[j];
            mp[ss]=i;
            if(i==0)
            {
                a[i].flag=0;
                a[i].cou=1;
                count++;
                index=1;
            }
            else
            {
                if(a[i].v==a[i-1].v)
                {
                    a[i].flag=count;
                    a[i].cou=++index;
                }
                else
                {
                    a[i].flag=i+1;
                    count=i+1;
                    a[i].cou=1;
                    index=1;
                }
            }
        }
        for(i=0;i<n;i++)
            printf("%s %d\n",a[i].s,a[i].v);
        scanf("%d",&m);
        while(m--)
        {
            string ss;
            cin>>ss;
            if(a[mp[ss]].cou>1)
            {
                printf("%d %d\n",a[mp[ss]].flag,a[mp[ss]].cou);
            }
            else printf("%d\n",mp[ss]+1);
        }
    }
}

 

posted @ 2015-10-13 22:30  sweat123  阅读(132)  评论(0编辑  收藏  举报