QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#456718 | #8089. Flaaffy | do_while_true | AC ✓ | 1882ms | 76196kb | C++20 | 3.7kb | 2024-06-28 11:32:34 | 2024-06-28 11:32:35 |
Judging History
answer
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<ctime>
#include<random>
#include<array>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n'
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n'
#define DE(fmt,...) fprintf(stderr, "Line %d : " fmt "\n",__LINE__,##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
r=0;bool w=0;char ch=getchar();
while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
inline int bit(int x){return 1<<x;}
const int N=100010;
const int inf=0x3f3f3f3f;
int f[46][N],g[46][N];
int ban[N][35];
struct Graph{
int head[N],ent;
struct Edge{
int nxt,c,j;
}e[N*6];
inline void adde(int x,int c,int j){
e[++ent]={head[x],c,j};head[x]=ent;
}
void clear(int n){
ent=0;for(int i=0;i<=n;i++)head[i]=0;
}
}ef,eg;
int dig[10];
int mn[7][161052],mx[7][161052];
int pc[35];
int cost(int x,int y){
int s=0;
while(x||y){
s+=(x%10)!=(y%10);
x/=10;y/=10;
}
return s;
}
void init(int n){
for(int i=0;i<=32;i++)pc[i]=__builtin_popcount(i);
for(int i=0;i<=n;i++){
int x=i;
for(int j=1;j<=5;j++)dig[j]=x%10,x/=10;
for(int S=0;S<bit(5);S++){
x=0;
for(int j=5;j>=1;j--){
if(bit(j-1)&S)x=x*11+10;
else x=x*11+dig[j];
}
ban[i][S]=x;
}
}
for(int i=0;i<=n;i++)
f[0][i]=g[0][i]=i;
for(int k=1;k<=43;k++){
for(int i=1;i<=6;i++)
for(int j=0;j<=ban[n][31];j++)
mn[i][j]=n+1,mx[i][j]=-1;
ef.clear(n);eg.clear(n);
for(int c=max(0,k-6);c<k;c++){
for(int j=0;j<n;j++){
eg.adde(g[c][j+1],c,j);
}
for(int j=1;j<=n;j++){
ef.adde(f[c][j-1],c,j);
}
}
for(int i=n-1;i>=0;i--){
f[k][i]=f[k-1][i];
for(int o=eg.head[i];o;o=eg.e[o].nxt){
int c=eg.e[o].c,j=eg.e[o].j;
for(int S=0;S<bit(5);S++){
int x=ban[j][S];
cmin(mn[k-c][x],j);
}
}
for(int S=0;S<bit(5);S++){
int x=ban[i+1][S];
int c=k-pc[S]-1;
if(c<0)continue;
int p=mn[k-c][x];
if(p==n+1)continue;
if(p==0)cmin(f[k][i],p);
else cmin(f[k][i],f[c][p-1]);
}
}
for(int i=1;i<=n;i++){
g[k][i]=g[k-1][i];
for(int o=ef.head[i];o;o=ef.e[o].nxt){
int c=ef.e[o].c,j=ef.e[o].j;
for(int S=0;S<bit(5);S++){
int x=ban[j][S];
cmax(mx[k-c][x],j);
}
}
for(int S=0;S<bit(5);S++){
int x=ban[i-1][S];
int c=k-pc[S]-1;
if(c<0)continue;
int p=mx[k-c][x];
if(p==-1)continue;
if(p==n)cmax(g[k][i],n);
else cmax(g[k][i],g[c][p+1]);
}
}
}
}
int calcf(int l,int r){
if(l>=r)return 0;
for(int k=0;k<=45;k++)
if(f[k][r]<=l)
return k;
return N;
}
int calcg(int l,int r){
if(l>=r)return 0;
for(int k=0;k<=45;k++)
if(g[k][l]>=r)
return k;
return N;
}
signed main(){
init(99999);
int T;read(T);
while(T--){
int l,r;read(l,r);
int ans=inf;
if(l==r)ans=0;
for(int k=l;k<=r;k++){
cmin(ans,max(calcf(l,k-1),calcg(k+1,r)) + 1 + cost(0,k));
}
cout<<ans<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1820ms
memory: 76020kb
input:
3 97 107 12043 12045 61 69
output:
6 5 7
result:
ok 3 number(s): "6 5 7"
Test #2:
score: 0
Accepted
time: 1797ms
memory: 75608kb
input:
36 19997 20007 9997 10012 59813 60010 39993 40004 89878 90009 9909 10028 89898 90005 373 3773 93012 94646 93013 94617 7171 10601 7546 10546 15290 20600 446 16446 6440 97000 9713 12175 6000 37766 55115 76767 16 161 1713 2062 1 99999 1024 65536 13000 99999 49999 50000 70000 70001 27999 28001 27998 280...
output:
8 9 19 10 19 19 18 28 28 27 28 28 31 35 41 28 37 36 16 20 42 40 41 2 2 3 6 4 6 2 14 2 4 5 4 4
result:
ok 36 numbers
Test #3:
score: 0
Accepted
time: 1766ms
memory: 75820kb
input:
50 1 9 4 7 1 6 1 7 5 10 4 6 3 11 8 10 2 3 5 11 4 9 2 11 6 9 1 8 8 11 6 10 2 7 8 9 3 10 3 6 4 8 4 11 7 10 5 8 1 2 7 11 2 10 3 7 2 5 2 8 7 9 5 9 1 11 9 10 3 4 6 7 4 5 2 6 2 4 1 3 3 8 5 6 1 10 6 8 1 4 9 11 4 10 6 11 5 7 1 5
output:
6 4 4 4 4 2 6 2 2 5 4 6 4 6 5 4 4 2 6 4 4 6 4 4 2 5 6 4 4 4 2 4 6 2 2 2 2 4 2 2 4 2 6 2 4 2 4 5 2 4
result:
ok 50 numbers
Test #4:
score: 0
Accepted
time: 1828ms
memory: 75928kb
input:
50 103 269 432 437 94 365 24 412 182 290 88 323 123 355 211 423 24 418 73 387 320 393 103 168 80 277 277 327 97 260 156 388 328 348 337 470 208 284 337 497 209 223 150 407 226 431 276 307 11 95 315 346 21 260 139 452 91 144 216 344 40 174 321 478 327 378 188 402 231 356 193 240 248 422 237 364 87 26...
output:
18 6 19 20 16 18 19 18 20 20 14 14 18 13 18 19 11 16 14 17 10 19 18 13 13 11 19 19 13 17 16 16 14 18 16 13 17 16 18 16 13 13 20 19 20 21 15 10 8 12
result:
ok 50 numbers
Test #5:
score: 0
Accepted
time: 1882ms
memory: 75696kb
input:
50 39167 59121 23404 46045 27925 84923 59663 77115 18813 71950 15888 32636 68462 70238 16041 39841 41943 93247 6427 65720 43636 98046 39590 70829 30265 79911 65353 94966 28268 76300 59266 99300 34668 81785 10336 68663 85282 94010 12991 22105 19092 32393 7828 99527 56520 66550 90798 94286 2967 92322 ...
output:
36 37 40 36 40 35 28 37 39 40 40 38 40 37 39 38 39 40 33 34 35 42 33 30 41 40 39 39 41 29 37 40 40 40 33 40 39 40 33 38 38 40 39 39 40 40 40 34 36 40
result:
ok 50 numbers
Test #6:
score: 0
Accepted
time: 1770ms
memory: 76196kb
input:
1 420 6969
output:
31
result:
ok 1 number(s): "31"
Test #7:
score: 0
Accepted
time: 1781ms
memory: 76188kb
input:
49 67369 67407 83713 88445 23619 23786 12412 12431 28713 31776 36593 36687 20943 20957 18919 19476 16313 17177 17113 99998 52363 52396 35013 35577 2383 2401 32112 34276 83513 85676 1713 18776 11113 47776 5473 5945 55585 55592 81919 83445 41712 50216 46873 47176 63519 63546 45128 45206 41113 62776 71...
output:
15 30 18 12 28 18 9 23 26 40 13 24 10 28 28 34 37 21 10 27 33 20 13 17 35 27 29 11 16 15 28 32 23 17 37 21 32 5 7 33 28 30 12 26 7 29 6 28 8
result:
ok 49 numbers
Test #8:
score: 0
Accepted
time: 1832ms
memory: 76060kb
input:
50 7113 34445 21113 57776 7112 54445 2713 10817 11113 99998 49772 62776 12113 17776 11112 67776 47112 94445 17112 99998 19113 27446 76113 81176 23113 31777 5113 10645 57112 84445 35112 56776 71113 92777 27113 74445 63113 68776 55113 68445 18112 31776 27113 54445 62713 70816 1113 22776 35113 48445 37...
output:
36 37 39 32 41 35 31 40 39 41 33 31 34 30 37 37 36 38 31 33 35 36 32 35 33 39 35 30 33 31 33 31 42 38 34 37 40 31 31 36 36 33 34 37 33 33 37 32 35 32
result:
ok 50 numbers
Test #9:
score: 0
Accepted
time: 1764ms
memory: 75456kb
input:
50 18612 20645 18473 19177 5113 8776 51073 51346 45318 45486 36113 39776 30173 31876 85713 94776 11112 14776 64712 67445 53912 54145 31273 32080 3273 3545 38213 40246 22113 22476 713 13777 1073 1545 60862 61065 9173 9645 5112 10645 40213 40577 55213 56116 35713 44777 39919 41185 56613 56826 47112 49...
output:
29 25 28 21 19 29 26 32 30 29 20 24 19 29 21 34 21 20 21 31 21 25 33 25 19 28 18 33 30 19 22 21 25 22 28 32 27 28 32 19 27 23 27 28 27 31 19 29 22 27
result:
ok 50 numbers