acdream 1222 Quantization Problem [dp]

题目:acdream 1222 Quantization Problem

题意:给出一个序列 a ,然后给出一个 n * m 的矩阵,让你从这个矩阵中选出一个序列k,使得sum(abs(ki - ai))尽可能的小,首先第一个数只能在矩阵的第一行选第 x 个,然后以后每个在第 x%n 行选,依次选出最小即可。每个点可以选多次、

分析:这个题目难度在于题意,题意读懂了就简单了。

很明显的一个dp题目,我们定义状态:dp 【i】【j】 :选第 i 个数 在第 j 列的最小和

则转移方程:dp 【i】【j】 = dp [ i - 1 ] [ k ]  +  abs ( a [ i ] - mp  [ k % s ] [ j ]  ) ;   k是枚举的前一次在第k行选

然后用一个pre数组保存一下路径就ok

AC代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1200;
const int M = 130;
int dp[N][M];
int mp[M][M];
int pre[N][M];
int a[N];
int main()
{
    //freopen("Input.txt","r",stdin);
    int n;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        int s,c;
        scanf("%d%d",&s,&c);
        for(int i=0; i<s; i++)
        {
            for(int j=0; j<c; j++)
                scanf("%d",&mp[i][j]);
        }
        memset(dp,inf,sizeof(dp));
        for(int i=0; i<c; i++)
        {
            int ff = abs(a[0]-mp[0][i]);
            dp[0][i]=min(dp[0][i],ff);
            //printf("%d ",dp[0][i]);
        }
        memset(pre,0,sizeof(pre));
        for(int i=1; i<n; i++)
        {
            for(int j=0; j<c; j++)
            {
                for(int k=0; k<c; k++)
                {
                    int ff = dp[i-1][k] + abs(a[i]-mp[k%s][j]); //枚举在所有行取。
                    if(ff<dp[i][j])
                    {
                        dp[i][j]=ff;
                        pre[i][j]=k;
                    }
                }
                //printf("%d ",dp[i][j]);
            }
            //printf("\n");
        }
        int ans = inf, rec;
        for(int i = 0; i < c; i++)
        {
            if(dp[n-1][i] < ans)
            {
                ans = dp[n-1][i];
                rec = i;
            }
        }
        printf("%d\n", ans);
        vector<int> res;
        int i = n-1;
        while(i != -1)
        {
            res.push_back(rec);
            rec = pre[i--][rec];
        }
        for(int i = res.size() - 1; 0 <= i; --i)
        {
            printf("%d%c",res[i],i==0?'\n':' ');
        }
    }
    return 0;
}
时间: 2024-10-16 16:42:38

acdream 1222 Quantization Problem [dp]的相关文章

ASC2 E Quantization Problem DP

题意:太难懂了,最开始给你一个数列 wi   ,还有一个转移矩阵 M[m][s], 你最开始只能从 M第一列选一个数 L1,如果选的第K个数,接下来只能从第 k&(m-1)取数, 问你|li-wi| 和的最小值及路径. 解题思路:DP加记录路径. 解题代码: 1 // File Name: e.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月14日 星期二 20时35分51秒 4 5 #include<vector> 6 #i

codeforces 706C Hard problem DP(动态规划)问题

题目链接:http://codeforces.com/problemset/problem/706/C 题目大意:  给定n个字符串, 每个字符串可以颠倒前后位置(第一个字母到最后一个,第二个字母到倒数第二位) 每次颠倒需要花费ci的力气, 要求将所给的n个字符串用最小力气按字典序排列, 输出力气值, 如果无法按字典序排列, 则输出-1 数据范围:2?≤?n?≤?100?000 . ci (0?≤?ci?≤?1e9) 所有字符串总长度不会超过1000000. 解题思路: 这是一道DP题, dp[

bzoj 1222: [HNOI2001]产品加工 dp

1222: [HNOI2001]产品加工 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 381  Solved: 218[Submit][Status][Discuss] Description 某 加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所 需的时间会不同,若同时由两台机器共同进行加工,所完成任务又会不同.某一天,加工厂接到n个产品加工的任务,

Codeforces 706 C. Hard problem (dp)

题目链接:http://codeforces.com/problemset/problem/706/C 给你n个字符串,可以反转任意一个字符串,反转每个字符串都有其对应的花费ci. 经过操作后是否能满足字符串str[i]>=str[i-1],能就输出最小花费,不能输出-1. dp[i][0] 表示不反转i的最小花费(str[i] >= str[i - 1] || str[i] >= reverse(str[i - 1])) dp[i][1] 则表示反转i的最小花费... 初始dp[1][

Light OJ 1004 - Monkey Banana Problem dp题解

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dim

hdu 5464 Clarke and problem(dp)

Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke turned into a student and read a book. Suddenly, a difficult problem appears: You are given a sequence of number a1,a2,...,an and a number p. Count the number

LightOJ 1004 Monkey Banana Problem (DP 数字三角形)

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "MonkeyBanana Problem". It states that, a monkey enters into a diamond shaped twodimen

Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度

Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description A group of k students from Cooking University living in the campus decided that each day of the semester one of them will p

HDU 5464Clarke and problem(DP)

Clarke and problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 289    Accepted Submission(s): 131 Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke