猜名次(Guess, ACM/ICPC Beijing 2006, UVa1612)

来源:互联网 发布:d3.js 难 编辑:程序博客网 时间:2024/06/05 08:43
贪心
题目描述:有n个选手,每个选手做三个题。对了得相应得分,错了不得分,问能否最终得出给你的那个排名。排名规则是:分高的排名高(名次号码低),分数相同的话,ID小的排名高。
题目分析:按照给出的排名对应的选手处理相应的数据,第一名一定是三题全对,其后的人分数不能大于上一名,或者是分数相同,其ID要大于上一名。还有一个关键,对浮点数的处理,这很关键。

代码如下:

#include <iostream>#include <cstdio>#include <vector>#include <algorithm>#include <iomanip>#include <cmath>using namespace std;struct player//定义一个选手{    int m_id;//选手的ID    double pro[3];//选手的三个成绩    player(int id,double p1,double p2,double p3):m_id(id)    {        pro[0]=p1;        pro[1]=p2;        pro[2]=p3;    }    player(){}};const int maxn=20000;vector <player> people;int ranked[maxn],n;//排名double res;bool solve(){    double maxx=people[ranked[0]-1].pro[0]+people[ranked[0]-1].pro[1]+people[ranked[0]-1].pro[2],sum;//第一名的分数    for(int i=1;i<n;i++)    {        sum=0;        for(int j=0;j<3;j++)        {            if(sum+people[ranked[i]-1].pro[j]>=maxx+1e-6||fabs(sum+people[ranked[i]-1].pro[j]-maxx)<1e-6&&people[ranked[i]-1].m_id<people[ranked[i-1]-1].m_id)            {//表示如果分数大于上一名,或者是等于上一名,且其ID小于上一名,不符合要求。                sum+=0;            }            else sum+=people[ranked[i]-1].pro[j];        }        if(people[ranked[i]-1].pro[1]+people[ranked[i]-1].pro[2]<maxx-1e-6||fabs(people[ranked[i]-1].pro[1]+people[ranked[i]-1].pro[2]-maxx)<1e-6&&people[ranked[i]-1].m_id>people[ranked[i-1]-1].m_id)        {//还需考虑,如果1题选了,但再选2或3会超的情况,但不能确定选2,3是否大于只选1.            sum=max(sum,people[ranked[i]-1].pro[1]+people[ranked[i]-1].pro[2]);        }        maxx=sum;        if(i!=(n-1)&&fabs(maxx-0)<1e-6&&people[ranked[i]-1].m_id<people[ranked[i-1]-1].m_id) return false;    }    res = maxx;    return true;}int main(){//    freopen("in.txt","r",stdin);//    freopen("out.txt","w",stdout);    int kase=0;    while(cin >> n&&n)    {        people.clear();        double p1,p2,p3,sum;        for(int i=0;i<n;i++)        {            scanf("%lf %lf %lf",&p1,&p2,&p3);            player temp(i+1,p1,p2,p3);            sort(temp.pro,temp.pro+3,greater<double>());            people.push_back(temp);        }        for(int i=0;i<n;i++)        {            scanf("%d",&ranked[i]);        }        printf("Case %d: ",++kase);        if(!solve()) cout << "No solution" << endl;        else cout <<  fixed << setprecision(2) <<res << endl;    }    return 0;}

WA两次一次是忽略了,2,3是否大于1的情况,另一次是没有处理浮点数。这很关键,处理了浮点数,正确答案立刻就出来了。浮点数不能忘记处理啊。


0 0