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. 使用语音识别JAVA SDK 的MAVEN源代码制作语音控制智能家居Java APP-------MAVEN工程加载问题解决

    一直想做一个可以录音的可执行JAVA APP,实现自然语言对话. 第一步就是实现把录音转成语义,比如你对着话筒说"你好",你获取回答相应的回复.你对着话筒说"今天的天气& ...

  2. pip&easy_install使用

    pip install ... easy_install ... ******************************************************************* ...

  3. Android -- AsyncTask源码解析

    1,前段时间换工作的时候,关于AsyncTask源码这个点基本上大一点的公司都会问,所以今天就和大家一起来总结总结.本来早就想写这篇文章的,当时写<Android -- 从源码解析Handle+ ...

  4. 调整ORACLE用户关闭密码有效期

    --调整ORACLE用户关闭密码有效期  ----------------------------------2013/11/12 在oracle中执行一下操作:1.查看用户的proifle是那个,一 ...

  5. hashlib,configparser,logging模块

    一.常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度 ...

  6. 【leetcode】147. Insertion Sort List

    Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...

  7. 再起航,我的学习笔记之JavaScript设计模式11(外观模式)

    经过一段时间的学习与分享,我们对创建型设计模式已经有了一定的认识,未来的一段时间里我们将展开新的篇章,开始迈入结构性设计模式的学习. 结构性设计模式与创建型设计模式不同,结构性设计模式更偏向于关注如何 ...

  8. MySQL Linux压缩版安装方法

    在诸多开源数据库中,MySQL是目前应用行业,特别是互联网行业发展最好的一个.借助灵活的架构特点和适应不同应用系统场景的Storage Engine,MySQL在很多方面已经有不次于传统商用数据库的表 ...

  9. 基于FPGA的中值滤波算法实现

    在这一篇开篇之前,我需要解决一个问题,上一篇我们实现了基于FPGA的均值滤波算法的实现,最后的显示效果图上发现有一些黑白色的斑点,我以为是椒盐噪声,然后在做基于FPGA的中值滤波算法的实验时,我发现黑 ...

  10. 关于 DropDownList 循环绑定中遇到的问题

    1绑定DataTable 简单直接下部分就ok了 this.DropDownList1.DataSource = DataTable; this.DropDownList1.DataTextField ...