QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#125909 | #6627. Line Town | starrylasky | 4 | 315ms | 44672kb | C++14 | 4.4kb | 2023-07-17 21:46:03 | 2023-07-17 21:46:04 |
Judging History
answer
///yl/hs/bx/yl
#include<bits/stdc++.h>
#define fep(i,l,r) for(int i=l;i<=r;++i)
#define feb(i,r,l) for(int i=r;i>=l;--i)
#define For(i,u) for(int i=head[u];i;i=e[i].nxt)
#define LL long long
// #define int long long
#define ld long double
#define pr pair<int,int>
#define mpr make_pair
using namespace std;
const int N = 5e5+5,mod = 1e9+7;
const LL INF = 1e15;
inline int read()
{
int s=0,w=1; char ch=getchar();
while(!(ch>='0'&&ch<='9')) {if(ch=='-') w=-1; ch=getchar();}
while( ch>='0'&&ch<='9') {s=(s<<1)+(s<<3)+ch-'0'; ch=getchar();}
return s*w;
}
inline int Mod(int x) {return x>=mod?x-mod:x;}
inline void addmod(int &x,int y) {x=Mod(x+y);}
inline void chkmin(LL &x,LL y) {x=min(x,y);}
namespace starrylasky
{
int n,num,S,a[N],b[N],rk[N],id[N],col[N]; vector<int > v[N];
LL f[2],g[2];
struct BIT
{
#define lowbit(x) (x&-x)
int sum[N];
inline void update(int x,int res) {for(;x<=n;x+=lowbit(x)) sum[x]+=res;}
inline int query(int x)
{
int ans=0;
for(;x;x-=lowbit(x)) ans+=sum[x];
return ans;
}
}b1,b2;
inline LL calc(int m)
{
LL ans=0;// cerr<<m<<"\n";
feb(i,m,1) ans+=b2.query(rk[id[i]]),b2.update(rk[id[i]],1);
fep(i,1,m) b2.update(rk[id[i]],-1);
return ans;
}
inline void work(int x,int cl,int cr)
{
if(g[cl]==INF) return ;
int siz=v[x].size(),cnt0=0,cnt1=0,c0=(siz+1)/2,c1=siz/2,n0=0,n1=0;
vector<int > a1,a0; a1.resize(siz); a0.resize(siz);
fep(i,1,siz)
{
rk[i]=b1.query(v[x][i-1]);
if(col[v[x][i-1]]) a1[++n1]=i,++cnt1;
else a0[++n0]=i,++cnt0;
}
if(cr) swap(c0,c1);
if(cl==(cr^((siz-1)&1)))
{
if(cnt0!=c0||cnt1!=c1) return ;
for(int i=1,j=1,k=1;i<=siz;i++) id[i]=(cr^((siz-i)&1)?a1[j++]:a0[k++]);
LL s=calc(siz);
fep(i,1,siz) s+=num-(siz-i)-rk[i];
chkmin(f[cl],g[cl]+s);// cerr<<"type1:"<<x<<" "<<s<<"\n";
fep(i,1,siz)
{
int j=id[i];
s+=rk[j]-j-(num-(siz-j)-rk[j]);// cerr<<s<<" ";
chkmin(f[cl^(i&1)],g[cl]+s);
}
//cerr<<"\n";
}
else if(c0==cnt0&&c1==cnt1)
{
for(int i=1,j=1,k=1;i<=siz;i++) id[i]=(cr^((siz-i)&1)?a1[j++]:a0[k++]);
LL s=calc(siz);
fep(i,1,siz) s+=num-(siz-i)-rk[i];
chkmin(f[cl],g[cl]+s);// cerr<<"type2:"<<x<<" "<<s<<"\n";
for(int i=1;i+1<=siz;i+=2)
{
int j=id[i],k=id[i+1];
s+=rk[j]-j-(num-(siz-j)-rk[j])+rk[k]-k-(num-(siz-k)-rk[k])+(rk[j]<rk[k]?1:-1);
chkmin(f[cl],g[cl]+s);// cerr<<j<<" "<<k<<" "<<s<<"\n";
}
}
else
{
(cr^((siz-1)&1)?c1:c0)--,(cl?c1:c0)++;
if(c1!=cnt1||c0!=cnt0) return ;
for(int i=1,j=1,k=1;i<=siz;i++)
if(i==1) id[i]=(cl?a1[j++]:a0[k++]);
else id[i]=(cr^(siz-i&1)?a1[j++]:a0[k++]);
LL s=calc(siz);
fep(i,1,siz) s+=num-(siz-i)-rk[i];
s+=rk[id[1]]-1-(num-(siz-id[1])-rk[id[1]]);
chkmin(f[cl^1],g[cl]+s);// cerr<<"type3:"<<x<<" "<<s<<"\n";
for(int i=2;i+1<=siz;i+=2)
{
int j=id[i],k=id[i+1];
s+=rk[j]-j-(num-(siz-j)-rk[j])+rk[k]-k-(num-(siz-k)-rk[k])+(rk[j]<rk[k]?1:-1);
chkmin(f[cl^1],g[cl]+s);
}
}
}
inline void Main()
{
n=S=read(); num=n;
fep(i,1,n) a[i]=read(),b[i]=abs(a[i]);
sort(b+1,b+1+S); S=unique(b+1,b+1+S)-b-1;
fep(i,1,n)
{
int t=lower_bound(b+1,b+1+S,abs(a[i]))-b;
v[t].emplace_back(i); a[i]=(a[i]>0?t:-t);
b1.update(i,1); col[i]=(a[i]<0)^(i&1);
}
f[0]=0,f[1]=INF;
feb(i,S,1)
{
if(b[i]==0) break;
fep(j,0,1) g[j]=f[j],f[j]=INF;
fep(j,0,1) work(i,j,j^(num&1));
for(auto p:v[i]) b1.update(p,-1);
num-=v[i].size();
}
printf("%d\n",min(f[0],f[1])==INF?-1:min(f[0],f[1]));
}
}
signed main()
{
int _T=1;
while(_T--) starrylasky::Main();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 3
Accepted
time: 4ms
memory: 26152kb
input:
10 1 1 1 1 1 -1 -1 -1 1 -1
output:
-1
result:
ok 1 number(s): "-1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 26152kb
input:
10 1 1 1 1 1 1 -1 1 1 -1
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: 0
Accepted
time: 1ms
memory: 26156kb
input:
1 -1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: -3
Wrong Answer
time: 6ms
memory: 26188kb
input:
2000 1 -1 -1 -1 -1 -1 1 -1 1 1 1 -1 -1 -1 1 1 -1 1 1 -1 -1 1 -1 -1 1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 -1 -1 -1 1 -1 -1 -1 1 -1 -1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1 1 1 -1 -1 -1 1 1 -1 1 -1 1 1 -1 -1 -1 1 1 1 -1 1 1 -1 -1 1 -1 1 1 1 1 -1 -1 -1 1 1 -1 -1 1 1 1 -1 -1 -1 -1 1 -1 -1 1 1 -1 1 -1 1 -1 -1 -1 1 -1 -1 ...
output:
15148
result:
wrong answer 1st numbers differ - expected: '15146', found: '15148'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 4
Accepted
Test #60:
score: 4
Accepted
time: 0ms
memory: 26160kb
input:
10 3 10 5 -9 7 2 -6 1 8 0
output:
-1
result:
ok 1 number(s): "-1"
Test #61:
score: 0
Accepted
time: 3ms
memory: 28204kb
input:
10 -9 0 -1 7 5 10 6 3 2 -8
output:
13
result:
ok 1 number(s): "13"
Test #62:
score: 0
Accepted
time: 1ms
memory: 26268kb
input:
2000 40667 -598150 -1084780 1201651 1570514 -1859539 -2029075 2941581 -2945945 3038404 3447919 5293872 -5335692 -5669647 5973784 6041345 6346915 -7222112 8820986 -9153143 9563103 9749206 -9894732 -11847193 11987150 12161864 13336572 13528051 -13722732 -13836176 -15497141 -15841563 15862227 16618123 ...
output:
-1
result:
ok 1 number(s): "-1"
Test #63:
score: 0
Accepted
time: 2ms
memory: 26200kb
input:
2000 3038404 -798315545 693574695 172661079 516504064 164016456 193562146 -131746730 382134316 -398886978 188767854 -834289064 -965673210 -826409444 -281381674 450991903 -592752625 81651101 -594873306 -352059270 -651772982 540062674 -769881300 68999588 307151563 -129950325 550154987 354801227 840540...
output:
658039
result:
ok 1 number(s): "658039"
Test #64:
score: 0
Accepted
time: 2ms
memory: 26204kb
input:
2000 -1095 -925 -1049 -1519 951 -1673 -776 345 -38 -1735 -276 -1730 123 -1629 -1896 -1576 -1115 1145 15 797 -948 287 1487 1195 1269 -1240 -1571 -275 -1915 -369 -1221 -1590 -1392 -100 1688 -1287 -241 1130 -1375 -965 669 -147 -307 -795 -1207 1939 120 -305 -915 -1078 -1448 1458 -603 1935 658 774 1471 7...
output:
668545
result:
ok 1 number(s): "668545"
Test #65:
score: 0
Accepted
time: 3ms
memory: 28288kb
input:
2000 1290 1487 -1947 -255 457 -1202 1313 36 -1511 898 1739 987 1809 -1986 -1015 -1127 -703 -223 179 557 199 349 1099 -259 -1401 -1244 -1116 646 -295 1713 1512 127 -1660 343 -1921 -1326 -549 831 1963 -1743 1655 -698 1792 366 1517 -51 404 -1853 -1295 1652 -130 -1562 -1850 -582 1504 1888 822 -24 1807 9...
output:
663841
result:
ok 1 number(s): "663841"
Test #66:
score: 0
Accepted
time: 5ms
memory: 26236kb
input:
2000 56 -1667 -1636 -671 -1311 348 976 1381 -710 -477 -1301 756 -510 495 -1215 -278 1134 950 59 1739 -33 -839 -862 605 761 827 -1708 -1180 -607 1624 -120 -1198 624 -1237 -1874 1788 1005 -331 1266 -467 -1213 1736 -182 594 775 1209 -832 300 1188 -994 -191 -217 1360 -1907 71 436 1294 -590 913 -747 -629...
output:
667052
result:
ok 1 number(s): "667052"
Test #67:
score: 0
Accepted
time: 0ms
memory: 28268kb
input:
1999 -758656 -113741 -374719 7680 -227905 -201318 -200890 -84484 777096 -167712 -126972 -244117 835074 161027 923025 -224756 973701 36622 -913757 -920737 -976062 461264 147694 -162457 358437 -308202 385370 808271 -523703 -303454 -522131 -664739 -505124 306509 948216 948694 -467953 -768055 769796 486...
output:
675957
result:
ok 1 number(s): "675957"
Test #68:
score: 0
Accepted
time: 0ms
memory: 24104kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #69:
score: 0
Accepted
time: 1ms
memory: 26160kb
input:
2 1000000000 999999999
output:
-1
result:
ok 1 number(s): "-1"
Test #70:
score: 0
Accepted
time: 4ms
memory: 28212kb
input:
2000 999998002 999998004 999998006 999998008 999998010 999998012 999998014 999998016 999998018 999998020 999998022 999998024 999998026 999998028 999998030 999998032 999998034 999998036 999998038 999998040 999998042 999998044 999998046 999998048 999998050 999998052 999998054 999998056 999998058 99999...
output:
999000
result:
ok 1 number(s): "999000"
Test #71:
score: 0
Accepted
time: 4ms
memory: 26184kb
input:
1999 -1000000000 -999012346 -998024692 -997037038 -996049384 -995061730 -994074076 -993086422 -992098768 -991111114 -990123460 -989135806 -988148152 -987160498 -986172844 -985185190 -984197536 -983209882 -982222228 -981234574 -980246920 -979259266 -978271612 -977283958 -976296304 -975308650 -9743209...
output:
0
result:
ok 1 number(s): "0"
Test #72:
score: 0
Accepted
time: 7ms
memory: 26200kb
input:
1999 1998 1997 1996 1995 1994 1993 1992 1991 1990 1989 1988 1987 1986 1985 1984 1983 1982 1981 1980 1979 1978 1977 1976 1975 1974 1973 1972 1971 1970 1969 1968 1967 1966 1965 1964 1963 1962 1961 1960 1959 1958 1957 1956 1955 1954 1953 1952 1951 1950 1949 1948 1947 1946 1945 1944 1943 1942 1941 1940 ...
output:
1998
result:
ok 1 number(s): "1998"
Subtask #6:
score: 0
Wrong Answer
Dependency #5:
100%
Accepted
Test #73:
score: 3
Accepted
time: 77ms
memory: 43472kb
input:
500000 -725 2759 -4173 -4473 4578 -5071 -7897 -7991 9738 -12600 17445 -18596 -20105 -21103 22718 26116 -26973 33169 -33830 34895 37480 -41216 -41665 43933 44687 45286 -46096 46958 47293 -50534 50597 -52520 -57079 57680 58680 -62109 63682 -64495 -64608 64674 -64848 -65420 67176 -74442 -76904 -77098 -...
output:
-1
result:
ok 1 number(s): "-1"
Test #74:
score: -3
Wrong Answer
time: 315ms
memory: 44672kb
input:
500000 716212992 819699933 394255912 695521313 788446410 -466519569 476323400 812543029 724100006 -681244028 -306686799 216473950 496416101 636791486 302599115 190055737 -908659874 -407112922 733684038 -282369420 36611820 323272468 -755065727 -735846631 -22777612 905154351 -694170466 -726666701 7098...
output:
-1278104993
result:
wrong answer 1st numbers differ - expected: '41671567967', found: '-1278104993'
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%