soj--4389: The water king of Sichuan

Source: Internet
Author: User
Tags array sort

description

_l roommate Hz likes to post on the Sichuan, according to legend, Hz in the Chuan bar posted on the number of posts has more than the total number of posts, is the River Lake people seal for the Chuan-Shui king, you can help _l quickly find out the Sichuan river paste the water king Hz ID?
It is known that the total number of posts in the Sichuan post is n, give the ID of the author of n posts, and find the ID of Hz.
Input

The input file contains multiple sets of test data. Number of data groups T (T<=40) for the first behavior test.
Then there is the T-group data, the first line enters the total number of posts N (1<=n<=10000000), the next line has n positive integers, respectively, the ID number of each post author (the ID number is a positive integer that does not exceed 10^9)
Output

Output T line, each behavior of the group test data of the Sichuan post bar water king Hz ID.
Example Input

2
3
1 2 2
5
1 2 3) 3 3
Example Output

2
3
Author

_l
parsing: In general, you can sort the input ID number, and then find it, but see the total number of n Max is 10000000, the conjecture should time out, but still quickly hit the code test, the result is really timed out, so this brute force method is not feasiblepost your own timeout code
#include <iostream> #include <algorithm>using namespace std;//defines an array of data that is set to a global variable const int M=10000000+5;int  A[m];int Main () {int t;//input T Group data cin >> T;while (t--) {int n;cin >> n;//input data for (int i=0; i<n; ++i) {cin >> A[i];} Sort the input array sort (a,a+n);//Find the number of occurrences greater than N/2 ID number int cnt=1, value=a[0];for (int i=1; i<n; ++i) {if (a[i]==value) {cnt++;// Determine if CNT meets the conditions if (CNT>N/2) {cout << value << endl;break;}} Else{value=a[i];cnt=1;}}} return 0;}

Then carefully read the question to analyze, the topic has an important message is that the number of the ID number appears more than half of the total number of times, you can use this principle to optimize the code.once the array can be obtained, the use of two variables K and J to save the median value, where K holds the value of each comparison, J storage times, iterating over the array, if the current array a[i] equals K j + + j--, that is, using the idea of offsetting, because the final number of times to find more than half of the total , so the number of times J must finally be greater than 0, that is, the use of this principle can reduce the complexity of O (n)Paste the AC code (be careful not to use CIN, cout)
#include <stdio.h>const int m=10000000+5;int a[m];int search (int a[], int length) {int k, j=0;for (int i=0; i<lengt H ++i) {if (j==0) {k=a[i];} if (K==a[i]) {++j;} Else{--j;}} return k;} int main () {int t;scanf ("%d", &t), while (t--) {int n;scanf ("%d", &n), for (int i=0; i<n; ++i) {scanf ("%d", &a[ I]);} Find the ID number more than half the number of occurrences printf ("%d\n", Search (A,n));}}


soj--4389: The water king of Sichuan

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.