Arranging Hat is a cushy job indeed; high impact work, absolute authority, and 364 days of holiday every year. However, the hat has decided that it can do even better—it would like very much to become a tenured professor. Recently the hat has been reading computer science papers in its ample spare time, and of course, being an arranging hat, it is particularly interested in learning more about sorting algorithms.
The hat’s new contribution is to a class of algorithms known as lossy sorting algorithms. These usually work by removing some of the input elements in order to make it easier to sort the input (e.g., the Dropsort algorithm), instead of sorting all the input.
The hat is going to go one better—it is going to invent a lossy sorting algorithm for numbers that does not remove any input numbers and even keeps them in their original place, but instead changes some of the digits in the numbers to make the list sorted.
The lossiness of the sorting operation depends on how many digits are changed. What is the smallest number of digits that need to be changed in one such list of numbers, to ensure that it is sorted?

题目大意:给你n个长度为m的正整数,你需要改变最少次数使其成为递增。

思路:明显的dp题,dp[i][j] 表示前i个数改变j次得到的第i个数的最小值,我们可以通过 dp[i][j] 贪心地得到 dp[i+1][j+k] 的最小值,前i个数我们最多可以改变i*m次,那么我们开的数组为 dp[n][n*m][m], 肯定爆空间了。但仔细想想,在最坏的情况下n为40,改变的次数最多也就10*(1+2+3+4)次。为什么?交给读者自己思考。

还有一个坑点,就是在给定次数贪心最小值。思路是从最高位开始贪心,不同的就使它们相等,直至用完次数。用完后发现还是比原数要小,就在改变的最低位加1,但前提是不能为‘9’,如果是9的话就往高位找,直至找到不为9的数令其+1,并且往后填0,当然,如果没有就返回false。

最后就是细节处理要注意了,附AC代码:

 #include<bits/stdc++.h>
#define CLR(a, b) memset(a, b, sizeof(a)) using namespace std;
const int maxn = + ;
const int maxf = + ;
const int maxm = + ; char dp[maxn][maxf][maxm];
char nm[maxn][maxm];
bool val[maxn][maxf];
int pre[maxn][maxf];
int n, m; bool change(const char *a, const char *b, int lim, char *temp) {
strcpy(temp, b);
int left = lim;
int p = ;
for(p=;p<m&&left;p++) if(temp[p]!=a[p])
temp[p] = a[p], left--;
if(strcmp(temp, a)>=)
return true;
left = lim;
for(--p;p>=&&temp[p]=='';) --p;
if(p<) return false;
int pos;
strcpy(temp, b);
for(pos=;pos<p;pos++) if(temp[pos]!=a[pos])
temp[pos] = a[pos], left--;
if(temp[p]- != a[p]) {
temp[p] = a[p]+; left --;
}
for(++p;p<m&&left;p++) {
temp[p] = '';
if(temp[p]!=a[p]) left--;
}
return true;
} void print_ans(int nn, int k) {
if(!nn) return ;
print_ans(nn-, pre[nn][k]);
printf("%s\n", dp[nn][k]);
} int main() {
scanf("%d%d", &n, &m);
for(int i=;i<=n;i++)
scanf("%s", nm[i]);
CLR(dp, );
CLR(val, false);
for(int i=;i<m;i++)
dp[][][i] = '';
dp[][][m] = '\0';
val[][] = true;
char temp[maxm];
CLR(pre, );
for(int i=;i<n;i++)
for(int j=;j<maxf;j++) if(val[i][j]) {
for(int k=;k<=m && k+j < maxf;k++) {
if(change(dp[i][j], nm[i+], k, temp) &&
(!val[i+][k+j] || strcmp(temp, dp[i+][k+j]) < )) {
val[i+][k+j] = true;
strcpy(dp[i+][k+j], temp);
pre[i+][k+j] = j;
}
}
}
int sign;
for(int i=;i<maxf;i++)
if(val[n][i]) { sign = i; break; }
print_ans(n, sign);
}

