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\n",ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值