洛谷P2587 [ZJOI2008] 泡泡堂
阿新 • • 發佈:2018-04-13
http sed hid sele for getch font 機會 IE
題目傳送門
分析:一道策略遊戲題,要求最大期望得分和最小期望得分。首先分析最大,很顯然是可以用一種類似於田忌賽馬的思維來做,將兩隊的實力按照從大到小(其實從小到大也可以)排序,然後就按照順序比較,可能會出現以下幾種情況:
我方最大>對方最大,則用我方最大對抗對方最大
我方最小>對方最小,則用我方最小對抗對方最小
如果以上兩種情況都不滿足,則用我方最小去對抗對方最大,為我方最大爭取機會。
這個正確性應該不難證明,那麽最大的得分就這麽A(shui)掉了;
再看最小得分,發現直接求最小得分並不容易,那麽轉換一下思維,求最大失分,再用2n-最大失分,正確性易證。那麽求最大失分思維就和上面一樣,只需轉換一下:
我方最大<對方最大,則用我方最大對抗對方最大
我方最小<對方最小,則用我方最小對抗對方最小
如果以上兩種情況都不滿足,則比較我方最大與對方最小,如果相等,則失一分,否則不失分。
然後,這題就這樣水過去了!
Code:
1 //It is made by HolseLee on 7th Apr 2018 2 //Luogu.org P2587 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<cmath> 7View Code#include<iostream> 8 #include<iomanip> 9 #include<algorithm> 10 using namespace std; 11 const int N=1e5+7; 12 int n,a[N],b[N]; 13 int ans,hm,tm,he,te; 14 inline int read() 15 { 16 char ch=getchar();int num=0;bool flag=false; 17 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)flag=true;ch=getchar();} 18 while(ch>=‘0‘&&ch<=‘9‘){num=num*10+ch-‘0‘;ch=getchar();} 19 return flag?-num:num; 20 } 21 bool cmp(int x,int y) 22 {return x>y;} 23 int main() 24 { 25 n=read(); 26 for(int i=1;i<=n;i++) 27 a[i]=read(); 28 for(int i=1;i<=n;i++) 29 b[i]=read(); 30 sort(a+1,a+n+1,cmp); 31 sort(b+1,b+n+1,cmp); 32 tm=te=n;hm=he=1; 33 while(hm<=tm){ 34 if(a[hm]>b[he])ans+=2,hm++,he++; 35 else if(a[tm]>b[te])ans+=2,tm--,te--; 36 else ans+=(a[tm]==b[he]),he++,tm--;} 37 printf("%d ",ans); 38 hm=he=1;tm=te=n;ans=0; 39 while(hm<=tm){ 40 if(a[hm]<b[he])ans+=2,hm++,he++; 41 else if(a[tm]<b[te])ans+=2,tm--,te--; 42 else ans+=(a[hm]==b[te]),hm++,te--;} 43 printf("%d",2*n-ans); 44 return 0; 45 }
洛谷P2587 [ZJOI2008] 泡泡堂