意甲冠军:

给n每行长度和字符可放置最大数量字m,每一行产生值至(m-x)^2,x是一个字符上线人数(包含空话之间格)。为了让所有的完成,产生的话值最小和。

分析:

动态规划非常重要的就是状态的定义,在由子问题向父问题推进的过程中,定义的状态要能对之前的全部情况进行总结。比方背包问题中dp[i][v]中的v,无论之前1~i-1个物品怎样取舍,他们的总重量肯定在0~v之中,故每步能把指数级的问题线性化。这题也是,刚考虑第i个单词时,前面全部单词无论怎么放最后一个的结束位置肯定在1~m之间,故定义dp[i][s](s<=m)表示放完前i个单词第i个单词末位位于该行s处的最小值。

代码:

//poj 3390
//sep9
#include <iostream>
using namespace std;
const int maxM=108;
const int maxN=10004;
int dp[maxN+10][maxM+10];
int L[maxN];
int main()
{
int cases;
scanf("%d",&cases);
while(cases--){
int m,n,s;
scanf("%d%d",&m,&n);
for(int i=1;i<=n;++i)
scanf("%d",&L[i]);
memset(dp,0x7f,sizeof(dp));
dp[0][m]=0;
for(int i=1;i<=n;++i){
int x=dp[maxN][maxM];
for(s=m;s>=0;--s)
x=min(x,dp[i-1][s]);
dp[i][L[i]]=x+(m-L[i])*(m-L[i]);
for(s=L[i]+2;s<=m;++s){
int x=s-L[i]-1;
if(dp[i-1][x]==dp[maxN][maxM])
continue;
int y=dp[i-1][x]-(m-x)*(m-x)+(m-s)*(m-s);
dp[i][s]=y;
}
}
int ans=dp[0][maxM];
for(s=0;s<=m;++s)
ans=min(ans,dp[n][s]);
printf("%d\n",ans);
}
return 0;
}

poj 3390 Print Words in Lines 动态规划的更多相关文章

  1. POJ 3390 Print Words in Lines(DP)

    Print Words in Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1624 Accepted: 864 D ...

  2. [CareerCup] 13.1 Print Last K Lines 打印最后K行

    13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...

  3. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

  4. POJ 3181 Dollar Dayz(高精度 动态规划)

    题目链接:http://poj.org/problem?id=3181 题目大意:用1,2...K元的硬币,凑成N元的方案数. Sample Input 5 3 Sample Output 5 分析: ...

  5. [poj] 1269 [zoj] 1280 Interesting Lines || 求两直线交点

    POJ原题 ZOJ原题 多组数据.每次给出四个点,前两个点确定一条直线,后两个点确定一条直线,若平行则输出"NONE",重合输出"LINE",相交输出" ...

  6. POJ 1269 (直线相交) Intersecting Lines

    水题,以前总结的模板还是很好用的. #include <cstdio> #include <cmath> using namespace std; ; int dcmp(dou ...

  7. POJ 3186 Treats for the Cows (动态规划)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  8. BZOJ2287 【POJ Challenge】消失之物 动态规划 分治

    原文链接http://www.cnblogs.com/zhouzhendong/p/8684027.html 题目传送门 - BZOJ2287 题意 有$n$个物品,第$i$个物品的体积为$w_i$. ...

  9. POJ 1952 BUY LOW, BUY LOWER 动态规划题解

    Description The advice to "buy low" is half the formula to success in the bovine stock mar ...

随机推荐

  1. [Angular2 Form] Reactive Form, FormBuilder

    Import module: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/comm ...

  2. javascript运算符应用

    下面的代码会输出什么?为什么? console.log(1 + "2" + "2"); console.log(1 + +"2" + &qu ...

  3. 10、V4L2摄像头获取单幅图片测试程序(MMAP模式)

    #include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h> ...

  4. occcont.cpp 925

    将InitInstance()中调用的 InitATL() 替换为 AfxOleInit()即可.

  5. 得到INI文件所有Section(所有节点名称)

    char SectionNames[MAX_PATH],*pSectionName; ZeroMemory(SectionNames,MAX_PATH); GetPrivateProfileSecti ...

  6. SDL2源码分析1:初始化(SDL_Init())

    ===================================================== SDL源码分析系列文章列表: SDL2源码分析1:初始化(SDL_Init()) SDL2源 ...

  7. PWA之Web 应用清单

    原文 简书原文:https://www.jianshu.com/p/5c96242188e8 大纲 1.什么是Web 应用清单 2.“清单文件”:Web App Manifest 规范的应用 3.we ...

  8. Java提高:采用异常链传递异常

    一.分析 异常需要封装,但是仅仅封装还是不够的,还需要传递异常.一个系统的友好型的标识,友好的界面功能是一方面,另一方面就是系统出现非预期的情况的处理方式了. 二.场景 比如我们的JEE项目一般都又三 ...

  9. [Node.js] Pass command line arguments to node.js

    Command line arguments are often used to modify the behavior of an application or specify needed par ...

  10. 【t070】二进制

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 求所有可以只用1和00拼成的长度为N的二进制数的个数除以15746的余数. 比如当N=4的时候,有5个 ...