QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#102374#2211. IOI Problem RevisedzhouhuanyiAC ✓2258ms105664kbC++115.0kb2023-05-03 11:49:162023-05-04 22:45:48

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-04 22:45:48]
  • 评测
  • 测评结果:AC
  • 用时:2258ms
  • 内存:105664kb
  • [2023-05-03 11:49:16]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>
#define N 400000
#define inf 4e17
#define INF 2e23
#define Inf 1e9
using namespace std;
long long read()
{
    char c=0;
    long long sum=0;
    while (c<'0'||c>'9') c=getchar();
    while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
    return sum;
}
struct reads
{
    int l,r,c;
};
reads dque[N+1];
int n,k,top,tong[N+1],length,st[N+1],leng,lg[N+1],L[N+1],R[N+1];
long long sL,a[N+1],s[N+1];
__int128 ans=INF,dp[N+1],DP[N+1];
map<int,__int128>P[N+1];
vector<int>sts;
__int128 F(int l,int r)
{
    if (l>r) return INF;
    return (s[r]-s[(l+r)>>1])-(s[(l+r-1)>>1]-s[l-1]);
}
__int128 solve(long long d)
{
    int x;
    for (int i=1;i<=n;++i) dp[i]=INF;
    dque[top=1]=(reads){0,n,0};
    for (int i=1;i<=n;++i)
    {
	x=0;
	for (int j=lg[top];j>=0;--j)
	    if (x+(1<<j)<=top&&dque[x+(1<<j)].l<=i)
		x+=(1<<j);
	dp[i]=dp[dque[x].c]+F(dque[x].c+1,i)+d,R[i]=R[dque[x].c]+1;
	while (top&&dp[i]+F(i+1,dque[top].l)<=dp[dque[top].c]+F(dque[top].c+1,dque[top].l)) top--;
	if (top&&dp[i]+F(i+1,dque[top].r)<=dp[dque[top].c]+F(dque[top].c+1,dque[top].r))
	{
	    x=dque[top].r;
	    for (int j=lg[n];j>=0;--j)
		if (x-(1<<j)>=dque[top].l&&dp[i]+F(i+1,x-(1<<j))<=dp[dque[top].c]+F(dque[top].c+1,x-(1<<j)))
		    x-=(1<<j);
	    dque[top].r=x-1;
	}
	if (dque[top].r+1<=n) dque[top+1]=(reads){dque[top].r+1,n,i},++top;
    }
    return R[n];
}
void solve2(long long d)
{
    int x,cnt=0;
    for (int i=1;i<=n;++i) dp[i]=INF;
    dque[top=1]=(reads){0,n,0};
    for (int i=1;i<=n;++i)
    {
	x=0;
	for (int j=lg[top];j>=0;--j)
	    if (x+(1<<j)<=top&&dque[x+(1<<j)].l<=i)
		x+=(1<<j);
	dp[i]=dp[dque[x].c]+F(dque[x].c+1,i)+d,L[i]=L[dque[x].c]+1;
	while (top&&dp[i]+F(i+1,dque[top].l)<dp[dque[top].c]+F(dque[top].c+1,dque[top].l)) top--;
	if (top&&dp[i]+F(i+1,dque[top].r)<dp[dque[top].c]+F(dque[top].c+1,dque[top].r))
	{
	    x=dque[top].r;
	    for (int j=lg[n];j>=0;--j)
		if (x-(1<<j)>=dque[top].l&&dp[i]+F(i+1,x-(1<<j))<dp[dque[top].c]+F(dque[top].c+1,x-(1<<j)))
		    x-=(1<<j);
	    dque[top].r=x-1;
	}
	if (dque[top].r+1<=n) dque[top+1]=(reads){dque[top].r+1,n,i},++top;
    }
    x=n,tong[++length]=n;
    for (int i=n;i>=1;--i)
	if (dp[i-1]+F(i,x)+d==dp[x]&&L[i-1]<=k-cnt-1)
	    x=i-1,tong[++length]=i-1,cnt++;
    reverse(tong+1,tong+length+1);
    return;
}
void solve3(int l,int r,int l2,int r2)
{
    int mid=(l+r)>>1,ps=0;
    __int128 minn=inf;
    for (int i=l2;i<=min(mid-1,r2);++i)
	if (dp[i]+F(i+1,mid)<minn)
	    minn=dp[i]+F(i+1,mid),ps=i;
    if (l==r)
    {
	DP[l]=minn;
	return;
    }
    solve3(l,mid,l2,ps),solve3(mid+1,r,ps,r2);
    return;
}
vector<int>solve4(int ps,vector<int>sl,vector<int>sr)
{
    int x;
    vector<int>p;
    for (int i=1;i<=k;++i) P[i].clear();
    for (int i=sl[0];i<=sr[0];++i) dp[i]=inf;
    dp[ps]=0,dque[top=1]=(reads){ps,ps+n,ps};
    for (int i=sl[0];i<=sr[0];++i) P[0][i]=dp[i];
    for (int i=1;i<=k;++i)
    {
	for (int j=sl[i];j<=sr[i];++j) DP[j]=inf;
	solve3(sl[i],sr[i],sl[i-1],sr[i-1]);
	for (int j=sl[i];j<=sr[i];++j) P[i][j]=dp[j]=DP[j];
    }
    ps+=n,p.push_back(ps),x=ps;
    for (int i=k-1;i>=0;--i)
    {
	for (int j=sr[i];j>=sl[i];--j)
	    if (P[i][j]+F(j+1,x)==P[i+1][x])
	    {
		x=j;
		break;
	    }
	p.push_back(x);
    }
    reverse(p.begin(),p.end());
    return p;
}
void solve5(int l,int r,vector<int>sl,vector<int>sr)
{
    if (l==r)
    {
	vector<int>p=solve4(l,sl,sr);
	if (dp[l+n]<ans) ans=dp[l+n],sts=p;
	return;
    }
    int mid=(l+r)>>1;
    vector<int>p=solve4(mid,sl,sr);
    solve5(l,mid,sl,p),solve5(mid+1,r,p,sr);
    return;
}
int main()
{
    //freopen("cir.in","r",stdin);
    //freopen("cir.out","w",stdout);
    vector<int>sl;
    vector<int>sr;
    vector<long long>tans;
    int rt=0,l=0,r=0,rst=Inf;
    long long x=0;
    __int128 minn=INF;
    for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
    n=read(),k=read(),sL=read();
    for (int i=1;i<=n;++i) a[i]=read(),a[i+n]=a[i]+sL;
    for (int i=1;i<=(n<<1);++i) s[i]=s[i-1]+a[i];
    if (k==1)
    {
	for (int i=1;i<=n;++i)
	    if (F(i,i+n-1)<minn)
		minn=F(i,i+n-1),rt=i;
	printf("%lld\n%lld\n",(long long)(minn),a[(rt+rt+n-1)>>1]%sL);
	return 0;
    }
    for (int i=log(inf)/log(2);i>=0;--i)
	if (x+(1ll<<i)<=inf&&solve(x+(1ll<<i))>=k)
	    x+=(1ll<<i);
    solve2(x);
    for (int i=2;i<=length;++i)
	if (tong[i]-tong[i-1]<rst)
	    rt=i-1,l=tong[i-1],r=tong[i],rst=tong[i]-tong[i-1];
    for (int i=rt;i<=length;++i) st[++leng]=tong[i];
    for (int i=2;i<=rt;++i) st[++leng]=tong[i]+n;
    for (int i=1;i<=leng;++i)
    {
	sl.push_back(st[i]);
	if (i!=leng) sr.push_back(st[i+1]);
	else sr.push_back(n<<1);
    }
    solve5(l,r,sl,sr),printf("%lld\n",(long long)(ans));
    for (int i=0;i+1<sts.size();++i) tans.push_back(a[(sts[i]+sts[i+1]+1)>>1]%sL);
    sort(tans.begin(),tans.end());
    for (int i=0;i<tans.size();++i) printf("%lld ",tans[i]);
    puts("");
    return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 8ms
memory: 23936kb

Test #2:

score: 0
Accepted
time: 3ms
memory: 23884kb

Test #3:

score: 0
Accepted
time: 0ms
memory: 23968kb

Test #4:

score: 0
Accepted
time: 7ms
memory: 23984kb

Test #5:

score: 0
Accepted
time: 1ms
memory: 23888kb

Test #6:

score: 0
Accepted
time: 3ms
memory: 23928kb

Test #7:

score: 0
Accepted
time: 6ms
memory: 24312kb

Test #8:

score: 0
Accepted
time: 19ms
memory: 24260kb

Test #9:

score: 0
Accepted
time: 22ms
memory: 24920kb

Test #10:

score: 0
Accepted
time: 25ms
memory: 24996kb

Test #11:

score: 0
Accepted
time: 177ms
memory: 32636kb

Test #12:

score: 0
Accepted
time: 205ms
memory: 34984kb

Test #13:

score: 0
Accepted
time: 177ms
memory: 31272kb

Test #14:

score: 0
Accepted
time: 197ms
memory: 34572kb

Test #15:

score: 0
Accepted
time: 214ms
memory: 32968kb

Test #16:

score: 0
Accepted
time: 1618ms
memory: 98156kb

Test #17:

score: 0
Accepted
time: 1553ms
memory: 91416kb

Test #18:

score: 0
Accepted
time: 1597ms
memory: 92464kb

Test #19:

score: 0
Accepted
time: 1455ms
memory: 78152kb

Test #20:

score: 0
Accepted
time: 1637ms
memory: 99504kb

Test #21:

score: 0
Accepted
time: 1671ms
memory: 100544kb

Test #22:

score: 0
Accepted
time: 1649ms
memory: 101212kb

Test #23:

score: 0
Accepted
time: 1474ms
memory: 85820kb

Test #24:

score: 0
Accepted
time: 1498ms
memory: 90760kb

Test #25:

score: 0
Accepted
time: 1564ms
memory: 94672kb

Test #26:

score: 0
Accepted
time: 1451ms
memory: 85760kb

Test #27:

score: 0
Accepted
time: 1465ms
memory: 86860kb

Test #28:

score: 0
Accepted
time: 1563ms
memory: 94556kb

Test #29:

score: 0
Accepted
time: 1420ms
memory: 81756kb

Test #30:

score: 0
Accepted
time: 1709ms
memory: 105664kb

Test #31:

score: 0
Accepted
time: 1694ms
memory: 96820kb

Test #32:

score: 0
Accepted
time: 1620ms
memory: 98260kb

Test #33:

score: 0
Accepted
time: 1639ms
memory: 100000kb

Test #34:

score: 0
Accepted
time: 1542ms
memory: 96628kb

Test #35:

score: 0
Accepted
time: 1618ms
memory: 98416kb

Test #36:

score: 0
Accepted
time: 1733ms
memory: 103516kb

Test #37:

score: 0
Accepted
time: 1613ms
memory: 96144kb

Test #38:

score: 0
Accepted
time: 1668ms
memory: 101056kb

Test #39:

score: 0
Accepted
time: 1427ms
memory: 65500kb

Test #40:

score: 0
Accepted
time: 1615ms
memory: 104348kb

Test #41:

score: 0
Accepted
time: 1453ms
memory: 77140kb

Test #42:

score: 0
Accepted
time: 1691ms
memory: 99552kb

Test #43:

score: 0
Accepted
time: 1411ms
memory: 81068kb

Test #44:

score: 0
Accepted
time: 1631ms
memory: 101064kb

Test #45:

score: 0
Accepted
time: 1642ms
memory: 103660kb

Test #46:

score: 0
Accepted
time: 1631ms
memory: 100660kb

Test #47:

score: 0
Accepted
time: 1551ms
memory: 88116kb

Test #48:

score: 0
Accepted
time: 1455ms
memory: 82272kb

Test #49:

score: 0
Accepted
time: 1385ms
memory: 73008kb

Test #50:

score: 0
Accepted
time: 1589ms
memory: 95076kb

Test #51:

score: 0
Accepted
time: 1594ms
memory: 96068kb

Test #52:

score: 0
Accepted
time: 1575ms
memory: 97280kb

Test #53:

score: 0
Accepted
time: 1493ms
memory: 84908kb

Test #54:

score: 0
Accepted
time: 1510ms
memory: 84128kb

Test #55:

score: 0
Accepted
time: 1642ms
memory: 99832kb

Test #56:

score: 0
Accepted
time: 1358ms
memory: 71736kb

Test #57:

score: 0
Accepted
time: 1412ms
memory: 83332kb

Test #58:

score: 0
Accepted
time: 1439ms
memory: 83452kb

Test #59:

score: 0
Accepted
time: 1531ms
memory: 93708kb

Test #60:

score: 0
Accepted
time: 1699ms
memory: 104000kb

Test #61:

score: 0
Accepted
time: 1461ms
memory: 83768kb

Test #62:

score: 0
Accepted
time: 1559ms
memory: 93548kb

Test #63:

score: 0
Accepted
time: 1544ms
memory: 77216kb

Test #64:

score: 0
Accepted
time: 1689ms
memory: 104036kb

Test #65:

score: 0
Accepted
time: 678ms
memory: 63432kb

Test #66:

score: 0
Accepted
time: 728ms
memory: 78052kb

Test #67:

score: 0
Accepted
time: 696ms
memory: 78168kb

Test #68:

score: 0
Accepted
time: 763ms
memory: 77900kb

Test #69:

score: 0
Accepted
time: 2258ms
memory: 75916kb

Test #70:

score: 0
Accepted
time: 1779ms
memory: 62868kb

Test #71:

score: 0
Accepted
time: 1692ms
memory: 57412kb

Test #72:

score: 0
Accepted
time: 2020ms
memory: 75892kb

Extra Test:

score: 0
Extra Test Passed