QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#116658 | #6320. Parallel Processing (Hard) | ExplodingKonjac | WA | 0ms | 3396kb | C++17 | 5.8kb | 2023-06-29 18:31:08 | 2023-06-29 18:31:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define OPENIOBUF
namespace FastIO
{
class FastIOBase
{
protected:
#ifdef OPENIOBUF
static const int BUFSIZE=1<<16;
char buf[BUFSIZE+1];
int buf_p=0;
#endif
FILE *target;
FastIOBase(FILE *f): target(f){}
~FastIOBase()=default;
public:
#ifdef OPENIOBUF
virtual void flush()=0;
#endif
};
class FastOutput final: public FastIOBase
{
#ifdef OPENIOBUF
public:
void flush()
{ fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
private:
void __putc(char x)
{
#ifdef OPENIOBUF
if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
putc(x,target);
#endif
}
template<typename T>
void __write(T x)
{
char stk[64],*top=stk;
if(x<0) return __putc('-'),__write(-x);
do *(top++)=x%10,x/=10; while(x);
for(;top!=stk;__putc(*(--top)+'0'));
}
public:
FastOutput(FILE *f=stdout): FastIOBase(f){}
#ifdef OPENIOBUF
~FastOutput(){ flush(); }
#endif
FastOutput &operator <<(char x)
{ return __putc(x),*this; }
FastOutput &operator <<(const char *s)
{ for(;*s;__putc(*(s++)));return *this; }
FastOutput &operator <<(const string &s)
{ return (*this)<<s.c_str(); }
template<typename T>
enable_if_t<is_integral<T>::value,FastOutput&> operator <<(const T &x)
{ return __write(x),*this; }
template<typename ...T>
void writesp(const T &...x)
{ initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
template<typename ...T>
void writeln(const T &...x)
{ initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...}; }
template<typename Iter>
void writesp(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<' '; }
template<typename Iter>
void writeln(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;
class FastInput final: public FastIOBase
{
#ifdef OPENIOBUF
public:
void flush()
{ buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
#endif
private:
bool __eof;
char __getc()
{
if(__eof) return EOF;
#ifdef OPENIOBUF
if(buf_p==BUFSIZE) flush();
char ch=buf[buf_p++];
#else
char ch=getc(target);
#endif
return ~ch?ch:(__eof=true,EOF);
}
void __ungetc(char c)
{
__eof=false;
#ifdef OPENIOBUF
buf_p--;
#else
ungetc(c,target);
#endif
}
public:
FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
{ buf_p=BUFSIZE; }
bool eof()const { return buf[buf_p]==EOF; }
#else
{}
bool eof()const { return feof(target); }
#endif
char peek() { return __getc(); }
explicit operator bool()const { return !__eof; }
FastInput &operator >>(char &x)
{ while(isspace(x=__getc()));return *this; }
template<typename T>
enable_if_t<is_integral<T>::value,FastInput&> operator >>(T &x)
{
char ch,sym=0;x=0;
while(isspace(ch=__getc()));
if(__eof) return *this;
if(ch=='-') sym=1,ch=__getc();
for(x=0;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=__getc());
return __ungetc(ch),sym?x=-x:x,*this;
}
FastInput &operator >>(char *s)
{
while(isspace(*s=__getc()));
if(__eof) return *this;
for(;!isspace(*s) && !__eof;*(++s)=__getc());
return __ungetc(*s),*s='\0',*this;
}
FastInput &operator >>(string &s)
{
char str_buf[(1<<8)+1]={0},*p=str_buf;
char *const buf_end=str_buf+(1<<8);
while(isspace(*p=__getc()));
if(__eof) return *this;
for(s.clear(),p++;;p=str_buf)
{
for(;p!=buf_end && !isspace(*p=__getc()) && !__eof;p++);
if(p!=buf_end) break;
s.append(str_buf);
}
__ungetc(*p),*p='\0',s.append(str_buf);
return *this;
}
template<typename ...T>
void read(T &...x)
{ initializer_list<int>{(this->operator>>(x),0)...}; }
template<typename Iter>
void read(Iter begin,Iter end)
{ while(begin!=end) (*this)>>*(begin++); }
}qin;
} // namespace FastIO
using FastIO::qin,FastIO::qout;
using LL=long long;
using LD=long double;
using UI=unsigned int;
using ULL=unsigned long long;
#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#else
#define FILEIO(file)
#endif
int n,a[1005];
vector<array<int,4>> ans;
void upd(int p1,int p2,int p3,int p4)
{
auto t=tie(a[p1],a[p2],a[p3],a[p4]);
t=make_tuple(a[a[p1]-1],a[a[p2]-1],a[a[p3]-1],a[a[p4]-1]);
a[1001]=1002;
}
void pushAns(int p1,int p2,int p3,int p4)
{
ans.push_back({p1,p2,p3,p4});
upd(p1,p2,p3,p4);
}
bool dfs(int N,int dep,int lim)
{
if(dep>lim) return count(a+1,a+N+1,1)==N;
vector<int> ord;
for(int i=1;i<=N;i++) if(a[i]!=1) ord.push_back(i);
int sz=ord.size(),_p[5]={-1};
ord.push_back(0);
auto deal=[&](int p1,int p2,int p3,int p4)
{
auto old=make_tuple(a[p1],a[p2],a[p3],a[p4]);
pushAns(p1,p2,p3,p4);
if(dfs(N,dep+1,lim)) return true;
ans.pop_back();
tie(a[p1],a[p2],a[p3],a[p4])=old;
return false;
};
#define FP(x) for(int p##x=(_p[x]=_p[x-1]+1,0);p##x=ord[_p[x]],_p[x]<sz;_p[x]++)
FP(1) FP(2) FP(3) FP(4)
if(deal(p1,p2,p3,p4)) return true;
FP(1) FP(2) FP(3)
if(deal(p1,p2,p3,1001)) return true;
FP(1) FP(2)
if(deal(p1,p2,1001,1001)) return true;
FP(1)
if(deal(p1,1001,1001,1001)) return true;
#undef FP
return false;
}
void solve(int N)
{
constexpr array<int,4> ans11[]={
{2,4,6,8},
{3,4,8,10},
{5,6,8,11},
{7,9,10,11},
};
if(N==11) ans=vector(ans11,ans11+4);
else if(N<11)
{
iota(a+1,a+N+1,1),a[1001]=1002;
ans.clear();
if(!dfs(N,1,max((2*N+2)/5,__lg(N-1)+1)))
assert(0);
}
else
{
solve(N-5);
auto lst=ans.back();
ans.pop_back();
swap(*find(lst.begin(),lst.end(),N-5),lst[0]);
pushAns(lst[0],lst[1],N-3,N-1);
pushAns(lst[2],lst[3],N-3,N);
pushAns(N-4,N,N-1,N-2);
}
}
int main()
{
qin>>n;
solve(n);
iota(a+1,a+n+1,1),a[1001]=1002;
qout<<ans.size()<<'\n';
for(auto &p: ans)
{
for(int i=0;i<4;i++)
qout<<p[0]<<' '<<a[p[0]-1]<<' '<<p[0]<<'\n';
upd(p[0],p[1],p[2],p[3]);
}
assert(count(a+1,a+n+1,1)==n);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3396kb
input:
17
output:
7 2 1 2 2 1 2 2 1 2 2 1 2 3 1 3 3 1 3 3 1 3 3 1 3 7 5 7 7 5 7 7 5 7 7 5 7 5 1 5 5 1 5 5 1 5 5 1 5 12 10 12 12 10 12 12 10 12 12 10 12 11 10 11 11 10 11 11 10 11 11 10 11 13 1 13 13 1 13 13 1 13 13 1 13
result:
wrong answer A[3] is not (1, …, 3)