HDU-3315 My Brute

先对于每种可能的PK情况进行判断胜负,然后求最小改动最大匹配。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <cctype>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define MAX 0x3fffffff
#define N 100
using namespace std;
int read()
{
	int x=0; char ch=getchar();
	while (!isdigit(ch)) ch=getchar();
	while (isdigit(ch)) { x=x*10+ch-'0'; ch=getchar(); } 
	return x;
}

int n, l[N], st[N], lx[N], ly[N], v[N][N], a1[N], a2[N], hp1[N], hp2[N], p[N];
bool vx[N], vy[N];

inline int pk(int x, int y)
{
	int c=min((hp1[x]-1)/a2[y], (hp2[y]-1)/a1[x])+1; 
	if (c*a1[x]>=hp2[y]) return p[x]; else return -p[x]; 
}

bool Find(int x)
{
	vx[x]=1;
	rep(y, 1, n) if (!vy[y])
	{
		int a=lx[x]+ly[y]-v[x][y];
		if (!a)
		{
			vy[y]=1; if (!l[y] || Find(l[y])) { l[y]=x; return 1; }
		}
		else st[y]=min(st[y], a);
	}
	return 0;
}

inline int km()
{
	clr(ly, 0); clr(l, 0); rep(i, 1, n) lx[i]=-MAX;
	rep(i, 1, n) rep(j, 1, n) if (lx[i]<v[i][j]) lx[i]=v[i][j];
	rep(i, 1, n)
	{
		rep(j, 1, n) st[j]=MAX;
		while (1)
		{
			clr(vx, 0); clr(vy, 0);
			if (Find(i)) break; int a=MAX;
			rep(j, 1, n) if (!vy[j] && a>st[j]) a=st[j];
			rep(j, 1, n) if (vx[j]) lx[j]-=a;
			rep(j, 1, n) if (vy[j]) ly[j]+=a; else st[j]-=a;
		}
	}
	int a=0;
	rep(i, 1, n) a+=lx[i]+ly[i];
	return a;
}

int main()
{
	while (~scanf("%d", &n) && n)
	{
		rep(i, 1, n) p[i]=read();
		rep(i, 1, n) hp1[i]=read();
		rep(i, 1, n) hp2[i]=read();
		rep(i, 1, n) a1[i]=read();
		rep(i, 1, n) a2[i]=read();
		rep(i, 1, n) rep(j, 1, n) v[i][j]=pk(i, j);
		rep(i, 1, n) rep(j, 1, n) v[i][j]*=N, v[i][j]+=i==j?1:0;
		int ans=km(); double k=ans%N;
		if (ans>0) printf("%d %.3lf%%
", ans/N, k/n*100); else printf("Oh, I lose my dear seaco!
");
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/NanoApe/p/4382109.html