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

本文详细解析了Codeforces Round #609 (Div.1) B题DominoforYoung的算法思路,通过贪心策略解决在Young图表中放置最多非重叠2x1或1x2多米诺骨牌的问题,并附带AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值