题目链接:
http://codeforces.com/problemset/problem/234/C
C. Weather
input input.txtoutput output.txt
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
Copy
4 -1 1 -2 1
output
Copy
1
input
Copy
5 0 -1 1 2 -5
output
Copy
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天的天气 T1, T2,T3,,,,,Tn,
要求: 有一个正整数k(1≤k≤n- 1), 使得t1< 0, t2< 0… tk< 0和tk+ 1> 0,tk+ 2> 0…tn> 0。 在满足条件的前提下,修改最少几天的温度可以满足要求
输出最少修改的天数
思路:计算前缀大于等于0的和,和计算后缀小于等于的0的和
当前为k=i 则需要修改的天数为pre[i]+bac[i+1]
This is the code:
#include<bits/stdc++.h>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
int ret=0, flag=0;
char ch;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
ret = ch - '0';
while((ch=getchar())>='0'&&ch<='9')
ret=ret*10+(ch-'0');
return flag ? -ret : ret;
}
const int maxn=100005;
int a[maxn];
int pre[maxn];
int bac[maxn];
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)
{
if(a[i]>=0)
pre[i]++;
pre[i]+=pre[i-1];
}
for(int i=n;i>=1;--i)
{
if(a[i]<=0)
bac[i]++;
bac[i]+=bac[i+1];
}
int minn=INT_INF;
for(int i=1;i<n;++i)
minn=min(minn,pre[i]+bac[i+1]);
printf("%d\n",minn);
return 0;
}