AtCoder Beginner Contest 059 C - Sequence(贪心)
2017-04-22 23:47
441 查看
C - Sequence
Time limit : 2sec / Memory limit : 256MBScore : 300 points
Problem Statement
You are given an integer sequence of length N.The i-th term in the sequence is ai.
In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
For every i (1≤i≤n),
the sum of the terms from the 1-st through i-th
term is not zero.
For every i (1≤i≤n−1),
the sign of the sum of the terms from the 1-st through i-th
term, is different from the sign of the sum of the terms from the 1-st through(i+1)-th
term.
Constraints
2≤n≤105|ai|≤109
Each ai is
an integer.
Input
Input is given from Standard Input in the following format:n a1 a2 … an
Output
Print the minimum necessary count of operations.Sample Input 1
Copy4 1 -3 1 0
Sample Output 1
Copy4
For example, the given sequence can be transformed into 1,−2,2,−2 by
four operations. The sums of the first one, two, three and four terms are 1,−1,1 and −1,
respectively, which satisfy the conditions.
Sample Input 2
Copy5 3 -6 4 -5 7
Sample Output 2
Copy0
The given sequence already satisfies the conditions.
Sample Input 3
Copy6
-1 4 3 2 -5 4
Sample Output 3
Copy8
题目大意:给n个数字组成的数组,可以对任何数字进行删减添加,每次删减添加1为一次操作步数,问满足以下两个条件下,最小操作次数
1.要求1-i个数字和不为0,1<=i<=n
2.要求1-(i-1)的和与i的正负相反,也就是说,当前i-1个数字为正数时候,要求加上第i个数字后,之后为负数
解题思路:这个就是当某个数不符合条件的时候,尽可能的使得和为-1或者1,这样才能让操作次数尽可能的小,考虑到这种情况,1到n只可能是正负正负正负,或者负正负正负正两种情况,同时判断,取个最小值,同时要注意的是,当第一个数是0的时候的处理,WA了无数发才发现对第一个数为0的设定不符合情况就尴尬了
#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<vector>
#include <bitset>
#include<algorithm>
#include <queue>
#include<map>
#include<stack>
using namespace std;
long long int ans1, ans2, sum1, sum2;
int n, i;
long long int a[100005];
int main()
{
cin >> n;
for (i = 1; i <= n; i++)
{
cin >> a[i];
}
if (a[1] >0)
{
sum1 = a[1];
ans2= a[1] + 1;
sum2 = -1;
}
else if (a[1] == 0)
{
sum1 = 1;
ans1 = ans2 = 1;
sum2 = -1;
}
else
{
sum1 = 1;
ans1 = abs(a[1]) + 1;
sum2 = a[1];
}
for (i = 2; i <= n; i++)
{
if (sum1 > 0)
{
if (a[i] + sum1 >= 0)
{
ans1 += a[i] + sum1 + 1;
sum1 = -1;
}
else
{
sum1 += a[i];
}
}
else
{
if (a[i] + sum1 <= 0)
{
ans1 += abs(sum1 + a[i]) + 1;
sum1 = 1;
}
else
{
sum1 += a[i];
}
}
}
if (sum1 == 0)
{
ans1++;
}
for (i = 2; i <= n; i++)
{
if (sum2 > 0)
{
if (a[i] + sum2 >= 0)
{
ans2 += a[i] + sum2 + 1;
sum2 = -1;
}
else
{
sum2 += a[i];
}
}
else
{
if (a[i] + sum2 <= 0)
{
ans2 += abs(sum2 + a[i]) + 1;
sum2 = 1;
}
else
sum2 += a[i];
}
}
if (sum2 == 0)
{
ans2++;
}
cout << min(ans1, ans2) << endl;
}
相关文章推荐
- AtCoder Beginner Contest 090 D - Remainder Reminder
- AtCoder Regular Contest 092 C - 2D Plane 2N Points 贪心 匈牙利算法模板
- AtCoder Regular Contest 093(AtCoder Beginner Contest 092)题解
- AtCoder Regular Contest 059 F Unhappy Hacking
- AtCoder Tenka1 Programmer Beginner Contest 解题报告
- 【贪心】AtCoder Regular Contest 079 E - Decrease (Judge ver.)
- AtCoder Grand Contest 020 B - Ice Rink Game(贪心+计算机乘法)
- AtCoder Grand Contest 005 E - Sugigma: The Showdown 博弈论+贪心
- AtCoder Beginner Contest 088
- AtCoder Regular Contest 092 C - 2D Plane 2N Points 贪心 匈牙利算法模板
- AtCoder Grand Contest 020 B - Ice Rink Game(贪心+计算机乘法)
- AtCoder Grand Contest 019 C - Fountain Walk 贪心+dp
- (最短路)AtCoder Beginner Contest 061 D - Score Attack
- AtCoder Beginner Contest 022 A.Best Body 水题
- AtCoder Regular Contest 077 E - guruguru(贪心+差分标记)
- Atcoder Grand Contest 010 C - Cleaning 树贪心(伪)
- AtCoder Grand Contest 020 B - Ice Rink Game(贪心+计算机乘法)
- AtCoder Beginner Contest 075
- AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle【暴力】
- AtCoder AtCoder Beginner Contest 076 C: Dubious Document 2(set与字典序)