NOJ [1118] Marisa's Affair

来源:互联网 发布:php分类源码 编辑:程序博客网 时间:2024/05/16 01:28
  • 问题描述
  • Everyone knows that Kirisame Marisa(きりさめ·まりさ) often has affaires with other witches in TouHou.
    For example, she has an affair with Reimu today and tomorrow is Flandre.
    You have to make clear that how many witches she has affairs with.

  • 输入
  • This problem has several cases.
    The first line of each case contains an integer N(0 < N <= 5000).
    Then follows N lines. The ith has a name NAMEi and an integer Hi(0 < Hi < 20000), means at the ith day Marisa has an affair with NAMEi, and NAMEi increases Hi likability.
    You can consider that every name only contains uppercase and lowercase letters, and not longer then 20;
  • 输出
  • For each case, you should output like that:
    At the first line, you should output how many witches Marisa has affaires with;
    Then each line output like that:
    The name of the witch, the likability with that witch and the time(s) they have affaires. Order by likability descending.
    If their likabilities are the same, then order by affair times descending.
    If their likabilities and affair times are the same, then order by lexicographical order increasing.

    看懂题之后,其实这是一道水题


    上代码
    #include<stdio.h>#include<iostream>#include<map>#include<string.h>#include<algorithm>using namespace std;struct affair{char name[25];long long  likability,times;}witches[5010];char temp[22];int cmp(affair a,affair b){if(a.likability != b.likability)return a.likability > b.likability;else{if(a.times != b.times)return a.times > b.times;elsereturn strcmp(a.name,b.name)<0;}}int main(){int n,i;while(~scanf("%d",&n)){long long d, k=1;map<string,int>affairs;//affairs.clear();for(i=0;i<=n;i++){witches[i].likability=0;witches[i].times=0;}for(i=0;i<n;i++){scanf("%s %lld",temp,&d);if(affairs[temp]==NULL){affairs[temp]=k;strcpy(witches[k].name,temp);witches[k].times=1;witches[k].likability+=d;k++;}else{witches[affairs[temp]].likability+=d;witches[affairs[temp]].times++;}}sort(witches+1,witches+k,cmp);printf("%d\n",k-1 );for(i=1;i<k;i++)printf("%s %lld %lld\n",witches[i].name,witches[i].likability,witches[i].times);}return 0;}


0 0