QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116661#6319. Parallel Processing (Easy)ExplodingKonjacAC ✓35ms3492kbC++175.8kb2023-06-29 18:32:532023-06-29 18:32:55

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-29 18:32:55]
  • 评测
  • 测评结果:AC
  • 用时:35ms
  • 内存:3492kb
  • [2023-06-29 18:32:53]
  • 提交

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[i]<<' '<<(a[p[i]]-1)<<' '<<p[i]<<'\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: 100
Accepted
time: 1ms
memory: 3392kb

input:

2

output:

1
2 1 2
1001 1001 1001
1001 1001 1001
1001 1001 1001

result:

ok AC

Test #2:

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

input:

4

output:

2
2 1 2
3 2 3
4 3 4
1001 1001 1001
3 1 3
4 2 4
1001 1001 1001
1001 1001 1001

result:

ok AC

Test #3:

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

input:

3

output:

2
2 1 2
3 2 3
1001 1001 1001
1001 1001 1001
3 1 3
1001 1001 1001
1001 1001 1001
1001 1001 1001

result:

ok AC

Test #4:

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

input:

5

output:

3
2 1 2
3 2 3
4 3 4
5 4 5
3 1 3
4 2 4
5 3 5
1001 1001 1001
5 1 5
1001 1001 1001
1001 1001 1001
1001 1001 1001

result:

ok AC

Test #5:

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

input:

6

output:

3
2 1 2
3 2 3
4 3 4
5 4 5
3 1 3
4 2 4
5 3 5
6 5 6
5 1 5
6 3 6
1001 1001 1001
1001 1001 1001

result:

ok AC

Test #6:

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

input:

7

output:

3
2 1 2
3 2 3
4 3 4
6 5 6
3 1 3
4 2 4
5 4 5
7 6 7
5 2 5
6 4 6
7 4 7
1001 1001 1001

result:

ok AC

Test #7:

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

input:

8

output:

3
2 1 2
4 3 4
6 5 6
8 7 8
3 2 3
4 2 4
7 6 7
8 6 8
5 4 5
6 4 6
7 4 7
8 4 8

result:

ok AC

Test #8:

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

input:

9

output:

4
2 1 2
3 2 3
4 3 4
5 4 5
3 1 3
4 2 4
6 5 6
8 7 8
5 3 5
6 3 6
7 6 7
9 8 9
7 3 7
8 6 8
9 6 9
1001 1001 1001

result:

ok AC

Test #9:

score: 0
Accepted
time: 35ms
memory: 3388kb

input:

10

output:

4
2 1 2
3 2 3
4 3 4
6 5 6
3 1 3
4 2 4
7 6 7
9 8 9
5 4 5
6 4 6
7 4 7
10 9 10
8 7 8
9 7 9
10 7 10
1001 1001 1001

result:

ok AC

Test #10:

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

input:

11

output:

4
2 1 2
4 3 4
6 5 6
8 7 8
3 2 3
4 2 4
8 6 8
10 9 10
5 4 5
6 4 6
8 4 8
11 10 11
7 6 7
9 8 9
10 8 10
11 8 11

result:

ok AC

Test #11:

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

input:

12

output:

5
2 1 2
3 2 3
4 3 4
6 5 6
3 1 3
4 2 4
5 4 5
7 6 7
7 4 7
6 4 6
9 8 9
11 10 11
5 2 5
1001 1001 1001
9 7 9
12 11 12
8 7 8
12 9 12
11 9 11
10 9 10

result:

ok AC

Test #12:

score: 0
Accepted
time: 2ms
memory: 3392kb

input:

13

output:

5
2 1 2
4 3 4
6 5 6
8 7 8
3 2 3
4 2 4
7 6 7
8 6 8
8 4 8
6 4 6
10 9 10
12 11 12
7 4 7
5 4 5
10 8 10
13 12 13
9 8 9
13 10 13
12 10 12
11 10 11

result:

ok AC

Test #13:

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

input:

14

output:

6
2 1 2
3 2 3
4 3 4
5 4 5
3 1 3
4 2 4
6 5 6
8 7 8
5 3 5
6 3 6
7 6 7
9 8 9
9 6 9
8 6 8
11 10 11
13 12 13
7 3 7
1001 1001 1001
11 9 11
14 13 14
10 9 10
14 11 14
13 11 13
12 11 12

result:

ok AC

Test #14:

score: 0
Accepted
time: 35ms
memory: 3400kb

input:

15

output:

6
2 1 2
3 2 3
4 3 4
6 5 6
3 1 3
4 2 4
7 6 7
9 8 9
5 4 5
6 4 6
7 4 7
10 9 10
10 7 10
9 7 9
12 11 12
14 13 14
8 7 8
1001 1001 1001
12 10 12
15 14 15
11 10 11
15 12 15
14 12 14
13 12 13

result:

ok AC

Test #15:

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

input:

16

output:

6
2 1 2
4 3 4
6 5 6
8 7 8
3 2 3
4 2 4
8 6 8
10 9 10
5 4 5
6 4 6
8 4 8
11 10 11
11 8 11
9 8 9
13 12 13
15 14 15
10 8 10
7 6 7
13 11 13
16 15 16
12 11 12
16 13 16
15 13 15
14 13 14

result:

ok AC