Algorithm Voting: Determining Single Option Selection
Algorithm Voting: Determining Single Option Selection
This problem involves a voting process where 'n' programmers choose their favorite algorithm from 'm' available options. Before the first round, all 'm' options are available. In each round, every programmer casts a vote for one of the remaining algorithms. After the round, only the algorithms with the maximum number of votes remain. The voting process ends when there is only one option left.
The challenge is to determine whether the voting process can continue indefinitely, or if, no matter how people vote, they will eventually choose a single option after a finite number of rounds.
Input:
The first line contains a single integer 't' (1≤t≤105) – the number of test cases.
Each test case consists of a single line containing two integers 'n' and 'm' (1≤n,m≤106) – the number of programmers and choice options respectively.
Output:
For each test case, output 'YES' if the programmers will eventually choose a single option, and 'NO' otherwise.
You can print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as a positive answer).
Example:
Input:
5
3 2
4 2
5 3
1000000 1000000
1 1000000
Output:
YES
NO
YES
NO
YES
Solution:
Here's a breakdown of the conditions that guarantee a single option will be chosen, and why other scenarios lead to indefinite voting:
-
'n' = 1 or 'm' = 1: If there's only one programmer or only one algorithm, the outcome is trivial. The single option will be chosen.
-
'n' > 'm': If the number of programmers is greater than the number of algorithms, at least one algorithm will always receive more than one vote. This means there will always be at least two algorithms with the maximum votes, preventing a single option from being chosen.
-
'n' = 'm': In this case, if each programmer votes for a distinct algorithm, the voting will end in one round with a single option remaining.
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
if(n==1 || m==1 || n>m) cout<<"YES"<<endl;
//只有一个人或只有一种算法或人比算法多,一定会选出来
else if(n==m) cout<<"YES"<<endl;
//人数和算法数相等,每人选一个不同的,最后只剩下一个
else cout<<"NO"<<endl;
//其他情况都不行
}
return 0;
}
Explanation of Code:
- The code iterates through each test case ('t').
- It reads the number of programmers ('n') and choice options ('m') for each case.
- It checks the conditions mentioned above to determine whether a single option will be chosen:
- If 'n' is 1, 'm' is 1, or 'n' is greater than 'm', the output is 'YES'.
- If 'n' is equal to 'm', the output is 'YES'.
- In all other cases, the output is 'NO'.
原文地址: https://www.cveoy.top/t/topic/nQlY 著作权归作者所有。请勿转载和采集!