题目描述
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);
}
}