Submission #3051392


Source Code Expand

#include <cstdio>
#include <cstring>
#include <algorithm>
#define lf (x<<1)
#define rg (lf|1)
#define mem(a) memset(a,0,sizeof(a))
#define rep(i,j,k) for (i=j;i<=k;i++)
using namespace std;
const int N=5e5+5,INF=1e9;
int n,k,wh,mode,i,a[N],p[N],deg[N],out[N];
int Min[N*4],cs[N*4],dec[N*4];
int sum[N];
template <typename T> void read(T &ret)
{
	char ch; ret=0;
	for (ch=getchar();ch<'0' || ch>'9';ch=getchar());
	for (;ch>='0' && ch<='9';ch=getchar()) ret=ret*10+ch-'0';
}
int lowbit(int x) {return x&(-x);}
void add(int x) {
	while (x<=n) { sum[x]++; x+=lowbit(x); }
}
int query(int x) {
	if (x<1) return 0;
	if (x>n) x=n;
	int ret=0; while (x>0) {ret+=sum[x]; x-=lowbit(x);}
	return ret;
}
void merge(int x)
{
	Min[x]=min(Min[lf],Min[rg]);
	cs[x]=min(cs[lf],cs[rg]);
}
void build(int x,int l,int r)
{
	if (l==r)
	{
		if (!deg[l]) { Min[x]=INF; cs[x]=l; }
		else Min[x]=deg[l],cs[x]=INF;
		return ;
	}
	int mid=(l+r)>>1;
	build(lf,l,mid);
	build(rg,mid+1,r);
	merge(x);
}
void Init()
{
	freopen("permutation.in","r",stdin);
	freopen("permutation.out","w",stdout);
	read(n); read(k);
	rep(i,1,n) read(a[i]),p[a[i]]=i;
	rep(i,1,n) {
		deg[p[i]]=query(p[i]+k-1)-query(p[i]-k);
		add(p[i]);
	}
	build(1,1,n);
}
void pushdown(int x)
{
	Min[lf]-=dec[x]; dec[lf]+=dec[x];
	Min[rg]-=dec[x]; dec[rg]+=dec[x];
	dec[x]=0;
}
void recalc(int x,int l,int r)
{
	if (l==r)
	{
		if (Min[x]<1) {
			Min[x]=INF; cs[x]=l;
		}
		return;
	}
	int mid=(l+r)>>1;
	pushdown(x);
	if (Min[lf]<1) recalc(lf,l,mid);
	if (Min[rg]<1) recalc(rg,mid+1,r);
	merge(x);
}
void modify(int x,int L,int R,int l,int r)
{
	if (l>R || r<L) {
		if (Min[x]<1) recalc(x,l,r);
		return;
	}
	if (L<=l && r<=R)
	{
		if (mode==1) cs[x]=INF;
		else {
			Min[x]--; dec[x]++;
		}
		if (Min[x]<1) recalc(x,l,r);
		return ;
	}
	int mid=(l+r)>>1;
	pushdown(x);
	modify(lf,L,R,l,mid);
	modify(rg,L,R,mid+1,r);
	merge(x);
}
void Solve()
{
	int L,R;
	rep(i,1,n)
	{
		wh=cs[1];
		out[wh]=i;
		mode=1;
		modify(1,wh,wh,1,n);
		mode=2;
		L=max(1,wh-k+1); R=min(n,wh+k-1);
		modify(1,L,R,1,n);
	}
}
void Outit()
{
	rep(i,1,n) printf("%d\n",out[i]);	
}
int main()
{
	Init();
	Solve();
	Outit();
	return 0;
}

Submission Info

Submission Time
Task F - Wide Swap
User ASURA
Language C++ (GCC 5.4.1)
Score 0
Code Size 2269 Byte
Status TLE
Exec Time 5259 ms
Memory 128 KB

Compile Error

./Main.cpp: In function ‘void Init()’:
./Main.cpp:49:37: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
  freopen("permutation.in","r",stdin);
                                     ^
./Main.cpp:50:39: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
  freopen("permutation.out","w",stdout);
                                       ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 2000
