USACO 5.5 Twofive
Twofive
IOI 2001
In order to teach her young calvess the order of the letters in the alphabet, Bessie has come up with a game to play with them. The calves are given a 5 x 5 grid on which they can put the letters 'A'-'Y', but the one rule is that all the letters going across the columns and down the rows must be in the order they appear in the alphabet.
There are a huge number of possible grids, so Bessie decides that they will be named by the string of letters that is given by reading across each row, going down the rows. For example, the grid:
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
would have the name ABCDEFGHIJKLMNOPQRSTUVWXY, which is coincidentally the first possible grid when the entire set of grids is ordered alphabetically. The second grid that meets this requirement is ABCDEFGHIJKLMNOPQRSUTVWXY, which is formed by switching the 'T' and 'U' in the above grid.
Help the calves gain bragging rights. Given a number, M, find which string is Mth in the list of all possible grids when they are sorted alphabetically, and, given a string of letters, find out what number the corresponding grid is in the list of all possible grids.
PROGRAM NAME: twofive
INPUT FORMAT
The first input line contains one of two letters, either an 'N' or a 'W'.
If the first input line contains an 'N', the second line will contain an integer, M, that corresponds to the number of a valid grid. If the first line contains a 'W', the second line will contain a string of 25 letters, which represents a valid grid.
OUTPUT FORMAT
If the input contained the number of a valid grid (first line 'N'), the output should contain a string of 25 letters on a line, which corresponds to the Mth grid in the sorted list of all possible grids.
If the input contained a string of letters indicating a grid (first line 'W'), the output should contain a single integer on a line, which corresponds to the number of the given grid in the list of all possible grids.
SAMPLE INPUT #1 (file twofive.in)
N
2
SAMPLE OUTPUT #1 (file twofive.out)
ABCDEFGHIJKLMNOPQRSUTVWXY
SAMPLE INPUT #2 (file twofive.in)
W
ABCDEFGHIJKLMNOPQRSUTVWXY
SAMPLE OUTPUT #2 (file twofive.out)
2
——————————————————————————————————题解
越刷神题越多……
这道题的思路和介个差不多虽然这道没有做出来
一开始的思路就是赤裸裸的暴力然后map,结果跑不动qwq
然后我要开始写一份详细的解题报告了
如果是编码转单词:
初始计数器s=0,编码为d
计算AB开头所有的单词x,如果s+x>=d,那么我们可以发现AB开头的所有单词囊括了所有1-d的单词,所以第二位就是B
如果s+x<d,那么1-d中包含了AB开头所有单词,那么我们继续枚举下一位,直到满足s+x>=d
由此可以计算出这个单词
如果是单词转编码:
初始计数器s=0
假如一个串AFKPUBGLQVCHMRWDINSXEJOTY
这个单词之前一定有AB,AC,AD,AE开头的所有单词,用s累加上他们的和
然后处理AFB开头的所有单词(处理出来会是0)AFC……AFJ开头的所有单词和,以下位同理
最后s+1就是我们想要的解
那么我们如何计算一个固定前缀开头所有单词的和,用记忆化搜索f[a][b][c][d][e]表示第一行有a个字符,第二行有b个字符,第三行有c个字符,第四行有d个字符,第五行有e个字符,其中这些字符是前a+b+c+d+e个字符,且a->e递减,我们再记一个ans序列是我们固定好的前缀,如果搜索到该位置要往里面填一个字母,这个位置没有字母或者这个位置的字母正好是我们ans里记录的,我们就填,否则跳过
这个记忆化搜索的边界条件是搜满了之后填1
据说用5位六进制数可以让程序变得美观然而我没有
/*
ID: ivorysi
LANG: C++
PROG: twofive
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#include <stack>
#include <map>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x5f5f5f5f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
ll f[][][][][],d,s;
int used[];
char ans[],str[];
void init() {
siji(i,,) {
siji(j,,) {
siji(k,,) {
siji(z,,) {
siji(o,,) {
f[i][j][k][z][o]=-;
}
}
}
}
}
}
int dfs(int a1,int a2,int a3,int a4,int a5,char t) {
if(f[a1][a2][a3][a4][a5]!=-)return f[a1][a2][a3][a4][a5];
if(t>'Y') return f[a1][a2][a3][a4][a5]=;
f[a1][a2][a3][a4][a5]=;
if(a1+<= && (ans[a1+]==t || ans[a1+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1+,a2,a3,a4,a5,t+);
if(a2+<=a1 && (ans[a2+]==t || ans[a2+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1,a2+,a3,a4,a5,t+);
if(a3+<=a2 && (ans[a3+]==t || ans[a3+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1,a2,a3+,a4,a5,t+);
if(a4+<=a3 && (ans[a4+]==t || ans[a4+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1,a2,a3,a4+,a5,t+);
if(a5+<=a4 && (ans[a5+]==t || ans[a5+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1,a2,a3,a4,a5+,t+);
return f[a1][a2][a3][a4][a5];
}
void solve() {
scanf("%s",str+);
if(str[]=='N') {
scanf("%d",&d);
ans[]='A';
siji(i,,) {
siji(j,,) {
if(!used[j]) {
ans[i]='A'+j;
used[j]=;
init();
int x=dfs(,,,,,'A');
if(s+x>=d) break;
else {used[j]=;s+=x;}
}
}
}
printf("%s\n",ans+);
}
else {
scanf("%s",str+);
ans[]='A';
//@used[1]=1这里多了一句这句话,实际上used是记录'A'+j被用过,如果这样我们就无法填B了
siji(i,,) {
xiaosiji(j,,str[i]-'A'){
if(!used[j]) {
ans[i]='A'+j;
init();
s+=dfs(,,,,,'A');
}
}
ans[i]=str[i];
used[str[i]-'A']=;
}
printf("%d\n",s+);
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("twofive.in","r",stdin);
freopen("twofive.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 5.5 Twofive的更多相关文章
- USACO 5.4 Twofive(DP)
非常不容易的一题,思路就是DP之后输出路径.但是此题,路径和DP的方式不一样,路径要按字典序输出. 开始写了一个版本,N 10000的时候就是过不了,后来才发现,自己的写法有问题,无法保证字典序.看了 ...
- USACO 5.5 章节
Picture 题目大意 IOI 1998 求n (<=5000)个矩形 覆盖的图形 的周长(包括洞), 坐标范围[-10000,10000] 题解 一眼离散化+2维线段树,但仔细一想 空间不太 ...
- USACO . Your Ride Is Here
Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2012 JAN三题(3)
USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...
随机推荐
- jquery 格式化数字字符串(小数位)
用于页面上格式化数字字符串,此代码为工作时所需,留作笔记,比较常用. /** * author: xg君 * 描述: 格式化数字字符串,格式化小数位 * obj为需要格式的对象(例如:input标签) ...
- mongo查询日期格式数据
/ali/mongodb/bin/mongo -u user -p '123456' 127.0.0.1:27017/KYElog ISODate方式 db.col_02.find({"Lo ...
- asp.net webapi http请求生命周期
先附上webapi http生命周期图. 原始的图片地址为:https://www.asp.net/media/4071077/aspnet-web-api-poster.pdf
- crontab定时任务_net
2017年2月25日, 星期六 crontab定时任务 19. crontab 定时任务 通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本.时间间隔 ...
- Windows系统环境下Solr之Java实战(二)配置从MySQL数据库批量导入索引
1.将D:\JavaWeb\Solr\solr-6.2.0\dist下面的solr-dataimporthandler-6.2.0.jar和solr-dataimporthandler-extras- ...
- Jekens Source Code Management None 源码管理没有Git
jekens安装完成后,在配置中Source Code Management没有Git的选项,只有none,搞了大半天,一直安装插件报错,网上找的各种文章均未能解决我的问题,多次尝试后终于解决了这个问 ...
- 【Foreign】动态规划 [分治][DP]
动态规划 Time Limit: 50 Sec Memory Limit: 128 MB Description 一开始有n个数,一段区间的价值为这段区间相同的数的对数. 我们想把这n个数切成恰好k ...
- VUE组件相关总结!
定义使用一个组件 <!doctype html> <html lang="en"> <head> <meta charset=" ...
- trace spring
package xx.com.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotati ...
- cobbler 无人值守系统安装
概述 本文适合centos6 | centos7 系统的安装 执行操作之前:检查系统防火墙,selinux是否关闭,网络链接是否畅通. Cobbler是一个免费开源系统安装部署软件,用于自动化网络安装 ...