Codeforces 1490E Accidental Victory
A championship is held in Berland, in which nn players participate. The player with the number ii has aiai (ai≥1ai≥1) tokens.
The championship consists of n−1n−1 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=4n=4, 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];
- 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];
- 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];
- 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 tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow.
The first line of each test case consists of one positive integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of players in the championship.
The second line of each test case contains nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the number of tokens the players have.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
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.
2 4 1 2 4 3 5 1 1 1 1 1
3 2 3 4 5 1 2 3 4 5
排序,求前缀和,倒着往前来判断
1 //2021-03-15 19:37:42 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 typedef long long ll; 9 10 const int N = 2e5+10; 11 12 int T, n; 13 ll sum[N]; 14 struct node{ 15 int id; 16 ll a; 17 }a[N]; 18 19 bool cmp(node a, node b){ 20 return a.a < b.a; 21 } 22 int b[N]; 23 24 int main(){ 25 scanf("%d", &T); 26 while(T--){ 27 scanf("%d", &n); 28 for(int i = 1; i <= n; i++){ 29 scanf("%lld", &a[i].a); 30 a[i].id = i; 31 } 32 sort(a+1, a+n+1, cmp); 33 for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + a[i].a; 34 ll ans = 1; 35 for(int i = n-1; i >= 1; i--){ 36 if(sum[i] >= a[i+1].a) ans++; 37 else break; 38 } 39 printf("%lld\n", ans); 40 int tot = 0; 41 for(int i = n-ans+1; i <= n; i++){ 42 b[++tot] = a[i].id; 43 } 44 sort(b+1, b+tot+1); 45 for(int i = 1; i <= tot; i++) 46 printf("%d ", b[i]); 47 printf("\n"); 48 49 50 } 51 52 return 0; 53 }