CF1197C

题意:

有一个递增的数列,把它分成k段,找到这样的分段方法,即每段最大值减最小值的和最小

解法:

分成k段,即要加k-1个隔断,这k-1个隔断,能隔开差值最大的几个,那最后得到分隔后的各段最大最小差值和最小

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector> using namespace std; #define LL long long
const int N = 3e5 + 100; int n,a[N],k;
vector<int> G; int main() {
scanf("%d%d",&n,&k);
for(int i = 1 ; i <= n ; ++i)
scanf("%d",&a[i]);
for(int i = 2 ; i <= n ; ++i)
G.push_back(a[i - 1] - a[i]);
sort(G.begin(), G.end());
int ans = a[n] - a[1];
for(int i = 0 ; i < k - 1 ; ++i) ans += G[i];
printf("%d",ans);
//system("pause");
return 0;
}

CF1197C的更多相关文章

随机推荐

  1. C# 在运行中拖拽,改变控件大小位置类(转载)

    原文地址:https://blog.csdn.net/zgke/article/details/3718989 copy的code /// <summary> /// 移动改变控件大小 / ...

  2. 结合python实现的netcat与python实现的tcp代理,建立一个流量隧道

    在proxy中 python2 proxy.py 127.0.0.1 3334 192.158.1.111 80 true 作为服务器在本地3334端口进行监听, 作为客户端连接远程web服务器192 ...

  3. SVN 问题解决之 Working copy path does not exist in repository

    同事的SVN更新时提示某个特定文件提示 Working copy path does not exist in repository svn更新会被这个错误打断,导致无法完全更新 删掉文件再更新仍然有 ...

  4. C# 哥德巴赫猜想的实现方式 region分区编写

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  5. 定位之z-index

    我们已经知道固定定位(fixed)和绝对定位(absolute)可以让盒子浮起来 相对定位(relactive)虽然不能让盒子浮起来,但也是可以让图层浮起来 那么既然大家都可以浮起来,就会存在一个问题 ...

  6. JavaScript引入

    三种引入方式 js标签引入的三种方式 1.行间式 写在标签的事件属性中 <div onclick="alert('hello')"></div>(点击出弹窗 ...

  7. C语言对齐、补齐

    加快CPU读取数据的速度 aligned(n) 让所作用的结构成员对齐在n字节自然边界上.如果结构中有成员的长度大于n,则按照最大成员的长度来对齐 struct s { char c; int i; ...

  8. leetcode-64. 最小路径和 · vector + DP

    题面 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right wh ...

  9. SAP官方发布的ABAP编程规范

    最近有朋友在公众号后台给我留言,"Jerry啊,你最近写的都是一些SAP研究院里面用到的新技术,能不能写点SAP传统的开发技术比如ABAP相关的东西"? 其实Jerry在刚开始写这 ...

  10. Kinect for Windows SDK开发入门(七):骨骼追踪基础 下

    http://www.cnblogs.com/yangecnu/archive/2012/04/09/KinectSDK_Skeleton_Tracking_Part2.html 上一篇文章用在UI界 ...