QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#373680#2833. HamiltonPetroTarnavskyiRE 1ms3764kbC++201.5kb2024-04-01 22:14:032024-04-01 22:14:03

Judging History

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

  • [2024-04-01 22:14:03]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3764kb
  • [2024-04-01 22:14:03]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); i--)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

int n;
vector<string> g;

bool ok(const VI& a)
{
	int mx = 0;
	FOR(i, 0, SZ(a))
	{
		int cur = a[i];
		int nx = a[(i + 1) % SZ(a)];
		
		int val = g[cur][nx] - '0';
		//cerr << val << "\n";
		if(val < mx)
			return 0;
		mx = max(mx, val);
	}
	return 1;
}

void solve()
{
	g.resize(n);
	FOR(i, 0, n)
		cin >> g[i];
	
	
	VI ans = {0, 1, 2};
	if(!ok(ans))
		ans = {1, 2, 0};
//	cerr << "\n";
	if(!ok(ans))
		ans = {2, 0, 1};
//	cerr << "\n";
	assert(ok(ans));
//	cerr << "\n";

	FOR(i, 3, n)
	{
		VI cands = {0, i};
		FOR(j, 0, i)
		{
			int pr = ans[(j + i - 1) % i];
			int cur = ans[j];
			int nx = ans[(j + 1) % i];

			if(g[cur][pr] == g[cur][nx])
				continue;
			
			cands.PB(j);
			cands.PB(j + 1);
		}
		
		for(int pos : cands)
		{
			VI nans = ans;
			nans.insert(nans.begin() + pos, i);
			if(ok(nans))
			{
				ans = nans;
				break;
			}
		}
		assert(SZ(ans) == i + 1);
	}
	
	
	
	FOR(i, 0, n)
	{
		if(i)
			cout << " ";
		cout << ans[i] + 1;
	}
	cout << "\n";
}


int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);
	
	while(cin >> n)
		solve();
	
	

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3764kb

input:

3
001
000
100
4
0000
0000
0000
0000

output:

1 2 3
4 1 2 3

result:

ok 2 cases.

Test #2:

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

input:

3
000
000
000
3
010
100
000
3
011
100
100
3
011
101
110

output:

1 2 3
2 3 1
2 3 1
1 2 3

result:

ok 4 cases.

Test #3:

score: -100
Runtime Error

input:

4
0000
0000
0000
0000
4
0000
0001
0000
0100
4
0100
1010
0100
0000
4
0111
1000
1000
1000
4
0010
0011
1101
0110
4
0111
1011
1100
1100
4
0111
1011
1101
1110
4
0000
0011
0101
0110
4
0101
1010
0100
1000
4
0011
0011
1100
1100
4
0010
0001
1000
0100

output:


result: