请佬们看看我的思路哪里错了,一直不通过
  • 板块CF1978D Elections
  • 楼主Auga
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/15 12:59
  • 上次更新2024/7/15 14:26:42
查看原帖
请佬们看看我的思路哪里错了,一直不通过
1334948
Auga楼主2024/7/15 12:59

我的思路是:先把一开始没投的c张票给第一个人,然后遍历整个成员a数组,然后想要第i个成员获胜,那么我可以把i编号之前的成员全部不允许参加,这样i就是最小编号的成员了,前面的所有的票都给了第i个成员;此时我把i和最大的票数比较,如果大于等于max,那就说明答案是i-1,否则就是i(把max的编号也去掉)

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
const int N = 2e5 + 10;

void solve(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int n, c;
    std::cin >> n >> c;
    int a[N], sum[N];
    for (int i = 1; i <= n; i++) std::cin >> a[i];
    a[1] += c;
    sum[1] = a[1];
    for (int i = 2; i <= n; i++){
        sum[i] = a[i] + sum[i - 1];
    }
    int *max = std::max_element(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++){
        if (a[i] == *max){
            std::cout << 0 << std::endl;
            return;
        }
        int person_num = i-1;
        if (sum[i] < *max){
            std::cout << person_num+1 << std::endl;
            return;
        }
        else{
            std::cout << person_num << std::endl;
            return;
        }
    }
}

int main()
{
    int t;
    std::cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
2024/7/15 12:59
加载中...