第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛 A

Problem 1603 - Minimum Sum

Time Limit: 2000MS   Memory Limit: 65536KB  
Total Submit: 564  Accepted: 157  Special Judge: No

Description

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.

Input

There are multiple test cases.
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.

Output

For each test case, output minimum Sum in a line.

Sample Input

4 2
5 1 7 10
5 3
1 8 6 3 10

Sample Output

2
8

题意: 长度为n的数组A 任意取 取出的数量为m 然后求   所有 abs( A[B[i]]-A[B[j]]  i<j ) 的和

题解: 比赛的时候 有一个思路  题目时间限制是2000ms 但woj 测评 2000+ms   过了

先sort排序 取连续出长度为m的序列re[]   求其sum值

预处理求 re[1]-re[2] =ans[2]  并记录前缀和

re[1]-re[3] =ans[3]

...

re[1]-re[m]=ans[m]

re[2]-re[3]=(re[1]-re[3])-(re[1]-re[2]) //

re[2]-re[4]=(re[1]-re[4])-(re[1]-re[2]) //将要求的量 转化为已知量

时间: 2024-10-10 06:42:17

第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛 A的相关文章

15.4.19 第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛

Problem 1564 - A - Circle Time Limit: 4000MS   Memory Limit: 65536KB   Total Submit: 349  Accepted: 73  Special Judge: No Description Here is a circle sequence S of length n, and you can choose a position and remove the number on it. After that,you w

第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 Problem 1566 - C - Spanning Tree

Description You are given a graph with N nodes and M edges. Then every time you are required to add an additional edge with weight Wi connecting the node Ai and Bi in the graph, and then calculate the sum of the edges' weight of the Minimum Spanning

第十四届华中科技大学程序设计竞赛决赛同步赛

第十四届华中科技大学程序设计竞赛决赛同步赛 A Beauty of Trees 思维,带权并查集 题意: 长度为 n 的序列,没告诉你具体数是多少.只给出 m 个查询,表示区间 [l,r] 的异或和为 k .但是第 i 个查询如果和前面的查询有矛盾,那就是错误的.输出所有的错误查询. tags: 对于一个查询,我们知道 sum[r] ^ sum[l-1] = k . 建成图就是 r -> (l-1) ,但要快速地求出异或值,就要用带权并查集处理.如有 sum[r]^sum[l-1]=k,即 r

湖南大学第十四届ACM程序设计新生杯 Dandan&#39;s lunch

Dandan's lunch Description: As everyone knows, there are now n people participating in the competition. It was finally lunch time after 3 hours of the competition. Everyone brought a triangular bread. When they were going to eat bread, some people fo

HDU 6467 简单数学题 【递推公式 &amp;&amp; O(1)优化乘法】(广东工业大学第十四届程序设计竞赛)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6467 简单数学题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 308    Accepted Submission(s): 150 Problem Description 已知 F(n)=∑i=1n(i×∑j=inCij) 求 F(n) m

2016年“计蒜客”杯华中师范大学第十四届程序设计竞赛题解

A. 寻找字符串 对于一个字符串 s,定义一个函数 S(x),表示 s 中有多少个子串 x.现在给定 S(a),S(b),S(ab),S(ba)的值,求满足这 4 个值的字符串 s,如果有多个满足条件的字符串,输出字典序最小的. s 中仅可能含有字符 a 和 b. 任何一个串的 S(ab) 与 S(ba) 的相差都不会超过1.可以分 S(ab)-S(ba) 为 0 -1 1 三种情况讨论.先构造出相应的 ab 相邻的串,再考虑按照字典序最小出要求将剩余的字母插入串中即可. 应该注意,这道问题有很

第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. And there are many different types of trees in HUST, each of which has a number represent its type. The doc

第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Now you're going to walk through a large forest. There is a path consisting of N stones winding its way to

第十四届华中科技大学程序设计竞赛--J Various Tree

链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 It’s universally acknowledged that there’re innumerable trees in the campus of HUST. And there are many different types o