这道题就是田忌赛马吧,首先排个序,然后将最强的和最强的比较,如果a > b,就当然打,若果打不过,就让a中最弱的去送死,然而如果这个最弱的能把对方最弱的打过,就不能让他去送死,因此这里要判断一下。
用四个指针代表a中当前最弱的和最强的以及b中最弱的和最强的,然后分情况移动指针就行。
至于最坏的情况,就是总分减去b的最优情况。
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cctype> 8 #include<vector> 9 #include<stack> 10 #include<queue> 11 using namespace std; 12 #define enter printf(" ") 13 #define space printf(" ") 14 #define Mem(a) memset(a, 0, sizeof(a)) 15 typedef long long ll; 16 typedef double db; 17 const int INF = 0x3f3f3f3f; 18 const int eps = 1e-8; 19 const int maxn = 1e5 + 5; 20 inline ll read() 21 { 22 ll ans = 0; 23 char ch = getchar(), last = ' '; 24 while(!isdigit(ch)) {last = ch; ch = getchar();} 25 while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();} 26 if(last == '-') ans = -ans; 27 return ans; 28 } 29 inline void write(ll x) 30 { 31 if(x < 0) x = -x, putchar('-'); 32 if(x >= 10) write(x / 10); 33 putchar(x % 10 + '0'); 34 } 35 36 int n, a[maxn], b[maxn]; 37 int ans = 0; 38 39 bool cmp(int a, int b) {return a > b;} 40 41 int work(int* a, int* b) 42 { 43 int ans = 0; 44 int la = 1, ra = n, lb = 1, rb = n; 45 while(la <= ra) 46 { 47 if(a[la] > b[lb]) {la++; lb++; ans += 2;} 48 else if(a[ra] > b[rb]) {ra--; rb--; ans += 2;} 49 else 50 { 51 if(a[la] == b[rb]) ans++; 52 la++; rb--; 53 } 54 } 55 return ans; 56 } 57 58 int main() 59 { 60 n = read(); 61 for(int i = 1; i <= n; ++i) a[i] = read(); 62 for(int i = 1; i <= n; ++i) b[i] = read(); 63 sort(a + 1, a + n + 1); 64 sort(b + 1, b + n + 1); 65 write(work(a, b)); space; write((n << 1) - work(b, a)); enter; 66 return 0; 67 }