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 Z40,
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 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 t1t2,..., 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.

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;
}
时间: 2024-08-25 02:07:51

UVa - 1623 - 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 Enther the Dragon 神龙喝水

贪心,每次遇到一个满水的湖要下暴雨的时候,就往前找之前最后一次满水之后的第一个没有下雨的且没有被用掉天day1. 因为如果不选这day1,那么之后的湖不一定能选上这一天.如果这一天后面还有没有下雨的天day2的话,选后面的,会使得day1到day2之前满水的湖选择减少. #include<bits/stdc++.h> #define PB push_back #define MP make_pair #define fi first #define second #define FOR(i,s

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

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

[2016-03-14][UVA][11292][Dragon of Loowater]

时间:2016-03-14 19:50:12 星期一 题目编号:[2016-03-14][UVA][11292][Dragon of Loowater] 题目大意: 有m个骑士要砍n条龙,每个骑士能看掉龙的头颅当且仅当其实的能力值大于龙的头的直径,每个其实砍掉一条龙需要付出数值上等于能力值的代价,问m个骑士能否砍完所有龙,能则输出最小代价,否则输出"Loowater is doomed!" 输入: 多组数据 每组数据 n m n行 龙头的直径 m行 骑士的能力值 输出: 如果能砍完所有

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

1 /*UVa 11292 Dragon of Loowater*/ 2 #include<iostream> 3 #include<cstdio> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 int n,m; 8 int main(){ 9 while(scanf("%d%d",&n,&m) && n!=0 &&

·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