codeforces895A Pizza Separation 思维

Pizza Separation

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Students Vasya and Petya are studying at the BSU (Byteland State University). At one of the breaks they decided to order a pizza. In this problem pizza is a circle of some radius. The pizza was delivered already cut into n pieces. The i-th piece is a sector of angle equal to ai. Vasya and Petya want to divide all pieces of pizza into two continuous sectors in such way that the difference between angles of these sectors is minimal. Sector angle is sum of angles of all pieces in it. Pay attention, that one of sectors can be empty.

Input
The first line contains one integer n (1?≤?n?≤?360)  — the number of pieces into which the delivered pizza was cut.

The second line contains n integers ai (1?≤?ai?≤?360)  — the angles of the sectors into which the pizza was cut. The sum of all ai is 360.

Output
Print one integer  — the minimal difference between angles of sectors that will go to Vasya and Petya.

Examples
inputCopy
4
90 90 90 90
outputCopy
0
inputCopy
3
100 100 160
outputCopy
40
inputCopy
1
360
outputCopy
360
inputCopy
4
170 30 150 10
outputCopy
0

思路:

题意大致就是有两个人要分一张360C的圆饼,圆饼共有n份,每份有一个角度,这两个人分到的饼必须是连续的,注意后面的数也是跟前面的连着的,我们可以这样思考,一个人分得的饼的度数越接近180,那么差值也就越小,于是这道题变成了,求一段连续子串和与180的最小差值,但子串不能与母串相同

代码:

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
int a[400];
int main()
{
	int n;
	scanf("%d",&n);
	for (int i = 0;i < n;i ++)
		scanf("%d",&a[i]);
	queue<int> que;
	int sum = 0,ans = inf;
	for (int i = 0;i < n;i ++)
	{
		ans = min(abs(360 - sum - sum),ans);
		que.push(a[i]); 
		sum += a[i];
		ans = min(abs(360 - sum - sum),ans);//对于和小于180的也要比较一下 
		while (sum >= 180)
		{
			int p = que.front();
			sum -= p;
			que.pop();
			ans = min(abs(360 - sum - sum),ans);
		}
			
	}
	if (que.size() == n)
		ans = min(abs(360 - (sum - a[0]) * 2),abs(360 - (sum - a[n - 1] * 2)));
	printf("%d\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/cloudy_happy/article/details/81843034
今日推荐