Codeforces - Lengthening Sticks

首先我是被这么一句话骗了的”In particular, it is allowed not to change order of elements at all.”,后来发现其实可以不管它,所以就可以排序后dp了。

 

分析:    按题目的意思,以及式子,可以想到,分成k组数,每组数计算后的和要最小。
            首先,每一组数得出的结果要最小,由于是|ai – ai+k|的结果最小当然是ai和ai+k是数组a中大小相邻的两个数,那么..那么,当然去排序啦。
            然后,对排序后的数组进行分组,分成k组,每组至少n/k个,至多n/k+1个,而且n/k+1要刚好有n%k个。
            接着,根据经验,dp。

 

/* ***********************************************
Author        :DarkTong
Created Time  :2016/5/29 10:42:05
File Name     :Minimization.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int maxn = 5000 + 100;
const int maxm = 300000 + 100;
typedef long long ll;
const long long INF = 100000000000000000LL;
ll dp[maxn][maxn], n, k;
ll a[maxm], sum[maxm], ts;
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%lld%lld", &n, &k);
    for(int i=1;i<=n;++i) scanf("%lld", &a[i]);
    ts=0;
    sort(a+1, a+n+1);
    int tn = n/k, tm = n%k;

        for(int i=1;i<tn;++i) ts+=llabs(a[i]-a[i+1]);
        sum[tn]=ts;
        for(int i=tn,j=1;i<=n;++i,++j)
        {
            ts += llabs(a[i+1]-a[i]);
            ts -= llabs(a[j]-a[j+1]);
            sum[i+1]=ts;
        }

    for(int i=1;i<=k;++i) dp[0][i]=INF;
    for(int i=1;i<=k;++i)
    {
        dp[i][0] = dp[i-1][0]+sum[i*tn];
        for(int j=1;j<=tm;++j)
        {
            dp[i][j] = min(dp[i-1][j]+sum[i*tn+j], dp[i-1][j-1]+sum[i*tn+j-1]+llabs(a[i*tn+j-1]-a[i*tn+j]));
        }
    }
    printf("%lld\n", dp[k][tm]);

    return 0;
}
时间: 2024-10-09 11:07:35

Codeforces - Lengthening Sticks的相关文章

CF 317 A. Lengthening Sticks(容斥+组合数学)

传送门:点我 A. Lengthening Sticks time limit per test 1 second You are given three sticks with positive integer lengths of a, b, and c centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks ca

Codeforces Round #258 (Div. 2) A. Game With Sticks(数学题)

题目链接:http://codeforces.com/contest/451/problem/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

题目传送门 1 /* 2 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 3 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 typedef long long ll; 12 13 co

Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks

It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends. Today he invented one simple game to play with Lena, with whom he shares a desk. The rules

Codeforces 451A Game With Sticks

Description After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of nhorizontal and m vertical sticks. An intersection point is any point on the grid which is formed by the in

Codeforces Round #258 (Div. 2/A)/Codeforces451A_Game With Sticks

解题报告 http://blog.csdn.net/juncoder/article/details/38102263 n和m跟木棍相交,问一人取一交点(必须是交点,且取完后去掉交点的两根木棍),最后谁赢 思路: 取最大正方形,以对角线上的交点个数判断输赢. #include <iostream> #include <cstdio> using namespace std; int main() { int m,n; while(cin>>n>>m) { i

【Codeforces 258A】 Game With Sticks

[题目链接] http://codeforces.com/contest/451/problem/A [算法] 若n和m中的最小值是奇数,则先手胜,否则后手胜 [代码] #include<bits/stdc++.h> using namespace std; int n,m; int main() { scanf("%d%d",&n,&m); if (min(n,m) % 2 == 0) printf("Malvika\n"); else

Codeforces 525C Ilya and Sticks

题意:给你n条线段,问你这些线段可以组成的矩形和最大是多少.每条线段可以-1,. 解题思路:贪心. 解题代码: 1 // File Name: d.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月27日 星期五 16时05分31秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<d

【模拟】【codeforces】451A Game With Sticks

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=52106 现在有n根横着的木棍,m根竖着的木棍,交叉出n*m个交点 两个玩家轮流从现有交点中选择一个,并去掉和这个交点相连的木棍 当一个玩家没有木棍可以去掉时,另外一位玩家获胜 获胜的关键就是使对方没有木棍,或对方的木棍只有横的或竖的木棍,以致没有交点可以选择. 在最开始,每人选择一个点,都是去掉一横一竖,当去掉一个方向全部的木棍时,就赢了. 1 #include<bits/