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 ...
随机推荐
- [补档][Poi2014]FarmCraft
[Poi2014]FarmCraft 题目 mhy住在一棵有n个点的树的1号结点上,每个结点上都有一个妹子. mhy从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装zhx牌杀毒 ...
- 发布内网网站服务器让公网可以访问,无需NAT
有些时候,我们的测试网站搭建在我们的测试环境中,网站正式上线前,需要先测试下我们的测试网站是否正常,就可以用下面的方式将其内网网站服务器放至公网上,用器提供的外网地址就可以直接访问我们的内网网站服务器 ...
- 利用GeoIP数据库及API进行地理定位查询 Java
地理定位查询的的数据库比较多,而且大多都开放一些free的版本 国内的有纯真数据库等,但是他只提供文本的地理位置信息,不提供经纬度数据 当应用到google map时,就不可以了 国外的有MaxMin ...
- kill 和killall----杀死进程
1.根据进程ip查看进程名 Liunx中 通过进程名查找进程PID可以通过 pidof [进程名] 来查找. 反过来 ,通过PID查找进程名则没有相关命令.但在linux根目录中,有一个/proc的 ...
- ASP.NET Core - Razor页面之Handlers处理方法
简介 在前一篇文章中,我们讨论了Razor页面.今天我们来谈谈处理方法(Handlers). 我们知道可以将代码和模型放在 .cshtml 文件里面或与 .cshtml 匹配的 .cshtml.cs ...
- 分辨率验证工具 - 【Firesizer】的使用
Firesizer是一款测试分辨率的插件. 下载方式:Firefox工具栏——〉工具——〉附加组件--〉搜索Firesizer并安装,浏览器会自动重启 使用方式:浏览器右下角直接切换分辨率即可,如下图 ...
- 版本管理工具Git(2)git的安装及使用
下载安装git 官方下载地址:https://git-scm.com/download/win 这里以windows为例,选择正确的版本: 验证是否安装成功,右键菜单中会出现如下菜单: Git工作流程 ...
- CSS常见英语单词属性一览
这些是css中常会用到的一些英文单词,大家可以多看看,多使用就会容易记得了. color : #999999; /*文字颜色*/ font-family : 宋体,sans-serif; /*文字字体 ...
- PHP 常见工厂设计模式
一.工厂模式 是一种类,它具有为您创建对象的某些方法.您可以使用工厂类创建对象,而不直接使用 new.这样,如果您想要更改所创建的对象类型,只需更改该工厂即可.使用该工厂的所有代码会自动更改. 下面代 ...
- MySQL (五)
1 连接查询简介 将多张表(可以大于2)进行记录的连接(按照某个指定的条件进行数据拼接). 最终结果:记录数可能会有变化,字段书一定会增加(至少两张表的合并). 连接查询:join,使用方式:左表 j ...