标签:uva acm oj 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 with water; if the lake is already full, then it will overflow, which will result in a natural disaster. Fortunately, the citizens have a dragon at their disposal (and they will not hesitate to use it). The dragon may drink the whole water from a lake in one sitting. Also, the mages of Ardenia already predicted the weather conditions for the next couple of years. The only question is: from which lake and when should the dragon drink to prevent a catastrophe?
The first line of the input instance contains two space-separated positive integers n106 and m106 , where n is the number of lakes. (There are at most 10 input instances for which n105 or m105.) The second line contains the weather forecast for the next m days: m space-separated integers t1, t2,..., tm (ti [0, n]). If ti [1, n], it means a heavy rainfall over lake ti at day i. If ti = 0, there is no rain at day i, and the dragon has the time to drink the water from one lake of your choice. Note that the dragon does not drink on a rainy day.
For each test case, your program has to write an output conforming to the format described below.
In the first line your program should output word `YES‘ if it is possible to prevent a catastrophic overflow and `NO‘ otherwise. In the former case, you should output the second line containing l integers from the range [0, n], where l is the number of zeros in the weather forecast description, i.e., the number of non-rainy days. Each of these integers denotes the number of the lake from which the dragon should drink; zero means the dragon should not drink from any lake (this might be necessary, as even the dragon cannot drink from an empty lake).
4 2 4 0 0 1 1 2 4 0 1 0 2 2 3 0 1 2 2 4 0 0 0 1
NO YES 1 2 NO YES 0 1 0贪心,用数组记录每个湖上次满水的日子,用集合记录不下雨的日子。下雨的时候,查找当前湖最后灌满的日子之后有没有不下雨的日子,龙在最近的一天喝光湖里的水。刚开始本来想一边读取一边处理的,但是函数跳转的时候流操作对一个数读取了两次,导致直接出错了,最后就先把输入存下来了。
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <cmath> #include <functional> using namespace std; const int maxn = 1000005; int n, m, A[maxn], ans[maxn], full[maxn]; void init() { memset(ans, 0, sizeof(ans)); memset(full, 0, sizeof(ans)); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> A[i]; } } void solve() { set<int> E; bool err = false; for (int i = 1; i <= m && !err; i++) { if (A[i] > 0) { // 查找当前湖最后灌满的日子之后有没有不下雨的日子 set<int>::iterator it = E.lower_bound(full[A[i]]); if (it != E.end() && *it > full[A[i]]) { ans[*it] = A[i]; E.erase(it); full[A[i]] = i; // 湖满水的日子 } else { err = true; } } else { E.insert(i); } } if (err) { cout << "NO\n"; } else { cout << "YES\n"; bool flag = false; for (int i = 1; i <= m; i++) { if (A[i] == 0) { if (flag) { cout << ' '; } flag = true; cout << ans[i]; } } cout << endl; } } int main() { ios::sync_with_stdio(false); int T; cin >> T; while (T--) { init(); solve(); } return 0; }
标签:uva acm oj 1623 enter the dragon
原文地址:http://blog.csdn.net/zyq522376829/article/details/46670191