#include<iostream>
#include<vector>
using namespace std;
const int MAX_N=55;
const int MAX=202;
bool stat[MAX+5][MAX+5];
int a[MAX_N];
int n,k;
vector<pair<int,int>>ans;
void rem(int target_row)
{
for(int row=target_row;row<=MAX;row++)
{
for(int col=1;col<=n;col++)
{
stat[row][col]=stat[row+1][col];
}
}
}
bool full(int row)
{
for(int col=1;col<=n;col++)
{
if(stat[row][col]==0)return 0;
}
return 1;
}
void fix()
{
for(int row=1;row<=MAX;row++)
{
while(full(row))rem(row);
}
for(int col=1;col<=n;col++)
{
a[col]=0;
while(stat[a[col]+1][col])a[col]++;
}
}
void puthorizontal(int x)
{
int y=0;
for(int col=x;col<=x+k-1;col++)
{
y=max(y,a[col]);
}
for(int col=x;col<=x+k-1;col++)
{
stat[y+1][col]=1;
}
fix();
ans.push_back({2,x});
}
void putvertical(int x)
{
int y=a[x]+1;
for(int row=y;row<=y+k-1;row++)
{
stat[row][x]=1;
}
fix();
ans.push_back({1,x});
}
int firstbad()
{
for(int i=2;i<=n;i++)
{
if(a[i]<a[i-1])return i;
}
return n+1;
}
int sum()
{
int res=0;
for(int i=1;i<=n;i++)res+=a[i];
return res;
}
int zeroes()
{
///assuming that we enter with at least one positive
for(int i=2;i<=n;i++)
{
if(a[i]!=0)return i-1;
}
}
int lessthaneq(int val)
{
for(int i=1;i<=n;i++)
{
if(a[i]>val)return i-1;
}
return n;
}
void check()
{
for(int i=1;i<=n;i++)
{
cout<<a[i]<<" ";
}cout<<"\n";
}
void solvek2even()
{
if(sum()%2==1)
{
cout<<-1<<"\n";
return;
}
while(firstbad()<=n)
{
int i=firstbad();
while(a[i]<a[i-1])
{
putvertical(i);
//check();
}
}
while(sum()!=0)
{
int idxzeroes=zeroes();
int nextval=a[idxzeroes+1];
nextval=(nextval/2);
while(nextval>0)
{
for(int i=1;i<=idxzeroes;i++)
{
putvertical(i);
}
nextval--;
}
if(a[idxzeroes+1]%2==0)continue;
for(int i=idxzeroes-1;i>=1;i-=2)
{
puthorizontal(i);
}
if(idxzeroes%2==0)continue;
int idxsecondbest=lessthaneq(1);
for(int i=idxsecondbest-1;i>=1;i-=2)
{
puthorizontal(i);
}
if(idxsecondbest%2==1)
{
putvertical(1);
}
}
}
void print()
{
cout<<ans.size()<<"\n";
for(auto [type,where]:ans)
{
cout<<type<<" "<<where<<"\n";
}
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
for(int row=1;row<=a[i];row++)stat[row][i]=1;
}
if(k==2)
{
if(n%2==0)solvek2even();
else;
}
else
{
;
}
print();
return 0;
}
Compilation message (stderr)
joiris.cpp: In function 'int zeroes()':
joiris.cpp:85:1: warning: control reaches end of non-void function [-Wreturn-type]
85 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |