[Virtual Judge]SGU171:Sarov zones

来源:互联网 发布:在数据库储存的是 编辑:程序博客网 时间:2024/04/30 12:33

点击打开题目链接

171. Sarov zones

time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard
output: standard



It is known that to participate the All-Russian Mathematic Olympiad one should do one of other olympiads enough good. These olympiads are called "zone olympiads" and the region from which people participate the same zone olympiad is called "zone". Sarov city of Nizhny Novgorod district is situated near the boundary of zones, so every year school students of Sarov participate several zones. 

This year K zones invited students from Sarov to participate their olympiads. i-th zone invited N[i] students, so N=N[1]+N[2]+...+N[K] totally students were invited. After the Sarov city olympiad, N students were selected, so now the olympiad authorities of Sarov have to decide, which students will participate which zone olympiad. Each student is characterized by his olympiad level and each zone is characterized by its zone level. If a student with olympiad level P participates zone with zone level Q, he will be invited to All-Russian olympiad if and only if P>Q. 

Every student is also characterized by his "weight". The Sarov olympiad authorities want, that the All-Russian Olympiad will be participated by students with maximal summary weight. You are to help them. Write a program which will divide the students between zones. Keep in mind that exactly N[i] students can go to i-th zone.

Input
On the first line of input there is an only integer K (1<=K<=100). On the second line there are K integers N[1], N[2], ... ,N[K] (0<=N[i]<=16000). On the third line there are K more integers Q[1], Q[2], ... ,Q[K] --- the zone levels of the zones. On the fourth line there are N integers P[1], P[2], ... P[N] --- olympiad levels of the students. (0<=Q[i]<=1000000, 0<=P[i]<=1000000). On the last (and fifth) line there are N integers w[1], w[2], ... w[k] --- the "weights" of students. 0<=w[i]<=100000. It is also guaranteed that 0<=N<=16000.

Output
Output only N integers --- Z[1], Z[2], ... Z[N] --- the numbers of zones which should be participated by students 1, 2, ... N.

Sample test(s)

Input
2 1 1 4 1 2 3 2 1
Output
2 1

=====================================题目大意=====================================


参加俄罗斯的数学奥林匹克全国赛是需要先参加区域赛的,因此每年XXX州都会有许多学生为了参加全国赛而先去参加各地的区域赛。

今年共有K个区域举办比赛且编号为i的区域给予了XXX州N[i]名的参赛名额,因此XXX州今年将有N=N[1]+N[2]+...N[K]名学生参赛。

各区域赛和各学生都有自己的等级,当参赛学生等级大于他所参加的区域赛等级时,他将会受到全国赛的邀请。

此外还给出了N名参赛学生的体重,XXX州的当局者希望自己州能够参加全国赛的学生的总体重最大(真不知道他是怎么想的)。

请编程帮助他决定各参赛学生应该前往哪个区域参赛。


=====================================算法分析=====================================


贪心算法:


1、优先保证体重大的学生能够参加比自己等级低的区域赛以使其受到全国赛的邀请(满足题目所需)。

2、当体重相等时,不需要优先考虑等级低的学生(第3点的存在保证了等级高的学生不会占用与其体重相等的等级低的学生的

   参赛机会)。

3、当前考虑的学生应该在等级比自己低的区域赛中选择等级最高的区域赛(为了给之后的学生提供更大的机会)。

4、若不存在比当前考虑的参赛学生等级低的区域赛,则应该让他参加当前可以参加的等级最高的区域赛(最大限度降低他所占

   用的机会)。


========================================坑========================================


前言:“惨象,以使我目不忍视了,我还有什么话好说呢?” 


根据题意:On the first line of input there is an only integer K (1<=K<=100).

写有代码:struct THEZONE { int ID,LV,N; } Zon[105];

提交结果:Runtime Error on test 8 X 14次

修改代码:struct THEZONE { int ID,LV,N; } Zon[16005];

提交结果:Accepted

此外还有:输出答案时每个数字后面都要跟空格,否则会Presentation Error。


查错依据:RE的错误原因是在对比了渊哥Runtime Error on test 8和Accepted的两份代码后得知的。

          PE的错误原因是在做之前就看了freezhan学姐的博客得知的(也是一把辛酸的PE史)。


总结:本坑就是我CSUST集训队用血肉之躯填的,还不知道是不是填平了。。。

 

=======================================代码=======================================

 


#include<cstdio>  #include<algorithm>using namespace std;int K,SumN,Ans[16005];struct THEZONE { int ID,LV,N; } Zon[16005];struct STUDENT { int ID,LV,W; } Stu[16005];bool cmpzon(THEZONE& Z1,THEZONE& Z2)                        {return (Z1.LV>Z2.LV);}bool cmpstu(STUDENT& S1,STUDENT& S2)                       {return (S1.W>S2.W);}bool ReadAndDealData()                                      {if(scanf("%d",&K)==1){SumN=0;int i;for(i=0;i<K;++i){Zon[i].ID=i;scanf("%d",&Zon[i].N);SumN+=Zon[i].N;}for(i=0;i<K;++i){scanf("%d",&Zon[i].LV);}//按照等级降序排列区域sort(Zon,Zon+SumN,cmpzon);                          for(i=0;i<SumN;++i){Stu[i].ID=i;scanf("%d",&Stu[i].LV);}for(i=0;i<SumN;++i){scanf("%d",&Stu[i].W);}//按照重量降序排列学生(题解第1点和第2点)sort(Stu,Stu+SumN,cmpstu);                          return true;}return false;}int main(){while(ReadAndDealData()){int i;for(i=0;i<SumN;++i)                                 {int maxlvloc=-1;                                int hopeloc=-1;//在等级比当前考虑的学生低的区域中选择等级最高的区域参赛(题解第2点)                                 for(int j=0;j<K;++j) if(Zon[j].N)               {        //由于区域按照等级降序排列,所以maxlvloc即当前可以参加的等级最高的区域赛                   if(maxlvloc==-1)                            { maxlvloc=j; } //由于区域按照等级降序排列,所以hopeloc即在等级比当前考虑的学生低的区域中等级最高的区域if(Stu[i].LV>Zon[j].LV) { hopeloc=j; break;    }}//若不存在hopeloc则应该让当前考虑的学生参加maxlvloc(题解第4点) int loc=(hopeloc==-1?maxlvloc:hopeloc);     --Zon[loc].N;Ans[Stu[i].ID]=Zon[loc].ID;}for(i=0;i<SumN;++i){printf("%d ",Ans[i]+1);}}return 0;}

原创粉丝点击