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: mspace-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.
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
#include <cstdio> #include <set> #include <cstring> using namespace std; int drink[1000010]; int water[1000010]; // 代表这次下雨已经喝过 int flag[1000010]; int lasttime[1000010]; typedef struct node { int num; int last_time; int place; bool operator < (const struct node&x) const { return last_time < x.last_time || (last_time == x.last_time && place < x.place); } }node; node array[1000010]; set<int> d; int main() { int Z; scanf("%d", &Z); int count = 0; while(count < Z) { int n, m; scanf("%d%d", &n, &m); // 读入情况 memset(drink, 0, sizeof(drink)); memset(lasttime, 0, sizeof(lasttime)); d.clear(); int false_flag = 0; for(int i = 0; i < m; i++) { scanf("%d", &water[i]); if(false_flag) continue; if(water[i] == 0) d.insert(i); else { set<int>::iterator p = d.lower_bound(lasttime[water[i]]); if(p == d.end()) false_flag = 1; else { drink[*p] = water[i]; lasttime[water[i]] = i; d.erase(*p); } } /* if(flag[water[i]] == 0) { flag[water[i]] = i; lasttime[i] = -1; } else { lasttime[i] = flag[water[i]]; flag[water[i]] = i; } if(water[i] != 0) { array[rain_num].num = water[i]; array[rain_num].last_time = lasttime[i]; array[rain_num].place = i; rain_num++; } */ } /* for(int i = 0; i < n; i++) flag[i] = 0; */ /* int i; for(i = 0; i < m; i++) { if(water[i] != 0) { int j; int find_flag = 0; for(j = lasttime[i]+1; j < i; j++) { if(water[j] == 0 && drink[j] == 0) { drink[j] = water[i]; find_flag = 1; break; } } if(!find_flag) break; } } */ /* sort(array, array+rain_num); int begin = 0; for(int i = 0; i < m; i++) { if(water[i] == 0) { if(begin < rain_num) { if(array[begin].last_time < i) { drink[i] = array[begin].num; begin++; } } } } */ if(false_flag) printf("NO\n"); else { printf("YES"); int first_time = 1; for(int i = 0; i < m; i++) { if(water[i] == 0) { if(first_time) { printf("\n%d", drink[i]); first_time = 0; } else printf(" %d", drink[i]); } } printf("\n"); } /* int empty_p = m-1; int false_flag = 0; for(int i = m-1; i >= 0; i--) { // 为该下雨天找一个能喝的天 if(water[i] != 0) { // empty_p--; if(empty_p >= i) empty_p = i-1; while(empty_p >= 0) { if(water[empty_p] == water[i]) { false_flag = 1; break; } else if(water[empty_p] == 0) { drink[empty_p] = water[i]; break; } empty_p--; } if(empty_p < 0 || false_flag) { false_flag = 1; break; } else empty_p--; } } if(false_flag) printf("NO\n"); else { printf("YES\n"); int first_time = 1; for(int i = 0; i < m; i++) { if(water[i] == 0) { if(first_time) { printf("%d", drink[i]); first_time = 0; } else printf(" %d", drink[i]); } } printf("\n"); } */ count++; } return 0; }
这个题一开始想错了,应该是每个下雨的日子距离上次该湖下雨以后取一个最近的不下雨日子。