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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值