zoukankan      html  css  js  c++  java
  • P3078 [USACO13MAR]扑克牌型Poker Hands

    题目描述

    Bessie and her friends are playing a unique version of poker involving a deck with N (1 <= N <= 100,000) different ranks, conveniently numbered 1..N (a normal deck has N = 13). In this game, there is only one type of hand the cows can play: one may choose a card labeled i and a card labeled j and play one card of every value from i to j. This type of hand is called a "straight".

    Bessie's hand currently holds a_i cards of rank i (0 <= a_i <= 100000). Help her find the minimum number of hands she must play to get rid of all her cards.

    一个牛有N堆牌,每堆排数量不等。一只牛一次可以将第i张到第j张各打一张出去,问最少几次打完

    输入输出格式

    输入格式:

    * Line 1: The integer N.

    * Lines 2..1+N: Line i+1 contains the value of a_i.

    输出格式:

    * Line 1: The minimum number of straights Bessie must play to get rid of all her cards.

    输入输出样例

    输入样例#1: 
    5 
    2 
    4 
    1 
    2 
    3 
    
    输出样例#1: 
    6 
    

    说明

    Bessie can play a straight from 1 to 5, a straight from 1 to 2, a straight from 4 to 5, two straights from 2 to 2, and a straight from 5 to 5, for a total of 6 rounds necessary to get rid of all her cards.

    Solution:

      本题思路比较巧妙~。

      我们可以先画图模拟一下,会发现每次连在一起的区间(不含$0$),肯定是同时去消除最优,即$ans$累加该区间最小值,整个区间减去这段的最小值),按照上述过程去模拟,求出的$ans$是一定正确的。因为我们尽可能的消除了每个连续区间的最小值,假设不消除整段区间,后面至少也会用相同的次数去消除这段区间,所以保证了后面不会有答案比此更优。(说的不够清白,可以画图模拟)。这样其实就可以用线段树维护了,但是可以更加简单。

      我们仔细思考上面模拟的过程,会发现,每次让一个数被清$0$的同时去尽可能多的让其它数也有所减少,一定是最优的。那么每读入一个数,我们就让其和前一个数看是否能组成一个区间去整体消除。

      也就是意味着开始时我们以第一次读入的数$a_1$作为区间最大值,然后$ans$至少需要累加$a_1$(很显然,消除一个数$a_1$至少要$a_1$次),当读入$a_2$时,若$a_2>a_1$那么要消除$a_2$至少还要$a_2-a_1$次,于是$ans$加上$a_2-a_1$,若$a_2leq a_1$那么就不需要累加,因为在前面$a_1$的消除中就能同时消去$a_2$。由于要是一段连续的区间,所以每个新读入的值$a_i$只与前一个数$a_{i-1}$有关(再往前就不保证区间一定连续了),于是重复上面的步骤就$OK$了。

      整体复杂度$O(n)$,实在是太巧妙了!~。

    代码:

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    ll a,b,ans,n;
    int main(){
        scanf("%lld",&n);
        while(n--){
            scanf("%lld",&a);
            if(a>b)ans+=(a-b);
            b=a;
        }
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    什么是BFC?
    获取JavaScript对象的键值对两种方法的不同之处
    浏览器什么时候会引起reflow,应该怎样避免reflow的开销呢?
    用js实现跳转页面的方法
    停止animate动画和判断是否处于动画状态
    解决slideDown(),slideUp()鼠标来回进入的问题
    IE7浏览器绝对定位被下边元素遮挡问题解决办法
    前端开发面试要点及对策
    inline-block元素之间空白间距的解决办法
    web前端开发和移动前端开发的本质区别在哪里?
  • 原文地址:https://www.cnblogs.com/five20/p/9076381.html
Copyright © 2011-2022 走看看