代码强制"The New President "

Codeforces "The New President "

本文关键字:New President The 代码      更新时间:2023-10-16

我试图回答写一个代码来解决这个问题,但在测试15中我仍然得到了错误的答案,我不知道我的代码中缺少了什么。我尝试了很多测试用例,但代码都正确地解决了它们。

我的代码:

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
    int c; cin >> c;
    int v; cin >> v;
    if (c == 1 && v == 0)
    {
        cout << 1 << " " << 1;
    }
    else 
    {
        int cArray[c + 1];
        int voting[v][c];
        for (int j = 0; j<v; j++)
        {
            for (int z = 0; z<c; z++)
            {
                int temp; cin >> temp;
                voting[j][z] = temp;
            }
        }
        for (int j = 0; j <= c; j++)cArray[j] = 0;
        for (int j = 0; j<v; j++)cArray[voting[j][0]]++;
        int maxim = 0;
        int maxN = 0;
        int count = 0;
        map<int, int > cand;
        for (int j = 1; j <= c; j++)
        {
            if (cArray[j]>maxN)
            {
                cand.clear();
                cand[j] = 1;
                maxN = cArray[j];
                maxim = j;
                count = 0;
            }
            else if (cArray[j] == maxN)
            {
                cand[j] = 1;
                count++;
            }
        }
        if (count == 0)
            cout << maxim << " " << 1;
        else 
        {
            for (int j = 0; j<v; j++)
            {
                for (int z = 1; z<c; z++) 
                {
                    if (cand.count(voting[j][z])) 
                    {
                        cArray[voting[j][z]]++;
                        break;
                    }
                }
            }
            maxim = 0;
            maxN = 0;
            count = 0;
            for (int j = 1; j <= c; j++)
            {
                if (cArray[j]>maxN)
                {
                    maxN = cArray[j];
                    maxim = j;
                    count = 0;
                }
                else if (cArray[j] == maxN)
                {
                    count++;
                }
            }
            cout << maxim << " " << 2;
        }
    }
    return 0;
}

您检查第一轮(获胜或前两名候选人)的算法似乎是错误的。看起来你希望前两名候选人的初选票数相同——事实并非如此。你想选出排名前两位的候选人,如果得票率超过50%,排名前一位的候选人就会获胜。

我不想给你答案(因为这是练习的重点),但你需要重新思考如何处理第一部分投票。

还要注意的是,一旦有人投票给了前两名候选人中的一名,他们的二次投票就不应该计入另一名候选人(你目前正在这样做)。