Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- vector<pair<int,int> > v;
- int main()
- {
- int n,half;
- cin>>n;
- map<int,int>left,right;
- int x,y;
- if(n%2)
- half=n/2+1;
- else
- half=n/2;
- //cout<<half<<endl;
- for(int i=0; i<n; i++)
- {
- cin>>x>>y;
- v.push_back(make_pair(x,y));
- left[x]++;
- right[y]++;
- if(x==y)right[y]--;
- }
- int ans=n;
- for(int i=0; i<n; i++)
- {
- if(left[v[i].first]>=half)
- {
- ans=0;
- break;
- }
- if(left[v[i].first]<half)
- {
- if(right[v[i].first]+left[v[i].first]>=half)
- {
- ans=min(ans,half-left[v[i].first]);
- }
- }
- if(right[v[i].first]+left[v[i].first]>=half)
- {
- ans=min(ans,half-left[v[i].first]);
- }
- if(right[v[i].second]+left[v[i].second]>=half)
- ans=min(ans,half-left[v[i].second]);
- if(right[v[i].second]>=half)
- ans=min(half,ans);
- }
- if(ans==n)
- ans=-1;
- cout<<ans<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement