代码强制"The New President "
Codeforces "The New President "
我试图回答写一个代码来解决这个问题,但在测试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%,排名前一位的候选人就会获胜。
我不想给你答案(因为这是练习的重点),但你需要重新思考如何处理第一部分投票。
还要注意的是,一旦有人投票给了前两名候选人中的一名,他们的二次投票就不应该计入另一名候选人(你目前正在这样做)。
相关文章:
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 重载运算符new[]的行为取决于析构函数
- 过载'operator new'如何导致无限循环?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- Codelite C++ new project
- 在类c++中使用new声明数组
- g++用户定义的动态链接库上的全局new和delete运算符
- 如果整个应用程序是虚拟映射的,为什么 new 会进行系统调用?
- 在将 new 与指针一起使用时,创建数组的指定长度
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- 为什么我的全局 new() 覆盖被绕过了?
- 声明C++数组(带或不带 "new" 关键字)
- 在C++中,如果我可以直接将整数分配给指针而不使用"new",为什么要使用"new"?
- 是否可以使用 new 指定具有宏常量的动态分配数组的元素?
- constexpr new 如何分配内存?
- 放置 new 和 uninitialized_fill() 的行为
- 使用 new 和 值进行数组初始化,但没有显式数量的元素
- different between int **arr =new int [ n]; and int a[i][j]?
- 代码强制"The New President "