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 ...
随机推荐
- mysql数据库 删除某几个字段相同的重复记录并根据另一字段留下一条记录
1.例如Mysql数据库中表a中的记录,id=2,id=6,id=7的记录是重复的(iId,cId等多个字段相同),现在想留下id最小(id=2)或最大(id=7)的一条记录
- Arduino语法-变量和常量
变量的声明: int led=11 一般变量的声明方法为类型名+变量名+变量初始化值.变量名的写法约定为首字母小写 变量的作用范围又称为作用域,变量的作用范围与该变量在哪儿声明有关,大致分为如下两种: ...
- select中option的onclick事件失效
html: <select id="pageSelect"> <option value="1" selected onclick=" ...
- SqlServer存储过程及函数
存储过程和函数类似于Java中的方法. ⒈存储过程 一组预先编译好的sql语句的集合,理解成批处理语句. 好处: ①提高代码的重用性 ②简化操作 ③减少了编译次数并且减少了和数据库服务器的连接次数,提 ...
- TinyMCE
段落段落段落段落 标题1 标题2 标题3 标题4 标题5 标题6
- 0ctf 2019 zero_task
一个条件竞争的洞,以前没有接触过.比赛完研究了wp复现下. new_thread结构体如下 new_thread: thread_area size0x8 thread_area+0x8 size 0 ...
- MQTT控制---pingreq
心跳请求 客户端向服务端发送PINGREQ报文用于: 在没有任何其他控制报文从client发给server时,告诉server,client还活着 请求server发送 响应确认它还活着 使用网络以确 ...
- 算法工程师<编程题>
<编程题> 1.[Maximum Product Subarray 求最大子数组乘积] 这个求最大子数组乘积问题是由最大子数组之和问题演变而来,但是却比求最大子数组之和要复杂,因为在求和的 ...
- Python+Flask搭建mock api server
Python+Flask搭建mock api server 前言: 近期由于工作需要,需要一个Mock Server调用接口直接返回API结果: 假如可以先通过接口文档的定义,自己模拟出服务器返回结果 ...
- tp5 修改默认的分页url
默认分页url:xx.com/xxx?page=1 个人主要感觉不美观,想变成xx.com/xxx/list_1.html这样的 框架本身默认使用的boostrap分页类,目录位置 simplewin ...