[Codeforces 8E] Beads
Brief Intro:
将所有n位二进制串中满足字典序不大于其逆序串,取反串,逆序取反串中按字典序排序的第m个输出
n<=50
Algorithm:
首次接触数位DP的题目
根据数据范围,我们不能枚举。从而只能一位一位确定
由于要求字典序第m大的,我们可以通过求出如果当前位为0的个数来确定该位为0还是1
接下来就是典型的数位DP了
数位DP其实也就是一个记忆化的过程,同时在状态中用变量对上界加以标识
此题中即为dp[cur][rev][inv],后两位表示是否达到反串上界和逆串上界
Code:
#include <bits/stdc++.h> using namespace std;
typedef long long ll; ll n,k,dp[][][],res[]; ll solve(ll cur,ll rev,ll inv)
{
if(dp[cur][rev][inv]>-) return dp[cur][rev][inv]; //记忆化
if(cur*>=n) return dp[cur][rev][inv]=; ll &ret=dp[cur][rev][inv]=;
for(int i=;i<;i++)
if(res[cur+]==i || res[cur+]==-)
for(int j=;j<;j++)
if(res[n-cur]==j || res[n-cur]==-)
if((i==j || *cur+!=n) && (!rev || i<=j) && (!inv || i<=-j))
ret+=solve(cur+,rev&(i==j),inv&(i!=j));
return ret;
} int main()
{
cin >> n >> k;k++; memset(dp,-,sizeof(dp));
memset(res,-,sizeof(res)); if(solve(,,)<k)
return cout << -,; for(int i=;i<=n;i++)
{
memset(dp,-,sizeof(dp));
res[i]=;
ll t=solve(,,); //判断该位的值
if(t<k) k-=t,res[i]=;
cout << res[i];
}
return ;
}
Review:
1、对于求第k个满足要求的数时
可以通过 已知前缀枚举满足要求的个数+逐位确定每一位 的方法来取代枚举
2、数位DP的特殊点仅仅在对上界状态的维护,其余和记忆化搜索相同
[Codeforces 8E] Beads的更多相关文章
- Codeforces Round #339 (Div. 1) C. Necklace 构造题
C. Necklace 题目连接: http://www.codeforces.com/contest/613/problem/C Description Ivan wants to make a n ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 树剖模板by fcdalao
#include<bits/stdc++.h> using namespace std; ; *MX]; *MX]; int n,Index,fir[MX],fa[MX],dfn[MX], ...
- HDU 5670
Machine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- cloudera manager 5.3完整卸载脚本
service cloudera-scm-agent stop service cloudera-scm-agent stop umount /var/run/cloudera-scm-agent/p ...
- rman备份与异机恢复
一.rman备份脚本并为定时任务 #!/bin/bashsource ~/.bash_profileexport LANG=en_USBACKUP_DATE=`date +%d`#RMAN_LOG_F ...
- postfix导致maillog填满磁盘空间的巨坑!
双休日回家pull在公司修改的代码...于是菜鸟的linux探索之路开始了 1.df -f发现磁盘又占满了(之前是node的error) 2.发现maillog整整10个G,无数条(Jul 7 04: ...
- Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...
- python中orm框架学习
安装sqlalchemy pip3 install sqlalchemy 创建表结构: from sqlalchemy import Column,String,create_engine from ...
- bzoj2002 弹飞绵羊 lct版
这道题就是维护一个有根的lct 一开始建树全部建虚边 求多少次弹出就是求他到根的距离(根为n+1) 这里有个小技巧 将n+1作为根而没有虚根操作起来会比较方便 #include<cstdio&g ...
- HDU1068 (二分图最大匹配匈牙利算法)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Day 14 python 之 字符串练习
一.字符串总结与练习 #! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "DaChao" # Date: ...