Codeforces Round #349 (Div. 2)-B. Coat of Anticubism(二分+贪心)

B. Coat of Anticubism
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Codeforces Round #349 (Div. 2)-B. Coat of Anticubism(二分+贪心)_第1张图片

As some of you know, cubism is a trend in art, where the problem of constructing volumetrical shape on a plane with a combination of three-dimensional geometric shapes comes to the fore.

A famous sculptor Cicasso, whose self-portrait you can contemplate, hates cubism. He is more impressed by the idea to transmit two-dimensional objects through three-dimensional objects by using his magnificent sculptures. And his new project is connected with this. Cicasso wants to make a coat for the haters of anticubism. To do this, he wants to create a sculpture depicting a well-known geometric primitive — convex polygon.

Cicasso prepared for this a few blanks, which are rods with integer lengths, and now he wants to bring them together. The i-th rod is a segment of length li.

The sculptor plans to make a convex polygon with a nonzero area, using all rods he has as its sides. Each rod should be used as a side to its full length. It is forbidden to cut, break or bend rods. However, two sides may form a straight angle .

Cicasso knows that it is impossible to make a convex polygon with a nonzero area out of the rods with the lengths which he had chosen. Cicasso does not want to leave the unused rods, so the sculptor decides to make another rod-blank with an integer length so that his problem is solvable. Of course, he wants to make it as short as possible, because the materials are expensive, and it is improper deed to spend money for nothing.

Help sculptor!

Input

The first line contains an integer n (3 ≤ n ≤ 105) — a number of rod-blanks.

The second line contains n integers li (1 ≤ li ≤ 109) — lengths of rods, which Cicasso already has. It is guaranteed that it is impossible to make a polygon with n vertices and nonzero area using the rods Cicasso already has.

Output

Print the only integer z — the minimum length of the rod, so that after adding it it can be possible to construct convex polygon with(n + 1) vertices and nonzero area from all of the rods.

Examples
input
3
1 2 1
output
1
input
5
20 4 3 2 1
output
11
Note

In the first example triangle with sides {1 + 1 = 2, 2, 1} can be formed from a set of lengths {1, 1, 1, 2}.

In the second example you can make a triangle with lengths {20, 11, 4 + 3 + 2 + 1 = 10}.


题意:
给出n条木板,要你将n条合为两条,在增加一条木板,而且要是构成三角形的最短的一块木板。
思路:
这题一下子就知道是二分+贪心的套路了,因为要是最短的才行,就符合二分规则了。

AC代码:

#include<iostream>
#include<functional>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<map>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
#define QWQ ios::sync_with_stdio(0)
typedef unsigned __int64 LL;
typedef  __int64 ll;
const int T = 100000+50;
const int mod = 1000000007;
const double PI = 3.1415926535898;
int a[T];

ll s1=0,s2=0;
bool jugde(ll mid)
{
	if(s1+mid>s2&&s1+s2>mid&&s2+mid>s1)return true;
	if(s1+s2<=mid)return true;
	return false;
}

int main()
{
#ifdef zsc
	freopen("input.txt","r",stdin);
#endif

	int n,i,j,k;
	while(~scanf("%d",&n))
	{
		for(i=0;i<n;++i)scanf("%d",&a[i]);
		sort(a,a+n);
		s1=0,s2=0;
		for(i=n-1;i>=0;--i){
			if(s1<=s2){
				s1 += a[i];
			}
			else {
				s2 += a[i];
			}
		}
		int L=1,R=1000000000;
		ll ans,mid;
		while(L<=R)
		{
			mid = (L+R)/2;
			if(jugde(mid)){
				ans = mid;
				R = mid - 1;
			}
			else {
				L = mid + 1;
			}
		}
		printf("%I64d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(codeforces,二分)