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 ...
随机推荐
- android studio run得时候 选择开启对话框
一个项目run 调试得时候,在Android studio3.0默认得设置是运行在上一个device上,我们通过改变设置,废话不多说,上图: 然后点击这个edit config 在miscellane ...
- hbase系列之:初识hbase
一.概述 在hadoop生态圈里,hbase可谓是鼎鼎大名.江湖传言,hbase可以实现数十亿行X数百万列的实时查询,可横向扩展存储空间.如果传言为真,那得好好了解了解hbase.本文从概念上介绍hb ...
- HTML面试基础问题
1.Doctype作用?严格模式与混杂模式如何区分?它们有何意义? 1)<!DICTYPE>声明位于文档中的最前面,处于<html>标签之前,告诉浏览器的解析器,用什么文档 ...
- Python json转字符串的一些细节
要调PHP那边的接口,php那边一直都校验不过,很是郁闷.没办法,只能让人把发送成功的代码拿过来看,不过是php写的,步骤都是一样: php端: 1. json对象转json字符串. 2. 对json ...
- Linux学习2-fork
复制进程映像 fork() 要想让进程同时执行多个函数,我们可以使用线程或从源程序中创建一个完全分离的进程,后者就像init的做法一样,而不像exec调用那样用新程序替换当前指向的线程. 我们可以通过 ...
- PHPCMS v9 安全防范教程!
一.目录权限设置很重要:可以有效防范黑客上传木马文件.如果通过 chmod 644 * -R 的话,php文件就没有权限访问了.如果通过chmod 755 * -R 的话,php文件的权限就高了. 所 ...
- MySQL Sakila样本数据库
Sakila样本数据库介绍 Sakila样本数据库是MySQL官方提供的一个模拟DVD租赁信息管理的数据库,提供了一个标准模式,可作为书中例子,教程.文章.样品,等等,对学习测试来说是个不错的选择. ...
- webpack构建react多页面应用
写这个的初衷是很难找一个简洁的项目脚手架,很多脚手架都有很多依赖,光看依赖就要很久,所以自己参照网上的内容,弄个这么一个简单的多页面的脚手架. 利用creat-react-app 新建一个react应 ...
- JavaScript 优雅的实现方式包含你可能不知道的知识点
有些东西很好用,但是你未必知道:有些东西你可能用过,但是你未必知道原理. 实现一个目的有多种途径,俗话说,条条大路通罗马.很多内容来自平时的一些收集以及过往博客文章底下的精彩评论,收集整理拓展一波,发 ...
- Hibernate5笔记4--单表查询
单表查询: Hibernate是DAO层技术,对数据的使用,查询是最为重要的.Hibernate的查询技术非常强大,支持原始SQL语句查询,支持QBC查询及Hibernate特有的HQL查询. H ...