显然每一位的限制独立,对于每一位求出仅限制该位下的最大数,然后求最小值即可。

假设当前要求数字$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的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. bzoj1385: [Baltic2000]Division expression

    欧几里得算法.可以发现规律,a[2]作为分母,其他作为分子,必定是最好的选择.判断是否为整数即可. #include<cstdio> #include<cstring> #in ...

  3. BZOJ 1385: [Baltic2000]Division expression

    题目 1385: [Baltic2000]Division expression Time Limit: 5 Sec  Memory Limit: 64 MB Description 除法表达式有如下 ...

  4. About Unixstickers - Unixstickers - stickers on unix, programming, software, development and open source

    About Unixstickers - Unixstickers - stickers on unix, programming, software, development and open so ...

  5. [LeetCode] Stickers to Spell Word 贴片拼单词

    We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...

  6. [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 ...

  7. 使用Stickers拓展集成iMessage简单功能

    添加一个target,选择Stickers拓展: 然后就会出现iMessage的文件夹:添加你需要的iMessage图片,这里图片遵循下面的要求: Small: 100 x 100 pt @3x sc ...

  8. LeetCode691. Stickers to Spell Word

    We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...

  9. 691. Stickers to Spell Word

    We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...

随机推荐

  1. Python核心编程笔记 第二章

    2.1   程序输出:print语句         可以使用print语句显示变量的字符串表示,或者仅用变量名查看该变量的原始值. 2.2   程序输出和raw_input()内建函数       ...

  2. python selenium 最简单示例

    使用 pip 安装  selenium 下载 chromedriver,添加在PATH中 # -*- coding: utf-8 -*- from selenium import webdriver ...

  3. 关于Java的volatile

    volatile的作用 1.防止指令重排序 首先要理解什么是指令重排序?指令重排序的利弊?后续举例说明 2.多线程访问共享资源时,缓解synchronized重量级锁带来的性能问题 但是volatil ...

  4. UOJ #460 新年的拯救计划

    清真的构造题 UOJ# 460 题意 求将$ n$个点的完全图划分成最多的生成树的数量,并输出一种构造方案 题解 首先一棵生成树有$ n-1$条边,而原完全图只有$\frac{n·(n-1)}{2}$ ...

  5. ubuntu 安装 lamp

    链接: http://www.cnblogs.com/CheeseZH/p/4694135.html

  6. python3 正则表达式学习笔记

    re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. ~匹配成功re.match方法返回一个匹配的对象,否则返回No ...

  7. iframe父页面和子页面调用

    我在页面中使用iframe标签,有的时候就需要两个页面交互 <iframe id="Iframe"  src="{{url('field/user')}}" ...

  8. java学习笔记07-循环

    java有三种主要的循环结构 while循环 do...while循环 for循环 while循环 while(布尔表达式){ //循环内容 } public static void main(Str ...

  9. Mysql 多实例 +表损坏

    什么是实例? 进程+多个线程+预分配的内存结构 MySQL多实例: 多个进程+多个线程+多个预分配内存结构 多个配置文件: 1)多个端口 2)多个数据目录 3)多个socket文件 ./mysql_i ...

  10. python学习:一

    第三章:实践题作业 1.编写一个名为 collatz()的函数,它有一个名为 number 的参数.如果参数是偶数,那么 collatz()就打印出 number // 2, 并返回该值.如果 num ...