usaco 5.5 Twofive(统计+DP)

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
题意:给你一个5*5的表格,你可以在里面填26个字母,保证左边的小于右边的,上边的小于下边的,把所有解按字典序编号,现在给你编号,要你输出解,或者给你一种方案,要求编号。。。
分析:其实想了好久都没想明白,大概就是统计,然后利用记忆化搜索加速。。。
我完全抄别人代码的,统计这块实在不熟练啊T_T
代码:
/*
ID: 15114582
PROG: twofive
LANG: C++
*/
#include<cstdio>
#include<cstring>
using namespace std;
int f[6][6][6][6][6];
char op[9],s[33],out[33];
int i,j,n,tmp;
bool ok(int x,int t)
{
    return (!out[x]||out[x]==t+'A');
}
int dfs(int a,int b,int c,int d,int e,int t)
{
    if(t>=25)return 1;
    int &tmp=f[a][b][c][d][e];
    if(tmp)return tmp;
    if(a<5&&ok(a,   t))tmp+=dfs(a+1,b,c,d,e,t+1);
    if(b<a&&ok(b+ 5,t))tmp+=dfs(a,b+1,c,d,e,t+1);
    if(c<b&&ok(c+10,t))tmp+=dfs(a,b,c+1,d,e,t+1);
    if(d<c&&ok(d+15,t))tmp+=dfs(a,b,c,d+1,e,t+1);
    if(e<d&&ok(e+20,t))tmp+=dfs(a,b,c,d,e+1,t+1);
    return tmp;
}
int main()
{
    freopen("twofive.in","r",stdin);
    freopen("twofive.out","w",stdout);
    while(~scanf("%s",op))
    {
        if(op[0]=='N')
        {
            scanf("%d",&n);
            for(i=0;i<25;++i)
                for(out[i]='A';1;++out[i])
                {
                    memset(f,0,sizeof(f));
                    tmp=dfs(0,0,0,0,0,0);
                    if(tmp>=n)break;
                    n-=tmp;
                }
            puts(out);
        }
        if(op[0]=='W')
        {
            scanf("%s",s);
            for(n=i=0;i<25;++i)
                for(out[i]='A';out[i]<s[i];++out[i])
                {
                    memset(f,0,sizeof(f));
                    n+=dfs(0,0,0,0,0,0);
                }
            printf("%d\n",n+1);
        }
    }
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是P4087 [USACO17DEC]Milk Measurement的c++代码: ```c++ #include<bits/stdc++.h> using namespace std; int n,d,i,x,minn=1e9,maxn=-1e9,sum=7;//注意sum要初始化为7,因为一开始有三个人挤奶! map<int,int> mp; struct node{ int day,milk,id;//day表示某一天,milk表示这一天的产奶量,id表示这头牛的编号 }a[100010]; bool cmp(node x,node y){ return x.day<y.day; } int main(){ scanf("%d%d",&n,&d); for(i=1;i<=n;i++){ scanf("%d%d%d",&a[i].day,&a[i].id,&a[i].milk); minn=min(minn,a[i].id);//记录最小的牛的编号 maxn=max(maxn,a[i].id);//记录最大的牛的编号 } sort(a+1,a+n+1,cmp);//排序 for(i=1;i<=n;i++){ int p=a[i].id; mp[p]+=a[i].milk;//记录每头牛产奶总量 if(mp[p]-a[i].milk>=mp[minn]&&mp[p]>=mp[minn]){//如果这头牛的产奶总量减去这一天的产奶量后等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 sum--; } if(mp[p]>=mp[maxn]&&mp[p]-a[i].milk<mp[maxn]){//如果这头牛的产奶总量大于等于最大产奶量且这头牛的产奶总量减去这一天的产奶量小于最大产奶量 sum++; } if(mp[p]-a[i].milk<mp[maxn]&&mp[p]>=mp[maxn]){//如果这头牛的产奶总量减去这一天的产奶量小于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]-mp[p]+a[i].milk>0)sum++; } mp[p]-=a[i].milk;//减去这一天的产奶量 if(i==n||a[i].day!=a[i+1].day){//如果到了新的一天或者到了最后一天 if(mp[maxn]!=mp[a[i].id]&&mp[a[i].id]>=mp[maxn])sum++;//如果这头牛的产奶总量不等于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]==mp[a[i].id]){//如果这头牛的产奶总量等于最大产奶量 if(a[i].id==maxn)sum+=0;//如果这头牛就是最大产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } if(mp[minn]!=mp[a[i].id]&&mp[a[i].id]>=mp[minn])sum++;//如果这头牛的产奶总量不等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 if(mp[minn]==mp[a[i].id]){ if(a[i].id==minn)sum+=0;//如果这头牛就是最小产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } } } printf("%d\n",sum); return 0; } ``` 该题的解题思路是模拟,需要注意细节问题。我们可以首先将输入的数据按天数排序,然后模拟每一天挤奶的情况,并根据题目要求进行计数即可。具体细节请见代码注释。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值