Gym-101522I

题目链接:http://codeforces.com/gym/101522/problem/I

题意:就是给你一些数,你可以改变任意一个区间里的每一个数的符号,求改变一次后 ,后一个数减去前一个数的绝对值的和最少。

题目分析: 做这个题目要冷静一点,其实你改变任意一个区间的符号,其实对结果有影响的只有首尾两个数。所以只要求使结果减少的 两个最大的变化值作为 这个区间变化的左右端就行了。

题目:
I. Inverted Signs
time limit per test1.0 s
memory limit per test256 MB
inputstandard input
outputstandard output

Arya lives in a magical world. You can view it as a number line.

There are N citizens in total. The i-th citizen have his own house in position i with height Hi, Noted that Hi could be non positive, which means their house is actually built beneath the horizon.

Arya is the hand of the king and she thinks that the buildings are a bit out of order. She denote the chaos index of the world as .

Noted that hi could be equal to zero as the world is magical.

She feels that the current chaos index of the world is too high. So she designs to change the world a bit by using her super power.

She could flip a continuous sequence of building, i.e., she could choose two arbitrary integer L and R, where 1 ≤ L ≤ R ≤ N, and invert the signs of Hi for all L ≤ i ≤ R (positive to negative and vice versa).

As Arya is weak, she would only do this operation exactly once. Arya wants to minimize the chaos index after the operation. Being a good guy, help Arya to find the lowest possible chaos index after exactly flipping one continuous sequence of building.

Input
The first line contains an integer N. (1 ≤ N ≤ 106)

The second line consist of N integers, the i-th integer is Hi. ( - 109 ≤ Hi ≤ 109)

Output
Output consist only one integer in a single line, the lowest possible chaos index after exactly flipping one continuous sequence of building.

Examples
inputCopy
4
1 -2 -3 4
output
3
inputCopy
5
-3 -2 0 -5 3
output
10
Note
In the first sample, Arya should flip house 2 to 3, so that the heights become 1, 2, 3, 4 and the chaos index would be |2 - 1| + |3 - 2| + |4 - 3| = 3.

|x - y| is the absolute difference between x and y.

代码:

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <queue>
//#include <bits/stdc++.h>
using namespace std;

const int maxn = 1000000+5;
int num[maxn];
int main()
{
    //freopen("in.txt", "r", stdin);
    int n;
    while(scanf("%d", &n) != EOF)
    {
        int i;
        memset(num, 0, sizeof(num));
        long long sum = 0;
        for(i = 0; i < n; i++)
        {
            scanf("%d", &num[i]);
            if(i)
                sum += abs(num[i]-num[i-1]);
        }
        int pos1 = -1;
        int pos2;
        int Min1 = 0;
        int Min2 = 0;
        for(i = 1; i < n; i++)
        {
            int tmp1 = abs(num[i] - num[i-1]);
            int tmp2 = abs(num[i] + num[i-1]);
            if(tmp1 > tmp2)
            {
                if(tmp1-tmp2 > Min1) {
                    pos1 = i;
                    Min1 = tmp1-tmp2;
                }
            }
        }
        for(i = 1; i < n; i++)
        {
            int tmp1 = abs(num[i] - num[i-1]);
            int tmp2 = abs(num[i] + num[i-1]);
            if(tmp1 > tmp2 && (i != pos1))
            {
                if(tmp1-tmp2 > Min2) {
                    pos2 = i;
                    Min2 = tmp1-tmp2;
                }
            }
        }
        sum = sum-Min1-Min2;
        cout <<sum <<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值