CodeForces 234 C.Weather(水~)

Description

给出一个长度为 n n 的序列a1,...,an,要求修改最少的值使得存在 1kn1 1 ≤ k ≤ n − 1 ,满足 t1,...,tk<0,tk+1,...,tn>0 t 1 , . . . , t k < 0 , t k + 1 , . . . , t n > 0

Input

第一行一整数 n n 表示序列长度,之后输入n个整数 a1,...,an a 1 , . . . , a n (2n105,|ai|109) ( 2 ≤ n ≤ 10 5 , | a i | ≤ 10 9 )

Output

输出最少需要修改值的个数

Sample Input

4
-1 1 -2 1

Sample Output

1

Solution

L[i] L [ i ] 表示前 i i 个数中非负数字个数,R[i]表示 ai,...,an a i , . . . , a n 中非正数字个数,那么答案即为 min1i<n(L[i]+R[i+1]) min 1 ≤ i < n ( L [ i ] + R [ i + 1 ] )

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100005;
int n,a[maxn],L[maxn],R[maxn]; 
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d",&n);
    int num=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        L[i]=L[i-1];
        if(a[i]>=0)L[i]++;  
    }
    for(int i=n;i>=1;i--)
    {
        R[i]=R[i+1];
        if(a[i]<=0)R[i]++;
    }
    int ans=n;
    for(int i=2;i<=n;i++)
        ans=min(ans,L[i-1]+R[i]);
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值