Transmission Door
a.[codeforces 631C] report
Test instructions
Each manager sorts the number of 1th to r[i] of array A, t[i]==1 ascending, and 2 descending. A sequence that is sorted by M manager.
Analysis
If the previous r is small, it will certainly be overwritten by a larger R operation, so we will find the descending sequence of R.
First find the largest R, then find the largest r in the back of this r, loop this way, find a descending sequence of R, and then simulate the sort.
But worst case scenario: "R is just from the maximum to the smallest, so each time you have to order the sequence" the complexity of Mlogm+m*nlogn, will time out.
Change to fill the complexity to become mlogm+n, fill the number is first give a of the largest r before the number of sequence, the largest r to the second between, if it is ascending, then put a big of that end filled in, if it is descending, the small end fill in. And so on before the interval.
But I went wrong when I looked for the descending sequence of R: As long as the ID is greater than the manager's ID (which appears later), and the sorting method is different, let the manager row.
int j=1;
solve(j);//第j个(按r排)manager来排a
for(int i=2; i<=m; i++)
{
if(b[i].id>b[j].id&&b[i].t!=b[j].t)
{
solve(i);
j=i;
}
}
WA was in Test 14, but because it was too long to see the complete data, he made the data and finally found out where it was wrong, a counter-example:
93
123456789
29
14
26
N=9,m=3
R:9 4 6
T:2 1 2
My program will first row 9, ascending, and then because 6 ascending, so do not row, so J did not update to 6, and then row 4, descending, this is wrong, because 4 actually appeared before 6, so should be ignored.
It would be nice to have this in the loop:
if(b[i].id>b[k].id)
{
if(b[i].t!=b[j].t)
{
r[cnt++]=i;
j=i;
}
k=i;
}
K is the ID of the last sort, if the ID is now large, is the back of the manager, the current row and the previous record in the R sequence is not the same, recorded in.
Code
#include<cstdio>
#include<algorithm>
#define N 200005
using namespace STD;
intN,m,a[n],c[n],r[n];
structMng
{
intR,t,id;
} B[n];
int cmp(mng a,mng b)
{
returna.r>b.r;
}
int Main ()
{
scanf("%d%d", &n,&m);
for(intI=1; i<=n; i++)
scanf("%d", &a[i]);
for(intI=1; i<=m; i++)
{
scanf("%d%d", &B[I].T,&B[I].R);
B[i].id=i;
}
Sort (b +1, B +1+M,CMP);//manager Press R to sort descending
intj=0, k=0, cnt=0;
//filter out R-Descending manager sequence
for(intI=1; i<=m; i++)
{
if(b[i].id>b[k].id)
{
if(b[i].t!=b[j].t)
{
R[cnt++]=i;
J=i;
}
K=i;
}
}
for(inti=b[1].r+1; i<=n; i++) C[i]=a[i];
Sort (A +1, A +1+b[1].R);//a The ascending order of the array before B[1].R
inttou=1, wei=b[1].R;
for(intI=0; i<cnt; i++)
if(b[r[i]].t==2)//Current R[i] Manager to descending
for(intJ=B[R[I]].R; j>b[r[i+1]].R; j--)//Small right-to-left to the past
C[j]=a[tou++];
Else
for(intJ=B[R[I]].R; j>b[r[i+1]].R; j--)
c[j]=a[wei--];//Large right-to-left
for(intI=1; i<=n; i++)
printf("%d", C[i]);
return 0;
}
B. Didn't look
"We have a girl in the varsity squad." Training Tournament 6