Codeforces Round #609 (Div. 1) B.Domino for Young

Codeforces Round #609 (Div. 1) B.Domino for Young

题目链接

You are given a Young diagram.

Given diagram is a histogram with n columns of lengths a1,a2,…,an (a1≥a2≥…≥an≥1).
在这里插入图片描述

Young diagram for a=[3,2,2,2,1].
Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1×2 or 2×1 rectangle.

Input

The first line of input contain one integer n (1≤n≤300000): the number of columns in the given histogram.

The next line of input contains n integers a1,a2,…,an (1≤ai≤300000,ai≥ai+1): the lengths of columns.

Output

Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram.

Example

input

5
3 2 2 2 1

output

4

比较有意思的一道题目,考虑贪心~
我们对每一列,先填 2×1 的多米诺骨牌,可以发现当这一列行数为偶数时,正好填满,如果为奇数时,正好空一格,我们把空出的一格移到最下面,填 1×2 的多米诺骨牌,不难发现,当出现奇数列和偶数列都有空格时就可以填一个 1×2 的多米诺骨牌,比如:
在这里插入图片描述

贪心得到最优解即可,AC代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int main()
{
    int n;
    stack<int>s;
    cin>>n;
    ll a[n+1],ans=0,flag=-1;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        ans+=a[i]/2;
        if(a[i]%2==0) continue;
        if(!s.size() || s.top()==i%2) s.push(i%2);
        else{
            s.pop();
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}
发布了479 篇原创文章 · 获赞 38 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/qq_43765333/article/details/105734862