Status
TLE × 3
TLE × 94
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, 060.txt, 061.txt, 062.txt, 063.txt, 064.txt, 065.txt, 066.txt, 067.txt, 068.txt, 069.txt, 070.txt, 071.txt, 072.txt, 073.txt, 074.txt, 075.txt, 076.txt, 077.txt, 078.txt, 079.txt, 080.txt, 081.txt, 082.txt, 083.txt, 084.txt, 085.txt, 086.txt, 087.txt, 088.txt, 089.txt, 090.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
000.txt TLE 5255 ms 128 KB
001.txt TLE 5255 ms 128 KB
002.txt TLE 5255 ms 128 KB
003.txt TLE 5255 ms 128 KB
004.txt TLE 5255 ms 128 KB
005.txt TLE 5255 ms 128 KB
006.txt TLE 5255 ms 128 KB
007.txt TLE 5255 ms 128 KB
008.txt TLE 5255 ms 128 KB
009.txt TLE 5255 ms 128 KB
010.txt TLE 5255 ms 128 KB
011.txt TLE 5255 ms 128 KB
012.txt TLE 5255 ms 128 KB
013.txt TLE 5255 ms 128 KB
014.txt TLE 5255 ms 128 KB
015.txt TLE 5255 ms 128 KB
016.txt TLE 5255 ms 128 KB
017.txt TLE 5255 ms 128 KB
018.txt TLE 5255 ms 128 KB
019.txt TLE 5255 ms 128 KB
020.txt TLE 5255 ms 128 KB
021.txt TLE 5255 ms 128 KB
022.txt TLE 5255 ms 128 KB
023.txt TLE 5255 ms 128 KB
024.txt TLE 5255 ms 128 KB
025.txt TLE 5255 ms 128 KB
026.txt TLE 5255 ms 128 KB
027.txt TLE 5255 ms 128 KB
028.txt TLE 5255 ms 128 KB
029.txt TLE 5255 ms 128 KB
030.txt TLE 5255 ms 128 KB
031.txt TLE 5255 ms 128 KB
032.txt TLE 5255 ms 128 KB
033.txt TLE 5255 ms 128 KB
034.txt TLE 5255 ms 128 KB
035.txt TLE 5255 ms 128 KB
036.txt TLE 5255 ms 128 KB
037.txt TLE 5255 ms 128 KB
038.txt TLE 5255 ms 128 KB
039.txt TLE 5255 ms 128 KB
040.txt TLE 5255 ms 128 KB
041.txt TLE 5255 ms 128 KB
042.txt TLE 5255 ms 128 KB
043.txt TLE 5255 ms 128 KB
044.txt TLE 5259 ms 128 KB
045.txt TLE 5255 ms 128 KB
046.txt TLE 5255 ms 128 KB
047.txt TLE 5255 ms 128 KB
048.txt TLE 5255 ms 128 KB
049.txt TLE 5255 ms 128 KB
050.txt TLE 5255 ms 128 KB
051.txt TLE 5255 ms 128 KB
052.txt TLE 5255 ms 128 KB
053.txt TLE 5255 ms 128 KB
054.txt TLE 5255 ms 128 KB
055.txt TLE 5255 ms 128 KB
056.txt TLE 5255 ms 128 KB
057.txt TLE 5255 ms 128 KB
058.txt TLE 5255 ms 128 KB
059.txt TLE 5255 ms 128 KB
060.txt TLE 5255 ms 128 KB
061.txt TLE 5255 ms 128 KB
062.txt TLE 5255 ms 128 KB
063.txt TLE 5255 ms 128 KB
064.txt TLE 5255 ms 128 KB
065.txt TLE 5255 ms 128 KB
066.txt TLE 5255 ms 128 KB
067.txt TLE 5255 ms 128 KB
068.txt TLE 5255 ms 128 KB
069.txt TLE 5255 ms 128 KB
070.txt TLE 5255 ms 128 KB
071.txt TLE 5255 ms 128 KB
072.txt TLE 5255 ms 128 KB
073.txt TLE 5255 ms 128 KB
074.txt TLE 5255 ms 128 KB
075.txt TLE 5255 ms 128 KB
076.txt TLE 5255 ms 128 KB
077.txt TLE 5255 ms 128 KB
078.txt TLE 5255 ms 128 KB
079.txt TLE 5255 ms 128 KB
080.txt TLE 5255 ms 128 KB
081.txt TLE 5255 ms 128 KB
082.txt TLE 5255 ms 128 KB
083.txt TLE 5255 ms 128 KB
084.txt TLE 5255 ms 128 KB
085.txt TLE 5255 ms 128 KB
086.txt TLE 5255 ms 128 KB
087.txt TLE 5255 ms 128 KB
088.txt TLE 5255 ms 128 KB
089.txt TLE 5255 ms 128 KB
090.txt TLE 5255 ms 128 KB
sample-01.txt TLE 5255 ms 128 KB
sample-02.txt TLE 5255 ms 128 KB
sample-03.txt TLE 5255 ms 128 KB