Minimum Sum(思维)

Problem 1603 - Minimum Sum

Time Limit: 2000MS   Memory Limit: 65536KB    Total Submit: 563  Accepted: 156  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

题解:题意就是求连续m个数字相互差的绝对值最小;

刚开始就想着先排序,从大到小模拟了下找到了3x1+x2-x3-3x4;由此可以看出规律;但是由于想着数据是1e5,就不敢写。。。然后队友写了下就过了。。。吊。。。其实是因为当N是1e5的时候,如果M也很大,那么i是从n-m开始的,所以也没啥事;

然后的思路就是从后往前大暴力。。。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN = 100010;
 7 int A[MAXN];
 8 int main(){
 9     int n,m;
10     while(~scanf("%d%d",&n,&m)){
11         for(int i = 0; i < n; i++){
12             scanf("%d",&A[i]);
13         }
14         sort(A, A + n);
15         int ans = 0x3f3f3f3f;
16         for(int i = n - m; i >= 0;i--){
17             int x = m - 1, temp = 0;
18             for(int j = i + m - 1; j >= i;j--){
19                 temp += x * A[j];
20             //    printf("x = %d a[%d] = %d temp = %d ",x,j,A[j],temp);
21                 x -= 2;
22             }
23         //    printf("i = %d\n",i);
24             ans = min(ans, temp);
25         }
26         printf("%d\n",ans);
27     }
28     return 0;
29 } 
时间: 2024-11-06 02:48:13

Minimum Sum(思维)的相关文章

Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】

Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 623  Accepted: 178  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[

Minimum Sum LCM(uva10791+和最小的LCM+推理)

L - Minimum Sum LCM Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10791 题意:输入正整数n,<注意n=2^31-1是素数,结果是2^31已经超int,用long long,>找至少两个数,使得他们的LCM为n且要输出最小的和: 思路:既然LCM是n,那么一定是n的质因子组成的数,又要使和最小,那么就是ans+=[

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

HDU 3473 Minimum Sum

Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2667    Accepted Submission(s): 609 Problem Description You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you

UVA - 10791 - Minimum Sum LCM (数论相关!)

题目链接:Minimum Sum LCM UVA - 10791 Minimum Sum LCM Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu SubmitStatus Description  Minimum Sum LCM  LCM (Least Common Multiple) of a set of integers is defined as the minimum number, whic

[email&#160;protected] Minimum sum partition (Dynamic Programming)

http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum. Input: The first line contains an i

hdu 3473 Minimum Sum(划分树-sum操作)

划分树.只是考虑求当前区间大于第k值的值得和,和小于第k值的和.显然可以在查询的时候直接搞出来.sum[d][i]表示第d层子区间l,r种l-i的和.写错了一个下标,检查了半辈子... #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue&g

HDOJ 3473 Minimum Sum

划分树,统计每层移到左边的数的和. Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2959    Accepted Submission(s): 684 Problem Description You are given N positive integers, denoted as x0, x1 ... x

Given a tree, find the node with the minimum sum of distances to other nodes

O(n) complexity, have a traversal for the tree. Get the information of all children, then traverse the tree again. #include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; class Node { publ