Codeforces Round #448 (Div. 2) A. Pizza Separation【前缀和/枚举/将圆(披萨)分为连续的两块使其差最小】
1 second
256 megabytes
standard input
standard 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 toai. 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.
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.
Print one integer — the minimal difference between angles of sectors that will go to Vasya and Petya.
4
90 90 90 90
0
3
100 100 160
40
1
360
360
4
170 30 150 10
0
In first sample Vasya can take 1 and 2 pieces, Petya can take 3 and 4 pieces. Then the answer is |(90 + 90) - (90 + 90)| = 0.
In third sample there is only one piece of pizza that can be taken by only one from Vasya and Petya. So the answer is |360 - 0| = 360.
In fourth sample Vasya can take 1 and 4 pieces, then Petya will take 2 and 3 pieces. So the answer is |(170 + 10) - (30 + 150)| = 0.
Picture explaning fourth sample:

Both red and green sectors consist of two adjacent pieces of pizza. So Vasya can take green sector, then Petya will take red sector.
【题意】:将圆分为连续的两块使其差最小。
【分析】:因为是连续区间并且区间可以为空,3个for枚举t或者前缀和或者尺取。去掉连续的话就是01背包变形:http://blog.csdn.net/pegasuswang_/article/details/25081783
【代码】:
#include <bits/stdc++.h> using namespace std;
int a[],sum[];
int main()
{
int n;
int ans=; scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
sum[i]=a[i]+sum[i-];
}
for(int i=;i<n;i++)
{
for(int j=i;j<n;j++)//t-(360-t)=2*t-360,t为区间所取值,因为是连续区间并且区间可以为空,3个for枚举t或者前缀和或者尺取
{
ans=min(ans,abs(*(sum[j]-sum[i-])-));
}
}
cout<<ans<<endl;
return ;
}
前缀和求连续区间和
#include <bits/stdc++.h> using namespace std;
int a[],sum;//环的话 数组起码2倍大
int main()
{
int n;
int ans=; scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
a[i+n]=a[i];
}
for(int i=;i<n;i++)
{
sum=; //注意置0的位置
for(int j=i;j<i+n;j++)
{
sum+=a[j];
ans=min(ans,abs( *sum-) );
}
}
cout<<ans<<endl;
return ;
} /*
输入数据直接复制了一遍放到后面,然后枚举拿的起点i,拿的终点j。然后计算拿了多少,差值是多少。
*/
复制数组模拟环
Codeforces Round #448 (Div. 2) A. Pizza Separation【前缀和/枚举/将圆(披萨)分为连续的两块使其差最小】的更多相关文章
- Codeforces Round #448(Div.2) Editorial ABC
被B的0的情况从头卡到尾.导致没看C,心情炸裂又掉分了. A. Pizza Separation time limit per test 1 second memory limit per test ...
- Codeforces Round #448 (Div. 2) B. XK Segments【二分搜索/排序/查找合法的数在哪些不同区间的区间数目】
B. XK Segments time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #511 (Div. 1) C. Region Separation(dp + 数论)
题意 一棵 \(n\) 个点的树,每个点有权值 \(a_i\) .你想砍树. 你可以砍任意次,每次你选择一些边断开,需要满足砍完后每个连通块的权值和是相等的.求有多少种砍树方案. \(n \le 10 ...
- Codeforces Round #448 (Div. 2) B
题目描述有点小坑,ij其实是没有先后的 并且y并不一定存在于a中 判断y的个数和所给数组无关 对于2 - 7来说 中间满足%2==0的y一共有3个 2 4 6 这样 可以看出对于每个数字a 都能够二分 ...
- Codeforces Round #448 (Div. 2)C. Square Subsets
可以用状压dp,也可以用线型基,但是状压dp没看台懂... 线型基的重要性质 性质一:最高位1的位置互不相同 性质二:任意一个可以用这些向量组合出的向量x,组合方式唯一 性质三:线性基的任意一个子集异 ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares 数学题枚举
D. Spongebob and Squares Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)
题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
随机推荐
- FMDB的线程安全
最近面试被问到FMDB的多线程处理问题,因为之前项目中是移植别人的代码,没有踩过这里的坑. 问题: 多线程同时访问数据库时,报数据库锁定的问题,错误信息是: Unknown error finaliz ...
- 新浪微博API Oauth2.0 认证
原文链接: http://rsj217.diandian.com/post/2013-04-17/40050093587 本意是在注销账号前保留之前的一些数据.决定用python 爬取收藏.可是未登录 ...
- 《Cracking the Coding Interview》——第6章:智力题——题目2
2014-03-19 06:57 题目:对于8x8的棋盘,如果拿掉对角位置的两个小块儿,能否用1x2的多米诺牌拼成剩下的棋盘? 解法:不可能.且不说8x8,NxN都是不可能的.如果N是奇数,NxN-2 ...
- Git上手:Git扫盲区
Git 自述Git 是由伟大的电脑程序员Linus Torvalds编写的一个开源的,分布式的版本控制系统软件. Git 核心原理Git 利用底层数据结构,通过指向索引对象的可变指针,保存文件快照. ...
- 每天一个Linux命令(7):pwd命令
pwd命令以绝对路径的方式显示用户当前工作目录. 语法 pwd(选项) 选项 --help:显示帮助信息: --version:显示版本信息. 实例 [root@localhost ~]# pwd / ...
- python-configparser模块,xml.etree模块
操作键值对文件 #文件db格式为 [section] a = 1 b = 2 [section1] d = 3 c = 4 import configparser #获取所有节点 config = c ...
- 上手Caffe(一)
@author:oneBite 本文记录编译使用caffe for windows 使用环境 VS2013 ultimate,win7 sp1,caffe-windows源码(从github上下载ca ...
- reinterpret_cast and const_cast
reinterpret_cast reinterpret意为“重新解释” reinterpret_cast是C++中与C风格类型转换最接近的类型转换运算符.它让程序员能够将一种对象类型转换为另一种,不 ...
- PHP7 深入理解
读 PHP7的内核剖析 php7-internal 记录 3 PHP的相关组成 3.1 SAPI php常见的四种运行模式 SAPI:Server Application Programming In ...
- eclipse搭建ssm框架的maven的工程
版本:eclipse:Indigo Service Release 2. jdk :jdk1.7.0_03. maven:apache-maven-3.3.3 . 上面的3个东西 先下载下来.然后运 ...