CF1490E Accidental Victory(人话:冠军赛)

11 篇文章 0 订阅
3 篇文章 0 订阅

Accidental Victory

题面翻译

n n n 支队伍参加比赛,每个队伍初始时有一些代币。

比赛每一轮随机挑两个代币数不为0的队伍,然后代币多的队伍获胜,代币少的队伍把代币全部给代币多的(代币数量相同则随机),直到最后只有一个队伍有代币, 这个队伍获胜。

求哪些队伍是有可能获胜的。

t ≤ 1 0 4 t ≤ 10^4 t104

∑ n ≤ 2 ∗ 1 0 5 \sum{n} ≤ 2*10^5 n2105

1 ≤ a i ≤ 1 0 9 1 ≤ a_i ≤ 10^9 1ai109

题目描述

A championship is held in Berland, in which n n n players participate. The player with the number i i i has a i a_i ai ( a i ≥ 1 a_i \ge 1 ai1 ) tokens.

The championship consists of n − 1 n-1 n1 games, which are played according to the following rules:

  • in each game, two random players with non-zero tokens are selected;
  • the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly);
  • the winning player takes all of the loser’s tokens;

The last player with non-zero tokens is the winner of the championship.

All random decisions that are made during the championship are made equally probable and independently.

For example, if n = 4 n=4 n=4 , a = [ 1 , 2 , 4 , 3 ] a = [1, 2, 4, 3] a=[1,2,4,3] , then one of the options for the game (there could be other options) is:

  • during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player’s tokens. Now a = [ 0 , 2 , 4 , 4 ] a = [0, 2, 4, 4] a=[0,2,4,4] ;
  • during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now a = [ 0 , 2 , 8 , 0 ] a = [0, 2, 8, 0] a=[0,2,8,0] ;
  • during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player’s tokens. Now a = [ 0 , 0 , 10 , 0 ] a = [0, 0, 10, 0] a=[0,0,10,0] ;
  • the third player is declared the winner of the championship.

Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players.

输入格式

The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104 ) — the number of test cases. Then t t t test cases follow.

The first line of each test case consists of one positive integer $ n $ ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105 ) — the number of players in the championship.

The second line of each test case contains n n n positive integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109 ) — the number of tokens the players have.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105 .

输出格式

For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input.

样例 #1

样例输入 #1

2
4
1 2 4 3
5
1 1 1 1 1

样例输出 #1

3
2 3 4 
5
1 2 3 4 5

简单思路

这题主要是一个结构体排序,和前缀和还带一点贪心。

当时是在和同学卡时间所以有一些奇怪操作。

完整代码

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 2e5 + 10;
struct node {
    int id, num;
};
node a[N];
int b[N], tp[N];
long long d[N];

bool cmp(node a, node b) {
	return a.num < b.num;
}

signed main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
       	scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
        	scanf("%d", &a[i].num); //由于还要保存地址所以要用结构体
            a[i].id = i;
        }
        sort(a + 1, a + 1 + n, cmp);
        // 前缀和
        for (int i = 1; i < n; i += 2) { //卡时用的循环展开
            d[i] = d[i - 1] + a[i].num;
            d[i + 1] = d[i] + a[i + 1].num;
        }
        int k;
        for (k = n; k >= 2; k--) {
            if (d[k - 1] < a[k].num) break;
        }
        int tmp = k;
        int res = 0;
        for (int i = tmp; i <= n; i++) {
            tp[++res] = a[i].id;
        }
        sort(tp + 1, tp + res + 1);
        printf("%d\n", res);
        for (int i = 1; i <= res; i++) {
            printf("%d", tp[i]);
            printf(" ");
        }
        printf("\n");
    }
    return 0;
}

祝大家csp s2/j2 RP++

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值