[uva-10142] Australian Voting (queue&vector)

今天下午到晚上才AC,
遇到了一些问题有:
1.runtime error 没有考虑只有一个候选人
2.compile error 没有加
3.WA的情况分别是
1)选票废掉之后选的还是淘汰的人
2)超过百分之五十写的>=…..
3)科科。必须按选号从小到大不能乱序搞TAT
4.系统一直报warning应该是还要把size转下int,不过我懒,警告就警告吧。

真的是很好的一道题你看
让我尝试了几乎所有的报错!
MMP

这里写图片描述

吃凉皮凉皮去了
何以解忧唯有暴饮暴食


#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
struct candidate
{
string name;
int votingnum;
int tot;
candidate(string a="",int b=0,int c=0)
{name=a,votingnum=b,tot=c;}
};

bool cmp(candidate A, candidate B)
{
    if(A.tot!=B.tot)
        return A.tot<B.tot;
    return A.votingnum<B.votingnum;
}

vector < queue<int> > ballot;
vector <candidate> Candi;
vector <candidate> Out;
int BallotTot,n;

bool judge()
{
    int siz=Candi.size();
    int DeleTot;

    if(2*Candi[siz-1].tot>BallotTot)
            return true;

    DeleTot=Candi[0].tot;
    while(!Candi.empty()&&Candi[0].tot==DeleTot)
    {
        Out.push_back(Candi[0]);
        Candi.erase(Candi.begin());
    }
    if(Candi.empty())return true;
    return false;
}

int main()
{
    int t,i,j;
    bool flag=false;
    string input;
    cin>>t;
    while(t--)
    {
        cin>>n;
        getline(cin,input);
        Candi.clear();
        ballot.clear();
        ballot.resize(1100);
        Out.clear();
        for(i=1;i<=n;i++)
        {
            getline(cin,input);
            Candi.push_back(candidate(input,i,0));
        }

        BallotTot=0;
        while(getline(cin,input))
        {
            if(input=="")break;
            int len=input.length();
            int num=0;
            for(i=0;i<len;i++)
            {
                if(input[i]>='0'&&input[i]<='9')
                    {
                        num=num*10+(input[i]-'0');
                        if(i==len-1)
                            ballot[BallotTot].push(num);
                    }
                else
                {
                    ballot[BallotTot].push(num);
                    num=0;
                }
            }BallotTot++;
        }

        for(i=0;i<BallotTot;i++)
        {
            int x=ballot[i].front();
            Candi[x-1].tot++;
        }
        sort(Candi.begin(),Candi.end(),cmp);
        while(!judge())
        {
            while(!Out.empty())
            {
                for(i=0;i<ballot.size();i++)
                {
                    if(!ballot[i].empty()&&ballot[i].front()==Out[0].votingnum)
                    {
                        ballot[i].pop();
                        int x=ballot[i].front();
                        for(j=0;j<Candi.size();j++)
                        {
                            if(Candi[j].votingnum==x)
                            {
                             Candi[j].tot++;break;
                            }
                            //废掉的选票上变成了是淘汰的候选人
                            if(j==Candi.size()-1&&ballot[i].size()>1)
                            {
                                ballot[i].pop();
                                x=ballot[i].front();
                                j=-1;
                            }
                        }
                    }
                }Out.erase(Out.begin());
            }sort(Candi.begin(),Candi.end(),cmp);
        }
        int siz=Candi.size();

        //空行
        if(flag)cout<<endl;
        else flag=true;
        //输出
        if(siz==0)
        {
            for(i=0;i<Out.size();i++)
                cout<<Out[i].name<<endl;
        }
        else
        {
            cout<<Candi[siz-1].name<<endl;
        }
    }
    return 0;
}   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值