zoukankan      html  css  js  c++  java
  • 排队

    洛洛在外出旅游的时候发现社会上文明的现象越来越多,人们在买票的时候都会自发地排队等候。
    遗憾的是排队的人身高参差不齐,有时候前后两人之间的身高相差太大,缺乏一些美感。
    如果把前后两人的身高差(差值为正数)表示为两者前后相邻时产生的违和度,一段连续的人群因为前后两人身高不同而产生的违和度之和就可以被称为违和值。洛洛希望知道在队伍哪一段,且该段队伍由连续的m个人组成,其违和值最小。

    输入
    第一行输入两个正整数 n,m,n表示队伍的总人数,m表示某一段的人数。
    第二行输入n个整数,表示队伍中n个人的身高(单位:厘米)。

    输出
    输出包含一个整数,即最小的违和值。

    样例输入 Copy
    样例输入1
    4 3
    1 2 4 7

    样例输入2
    4 3
    10 7 5 4
    样例输出 Copy
    样例输出1
    3

    样例输出2
    3

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1e6 + 10;
    int mp[maxn],mm[maxn];
    int n,m,ans,a;
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        cin >> n >> m;
        for(int i = 0; i < n; i++){
            cin >> mp[i];
            if(i) mm[i] = abs(mp[i] - mp[i - 1]);
        }
    
        for(int i = 1; i < m;i++)
            ans += mm[i];
        a = ans;
        for(int i = m; i < n; i++){
            ans = ans - mm[i - m + 1] + mm[i];
            a = min(ans, a);
        }
        cout << a << endl;
        return 0;
    }
    
  • 相关阅读:
    SHA1 VS RSA: what's the difference between them?
    TLS Security
    TLS Handshake Protocol
    Building Cython code
    Getting started with JupyterLab
    Installing Cython
    【转贴】libcrypto.so.10丢失导致sshd无法运行解决方案
    [Typescript] Function Overloads
    [Typescript] Function Generics
    [Typescript] Discriminated (Tagged) Unions
  • 原文地址:https://www.cnblogs.com/xcfxcf/p/12301593.html
Copyright © 2011-2022 走看看