234C Weather

题目链接

234C. Weather Rating:1300

题目描述

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 t 1 ,   t 2 ,   . . . ,   t n t1, t2, ..., tn 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 ) k (1 ≤ k ≤ n - 1) k(1 kn 1) such that t 1   <   0 ,   t 2   <   0 ,   . . . ,   t k   <   0 t1 < 0, t2 < 0, ..., tk < 0 t1 < 0,t2 < 0, ...,tk< 0 and t k   +   1   >   0 ,   t k   +   2   >   0 ,   . . . ,   t n   >   0 tk + 1 > 0, tk + 2 > 0, ..., tn > 0 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   ≤   1 0 5 2 ≤ n ≤ 10^5 2 n 105) — the number of days for which Vasya has been measuring the temperature.

The second line contains a sequence of n integers t 1 ,   t 2 ,   . . . ,   t n ( ∣ t i ∣   ≤   1 0 9 ) t1, t2, ..., tn (|ti| ≤ 10^9) 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.

inputCopy

4
-1 1 -2 1

outputCopy

1

inputCopy

5
0 -1 1 2 -5

outputCopy

2

大致题意:

给你一个长度为 n的数组,里面的元素有正有负。你的一次操作可以将任意正数变为负数 或者 将任意负数变为正数

求让原序列 变为 “----+++++…” 的序列的最少操作次数。即前面全是负数,后面全是负数(也可能这部分没有)。

分析:

定义 n o n n e g ( i ) nonneg(i) nonneg(i) 为区间 [ 1 , i ] [1,i] [1,i] 中的 非负数 个数。
定义 n o n p o s ( i ) nonpos(i) nonpos(i) 为区间 [ i , n ] [i,n] [i,n] 中的 非正数 的个数。

如果我们让 [ 1 , i ] [1,i] [1,i] 全部变为负数, [ i + 1 , n ] [i+1,n] [i+1,n] 全部变为正数的 操作次数为 n o n n e g ( i ) + n o n p o s ( i + 1 ) nonneg(i) + nonpos(i+1) nonneg(i)+nonpos(i+1)

我们只需要 从 [ 1 , n − 1 ] [1,n-1] [1,n1] 遍历一遍,取最小的值,就是最小的操作次数。

时间复杂度: O ( n ) O(n) O(n)

代码:

# include <cstdio>
# include <algorithm>
 
using namespace std;
const int N = 1e5+10;
 
int a[N];
int nonneg[N],nonpos[N];
 
int main()
{

    FILE *f1,*f2;
    f1=fopen("input.txt","r");
    f2=fopen("output.txt","w");
    
    int n;
    fscanf(f1,"%d",&n);
    
    for(int i=1;i <= n;i++)
        fscanf(f1,"%d",a+i);
    
    nonneg[1]=(a[1]>=0)?1:0;
    for(int i=2;i<=n;i++)
        nonneg[i]=nonneg[i-1]+((a[i]>=0)?1:0);
    
    nonpos[n]=(a[n]<=0)?1:0;
    for(int i=n;i>=1;i--)
        nonpos[i]=nonpos[i+1]+((a[i]<=0)?1:0);
    
    int ans=n;
    for(int i=1;i < n;i++)
        ans=min(ans,nonneg[i]+nonpos[i+1]);
    
    fprintf(f2,"%d\n",ans);
    
    fclose(f1);
    fclose(f2);
    
 
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值