D. 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
inputCopy

5
3 2 2 2 1
outputCopy
4
Note
Some of the possible solutions for the example:

解析:基于涂色用黑白两色给图上色,相邻方格不同色。数量少的颜色一定可匹配完。取两者最小值



#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1000;
typedef long long ll;
int c[N],n; 
ll b[N];
int main()
{
	cin>>n;
	for(ll i=0;i<n;i++)
	{
		cin>>c[i];
		b[i%2]+=c[i]/2+c[i]%2;
		b[(i+1)%2]+=c[i]/2;
	}
	cout<<min(b[0],b[1])<<endl;
}
发布了309 篇原创文章 · 获赞 6 · 访问量 5261

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104077099