CF1269D. Domino for Young (贪心)

You are given a Young diagram.

Given diagram is a histogram with 𝑛 columns of lengths 𝑎1,𝑎2,…,𝑎𝑛 (𝑎1≥𝑎2≥…≥𝑎𝑛≥1).

Young diagram for 𝑎=[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 𝑛 (1≤𝑛≤300000): the number of columns in the given histogram.

The next line of input contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤300000,𝑎𝑖≥𝑎𝑖+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
inputCopy
5
3 2 2 2 1
outputCopy
4
Note
Some of the possible solutions for the example:

题意: 给出方块,求出最多能放多少个1X2或者2X1的多米诺骨牌
思路: 试着放一放,发现每一列首先要放到只剩下一个或者没有,也就是先放2X1(竖)。再可以通过移动方块,尽量连接剩下的方块。怎么连接呢?
贪心的想的话,每一列最上面那一个,可能是个1X1空方格,该列方块同时上移,把空方格全部转移到下层。我们要使得底层的补全1X2(横)。很明显(并没有),当空方格间隔为偶数的时候,可以移到一个1X2,因为偶数就可以拆成1X2。你可能会问,一开始填的是竖的,你最底下一层移动了,中间那些竖的会不会空出1X1的。这个好办,中间那一坨一定是偶数!画图就可以看出来了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>

using namespace std;

typedef long long ll;

int a[300005];
stack<int>s;

int main()
{
    int n;scanf("%d",&n);
    ll ans = 0;
    for(int i = 1;i <= n;i++)
    {
        scanf("%d",&a[i]);
        ans += a[i] / 2;
        a[i] %= 2;
        if(a[i] == 0)continue;
        if(!s.size() || s.top() == i%2) s.push(i%2);
        else {
            s.pop();
            ans++;
        }
    }
    
//    for(int i = 1;i <= n;i++)//一开始只考虑相邻两个1的情况,但是不完全
//    {
//        if(a[i] == 1 && a[i + 1] == 1)
//        {
//            ans++;
//            i++;
//        }
//    }
    printf("%lld\n",ans);
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值