Pat 1055 first k

Source: Internet
Author: User

The second group of data is relatively large. If it is simply sorted, direct retrieval times out because all data is traversed each time.

N/200 = 500, indicating that up to 500 people of the same age can be found, while M = 100 is relatively small, meaning that all people of the same age after 100 will not be found.

 

#include<iostream>#include<cstring>#include<cstdio>#include <algorithm>using namespace std;struct node{char name[10];int age,worth;}a[100005];int b[20005];int c[205];bool cmp2(const node& p,const node& q){if(p.worth==q.worth){if (p.age==q.age){return strcmp(p.name,q.name)<=0;}return p.age<q.age;}return p.worth>q.worth;}int main(){int n,q,i,M,ma,mb,txt=1,len;while(~scanf("%d %d",&n,&q)){memset(c,0,sizeof(c));for (i=0;i<n;++i){scanf("%s%d%d",a[i].name,&a[i].age,&a[i].worth);}sort(a,a+n,cmp2);for (len=i=0;i<n;++i){if (c[a[i].age]<=100){b[len++]=i;c[a[i].age]++;}}// printf("----\n");// for (i=0;i<n;++i){// printf("%s %d %d\n",a[b[i]].name,a[b[i]].age,a[b[i]].worth);// }while(q--){scanf("%d%d%d",&M,&ma,&mb);if(ma>mb){ma^=mb;mb^=ma;ma^=mb;}printf("Case #%d:\n",txt++);int cnt=0;for (i=0;i<len&&cnt<M;++i){if(a[b[i]].age>=ma&&a[b[i]].age<=mb){printf("%s %d %d\n",a[b[i]].name,a[b[i]].age,a[b[i]].worth);cnt++;}}if(cnt==0){printf("None\n");}}}return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.