Soj 4389 Simulation
Background: Question A of the weekly competition,
Learning: 1. At first, I did not notice the descriptions about the number of posts greater than 1/2, pure brute force count, and various super!
2. I found that scanning each number in order to see if there are more than 1/2 entries in the array is found. My code:
#include
#include
int str[10000000];int main(void){int n;scanf("%d",&n);while(n--){int m,cout=0;scanf("%d",&m);for(int i=0;i
=m/2+1) {printf("%d\n",str[i]);goto l1; } }}}str[i]=-1;cout=0;}l1: cout=0;}return 0;}
3. In this way, we can pass but open a huge amount of memory (the oj limit is 10 to the power of 8 int ?)
The solution Report provides a more advanced method: assume that the first number is and the count is counted as 1. If the count is the same, the count is automatically incremented by one. When the count is 0, it can be changed to the number currently read, and the remaining value must be a number greater than 1/2. Code:
#include
#include
int str[10000000];int main(void){int n;scanf("%d",&n);while(n--){ int m,count=0,x,key=-1;scanf("%d",&m);while(m--){scanf("%d",&x);if(x!=key) count--;else count++;if(count<0) {key=x;count=0;}}printf("%d\n",key);} return 0;}