uva 1623 - Enter The Dragon

来源:互联网 发布:淘宝如何申请海外买手 编辑:程序博客网 时间:2024/05/09 23:45

题目大意:一个城市里有n个湖,每个湖中是装满水的。现在天气预报给定了后面m天得天气情况,对每一天,要么不下雨,要么就对某一个湖下雨。在一个装满水的湖上下雨,会导致水灾,所以请来了龙来喝水,龙只在不下雨的时候喝水,现在问如何安排在不下雨的天喝哪壶水才能使得不发生水灾。


解题:贪心。即将要下雨的湖肯定是不能够装满水的,所以我们得找到在它之前的最远的一个不下雨的天,来喝它这湖水。注意湖要是装满水的才能喝,所以呢,这个最远位置又不能小于其向左看的最近的要在这个湖下雨的那天。。。。这样的话这一题就解决了。虽然时间上不是很乐观

0.600s。


////  main.cpp//  uva 1623 - Enter The Dragon////  Created by XD on 15/8/19.//  Copyright (c) 2015年 XD. All rights reserved.//#include <iostream>#include <string>#include <queue>#include <stack>#include <stdio.h>#include <stdlib.h>#include <math.h>#include<vector>#include <string.h>#include <string>#include <algorithm>#include <set>#include <map>#include <cstdio>using namespace std ;const int maxn = 1000000 + 5  ;int forecast[maxn] ;int l[maxn] ;int pre[maxn] ;int rain[maxn]  ;set<int > notrain ;int notrainday[maxn] ;int vis[maxn] ;int main() {    int T,n,m ;    scanf("%d" ,&T) ;    while (T--) {        scanf("%d%d" ,&n,&m) ;        memset(l, 0 , sizeof(l)) ;        int t = 0 ;        int t1 = 0 ;//        l.clear() ;        notrain.clear() ;        for (int i = 0; i < m ; i++) {            scanf("%d" ,&forecast[i]) ;            if (forecast[i] == 0 ) {                notrain.insert(i) ;                notrainday[t1++] = i  ;                continue ;            }            rain[t++] = i ;            if (l[forecast[i]] != 0 ) {                  pre[i] = l[forecast[i]];                  l[forecast[i]] = i ;                }            else{                l[forecast[i]] = i ; pre[i] = -1 ;            }            //            if (l.count(forecast[i])) {//                pre[i] = l[forecast[i]] ;//            }//            else  {//                l[forecast[i]] = i ; pre[i] = -1 ;//            }        }        memset(vis, 0, sizeof(vis)) ;        if (t > m - t ) {            printf("NO\n") ;            continue ;        }        else{            int flag = 1 ;            notrain.insert(m + 1) ;            for(int i = 0 ;i <t ; i++)            {                set<int>::iterator it = notrain.lower_bound(pre[rain[i]]) ;                if (*it >= rain[i]||*it > m) {                    flag = 0 ;                    break ;                }                else{                    vis[*it] = forecast[rain[i]] ;                    notrain.erase(*it) ;                }            }            if (flag == 0 ) {                printf("NO\n") ;continue ;            }            else{                printf("YES\n") ;                notrain.erase(m+1) ;                for(int i = 0 ; i < t1-1  ; i++)                {                    printf("%d " , vis[notrainday[i]]) ;                }                printf("%d\n" ,vis[notrainday[t1-1]] ) ;            }        }    }    return 0;}


0 0
原创粉丝点击