uva 1623 - Enter The Dragon

题目大意:一个城市里有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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值