考试---订错2--2

我用了纯模拟,数组不但爆了而且因为没有及时优化n2 只得了十分

别人的三十分代码

#include<bits/stdc++.h>
using namespace std;
int n,ans,mid,flag,maxx,ru[15000000],chu[15000000];
int main()
{
    freopen("gift.in","r",stdin);
    freopen("gift.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
    {    
        int a,b;
        cin>>a>>b;
        ru[a]++;
        maxx=max(max(a,b),maxx);//减少了之后找的量
        if(a!=b)
            chu[b]++;
    }
    if(n&1)
        mid=n/2+1;
    else
        mid=n/2;
    for(int i=1;i<=maxx;i++)
        if(ru[i]>=mid)
        {
            cout<<"0";
            return 0;
        }//先找一遍是否已经能够满足
    for(int i=1;i<=maxx;i++)
    {
        if((ru[i]+chu[i])>=mid)
        {    
                ans=mid-ru[i];
                flag=1;
                break;
        }
    }
    if(!flag)
        cout<<"-1";
    else 
        cout<<ans;
    return 0;
}

我的10分代码

#include<iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int MAXN=1000001;
int n;
int col[MAXN][2]={0};
int require;
int minn=0x7f;
int main()
{
    int i,j,k,a,b;
    freopen("gift.in","r",stdin);
    freopen("gift.out","w",stdout);
    cin>>n;
    if(n%2==0) require=n/2;
         else require=(n+1)/2;
    for(i=1;i<=n;i++)
    {
      cin>>a>>b;
      col[a][1]=col[a][1]+1;
      col[b][2]=col[b][2]+1;
    }
    for(i=1;i<=n;i++)
    {
        k=require-col[i][1];
        if((k<=col[i][2])&&k<minn) minn=k;
    }
    if(minn<0x7f-1) {cout<<minn<<endl;}
    else cout<<"-1"<<endl;
    return 0;
}

大佬的100分代码-------map

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,cnt;//cnt---颜色种数
ll zheng[200009],fan[200009],ans=99999999999;
map<ll,int>mapp;
int main()
{
    freopen("gift.in","r",stdin);
    freopen("gift.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        ll c1,c2;
        scanf("%lld%lld",&c1,&c2);
        if(!mapp[c1]){//该种颜色数没有   mapp存该色映射的张数
            zheng[++cnt]++;//颜色种类数++,正面有该色的张数++
            mapp[c1]=cnt;//有该色数的是第几种
        }
        else {
            zheng[mapp[c1]]++;
        }
        if(c1==c2)continue;
        if(!mapp[c2]){
            fan[++cnt]++;
            mapp[c2]=cnt;
        }
        else{
            fan[mapp[c2]]++;
        }
    }
    int mid=(n+1)/2;
    for(int i=1;i<=cnt;i++)
    {
        if(zheng[i]>=mid){
            cout<<"0"<<endl;
            return 0;
        }
        else if(zheng[i]+fan[i]>=mid){
            ans=min(ans,mid-zheng[i]);
        }
    }
    if(ans<99999999999)
    cout<<ans<<endl;
    else cout<<"-1"<<endl;
    return 0;
}

 之后又偷懒订正的代码 --80

!!!若x==y

#include<iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
const int MAXN=1000001;
map<long long,int>mapp; 
int n,cnt=0,ans=0x7f;
int mid;
int zheng[MAXN],fan[MAXN];
int main()
{
    int i,j,x,y;
    cin>>n;
    mid=(n+1)/2;
    for(i=1;i<=n;i++)
    {
       cin>>x>>y;
       if(mapp[x]==0){
            zheng[++cnt]++;
            mapp[x]=cnt;    
       }    
       else {
            zheng[mapp[x]]++;        
            }
       if(mapp[y]==0){
            fan[++cnt]++;
            mapp[y]=cnt;    
       }    
       else {
            fan[mapp[x]]++;        
            } 
    }
    for(i=1;i<=cnt;i++)
       {
            if(zheng[i]>=mid) {cout<<"0"<<endl; return 0;}
            else if(zheng[i]+fan[i]>=mid)
            {
                ans=min(ans,mid-zheng[i]);
            }
       }
    if(ans<0x7f-1) cout<<ans<<endl;
    else cout<<"-1"<<endl;
    
    return 0;
}

 

posted @ 2017-08-22 16:19  SH,Y  阅读(133)  评论(0编辑  收藏  举报