NWERC2016-Problem A(Arranging Hat)
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)的更多相关文章
- Gym 101170A Arranging Hat dp
Arranging Hat 题目大意: 给你n,m n个m位的数,保证m位,问要是n个按照从小到大排序,求改变最少个数字,使得这n个按照不递增排序,求最后排序的结果. //dp[i][j] 表示前i个 ...
- 【计算几何】【预处理】【枚举】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem K. Kiwi Trees
发现由于角的度数和边的长度有限制,那俩圆如果放得下的话,必然是塞在两个角里. 于是预处理n个圆心的位置(注意要判断那个圆会不会和其他的边界相交),然后n^2枚举俩角即可. #include<cs ...
- 【枚举】【SPFA】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem I. Iron and Coal
那个人派出的队伍的行走的路径一定前半程是重合的,后半程分叉开来. 于是预处理每个点离1号点的最短路,到最近的铁的最短路,到最近的煤的最短路.(三次BFS / SPFA)然后枚举分岔点,尝试更新答案即可 ...
- 【二分】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 ...
- 【强连通分量缩点】【DFS】【动态规划】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem B. British Menu
有向图,不经过重复点的最长链,强连通分量大小不超过5. 每个强连通分量内部暴力预处理任意两对点之间的最长路,外面DAG上dp. 不是很好写,但是预处理完了之后,可以重构每个强连通分量内部的结构,然后整 ...
- hdu1247 Hat’s Words
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247 题目: Hat's Words Time Limit: 2000/1000 MS (Ja ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 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 ...
- hdu--(1247)Hat’s Words(trie树)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- 18. leetcode 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- 利用Apache commons-net 包进行FTP文件和文件夹的上传与下载
首先声明:这段代码是我在网上胡乱找的,调试后可用. 需要提前导入jar包,我导入的是:commons-net-3.0.1,在网上可以下载到.以下为源码,其中文件夹的下载存在问题:FTPFile[] a ...
- 是什么让javascript变得如此奇妙
What Makes Javascript Weird...and AWESOME -> First Class Functions -> Event-Driven Evironment ...
- matplotlib实现数据可视化
一篇matplotlib库的学习博文.matplotlib对于数据可视化非常重要,它完全封装了MatLab的所有API,在python的环境下和Python的语法一起使用更是相得益彰. 一.库的安装和 ...
- Vue表单控件绑定
前面的话 本文将详细介绍Vue表单控件绑定 基础用法 可以用 v-model 指令在表单控件元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.v-model本质上不过是语法糖,它负 ...
- Linux网络配置文件详解
--Linux网络配置文件详解----------------------2013/10/03 目前在企业级服务器的Linux系统中,RHEL占有绝对的优势,不管是曾经在互联网公司还是在目前测试Vir ...
- Android WebView 调试方法
调试Android WebView中的h5页面,通常就是通过alert和抓包工具来定位问题,效率低且无法直接调试样式或打断点,可谓是事倍功半.本文介绍一下我在项目中使用的新方法,能够通过chrome的 ...
- python之----------字符编码具体原理
1.内存和硬盘都是用来存储的. CPU:速度快 硬盘:永久保存 2.文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就可以启动一个进程,是在内存中的,所以在编辑器编 ...
- java8之stream
lambda表达式是stream的基础,初学者建议先学习lambda表达式,http://www.cnblogs.com/andywithu/p/7357069.html 1.初识stream 先来一 ...
- CSS 学习笔记 - Flex 布局
传统布局方式的局限性 传统的网页布局方式,采用 display + position + float 的方式来实现.这种方式,无法实现一些复杂的布局,并且在实现某些布局时,会有一些局限性. 比如,最常 ...