BZOJ1386 : [Baltic2000]Stickers
显然每一位的限制独立,对于每一位求出仅限制该位下的最大数,然后求最小值即可。
假设当前要求数字$d$的答案:
考虑填数字的过程,可以看作依次考虑一个序列中的每个数,当前缀和$<0$时退出。
设$dp[i][j][k]$表示正在考虑最低的$i$位,高位部分有$j$个$d$,第$i$位能不能填$0$为$k$时,所有可能的数字形成的序列的信息。
这个信息需要维护两个值:
- $f$:前缀和最小值。
- $s$:总和。
显然这个信息可以进行合并。
求出答案的位数后,再从高到低逐位确定即可。
时间复杂度$O(\log^2n)$。
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100,B=10000,MAXL=25;
int cur,v[N][N][2];
struct Num{
int a[MAXL],len,fu;
Num(){len=1,fu=a[1]=0;}
void clr(){len=1,fu=a[1]=0;}
Num operator+(const Num&b)const{
Num c;
c.len=max(len,b.len)+2;
int i;
for(i=1;i<=c.len;i++)c.a[i]=0;
if(fu==b.fu){
for(i=1;i<=len;i++)c.a[i]=a[i];
for(i=1;i<=b.len;i++)c.a[i]+=b.a[i];
for(i=1;i<=c.len;i++)if(c.a[i]>=B)c.a[i+1]++,c.a[i]-=B;
while(c.len>1&&!c.a[c.len])c.len--;
c.fu=fu;
}else{
bool flag=0;
if(len==b.len){
for(i=len;i;i--)if(a[i]!=b.a[i]){
if(a[i]>b.a[i])flag=1;
break;
}
}else{
if(len>b.len)flag=1;
}
if(flag){
for(i=1;i<=len;i++)c.a[i]=a[i];
for(i=1;i<=b.len;i++)c.a[i]-=b.a[i];
for(i=1;i<=c.len;i++)if(c.a[i]<0)c.a[i+1]--,c.a[i]+=B;
while(c.len>1&&!c.a[c.len])c.len--;
c.fu=fu;
}else{
for(i=1;i<=b.len;i++)c.a[i]=b.a[i];
for(i=1;i<=len;i++)c.a[i]-=a[i];
for(i=1;i<=c.len;i++)if(c.a[i]<0)c.a[i+1]--,c.a[i]+=B;
while(c.len>1&&!c.a[c.len])c.len--;
c.fu=b.fu;
}
}
return c;
}
Num operator-(Num b)const{
b.fu^=1;
return *this+b;
}
Num operator*(const Num&b)const{
Num c;
c.len=len+b.len+2;
c.fu=fu^b.fu;
int i,j;
for(i=1;i<=c.len;i++)c.a[i]=0;
for(i=1;i<=len;i++)for(j=1;j<=b.len;j++){
c.a[i+j-1]+=a[i]*b.a[j];
if(c.a[i+j-1]>=B){
c.a[i+j]+=c.a[i+j-1]/B;c.a[i+j-1]%=B;
if(c.a[i+j]>=B)c.a[i+j+1]+=c.a[i+j]/B,c.a[i+j]%=B;
}
}
while(c.len>1&&!c.a[c.len])c.len--;
return c;
}
bool iszero()const{
return len==1&&!a[1];
}
void write(){
if(len==1&&!a[1])fu=0;
if(fu)putchar('-');
printf("%d",a[len]);
for(int i=len-1;i;i--)printf("%04d",a[i]);
}
void set(int x){
if(x<0)fu=1,x=-x;else fu=0;
if(x>=B){
len=2;
a[1]=x%B;
a[2]=x/B;
}else{
len=1;
a[1]=x;
}
}
int sgn()const{
if(iszero())return 0;
return fu==1?-1:1;
}
int cmp(const Num&b)const{
int x=sgn(),y=b.sgn();
if(x!=y)return x<y?-1:1;
if(!x)return 0;
if(x>0){
if(len!=b.len)return len<b.len?-1:1;
for(int i=len;i;i--)if(a[i]!=b.a[i])return a[i]<b.a[i]?-1:1;
return 0;
}
if(len!=b.len)return len>b.len?-1:1;
for(int i=len;i;i--)if(a[i]!=b.a[i])return a[i]>b.a[i]?-1:1;
return 0;
}
bool operator<(const Num&b)const{return cmp(b)<0;}
bool operator==(const Num&b)const{return cmp(b)==0;}
bool operator>(const Num&b)const{return cmp(b)>0;}
}ans,val[11];
struct P{
Num f,s;
P(){f.clr();s.clr();}
void clr(){f.clr();s.clr();}
P(Num _f,Num _s){f=_f,s=_s;}
P operator+(const P&b)const{return P(min(f,s+b.f),s+b.s);}
void operator+=(const P&b){*this=*this+b;}
}base,f[N][N][2];
P dfs(int x,int y,int z){
if(x==0){
P t;
t.f.set(-y);
t.s.set(-y);
return base+t;
}
if(v[x][y][z]==cur+1)return f[x][y][z];
v[x][y][z]=cur+1;
P t;
t.clr();
for(int i=z;i<10;i++)t+=dfs(x-1,y+(i==cur),0);
return f[x][y][z]=t;
}
Num solve(int _cur,int _base){
cur=_cur;
base.f.set(0);
base.s.set(_base);
int i,j,len;
P pre;
pre.clr();
for(len=1;;len++){
P now=dfs(len,0,1);
if((pre+now).f.sgn()<0)break;
pre+=now;
}
Num ans=val[0];
int sum=0;
for(i=len;i;i--)for(j=i==len?1:0;;j++){
int nowsum=sum+(j==cur);
P now=dfs(i-1,nowsum,0);
if((pre+now).f.sgn()<0){
sum=nowsum;
ans=ans*val[10]+val[j];
break;
}
pre+=now;
}
return ans;
}
int main(){
for(int i=0;i<11;i++)val[i].set(i);
for(int i=0;i<10;i++){
int x;
scanf("%d",&x);
if(i==0)ans=solve(i,x);
else ans=min(ans,solve(i,x));
}
ans=ans-val[1];
ans.write();
return 0;
}
BZOJ1386 : [Baltic2000]Stickers的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- bzoj1385: [Baltic2000]Division expression
欧几里得算法.可以发现规律,a[2]作为分母,其他作为分子,必定是最好的选择.判断是否为整数即可. #include<cstdio> #include<cstring> #in ...
- BZOJ 1385: [Baltic2000]Division expression
题目 1385: [Baltic2000]Division expression Time Limit: 5 Sec Memory Limit: 64 MB Description 除法表达式有如下 ...
- About Unixstickers - Unixstickers - stickers on unix, programming, software, development and open source
About Unixstickers - Unixstickers - stickers on unix, programming, software, development and open so ...
- [LeetCode] Stickers to Spell Word 贴片拼单词
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- [Swift]LeetCode691. 贴纸拼词 | Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- 使用Stickers拓展集成iMessage简单功能
添加一个target,选择Stickers拓展: 然后就会出现iMessage的文件夹:添加你需要的iMessage图片,这里图片遵循下面的要求: Small: 100 x 100 pt @3x sc ...
- LeetCode691. Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- 691. Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
随机推荐
- iTOP-4418开发板支持Android4.4/5.1.1系统、Linux3.4.39、QT2.2/4.7/5.7、Ubuntu12.04
核心板参数 尺寸:50mm*60mm 高度:核心板连接器组合高度1.5mm PCB层数:6层PCB沉金设计 4418 CPU:ARM Cortex-A9 四核 S5P4418处理器 1.4GHz 68 ...
- 基于Gecko内核的简单浏览器实现
分享一个基于Gecko内核的简单浏览器实现过程. 项目需要需要开发一个简单浏览器,由于被访问的网页中有大量Apng做的动画,使用IE内核的webbrowser不能播放,使用基于WebKit和Cefsh ...
- spring对bean的管理细节
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 内核模块加载错误 “Invalid module format” 解决办法
参考链接: http://blog.chinaunix.net/uid-20448327-id-172345.html
- 阿里云服务器+ftp文件操作+基于Centos7的vsftpd配置
路径问题:一定要注意此位置是否需要加入"/" 文件上传方式:被动模式 vsftp完整配置: # # The default compiled in settings are fai ...
- 20165221 《网络对抗技术》EXP2:后门原理与实践
20165221 <网络对抗技术>EXP2:后门原理与实践 实验任务 任务一:使用netcat获取主机操作Shell,cron启动 (0.5分) 任务二:使用socat获取主机操作Shel ...
- Mathematica 2
如今的数值分析,如果没有高等代数的基础,都不好意思打招呼说自己是 "有数学基础". 高等代数,解决问题的一大神器. 1,初等变换 2,特征值 | A-λE | = | λE - A ...
- Linux下定时备份文件
一. 编写脚本 编写一个脚本文件,使脚本可以执行备份命令. 例如,将文件目录 /home/backups/balalala 备份到/home目录下,并压缩. 1. 创建脚本 命令格式: touch 路 ...
- 增加swap分区
起因:开发人员说tomcat关闭了,然后我排查了下,发现内存耗尽,然后临时用swap分区,以供当前运行的程序使用. 先用free -h查看一下swap的大小 1.添加swap分区 使用dd命令创建/h ...
- ubuntu 18.04 配置远程ssh/远程ftp/远程vnc登陆
18.04相比过去采用了新的桌面,配置环境稍微有一些不同了. 首先是远程登录,windows用Tera Trem连接,ip地址得自己根据实际情况来. ubuntu上,sudo apt-get inst ...