UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213774#582. t1Origami_Tobiichi100170ms16532kbC++115.7kb2024-11-13 19:21:202024-11-13 23:01:27

answer

#include <bits/stdc++.h>
using namespace std;
namespace Octane
{
// non terrae plus ultra
#define OCTANE
#define BUFFER_SIZE 100000
#define ll long long
#define db double
#define ldb long double
	char ibuf[BUFFER_SIZE], obuf[BUFFER_SIZE];
	char *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#ifdef ONLINE_JUDGE
#define getchar() ((p1 == p2) and (p2 = (p1 = ibuf) + fread(ibuf, 1, BUFFER_SIZE, stdin), p1 == p2) ? (EOF) : (*p1++))
#define putchar(x) ((p3 == obuf + BUFFER_SIZE) && (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf), *p3++ = x)
#endif // fread in OJ, getchar in local

#define isdigit(ch) (ch > 47 && ch < 58)
#define isspace(ch) (ch < 33 && ch != EOF)

	const ll pow10[] = {
		(ll)1e0,
		(ll)1e1,
		(ll)1e2,
		(ll)1e3,
		(ll)1e4,
		(ll)1e5,
		(ll)1e6,
		(ll)1e7,
		(ll)1e8,
		(ll)1e9,
		(ll)1e10,
		(ll)1e11,
		(ll)1e12,
		(ll)1e13,
		(ll)1e14,
		(ll)1e15,
		(ll)1e16,
		(ll)1e17,
		(ll)1e18,
	};

	struct Octane_t
	{
		~Octane_t()
		{
			fwrite(obuf, p3 - obuf, 1, stdout);
		}
		bool flag = false;
		operator bool()
		{
			return flag;
		}
	} io;

	template <typename T>
	inline T read()
	{
		T s = 0;
		int w = 1;
		char ch;
		while (ch = getchar(), !isdigit(ch) && (ch != EOF))
			if (ch == '-')
				w = -1;
		if (ch == EOF)
			return 0;
		while (isdigit(ch))
			s = s * 10 + ch - 48, ch = getchar();
		if (ch == '.')
		{
			ll flt = 0;
			int cnt = 0;
			while (ch = getchar(), isdigit(ch))
				if (cnt < 18)
					flt = flt * 10 + ch - 48, cnt++;
			s += (db)flt / pow10[cnt];
		}
		return s *= w;
	}
	template <typename T>
	inline bool read(T &s)
	{
		s = 0;
		int w = 1;
		char ch;
		while (ch = getchar(), !isdigit(ch) && (ch != EOF))
			if (ch == '-')
				w = -1;
		if (ch == EOF)
			return false;
		while (isdigit(ch))
			s = s * 10 + ch - 48, ch = getchar();
		if (ch == '.')
		{
			ll flt = 0;
			int cnt = 0;
			while (ch = getchar(), isdigit(ch))
				if (cnt < 18)
					flt = flt * 10 + ch - 48, cnt++;
			s += (db)flt / pow10[cnt];
		}
		return s *= w, true;
	}
	inline bool read(char &s)
	{
		while (s = getchar(), isspace(s))
			;
		return s != EOF;
	}
	inline bool read(char *s)
	{
		char ch;
		while (ch = getchar(), isspace(ch))
			;
		if (ch == EOF)
			return false;
		while (!isspace(ch))
			*s++ = ch, ch = getchar();
		*s = '\000';
		return true;
	}
	template <typename T>
	void print(T x)
	{
		static int t[20];
		int top = 0;
		if (x < 0)
			putchar('-'), x = -x;
		do
		{
			t[++top] = x % 10;
			x /= 10;
		} while (x);
		while (top)
			putchar(t[top--] + 48);
	}
	struct empty_type
	{
	};
	int pcs = 8;
	empty_type setpcs(int cnt)
	{
		return pcs = cnt, empty_type();
	}
	inline void print(empty_type x) {}
	inline void print(double x)
	{
		if (x < 0)
			putchar('-'), x = -x;
		x += 5.0 / pow10[pcs + 1];
		print((ll)(x));
		x -= (ll)(x);
		if (pcs != 0)
			putchar('.');
		for (int i = 1; i <= pcs; i++)
			x *= 10, putchar((int)x + '0'), x -= (int)x;
	}
	inline void print(float x)
	{
		if (x < 0)
			putchar('-'), x = -x;
		x += 5.0 / pow10[pcs + 1];
		print((ll)(x));
		x -= (ll)(x);
		if (pcs != 0)
			putchar('.');
		for (int i = 1; i <= pcs; i++)
			x *= 10, putchar((int)x + '0'), x -= (int)x;
	}
	inline void print(char x)
	{
		putchar(x);
	}
	inline void print(char *x)
	{
		for (int i = 0; x[i]; i++)
			putchar(x[i]);
	}
	inline void print(const char *x)
	{
		for (int i = 0; x[i]; i++)
			putchar(x[i]);
	}

// support for string
#ifdef _GLIBCXX_STRING
	inline bool read(std::string &s)
	{
		s = "";
		char ch;
		while (ch = getchar(), isspace(ch))
			;
		if (ch == EOF)
			return false;
		while (!isspace(ch))
			s += ch, ch = getchar();
		return true;
	}
	inline void print(std::string x)
	{
		for (string::iterator i = x.begin(); i != x.end(); i++)
			putchar(*i);
	}
	inline bool getline(Octane_t &io, string s)
	{
		s = "";
		char ch = getchar();
		if (ch == EOF)
			return false;
		while (ch != '\n' and ch != EOF)
			s += ch, ch = getchar();
		return true;
	}
#endif

// support for initializer_list
#if __cplusplus >= 201103L
	template <typename T, typename... T1>
	inline int read(T &a, T1 &...other)
	{
		return read(a) + read(other...);
	}
	template <typename T, typename... T1>
	inline void print(T a, T1... other)
	{
		print(a);
		print(other...);
	}
#endif

