HLG1067QQ Farm【状压dp】
QQ Farm | ||||||
|
||||||
Description | ||||||
Many boys and girls play the game QQ farm, For example, in this picture we can see xnby、 Lance and so on. Sometimes there are some mosquitoes in our farm, we use a rectangular-shaped pad to kill them, and our experience will increase by 3 points after killing each mosquito .But now we find that using a square-shaped pad works better. when using the square-shaped pad, if we kill n mosquito at one time, our experience will increase by n×n points. For example, if we kill 4 mosquitoes at one time, our experience will increase by 16 points. A mosquito was killed when it is covered by the pad, but killing them is not so easy, because them are flying all the time. Now we got the radius of the pad and the location of every mosquito in each second, in each second we could use the pad just once, and we must kill at least one mosquito every second. You are asked to write a program to compute how many points we can get at most. |
||||||
Input | ||||||
There are multiple test cases. In each case, The first line contains two integers: N, T, which indicate the number of mosquitoes and the number of seconds. The second line contains one Real number R, the length of the pad’s side. The following T lines, each contains N pairs of Real numbers: (X1, Y1), (X2, Y2)…(XT, YT), which indicate the locations of those mosquitoes at the corresponding second. Those real numbers are rounded to two digits after the decimal point. The sides of the pad must be parallel to x-axis or Y-axis. ( 0 < N ≤ 10, 0 < T ≤ N, -1000000 ≤ R, Xi, Yi ≤ 1000000 ) |
||||||
Output | ||||||
The number of points we could get at most. |
||||||
Sample Input | ||||||
3 2
2.00
1.00 1.00 2.00 2.00 3.00 3.00
0.00 0.00 2.00 2.00 4.00 4.00
3 1
2.00
1.00 1.00 2.00 2.00 3.00 3.00
|
||||||
Sample Output | ||||||
5
9
|
||||||
Author | ||||||
yiyi4321@Footmen |
啊啊啊啊啊啊啊啊 这个题卡我两天啊 啊啊啊啊啊啊啊啊啊啊
大意:有n个蚊子 t个时期 告诉你每个时期每个蚊子的坐标
有一个正方形的苍蝇拍 每个时期最少拍一个蚊子 point+拍死蚊子个数的额平方
问最后最多的point
分析:
由于n<= 10所以状压
dp[i][j] 代表i时期状态为j最大的point
dp[i][j] = max(dp[i - 1][k] + cnt * cnt)
然后这时候遇到一个问题就是苍蝇拍该如何放的问题
刚开始的时候我是这么处理的
枚举所有的一对点i,j然后求出以这两个点位边缘的正方形有包含哪几个点
然后wa一次
原因是不可能每一次都拍的最大的点的个数 有可能它选择一个少的个数的拍
然后我又想到一个思路就是用一个vector存所有的拍的情况
vector v[i][j]代表 i时期 最多拍死j个蚊子有哪些团
这时候就能用10*10来枚举所有的状态
然后又wa无数次
之后跟wht讨论中发现了错误
原来每次枚举拍死x个蚊子的时候它可能拍死的不止x个蚊子
例如1011他拍死的肯能是1111但是不会被发现
处理就是枚举一下所有的点看其它点是不是也被收入之中 若没有被收入 那么就是合法的删除状态
a
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <cmath> 6 using namespace std; 7 8 const int maxn = 11; 9 const double INF = 1000000000.0; 10 int dp[maxn][1 << maxn]; 11 vector<int>v[maxn][maxn]; 12 int ok[maxn]; 13 double x[maxn][maxn], y[maxn][maxn]; 14 int t, n; 15 double r; 16 int eps(double x) { 17 if(fabs(x) < 1e-6) { 18 return 0; 19 } 20 if(x < 0) return -1; 21 return 1; 22 } 23 24 void init() { 25 for(int i = 1; i < maxn; i++) { 26 for(int j = 1; j < maxn; j++) { 27 v[i][j].clear(); 28 } 29 } 30 int Max = 1 << n; 31 double nx, ny, mx, my; 32 for(int i = 1; i <= t; i++) { 33 for(int j = 0; j < Max; j++) { 34 int cnt = 0; nx = ny = INF; mx = my = - INF; 35 for(int k = 1; k <= n; k++) { 36 if((j & ( 1 << ( k - 1 ) ) )) { 37 // printf(" j == %d k == %d ans = %d\n", j, k, (j & ( 1 << ( k - 1 ) ) )); 38 ok[cnt++] = k; 39 nx = min(nx, x[i][k]); 40 ny = min(ny, y[i][k]); 41 42 mx = max(mx, x[i][k]); 43 my = max(my, y[i][k]); 44 } 45 } 46 if(cnt && eps(mx - nx - r) <= 0 && eps(my - ny - r) <= 0) { 47 int vis[maxn] = { 0 }; 48 bool flag = true; 49 for(int k = 0; k < cnt; k++) { 50 vis[ok[k]] = 1; 51 } 52 for(int l = 1; l <= n; l++) { 53 if(!vis[l] && eps(x[i][l] - nx) >= 0 && eps(x[i][l] - mx) <= 0 && eps(y[i][l] - ny) >= 0 && eps(y[i][l] - my) <= 0) { 54 flag = false; 55 break; 56 } 57 } 58 if(flag) 59 for(int k = 0; k < cnt; k++) { 60 v[i][cnt].push_back(ok[k]); 61 } 62 } 63 } 64 } 65 // for(int i = 1; i <= t; i++) { 66 // for(int j = 1; j <= n; j++) { 67 // for(int k = 0; k < v[i][j].size(); k++) { 68 // printf("%d %d %d\n", i, j, v[i][j][k]); 69 // } 70 // } 71 // } 72 // puts(""); 73 } 74 75 void DP() { 76 memset(dp, -1, sizeof(dp)); 77 dp[0][0] = 0; 78 int Max = 1 << n; 79 for(int i = 1; i <= t; i++) { 80 for(int j = 0; j < Max; j++) { 81 if(dp[i - 1][j] != -1) { 82 for(int k = 1; k <= n; k++) { 83 int vs = v[i][k].size(); 84 if(vs) { 85 int flag = j; int cnt = 0; 86 for(int l = 0; l < vs; l++) { 87 int id = v[i][k][l]; 88 // printf("i == %d k == %d ans = %d\n", i, k, v[i][k][l]); 89 if((flag & ( 1 << ( id - 1 ) ) ) == 0) { 90 flag |= ( 1 << ( id - 1 ) ); 91 cnt ++; 92 } 93 if((l + 1) % k == 0 && cnt) { 94 dp[i][flag] = max(dp[i][flag], dp[i - 1][j] + cnt * cnt); 95 flag = j; 96 cnt = 0; 97 } 98 } 99 } 100 } 101 } 102 } 103 } 104 } 105 106 int ANS() { 107 int ans = 0; int Max = 1 << n; 108 for(int i = 0; i < Max; i++) { 109 ans = max(ans, dp[t][i]); 110 } 111 return ans; 112 } 113 114 int main() { 115 while(EOF != scanf("%d %d",&n, &t) ) { 116 scanf("%lf",&r); 117 for(int i = 1; i <= t; i++) { 118 for(int j = 1; j <= n; j++) { 119 scanf("%lf %lf",&x[i][j], &y[i][j]); 120 } 121 } 122 init(); 123 DP(); 124 printf("%d\n", ANS()); 125 } 126 return 0; 127 }