NWERC2016-Problem A(Arranging Hat)的更多相关文章

  1. Gym 101170A Arranging Hat dp

    Arranging Hat 题目大意: 给你n,m n个m位的数,保证m位,问要是n个按照从小到大排序,求改变最少个数字,使得这n个按照不递增排序,求最后排序的结果. //dp[i][j] 表示前i个 ...

  2. 【计算几何】【预处理】【枚举】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem K. Kiwi Trees

    发现由于角的度数和边的长度有限制,那俩圆如果放得下的话,必然是塞在两个角里. 于是预处理n个圆心的位置(注意要判断那个圆会不会和其他的边界相交),然后n^2枚举俩角即可. #include<cs ...

  3. 【枚举】【SPFA】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem I. Iron and Coal

    那个人派出的队伍的行走的路径一定前半程是重合的,后半程分叉开来. 于是预处理每个点离1号点的最短路,到最近的铁的最短路,到最近的煤的最短路.(三次BFS / SPFA)然后枚举分岔点,尝试更新答案即可 ...

  4. 【二分】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem C. Careful Ascent

    二分Vx即可. #include<cstdio> #include<algorithm> using namespace std; #define EPS 0.00000000 ...

  5. 【强连通分量缩点】【DFS】【动态规划】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem B. British Menu

    有向图,不经过重复点的最长链,强连通分量大小不超过5. 每个强连通分量内部暴力预处理任意两对点之间的最长路,外面DAG上dp. 不是很好写,但是预处理完了之后,可以重构每个强连通分量内部的结构,然后整 ...

  6. hdu1247 Hat’s Words

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247 题目: Hat's Words Time Limit: 2000/1000 MS (Ja ...

  7. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. HDU 1247 Hat's Words (map+string)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. hdu--(1247)Hat’s Words(trie树)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. H5入门——HTML部分

    一.HTML的基本构成 1.<!DOCTYPE html>文档类型声明 <!--HTML的文档类型声明.声明这个文件是HTML5文件,让浏览器按照HTML5准备进行解析显示.文档类型 ...

  2. akoj-1319-四方定理

    四方定理 Time Limit:1000MS  Memory Limit:65536K Total Submit:28 Accepted:11 Description 数论中著名的"四方定理 ...

  3. 转载的log4cplus使用指南

    以下转载的log4cplus使用指南的版本可能不是最新,仅作参考了解.应以最新安装包中的示例代码为准. 目    录1 Log4cplus简介    52 安装方法    53 主要类说明    64 ...

  4. JavaScript中的EcMAScript学习笔记

    一.Javascript概述(知道)    a.一种基于对象和事件驱动的脚本语言    b.作用: 给页面添加动态效果    c.历史: 原名叫做livescript.W3c组织开发的标准叫ECMAs ...

  5. HTML 基本标签02

    02-html基本标签 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...

  6. react入门之使用react-bootstrap当轮子造车(二)

    react入门之使用react-bootstrap当轮子造车(二) 上一篇我们谈了谈如何配置react的webpack环境 react入门之搭配环境(一) 可能很多人已经打开过官方文档学习了react ...

  7. 【LeetCode】数组-5(566)-按照要求输出矩阵

    题目要求: 思路一:借助队列,先顺序读入input矩阵,然后按照output要求向output矩阵输入 [正确代码] class Solution { public int[][] matrixRes ...

  8. Python 第七天

    OOP 面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.在Python中,所有数据类型都可以视为对象,当然也可以自定义对象.自定义的对象数据类型 ...

  9. Java 开发环境配置(Windows篇)

    window系统安装java 下载JDK 首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downloa ...

  10. Java6和Java8在Windows上共存

    0x00 需求 最近在做一个Android的项目,一开始安装的是Java8用于项目的开发.但是在项目后期需要用到drozer用于检测项目的安全性,要搭建drozer的测试环境必须要使用Java6,否则 ...