	//  give up iostream
	template <typename T>
	Octane_t &operator>>(Octane_t &io, T &b)
	{
		return io.flag = read(b), io;
	}
	Octane_t &operator>>(Octane_t &io, char *b)
	{
		return io.flag = read(b), io;
	}
	template <typename T>
	Octane_t &operator<<(Octane_t &io, T b)
	{
		return print(b), io;
	}

#define cout io
#define cin io
#define endl '\n'
#undef ll
#undef db
#undef ldb
#undef BUFFER_SIZE
}
using namespace Octane;
const int inf = 1e9 + 7, N = 1e5 + 7;
int a[N];
vector<int> G[N], g[N];
priority_queue<pair<int, int>> dfs(int u)
{
	priority_queue<pair<int, int>> pq;
	for (int v : G[u])
	{
		auto tt = dfs(v);
		if (tt.size() > pq.size())
			swap(tt, pq);
		while (tt.size())
		{
			auto e = tt.top();
			tt.pop();
			pq.push(e);
		}
	}
	while (pq.size() && pq.top().first > a[u])
	{
		int v = pq.top().second;
		pq.pop();
		g[u].push_back(v);
	}
	pq.push({a[u], u});
	return pq;
}
string ans;
void dfs2(int u)
{
	sort(g[u].begin(), g[u].end(), [&](int x, int y)
		 { return a[x] < a[y]; });
	for (int v : g[u])
		dfs2(v);
	if (ans.size())
		ans += ' ';
	ans += to_string(u);
}
signed main()
{
	int n;
	cin >> n;
	for (int i = 2, p; i <= n; i++)
		cin >> p, G[p].push_back(i);
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	a[1] = -inf, dfs(1), dfs2(1);
	cout << ans << endl;
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 5920kb

input:

100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...

output:

52 75 61 96 94 85 99 65 92 64 100 53 79 67 84 89 69 62 88 58 87 68 51 76 56 70 74 55 91 72 59 82 71 ...

result:

ok 100 numbers

Test #2:

score: 10
Accepted
time: 0ms
memory: 5920kb

input:

100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...

output:

97 98 56 64 88 71 85 54 94 81 69 74 77 82 66 76 100 95 78 57 93 86 80 83 63 90 68 99 53 52 73 91 96 ...

result:

ok 100 numbers

Test #3:

score: 10
Accepted
time: 0ms
memory: 5920kb

input:

100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...

output:

68 100 83 82 66 57 73 90 74 89 92 84 78 52 99 65 75 54 71 87 85 77 61 93 72 63 64 58 60 69 91 96 67 ...

result:

ok 100 numbers

Test #4:

score: 10
Accepted
time: 0ms
memory: 6040kb

input:

1000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35...

output:

720 630 795 982 858 778 764 843 958 636 655 718 999 640 819 553 685 815 801 611 580 660 848 722 508 ...

result:

ok 1000 numbers

Test #5:

score: 10
Accepted
time: 0ms
memory: 6036kb

input:

1000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35...

output:

993 967 945 661 760 801 836 832 798 725 609 708 916 659 785 955 912 802 778 739 850 529 680 664 847 ...

result:

ok 1000 numbers

Test #6:

score: 10
Accepted
time: 0ms
memory: 6036kb

input:

1000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35...

output:

865 829 713 826 804 598 835 729 919 613 859 604 586 677 926 808 871 883 512 573 946 688 540 980 501 ...

result:

ok 1000 numbers

Test #7:

score: 10
Accepted
time: 42ms
memory: 16524kb

input:

100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

60966 84526 92926 81979 52715 89039 62563 60694 55151 70404 50693 82115 87107 88733 92525 69003 5623...

result:

ok 100000 numbers

Test #8:

score: 10
Accepted
time: 43ms
memory: 16532kb

input:

100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

73271 97790 79288 53984 60747 85301 62325 94837 68122 92850 54755 72761 86308 93120 85924 58122 5785...

result:

ok 100000 numbers

Test #9:

score: 10
Accepted
time: 41ms
memory: 16532kb

input:

100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

96945 71333 97384 73952 87860 78638 54641 53438 84679 65487 87180 73489 72817 69821 95923 70797 7798...

result:

ok 100000 numbers

Test #10:

score: 10
Accepted
time: 44ms
memory: 16532kb

input:

100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

86618 75549 54929 84432 89351 73329 74867 54756 55393 96686 62390 82968 79815 69008 65957 58416 6517...

result:

ok 100000 numbers

Extra Test:

score: 0
Extra Test Passed