Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】
Total Submit: 623 Accepted: 178 Special Judge: No
There are n numbers A[1] , A[2] .... A[n], you can select m numbers of it A[B[1]] , A[B[2]] ... A[B[m]] ( 1 <= B[1] < B[2] .... B[m] <= n ) such that Sum as small as possible.
Sum is sum of abs( A[B[i]]-A[B[j]] ) when 1 <= i < j <= m.
First line of each case contains two integers n and m.( 1 <= m <= n <= 100000 )
Next line contains n integers A[1] , A[2] .... A[n].( 0 <= A[i] <= 100000 )
It's guaranteed that the sum of n is not larger than 1000000.
5 1 7 10
5 3
1 8 6 3 10
8
- #include<stdio.h>
- #include<algorithm>
- #include<string.h>
- #include<vector>
- #include<iostream>
- using namespace std;
- const int maxn = 1e5+200;
- typedef long long LL;
- int a[maxn], sum[maxn];
- int main(){
- int n, m;
- while(scanf("%d%d",&n,&m)!=EOF){
- for(int i = 1; i <= n; i++){
- scanf("%d",&a[i]);
- }
- sort(a+1,a+1+n);
- for(int i = 1; i <= n; i++){
- sum[i] = sum[i-1]+a[i];
- }
- int ans = 0, tmp;
- for(int i = 2; i <= m; i++){
- ans += a[i]*(i-1) - (sum[i-1] - sum[0]);
- }
- tmp = ans;
- if(n == m){
- printf("%d\n",ans); continue;
- }
- for(int i = m+1; i <= n; i++){
- tmp = tmp + a[i]*(m-1) - (sum[i-1] - sum[i-m]) - ((sum[i-1]-sum[i-m]) - a[i-m]*(m-1));
- ans = min(ans,tmp);
- }
- printf("%d\n",ans);
- }
- return 0;
- }
Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】的更多相关文章
- 数学 - Whu 1603 - Minimum Sum
Minimum Sum Problem's Link ------------------------------------------------------------------------- ...
- Minimum Sum(思维)
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 563 Accepted ...
- Minimum Sum of Array(map迭代器)
You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...
- Minimum Sum of Array(map)
You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...
- Selenium定位一 --单个元素定位方法
Selenium-Webdriver 提供了强大的元素定位方法,支持以下三种方法. 单个对象的定位方法 多个对象的定位方法 层级定位 定位单个元素在定位单个元素时,selenium-webdriver ...
- geeksforgeeks@ Minimum sum partition (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...
- Minimum Sum LCM(uva10791+和最小的LCM+推理)
L - Minimum Sum LCM Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- 定义一个Collection接口类型的变量,引用一个Set集合的实现类,实现添加单个元素, 添加另一个集合,删除元素,判断集合中是否包含一个元素, 判断是否为空,清除集合, 返回集合里元素的个数等常用操作。
package com.lanxi.demo2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...
- UVA.10791 Minimum Sum LCM (唯一分解定理)
UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...
随机推荐
- Socket 简易静态服务器 WPF MVVM模式(二)
command类 标准来说,command会有三种模式,委托命令 准备命令 附加命令 1.DelegateCommand 2.RelayCommand 3.AttachbehaviorCommand ...
- 跳转iPhone设置页面,绕过审核
1.问题描述 跳转iPhone设置页面之前都是通过 App-Prefs:root=WIFI 来跳转,但是2018年6月废弃了这个函数,被认为是私有函数,审核会被拒绝. 有心人采用了字符串转码的方式来规 ...
- IP地址和子网划分
前期知识准备 二进制 和十进制 二进制数据是用0和1表示的数,进位规则为缝二进1, 二进制和十进制的关系 二进 十进 0 1 10 2 100 4 1000 8 10000 16 10000 ...
- golang并发练习代码笔记
golang语言的精髓就是它的并发机制,十分简单,并且极少数在语言层面实现并发机制的语言,golang被成为网络时代的c语言,golang的缔造者也有c语言的缔造者,Go语言是google 推出的一门 ...
- c++多线程基础2(命名空间 this_thread)
整理自:zh.cppreference.com/w/cpp/thread std::this_thread::yield: 定义于头文件 <thread> 函数原型:void yield( ...
- 洛谷 P4093 [HEOI2016/TJOI2016]序列(Cdq+dp)
题面 luogu 题解 \(Cdq分治+dp\) \(mx[i],mn[i]\)分别表示第\(i\)位最大,最小能取到多少 那么有 \(j < i\) \(mx[j] \le a[i]\) \( ...
- WPF 布局
WPF布局原则 WPF窗口只能包含单个元素,为在WPF窗口中放置多个元素并创建更贴近使用的用户界面,需要在窗口上放置一个容器,然后在这个容器中添加其他元素 遵循以下几条重要原则 不应显式设定元素(如控 ...
- C#工具类之XmlNode扩展类
using System; using System.Linq; using System.Xml; /// <summary> /// XmlNodeHelper /// </su ...
- 关于vue2非表单元素使用contenteditable="true"实现textarea高度自适应
<template> <div ref="sendContent" contenteditable="true" v-html="s ...
- 用leiningen来运行和打包clojure项目
今天是2017年5月24日.隔壁team用clojure写了个工具,我们要拿来用,于是弄了个leiningen来尝试.还没有最后成功,先记录一下一些连蒙带猜的步骤,以免忘记. 一.单独运行的cloju ...