Question:
N grids each grid has a dragon or a princess warrior from 1 to n passing by the dragon can be killed can not kill the rich take passing by the princess if the number of killed dragons meet the princess requirements will stop walking ask the warrior to think more take the money, but the princess must meet the requirements of N grids.
Ideas:
The princess only limits the number of dragons, so she doesn't want to stop and get married, so she can control the number of dragons to kill. If she wants to give up some dragons, she will be greedy and give up the dragon with less money. Finally, she can determine whether she can marry the princess.
Code:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<map>#include<set>#include<vector>#include<queue>#include<cstdlib>#include<ctime>#include<cmath>using namespace std;#define N 200010int n;struct dragon {int x, id;bool operator<(const dragon ff) const {return x > ff.x;}} now;priority_queue<dragon> q;int ans[N], tot, sum;int main() {int i, k, s;char who[100];while (!q.empty())q.pop();scanf("%d", &n);for (i = 2; i <= n; i++) {scanf("%s%d", who, &k);if (i == n)break;if (who[0] == 'd') {now.x = k;now.id = i;q.push(now);} else {s = q.size();if (s >= k) {k = s - k + 1;while (k--)q.pop();}}}s = q.size();if (s < k)printf("-1\n");else {tot = sum = 0;while (!q.empty()) {now = q.top();q.pop();sum += now.x;ans[tot++] = now.id;}printf("%d\n", sum);printf("%d\n", tot);sort(ans, ans + tot);for (i = 0; i < tot; i++)printf("%d%s", ans[i], (i == tot - 1) ? "\n" : " ");}return 0;}
Sgu 548 dragons and princesses