UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#202359#3537. 序列rua1001194ms13972kbC++113.2kb2024-02-15 11:13:352024-02-15 12:34:48

answer

#include <bits/stdc++.h>
#define inf INT_MAX
#define NN INT_MIN
typedef long long ll;
using namespace std;
#define int ll
int n;
ll a[200015];
ll fac[2000015], infac[2000015];
ll bigger[200015], smll[200015], eql[200015];
const ll mod = 1e9 + 7;
bool vis1[200015];
struct node
{
	ll data, i;
	bool operator<(const node &t) const
	{
		return data < t.data;
	}
} b[200015];
ll qpow(ll a, ll n)
{
	ll ans = 1;
	while (n)
	{
		if (n & 1)
			ans = (ans * a) % mod;
		a = (a * a) % mod;
		n >>= 1;
	}
	return ans % mod;
}
ll femat(ll x)
{
	return qpow(x, mod - 2);
}
void init()
{
	fac[0] = 1;
	for (int i = 1; i <= 200030; i++)
		fac[i] = fac[i - 1] * i % mod;
	for (int i = 0; i <= 200030; i++)
		infac[i] = femat(fac[i]) % mod;
}
ll C(int n, int m)
{
	if (n == 0 || m == 0)
		return 1;
	return fac[n] * infac[m] % mod * infac[n - m] % mod;
}
signed main()
{
#ifdef WHX_AK_IOI
	freopen("data.in", "r", stdin);
// freopen("dataout.txt","w",stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	init();
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		b[i].data = a[i];
		b[i].i = i;
	}
	// cout << infac[7000] << endl;
	sort(b + 1, b + n + 1);
	for (int i = 1; i <= n; i++)
	{
		smll[b[i].i] = i - 1;
		bigger[b[i].i] = n - i;
		while (b[i].data == b[i + 1].data)
			i++, smll[b[i].i] = smll[b[i - 1].i], bigger[b[i].i] = bigger[b[i - 1].i];
	}
	// for (int i = 1; i <= n; i++)
	// 	cout << smll[i] << endl;
	for (int i = 1; i <= n; i++)
		while (b[i].data == b[i + 1].data)
			i++, eql[b[i].i] = eql[b[i - 1].i] + 1;
	// for (int i = 1; i <= n; i++)
	// 	cout << eql[b[i].i] << endl;
	for (int i = 1; i <= n; i++)
	{
		int j = i;
		while (eql[b[i + 1].i] > eql[b[i].i])
			i++;
		for (int k = j; k <= i; k++)
			eql[b[k].i] = eql[b[i].i];
	}
	// for (int i = 1; i <= n; i++)
	// 	cout << eql[b[i].i] << endl;
	// for (int i = 1; i <= n; i++)
	// 	cout << bigger[b[i].i] << endl;
	// for (int i = 1; i <= n; i++)
	// 	bigger[i] -= eql[i];
	// for (int i = 1; i <= n; i++)
	// 	cout << smll[i] << ' ';
	// cout << endl;
	// for (int i = 1; i <= n; i++)
	// 	cout << eql[i] << ' ';
	// cout << endl;
	// for (int i = 1; i <= n; i++)
	// 	cout << bigger[i] << ' ';
	// cout << endl;
	ll ans = 0;
	for (int i = 1; i <= n; i++)
	{
		if (vis1[a[i]] == 1)
			continue;
		vis1[a[i]] = 1;
		if (eql[i] < a[i] - 1 - smll[i])
			continue;
		eql[i] -= max(0LL, a[i] - 1 - smll[i]);
		bigger[i] -= max(0LL, a[i] - 1 - smll[i]);
		if (smll[i] < a[i] - 1)
			smll[i] = a[i] - 1;

		// cout << endl
		// 	 << endl;
		// cout << "case:" << i << endl
		// 	 << "sml:" << smll[i] << endl
		// 	 << "eql:" << eql[i] << endl
		// 	 << "bg:" << bigger[i] << endl
		// 	 << endl;
		ans = (ans + C(smll[i], a[i] - 1) * qpow(2, bigger[i]) % mod) % mod;
		for (int j = 1; j <= eql[i]; j++)
		{
			smll[i]++, bigger[i]--;
			// cout << "case:" << i << endl
			// 	 << "sml:" << smll[i] << endl
			// 	 << "eql:" << eql[i] << endl
			// 	 << "bg:" << bigger[i] << endl
			// 	 << endl;
			ans = (ans + C(smll[i], a[i] - 1) * qpow(2, bigger[i]) % mod) % mod;
		}
	}
	cout << ans << endl;
	// system("pause");
	return 0;
}

详细

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

