HDU 3412 杭州省赛 An Odd Award Rule

来源:互联网 发布:mac qq截屏 编辑:程序博客网 时间:2024/05/16 04:39

An Odd Award Rule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 96    Accepted Submission(s): 44

Problem Description
The education of primary school in China is a big problem now. A teacher must be very careful not only when he/she is criticizing the students, but also when he/she is giving awards to good students. Teacher Liu always gave the top ten students on his examination some little awards before, but some parents are a little bit angry about this now. They say that their little kids may get hurt because they will never get the awards. Teacher Liu has to change his award rule. He wants all students have a chance to win the awards, no matter their scores are good or poor. But he still wants good students to get more chance. So the new rule seems a little bit odd: anyone whose score equals to the sum of the scores of OTHER 3 or 2 students, will win the award. Now figuring out who is qualified for the awards seems a little bit hard for Teacher Liu. As the monitor of his class and a little programmer, you should help him to do this.
 

 

Input
For each test case, first print an integer in a line, indicating how many students win the awards. Then print the names of those who win the awards in alphabetic order, each name in a line.
 

 

Output
For each test case, print one line containing an integer which equals to the formula’s value mod P.
 

 

Sample Input
15SIKE 12WORRY 20LUCENT 8KILI 3TOM 1
 

 

Sample Output
2SIKEWORRY
 

 

Source
2010 National Programming Invitational Contest Host by ZSTU
 

 

Recommend
wxl

 

 

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct T
{
    string name;
    int s;
}t[200];
string stu[200];
bool flag;
int n;
bool cmp(T a,T b)
{
    return a.s>b.s;
}
bool cmp2(string a,string b)
{
    return a<b;
}
void dfs(int x,int sum,int step)
{
    if(sum==0)
    {
        if(step==2||step==3)  flag=true;
        return;
    }
    if(step==3&&sum!=0)   return;
    if(sum<t[n-1].s)  return;
    if(x+1>=n)   return;
    for(int i=x+1;i<n;i++)
    {
        if(flag)   return;
        if(sum-t[i].s>=0&&step+1<=3)   dfs(i,sum-t[i].s,step+1);
    }
    return;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)   cin>>t[i].name>>t[i].s;
        sort(t,t+n,cmp);
        int k=0;
        for(int i=n-3;i>=0;i--)
        {
            flag=false;
            dfs(i,t[i].s,0);
            if(flag)  stu[k]=t[i].name,k++;
        }
        sort(stu,stu+k,cmp2);
        cout<<k<<endl;
        for(int i=0;i<k;i++)
        cout<<stu[i]<<endl;
    }
    return 0;
}

原创粉丝点击