답안 #1099870

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1099870 2024-10-12T05:40:21 Z model_code 상형문자열 (IOI24_hieroglyphs) C++17
14 / 100
888 ms 2097152 KB
// time_limit/yiping-n2.cpp

#include<bits/stdc++.h>
#include"hieroglyphs.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

struct Data
{
	int d;
	vector<int> a, cnt, rnk;
	vector< vector<int> > pos;
	
	Data(vector<int> _a, int _d)
	{
		a = _a; d = _d;
		cnt.resize(d); pos.resize(d); rnk.resize(a.size());
		for(int i=0; i<(int)a.size(); ++i)
		{
			int x = a[i];
			rnk[i] = cnt[x]; ++cnt[x];
			pos[x].emplace_back(i);
		}
	}
	bool check(int x,int cx,int y,int cy) const
	{
		if(cx > (int)pos[x].size() || cy > (int)pos[y].size())
			return 0;
		if(cx == 0 || cy == 0) return 1;
		return pos[x][cx - 1] < pos[y][(int)pos[y].size() - cy];
	}
};

vector<int> get_cand(const Data &aa, const Data &bb)
{
	int d = aa.d;
	vector<int> type(d), need(d);
	for(int i=0; i<d; ++i)
	{
		type[i] = aa.cnt[i] <= bb.cnt[i]? 0: 1;
		need[i] = min(aa.cnt[i], bb.cnt[i]);
	}
	
	vector<pii> veca, vecb;
	for(int i=0; i<(int)aa.a.size(); ++i)
		if(type[aa.a[i]] == 0)
			veca.emplace_back(aa.a[i], aa.rnk[i]);
	for(int i=0; i<(int)bb.a.size(); ++i)
		if(type[bb.a[i]] == 1)
			vecb.emplace_back(bb.a[i], bb.rnk[i]);
	
	auto check = [&] (pii x,pii y)
	{
		return aa.check(x.first, x.second + 1, y.first, need[y.first] - y.second)
		    && bb.check(x.first, x.second + 1, y.first, need[y.first] - y.second);
	};
	
	vector<int> c;
	int i = 0, j = 0;
	while(i<(int)veca.size() && j<(int)vecb.size())
	{
		bool tx = check(veca[i], vecb[j]);
		bool ty = check(vecb[j], veca[i]);
		
		if(tx == ty) return {-1};
		
		if(tx) c.emplace_back(veca[i].first), ++i;
		else c.emplace_back(vecb[j].first), ++j;
	}
	
	while(i<(int)veca.size()) c.emplace_back(veca[i].first), ++i;
	while(j<(int)vecb.size()) c.emplace_back(vecb[j].first), ++j;
	
	return c;
}

bool is_invalid(const vector<int> &c)
{
	return c.size() == 1 && c[0] == -1;
}

bool is_subseq(const vector<int> &a, const vector<int> &c)
{
	int j = 0;
	for(int i=0; i<(int)a.size() && j<(int)c.size(); ++i)
		if(a[i] == c[j]) ++j;
	return j >= (int)c.size();
}

vector<int> ucs(vector<int> a, vector<int> b)
{
	vector<int> dsc;
	dsc.insert(dsc.end(), a.begin(), a.end());
	dsc.insert(dsc.end(), b.begin(), b.end());
	sort(dsc.begin(), dsc.end());
	dsc.erase(unique(dsc.begin(), dsc.end()), dsc.end());
	
	int d = (int)dsc.size();
	for(auto &t: a)
		t = lower_bound(dsc.begin(), dsc.end(), t) - dsc.begin();
	for(auto &t: b)
		t = lower_bound(dsc.begin(), dsc.end(), t) - dsc.begin();
	
	Data aa(a, d), bb(b, d);
	
	auto c = get_cand(aa, bb);
	if(is_invalid(c)) return c;
	
	if(!is_subseq(a, c) || !is_subseq(b, c))
		return {-1};
	
	vector< vector<int> > nxt((int)c.size() + 1);
	nxt[(int)c.size()] = vector<int>(d, (int)c.size());
	for(int i=(int)c.size() - 1; i>=0; --i)
	{
		nxt[i] = nxt[i+1];
		nxt[i][c[i]] = i;
	}
	
	vector< vector<int> > dp(a.size() + 1, vector<int>(b.size() + 1, 0));
	for(int i=0; i<(int)a.size(); ++i)
		for(int j=0; j<(int)b.size(); ++j)
		{
			dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
			dp[i][j+1] = max(dp[i][j+1], dp[i][j]);
			if(a[i] == b[j])
			{
				int k = dp[i][j];
				if(nxt[k][a[i]] >= (int)c.size())
					return {-1};
				dp[i+1][j+1] = max(dp[i+1][j+1], nxt[k][a[i]] + 1);
			}
		}
	
	for(auto &t: c)
		t = dsc[t];
	
	return c;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 7 ms 8284 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Runtime error 793 ms 2097152 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 7 ms 8284 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Runtime error 793 ms 2097152 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 888 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 888 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 31 ms 35928 KB Output is correct
11 Correct 35 ms 35928 KB Output is correct
12 Correct 31 ms 35932 KB Output is correct
13 Correct 42 ms 35928 KB Output is correct
14 Correct 4 ms 4700 KB Output is correct
15 Correct 30 ms 33636 KB Output is correct
16 Correct 31 ms 34908 KB Output is correct
17 Correct 27 ms 31364 KB Output is correct
18 Correct 32 ms 36952 KB Output is correct
19 Correct 38 ms 41308 KB Output is correct
20 Correct 47 ms 45400 KB Output is correct
21 Correct 43 ms 51292 KB Output is correct
22 Correct 29 ms 36956 KB Output is correct
23 Correct 33 ms 38188 KB Output is correct
24 Correct 41 ms 45336 KB Output is correct
25 Correct 30 ms 52060 KB Output is correct
26 Correct 28 ms 41304 KB Output is correct
27 Correct 21 ms 25948 KB Output is correct
28 Correct 40 ms 46428 KB Output is correct
29 Correct 39 ms 41560 KB Output is correct
30 Correct 30 ms 37720 KB Output is correct
31 Correct 33 ms 36444 KB Output is correct
32 Correct 32 ms 36184 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 4 ms 4956 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 34 ms 33428 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 8284 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Runtime error 793 ms 2097152 KB Execution killed with signal 9
9 Halted 0 ms 0 KB -