zoukankan      html  css  js  c++  java
  • UPC-5588 Simplified mahjong(思维)

    题目描述
    Snuke has a large collection of cards. Each card has an integer between 1 and N, inclusive, written on it. He has Ai cards with an integer i.

    Two cards can form a pair if the absolute value of the difference of the integers written on them is at most 1.

    Snuke wants to create the maximum number of pairs from his cards, on the condition that no card should be used in multiple pairs. Find the maximum number of pairs that he can create.

    Constraints
    1≤N≤105
    0≤Ai≤109(1≤i≤N)
    All input values are integers.
    输入
    The input is given from Standard Input in the following format:

    N
    A1
    :
    AN
    输出
    Print the maximum number of pairs that Snuke can create.
    样例输入
    4
    4
    0
    3
    2
    样例输出
    4
    提示
    For example, Snuke can create the following four pairs: (1,1),(1,1),(3,4),(3,4).

    题意:给n堆卡牌,每堆卡牌有ai张,每张上的值是i,相差绝对值小于1的卡牌可以做一对,问最多可以凑多少对。
    那么直接对每堆卡除2模2,然后对剩余的卡牌两两尽量凑成一对。对于最后捡剩余1张卡凑对的情况,我们应尽量使单个留到的后面,那么我们就将不是1个单独卡牌和下一张卡牌预先凑在一起,这样就形成了尽量将单张卡牌后挪的操作。

    #include<bits/stdc++.h>
    #define LL long long
    using namespace std;
    int main()
    {
        int n,a[100005];
        bool vis[100005];
        while(scanf("%d",&n)!=EOF)
        {
            LL ans=0;
            memset(vis,false,sizeof(vis));
            for(int i=0; i<n; i++)
            {
                scanf("%d",&a[i]);
                if(a[i])vis[i]=true;
                ans+=a[i]/2;
                a[i]%=2;
            }
            for(int i=1; i<n; i++)
            {
                if(a[i]&&a[i-1])
                {
                    a[i]=0;
                    a[i-1]=0;
                    ans++;
                }
                else if(a[i]==0&&a[i-1])
                    if(vis[i]) a[i]++,a[i-1]--;
            }
            printf("%lld
    ",ans);
        }
    }
    
  • 相关阅读:
    pip install selenium==版本号 报错
    解决phantomjs输出中文乱码
    phantomjs学习之网页访问测速
    phantomjs学习之截图
    bzoj1069-最大土地面积
    hdu4372-Count the Buildings
    bzoj3786-星系探索
    Codeforces633H-Fibonacci-ish II
    hdu3625-Rooms
    斯特林数
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135795.html
Copyright © 2011-2022 走看看