QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#371443 | #244. Turn Off The Light | AFewSuns | 100 ✓ | 294ms | 43788kb | C++17 | 2.5kb | 2024-03-30 12:22:40 | 2024-03-30 12:22:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define ll long long
#define bl bool
ll my_pow(ll a,ll b,ll mod){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll qpow(ll a,ll b){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res*=a;
a*=a;
b>>=1;
}
return res;
}
#define db double
#define pf printf
#define pc putchar
#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
#define go(u) for(ll i=head[u];i;i=e[i].nxt)
#define enter pc('\n')
#define space pc(' ')
#define fir first
#define sec second
#define MP make_pair
#define il inline
#define inf 8e18
#define random(x) rand()*rand()%(x)
#define inv(a,mod) my_pow((a),(mod-2),(mod))
il ll read(){
ll sum=0,f=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
sum=sum*10+(ch^48);
ch=getchar();
}
return sum*f;
}
il void write(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9) write(x/10);
pc(x%10+'0');
}
il void writeln(ll x){
write(x);
enter;
}
il void writesp(ll x){
write(x);
space;
}
}
using namespace my_std;
#define mod 1000000007
pair<ll,bl> pre[1000010];
ll T,n,l,r,c[1000010],suf[1000010][2],ans;
char s[1000010];
il void init(){
if((s[l]^1)=='0') pre[l]=MP(1,1);
else pre[l]=MP(3,0);
fr(i,l+1,n){
if((s[i]^1^pre[i-1].sec)=='0'){
pre[i].fir=pre[i-1].fir+1;
pre[i].sec=1;
}
else{
pre[i].fir=pre[i-1].fir+3;
pre[i].sec=0;
}
}
fr(o,0,1){
if((s[r-1]^o)=='0') suf[r-1][o]=1;
else suf[r-1][o]=2;
}
pfr(i,r-2,1){
fr(o,0,1){
if((s[i]^o)=='0') suf[i][o]=suf[i+1][1]+1;
else suf[i][o]=suf[i+1][0]+3;
}
}
}
il void solve(ll x){
if(!l){
c[x]=min(c[x],0ll);
return;
}
if(l==r&&l==x){
c[x]=min(c[x],3ll);
return;
}
if(x<r){
if(x<=l) c[x]=min(c[x],suf[x][0]);
else c[x]=min(c[x],x-l+pre[x-1].fir+suf[x][pre[x-1].sec]);
}
}
int main(){
T=read();
while(T--){
n=read();
scanf("%s",s+1);
l=r=ans=0;
pfr(i,n,1) if(s[i]=='1') l=i;
fr(i,1,n) if(s[i]=='1') r=i;
fr(i,1,n) c[i]=inf;
init();
fr(i,1,n) solve(i);
reverse(s+1,s+n+1);
reverse(c+1,c+n+1);
l=r=0;
pfr(i,n,1) if(s[i]=='1') l=i;
fr(i,1,n) if(s[i]=='1') r=i;
init();
fr(i,1,n) solve(i);
reverse(c+1,c+n+1);
fr(i,1,n) ans=(ans+i*c[i])%mod;
writeln(ans);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 294ms
memory: 43788kb
input:
146645 2 00 2 10 2 01 2 11 3 000 3 100 3 010 3 110 3 001 3 101 3 011 3 111 4 0000 4 1000 4 0100 4 1100 4 0010 4 1010 4 0110 4 1110 4 0001 4 1001 4 0101 4 1101 4 0011 4 1011 4 0111 4 1111 5 00000 5 10000 5 01000 5 11000 5 00100 5 10100 5 01100 5 11100 5 00010 5 10010 5 01010 5 11010 5 00110 5 10110 5...
output:
0 5 7 6 0 14 10 12 14 24 12 26 0 34 22 36 18 40 20 38 26 60 40 58 24 62 42 60 0 69 47 66 33 80 50 83 31 90 60 93 34 87 57 80 45 120 90 123 64 117 87 110 42 123 93 120 67 118 88 129 0 123 89 126 63 128 86 125 49 150 108 147 70 153 111 152 51 168 126 165 88 171 129 170 54 165 123 168 85 154 112 159 73...
result:
ok 146645 lines