Problem Description
Scientists say a lot about the problems of global warming and cooling of the Earth. Indeed, such natural phenomena strongly influence all life on our planet.
Our hero Vasya is quite concerned about the problems. He decided to try a little experiment and observe how outside daily temperature changes. He hung out a thermometer on the balcony every morning and recorded the temperature. He had been measuring the temperature for the last n days. Thus, he got a sequence of numbers t1, t2, ..., tn, where the i-th number is the temperature on the i-th day.
Vasya analyzed the temperature statistics in other cities, and came to the conclusion that the city has no environmental problems, if first the temperature outside is negative for some non-zero number of days, and then the temperature is positive for some non-zero number of days. More formally, there must be a positive integer k (1 ≤ k ≤ n - 1) such that t1 < 0, t2 < 0, ..., tk < 0 and tk + 1 > 0, tk + 2 > 0, ..., tn > 0. In particular, the temperature should never be zero. If this condition is not met, Vasya decides that his city has environmental problems, and gets upset.
You do not want to upset Vasya. Therefore, you want to select multiple values of temperature and modify them to satisfy Vasya's condition. You need to know what the least number of temperature values needs to be changed for that.
Input
The first line contains a single integer n (2 ≤ n ≤ 105) — the number of days for which Vasya has been measuring the temperature.
The second line contains a sequence of n integers t1, t2, ..., tn (|ti| ≤ 109) — the sequence of temperature values. Numbers ti are separated by single spaces.
Output
Print a single integer — the answer to the given task.
Examples
Input
4
-1 1 -2 1Output
1
Input
5
0 -1 1 2 -5Output
2
Note
Note to the first sample: there are two ways to change exactly one number so that the sequence met Vasya's condition. You can either replace the first number 1 by any negative number or replace the number -2 by any positive number.
题意:给出 n 个有正有负的温度,先可对这些温度进行修改,使得对这些温度中的第 k(1<=k<=n-1) 个起,k 及其前面的数均小于 0,k 后面的数均大于零,问最小修改次数
思路:前缀和
维护两个前缀和,第一个维护正的温度,第二个维护负的温度,最后进行枚举,对于第 i 个温度来说,将第 i 个正的温度与从第 i 个开始到第 n 个负的温度的区间加和,即为要修改的次数,在此过程中记录一个最小值即可
此外,注意本题要使用文件读写
Source Program
#include<bits/stdc++.h>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int a[N];
int posi[N],nega[N];
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
posi[i]=posi[i-1];
if(a[i]>=0)
posi[i]++;
}
for(int i=1;i<=n;i++){
nega[i]=nega[i-1];
if(a[i]<=0)
nega[i]++;
}
int minn=INF;
for(int i=1;i<=n-1;i++){
int seq=nega[n]-nega[i];
int sum=posi[i]+seq;
minn=min(minn,sum);
}
printf("%d\n",minn);
return 0;
}