Sgu 548 dragons and princesses

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.