Test #1:

score: 5
Accepted
time: 35ms
memory: 4416kb

input:

15 8 6 14 11 4 9 15 14 15 9 14 12 3 6 13

output:

1

result:

ok single line: '1'

Test #2:

score: 5
Accepted
time: 35ms
memory: 4424kb

input:

15 10 9 3 3 2 4 10 10 14 13 14 3 10 1 10

output:

46732

result:

ok single line: '46732'

Test #3:

score: 5
Accepted
time: 35ms
memory: 4424kb

input:

15 3 4 2 13 8 15 8 13 11 6 3 2 9 9 3

output:

32999

result:

ok single line: '32999'

Test #4:

score: 5
Accepted
time: 34ms
memory: 4424kb

input:

15 9 9 10 12 8 8 11 14 6 5 6 13 9 11 6

output:

288

result:

ok single line: '288'

Test #5:

score: 5
Accepted
time: 35ms
memory: 4424kb

input:

15 3 2 15 6 12 1 15 12 5 10 13 4 5 9 6

output:

38637

result:

ok single line: '38637'

Test #6:

score: 5
Accepted
time: 36ms
memory: 4516kb

input:

2000 25 484 1870 583 668 1298 1198 1838 1248 567 1864 1118 1554 564 1303 1047 1053 960 1729 1932 196...

output:

385129497

result:

ok single line: '385129497'

Test #7:

score: 5
Accepted
time: 35ms
memory: 4512kb

input:

2000 34 1417 31 1345 46 1660 875 1911 518 1609 41 1402 1088 1120 1649 1214 1927 1602 645 589 162 161...

output:

44472341

result:

ok single line: '44472341'

Test #8:

score: 5
Accepted
time: 38ms
memory: 4512kb

input:

2000 1024 1019 1146 1656 1906 411 258 1698 1376 1730 998 806 1959 1347 1797 1035 1464 216 179 1618 1...

output:

477127421

result:

ok single line: '477127421'

Test #9:

score: 5
Accepted
time: 31ms
memory: 4516kb

input:

2000 904 1371 1577 708 325 661 1064 1928 1337 1980 1275 583 1688 1560 257 1281 789 1589 787 1875 176...

output:

409666345

result:

ok single line: '409666345'

Test #10:

score: 5
Accepted
time: 35ms
memory: 4512kb

input:

2000 736 1091 1058 755 653 1211 205 1131 1494 1153 1816 35 1455 1411 663 874 1851 510 494 1962 1668 ...

output:

962962192

result:

ok single line: '962962192'

Test #11:

score: 5
Accepted
time: 72ms
memory: 13968kb

input:

200000 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:

175895281

result:

ok single line: '175895281'

Test #12:

score: 5
Accepted
time: 70ms
memory: 13972kb

input:

200000 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:

175895281

result:

ok single line: '175895281'

Test #13:

score: 5
Accepted
time: 73ms
memory: 13968kb

input:

200000 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:

175895281

result:

ok single line: '175895281'

Test #14:

score: 5
Accepted
time: 61ms
memory: 13968kb

input:

200000 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:

175895281

result:

ok single line: '175895281'

Test #15:

score: 5
Accepted
time: 77ms
memory: 13968kb

input:

200000 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:

175895281

result:

ok single line: '175895281'

Test #16:

score: 5
Accepted
time: 80ms
memory: 13972kb

input:

200000 166590 20849 126411 133366 36333 18332 172547 57470 145664 110620 37556 113908 123961 195783 ...

output:

426667668

result:

ok single line: '426667668'

Test #17:

score: 5
Accepted
time: 86ms
memory: 13972kb

input:

200000 169672 89425 34124 104143 93288 70493 61048 192113 292 79680 52311 52428 142153 144390 91082 ...

output:

462091315

result:

ok single line: '462091315'

Test #18:

score: 5
Accepted
time: 109ms
memory: 13968kb

input:

200000 184655 166160 76584 90054 51931 198051 58297 90616 113758 140994 13505 93311 4251 53203 31173...

output:

697086497

result:

ok single line: '697086497'

Test #19:

score: 5
Accepted
time: 106ms
memory: 13968kb

input:

200000 80079 124181 170434 31758 99420 136529 30673 145269 111549 163488 37324 132314 122035 39583 1...

output:

512465333

result:

ok single line: '512465333'

Test #20:

score: 5
Accepted
time: 111ms
memory: 13968kb

input:

200000 83684 149238 26953 164914 50552 195667 72114 83448 182923 88943 196844 92388 134484 105724 63...

output:

335807702

result:

ok single line: '335807702'

Extra Test:

score: 0
Extra Test Passed