1. 程式人生 > >HDU1052 貪心—田忌賽馬

HDU1052 貪心—田忌賽馬

 並沒有想象的簡單,思想就是田忌最弱的馬儘量去拼King最厲害的馬,但是還要看這個馬死的值不值。

比如 田忌 200  20  King 100 10,這個時候你就不能直接讓20去拼掉100。

解決的方法就是先比較末尾的情況,然後比較頭部的情況判斷末尾的情況如何處理。

//田忌賽馬 
#include <iostream>
#include <cstdio>
#include <map>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#define ll long long
#define INF 0x3f3f3f
using namespace std;
const int maxn=1000+10;
int T[maxn],K[maxn];

int main()
{
	int n;
	while(EOF!=scanf("%d",&n) && n)
	{
		int cnt=0;
		int i,j;
		for(i=0;i<n;i++)	scanf("%d",&T[i]);
		for(i=0;i<n;i++)	scanf("%d",&K[i]);
		sort(T,T+n,greater<int>());
		sort(K,K+n,greater<int>());
		int t=0;
		int ST=0,SK=0;
		for(i=n-1,j=n-1;i>=ST;){
				if(T[i]<K[j]){
					t--;i--;SK++;
				}else if(T[i]>K[j]){
					t++;i--;j--;
				}else{
					if(T[ST]>K[SK]){//死的沒價值 
						ST++;SK++;t++;
					}else if(T[ST]<K[SK]){
						t--;i--;SK++;
					}else{
						if(T[i]<K[SK])	 t--;
						SK++;
						i--; 
					}
				}
		}
		cout << 200*t << endl;
	}
	return 0;
}