UVa1623 Enter The Dragon (贪心)

链接:http://bak2.vjudge.net/problem/UVA-1623

分析:用集合st保存当前还可以让神龙喝干湖水的日子,数组full[i]保存i号湖上次满水的日子。每当到了要往湖里下雨的日子,在集合st里查找这个湖最后一次满水的日子后第一个没被用掉不下雨的日子去喝干这个湖水,维护这个湖最后一次满水的日子,把用掉的日子从集合st里删除。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <set>
 5 using namespace std;
 6
 7 const int maxn = 1000000 + 5;
 8
 9 int n, m, full[maxn], ans[maxn];
10 set<int> st;
11
12 int main() {
13     int T;
14     scanf("%d", &T);
15     while (T--) {
16         scanf("%d%d", &n, &m);
17         memset(full, 0, sizeof(full));
18         memset(ans, 0, sizeof(ans));
19         st.clear();
20         bool ok = true;
21         for (int i = 1; i <= m; i++) {
22             int t; scanf("%d", &t);
23             if (!ok) continue;
24             if (!t) st.insert(i);
25             else {
26                 ans[i] = -1;
27                 set<int>::iterator it = st.upper_bound(full[t]);
28                 if (it == st.end()) ok = false;
29                 else {
30                     ans[*it] = t;
31                     full[t] = i;
32                     st.erase(*it);
33                 }
34             }
35         }
36         if (!ok) printf("NO\n");
37         else {
38             printf("YES");
39             bool first = true;
40             for (int i = 1; i <= m; i++)
41                 if (ans[i] >= 0) printf("%c%d", first?(first = false, ‘\n‘) : ‘ ‘, ans[i]);
42             printf("\n");
43         }
44     }
45     return 0;
46 }
时间: 2024-08-09 17:55:02

UVa1623 Enter The Dragon (贪心)的相关文章

UVA - 1623 Enter The Dragon(贪心)

题目: 思路: 读完题之后有了以下想法: 当遇到下雨的天,就找这个湖泊上一次下雨满了之后又一次不下雨的日期.有就在这个日期下记录被神龙喝干的湖的编号,没有就是不符合题意. 这个想法是对的,但是却被代码卡的死死的.知道看到了大佬用set写的-- set本身是有序的,而且也有二分查找的方法. 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1e3 #define FRE() freopen("in.txt&q

UVa - 1623 - Enter The Dragon

The capital of Ardenia is surrounded by several lakes, and each of them is initially full of water. Currently, heavy rainfalls are expected over the land. Such a rain falls to one of the lakes: if the lake is dry and empty, then it will be filled wit

UVA 11292 Dragon of Loowater(简单贪心)

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato

·UVa」 11292 - Dragon of Loowater( 贪心 )

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato

[UVA] 11292 - Dragon of Loowater [贪心+队列]

Problem C: The Dragon of Loowater Time limit: 1.000 seconds Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese.

Uva11292--------------(The Dragon of Loowater)勇者斗恶龙 (排序后贪心)

---恢复内容开始--- 题目: Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predators, the geese pop

HDU 4362 Dragon Ball 贪心DP

Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon balls will appear. some dragon balls will appear in a line at the same time for each period.Since the time you got one of them,the other dragon ball wil

UVa 11292 - Dragon of Loowater(排序贪心)

Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese.Due to the lack of predators,the geese population was out of c

poj 3646 The Dragon of Loowater 排序+贪心

水题,直接贴代码. //poj 3646 //sep9 #include <iostream> #include <algorithm> using namespace std; const int maxN=20012; int a[maxN],b[maxN]; int main() { int n,m; while(scanf("%d%d",&n,&m)==2&&n+m){ int i,j; for(i=0;i<n;++