1034: [ZJOI2008]泡泡堂BNB

1034: [ZJOI2008]泡泡堂BNB

链接

分析:

  开始想的贪心是尽量小去胜或者平最小的,但是无法处理胜还是平的问题,比如3,5和4,5。。。

  正确的贪心:小的胜小的,大的胜大的,如果小的无法胜小的,大的无法胜大的,那么用A中小的去和B中大的比,相当于A中小的左移一位,然后和B小的比,B最大的右移一位,和A中最大的比。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 
 5 inline int read() {
 6     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
 7     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
 8 }
 9 
10 const int N = 100100;
11 int a[N],b[N];
12 
13 int work(int n,int *a,int *b) {
14     int L1 = 1,R1 = n,L2 = 1,R2 = n,ans = 0;
15     while (L1 <= R1 && L2 <= R2) {
16         if (a[L1] > b[L2]) ans+=2, L1++, L2++;
17         else if (a[R1] > b[R2]) ans += 2, R1--, R2--;
18         else {
19             ans += (a[L1] == b[R2]); L1++; R2--;
20         }
21     }
22     return ans;
23 }
24 
25 int main() {
26     int n = read();
27     for (int i=1; i<=n; ++i) a[i] = read();
28     for (int i=1; i<=n; ++i) b[i] = read();
29     sort(a+1,a+n+1);
30     sort(b+1,b+n+1);    
31     cout << work(n,a,b) << " " << 2 * n - work(n,b,a);
32     return 0;
33 }

 

posted @ 2018-07-05 15:21  MJT12044  阅读(167)  评论(0编辑  收藏  举报