hihocoder 2017微软探星夏令营在线技术笔试 1534 Array Partition
2017-07-20 14:17
337 查看
#1534 : Array Partition
时间限制:10000ms单点时限:1000ms
内存限制:256MB
描述
Given an integer array A1, A2 ...AN, you are asked to split the array into three continuous parts:
A1, A2 ... Ap | Ap+1,
Ap+2, ... Aq | Aq+1,
Aq+2, ... AN.
Let S1, S2 and S3 denote the sums of each part:
S1 = A1 + A2 + ... +
Ap
S2 = Ap+1 + Ap+2 + ... +
Aq
S3 = Aq+1 + Aq+2 + ... +
AN
A partition is acceptable if and only if the differences between S1,
S2 and S3 (i.e. |S1 -
S2|, |S2 - S3|, |S3 -
S1|) are no more than 1.
Can you tell how many different partitions are acceptable?
输入
The first line contains an integer N.The second line contains N integers, A1, A2 ...
AN.
For 30% of the data, N ≤ 100
For 70% of the data, N ≤ 1000
For 100% of the data, N ≤ 100000, -1000000 ≤ Ai ≤ 1000000
输出
The number of acceptable partitions.样例解释
The three acceptable partitions are:3 | 2 | 1 0 2
3 | 2 1 | 0 2
3 | 2 1 0 | 2
样例输入
5 3 2 1 0 2
这个题不难想到保证满足题目条件的话,s1,s2,s3至少有两个相等,那么把有至少有两个相等的所有情况写出来,就可以看出来可以分总和%3的情况讨论一下:
sum%3 == 0:分成的三个数必须都是相等的sum / 3
sum%3 == 1:必须是有两个是sum/3 + 1,另外一个是sum / 3
所以题目就变成了s1 = a, s2 = b, s3 = c, a + b + c = s 的方案数。
在这里我当时出问题了,没考虑清楚一些0的情况,看了下别人的思路还是挺巧妙地。
map<ll, ll> ma;
ll work(ll x, ll y, ll z) {
ll res = 0;
ma.clear();
ll as = 0;
for (int i = 0; i < n - 1; i++) {
as += a[i];
if(as == x + y) {
res += ma[x];
}
ma[as]++;
}
return res;
}
相关文章推荐
- 2017微软探星夏令营在线技术笔试
- 2016微软探星夏令营在线技术笔试题解(1)
- 2016微软探星夏令营在线技术笔试题解(2)
- 2016微软探星夏令营在线技术笔试题解(3)
- Constraint Checker 2016微软探星夏令营在线技术笔试
- 2016微软探星夏令营在线技术笔试题解(4)
- 【在线笔试题解题报告系列】微软在线笔试之 2016微软探星夏令营在线技术笔试(时间:2016.07.17)
- 2016微软探星夏令营在线技术笔试-#1342 : Full Binary Tree Picture
- 2016微软探星夏令营在线技术笔试-#1341 : Constraint Checker
- 微软2017实习生在线笔试题——hihocoder 1289——403 Forbidden
- 2016微软探星夏令营在线技术笔试
- 【hihocoder#1341】(微软探星夏令营在线技术笔试第一题)
- hihocoder 1290 -- 微软2016校园招聘4月在线笔试-3-Demo Day
- 2017微软秋季校园招聘在线编程笔试(第一题)
- hihocoder 1534 微软探星夏令营笔试A题 (STL)
- 2017微软秋季校园招聘在线编程笔试 Composition
- 2017微软秋季校园招聘在线编程笔试(第二题)
- 微软2016校园招聘4月在线笔试 hihocoder 1290 Demo Day (dp)
- 【hihocoder1290 微软2016校园招聘4月在线笔试C】【二维DP】 Demo Day 机器人遇到障碍向右走向下走 最少调整数使得左上角走到右下角
- hihocoder - 1489 (微软2017笔试A题)概率 + 模拟