CodeForces - 234C:Weather(思维)

Discription
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 1
Output
1
Input
5
0 -1 1 2 -5
Output
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.

题意
给定一个数组,找到一个位置k,是的改变最小的次数,使>k的数都大于0,<=k的数都小于0,求最小的改变次数。

思路
我想的方法是用两个数组分别记录前i项大于等于0的数的个数和后i项的小于等于0的数的个数。然后用一重循环枚举每个位置需要的步数,取最小值即可。样例过了但是就是一直WA,后来才知到,i=1和i=n的边界条件需要特判,不能直接加。

AC代码

#include<bits/stdc++.h>
using namespace std;
int n;
int a[100100];
int s1[100100];
int s2[100100];
int minn=99999999;
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
    }
    for(int i=2; i<=n-1; i++)
    {
        if(a[i]>=0)
            s2[i]=s2[i-1]+1;
        else
            s2[i]=s2[i-1];
    }
    for(int i=n-1;i>=2; i--)
    {
        if(a[i]<=0)
            s1[i]=s1[i+1]+1;
        else
            s1[i]=s1[i+1];
    }
    for(int i=1; i<=n-1; i++)
    {
        if(s2[i]+s1[i+1]<minn)
            minn=s2[i]+s1[i+1];
    }
    if(a[1]>=0)
        minn++;
    if(a[n]<=0)
        minn++;
    cout<<minn<<endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值