Soj 4389 Simulation

Source: Internet
Author: User

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;} 
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.