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 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?

Input 

The input contains several test cases. The first line of the input contains a positive integer  Z$ \le$40 , denoting the number of test cases. Then  Z  test cases follow, each conforming to the format described below.

The first line of the input instance contains two space-separated positive integers n$ \le$106 and m$ \le$106 , where n is the number of lakes. (There are at most 10 input instances for which n$ \ge$105 or m$ \ge$105.) The second line contains the weather forecast for the next m days: m space-separated integers t1t2,..., tm (ti $ \in$ [0, n]). If ti $ \in$ [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.

Output 

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).

Sample Input 

4 
2 4 
0 0 1 1
2 4 
0 1 0 2
2 3 
0 1 2 
2 4 
0 0 0 1

Sample Output 

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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值