HDU 6396 贪心+优先队列+读入挂
Swordsman
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2587 Accepted Submission(s): 791
Problem Description
Lawson is a magic swordsman with k kinds of magic attributes v1,v2,v3,…,vk. Now Lawson is faced with n monsters and the i-th monster also has k kinds of defensive attributes ai,1,ai,2,ai,3,…,ai,k. If v1≥ai,1 and v2≥ai,2 and v3≥ai,3 and … and vk≥ai,k, Lawson can kill the i-th monster (each monster can be killed for at most one time) and get EXP from the battle, which means vj will increase bi,j for j=1,2,3,…,k.
Now we want to know how many monsters Lawson can kill at most and how much Lawson's magic attributes can be maximized.
Now we want to know how many monsters Lawson can kill at most and how much Lawson's magic attributes can be maximized.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line has two integers n and k (1≤n≤105,1≤k≤5).
The second line has k non-negative integers (initial magic attributes) v1,v2,v3,…,vk.
For the next n lines, the i-th line contains 2k non-negative integers ai,1,ai,2,ai,3,…,ai,k,bi,1,bi,2,bi,3,…,bi,k.
It's guaranteed that all input integers are no more than 109 and vj+∑i=1nbi,j≤109 for j=1,2,3,…,k.
It is guaranteed that the sum of all n ≤5×105.
The input data is very large so fast IO (like `fread`) is recommended.
The first line has two integers n and k (1≤n≤105,1≤k≤5).
The second line has k non-negative integers (initial magic attributes) v1,v2,v3,…,vk.
For the next n lines, the i-th line contains 2k non-negative integers ai,1,ai,2,ai,3,…,ai,k,bi,1,bi,2,bi,3,…,bi,k.
It's guaranteed that all input integers are no more than 109 and vj+∑i=1nbi,j≤109 for j=1,2,3,…,k.
It is guaranteed that the sum of all n ≤5×105.
The input data is very large so fast IO (like `fread`) is recommended.
Output
For each test case:
The first line has one integer which means the maximum number of monsters that can be killed by Lawson.
The second line has k integers v′1,v′2,v′3,…,v′k and the i-th integer means maximum of the i-th magic attibute.
The first line has one integer which means the maximum number of monsters that can be killed by Lawson.
The second line has k integers v′1,v′2,v′3,…,v′k and the i-th integer means maximum of the i-th magic attibute.
Sample Input
1
4 3
7 1 1
5 5 2 6 3 1
24 1 1 1 2 1
0 4 1 5 1 1
6 0 1 5 3 1
Sample Output
3
23 8 4
Hint
For the sample, initial V = [7, 1, 1]① kill monster #4 (6, 0, 1), V + [5, 3, 1] = [12, 4, 2]
② kill monster #3 (0, 4, 1), V + [5, 1, 1] = [17, 5, 3]
③ kill monster #1 (5, 5, 2), V + [6, 3, 1] = [23, 8, 4]
After three battles, Lawson are still not able to kill monster #2 (24, 1, 1)
because 23 < 24.
Source
解析 策略当然是先选最小的符合条件的在一步一步扩大,所以我们就借助优先队列来解决,把上一个满足属性条件的怪兽 转移到下一个优先队列 然后当它被标记为了k次那么就可以杀掉他了,加到属性上 进行下一步操作。
必须用这个读入挂不然会超时,注意debug时不能使用读入挂,提交的时候改过来就可以。
AC代码
1 #include <bits/stdc++.h> 2 #define pb push_back 3 #define mp make_pair 4 #define fi first 5 #define se second 6 #define all(a) (a).begin(), (a).end() 7 #define fillchar(a, x) memset(a, x, sizeof(a)) 8 #define huan printf("\n") 9 #define debug(a,b) cout<<a<<" "<<b<<" "<<endl 10 #define ffread(a) fastIO::read(a) 11 using namespace std; 12 typedef long long ll; 13 typedef pair<int,int> pii; 14 const int maxn=5e5+10,inf=0x3f3f3f3f; 15 const ll mod=1e9+7; 16 #define reads(n) FastIO::read(n) 17 namespace FastIO 18 { 19 const int SIZE = 1 << 16; 20 char buf[SIZE], obuf[SIZE], str[60]; 21 int bi = SIZE, bn = SIZE, opt; 22 int read(char *s) 23 { 24 while (bn) 25 { 26 for (; bi < bn && buf[bi] <= ' '; bi++); 27 if (bi < bn) 28 break; 29 bn = fread(buf, 1, SIZE, stdin); 30 bi = 0; 31 } 32 int sn = 0; 33 while (bn) 34 { 35 for (; bi < bn && buf[bi] > ' '; bi++) 36 s[sn++] = buf[bi]; 37 if (bi < bn) 38 break; 39 bn = fread(buf, 1, SIZE, stdin); 40 bi = 0; 41 } 42 s[sn] = 0; 43 return sn; 44 } 45 bool read(int& x) 46 { 47 int n = read(str), bf; 48 if (!n) 49 return 0; 50 int i = 0; 51 if (str[i] == '-') 52 bf = -1, i++; 53 else 54 bf = 1; 55 for (x = 0; i < n; i++) 56 x = x * 10 + str[i] - '0'; 57 if (bf < 0) 58 x = -x; 59 return 1; 60 } 61 }; 62 priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q[10]; 63 int a[maxn][11],v[10],vis[maxn]; 64 int main() 65 { 66 int t,n,k; 67 reads(t); 68 //cin>>t; 69 while(t--) 70 { 71 //cin>>n>>k; 72 reads(n); 73 reads(k); 74 for(int i=1; i<=k; i++) 75 //cin>>v[i]; 76 reads(v[i]); 77 for(int i=1; i<=n; i++) 78 for(int j=1; j<=2*k; j++) 79 //cin>>a[i][j]; 80 reads(a[i][j]); 81 for(int i=1; i<=k; i++) 82 while(!q[i].empty()) 83 q[i].pop(); 84 for(int i=1; i<=n; i++) 85 q[1].push(mp(a[i][1],i)); 86 int ans=0; 87 fillchar(vis,0); 88 while(1) 89 { 90 int flag=1; 91 for(int i=1; i<=k; i++) 92 { 93 while(!q[i].empty()) 94 { 95 pii p=q[i].top(); 96 if(v[i]<p.fi) 97 break; 98 else 99 vis[p.se]++; 100 if(vis[p.se]==k) 101 { 102 flag=0; 103 for(int j=k+1; j<=2*k; j++) 104 v[j-k]+=a[p.se][j]; 105 ans++; 106 } 107 else 108 { 109 q[i+1].push(mp(a[p.se][i+1],p.se)); 110 } 111 q[i].pop(); 112 } 113 } 114 if(flag) 115 break; 116 } 117 printf("%d\n",ans); 118 for(int i=1; i<k; i++) 119 { 120 printf("%d ",v[i]); 121 } 122 printf("%d\n",v[k]); 123 } 124 }