UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#203930#528. 神树和排列snow_trace1001311ms1640kbC++111.6kb2024-03-26 09:39:222024-03-26 12:41:59

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1000000007;
int n,m,k;
int sum[10005],ok[10005];
int a[10005];
int dp[8005],pre[8005],f[8005];
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m;memset(a,-1,sizeof(a));for(int i =1;i<=n;i++)ok[i] = 1;
	for(int i = 1;i<=m;i++){
		int pos,x;cin >> pos >> x;a[pos] = x;ok[x] = 0;
	}if(a[n]!=-1 and a[n]!=n){
		cout << 0 << endl;return 0;
	}for(int i= 1;i<=n;i++)sum[i] = sum[i-1]+ok[i];int tt =0;
	f[0] = 1;for(int i = 0;i<=n;i++)pre[i] = 1;
	for(int i =1;i<=n;i++){
	//	cout << tt << endl;
		if(a[i] != -1){tt++;
			dp[a[i]] = pre[a[i]-1]*2%mod;
			for(int j = a[i]+1;j<=n;j++){
				dp[j] = f[j];
			}
		}else{
			for(int j = i;j<=n;j++){
				//第一种,前面最大值就是这个,这个位置上的元素要比它小
				//cout << " " << j << " " << f[j] << " " << sum[j-1] << " " << (sum[j-1]-(i-1-tt-(ok[j]))) << endl;
				if(sum[j-1]>=i-tt-1-ok[j] and i!=1 and i!=n)dp[j] = (dp[j]+f[j]*(sum[j-1]-(i-1-tt-(ok[j]))))%mod;
				//第二种自己是最大值
				if(ok[j])dp[j] = (pre[j-1]*2+dp[j])%mod; 
			}
		}memcpy(f,dp,sizeof(f));memset(dp,0,sizeof(dp));
		pre[i-1] = 0;for(int j = i;j<=n;j++)pre[j] = (pre[j-1]+f[j])%mod;
		//for(int j = 1;j<=n;j++)cout << f[j] << " ";cout<< endl;
	}cout << f[n]*((mod+1)/2)%mod << endl; 
	return 0;
}/*
注意到一个F若有s个前缀最大值,那么它对应的P有2^{k-1}个
考虑由此设计 dp
dp_i_j 表示第 i 个位置,前缀最大值为 j
然后随便转移一下。 
5 0
*/

详细

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

Test #1:

score: 5
Accepted
time: 1ms
memory: 1460kb

input:

9 1
4 3

output:

40320

result:

ok "40320"

Test #2:

score: 5
Accepted
time: 0ms
memory: 1456kb

input:

9 3
1 2
7 3
8 8

output:

2400

result:

ok "2400"

Test #3:

score: 5
Accepted
time: 0ms
memory: 1456kb

input:

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

output:

120

result:

ok "120"

Test #4:

score: 5
Accepted
time: 0ms
memory: 1456kb

input:

10 1
5 10

output:

0

result:

ok "0"

Test #5:

score: 5
Accepted
time: 0ms
memory: 1460kb

input:

80 30
1 25
3 71
8 48
10 59
11 29
13 61
18 22
22 74
27 47
28 20
37 43
38 64
40 14
41 18
42 1
43 76
46...

output:

300989462

result:

ok "300989462"

Test #6:

score: 5
Accepted
time: 0ms
memory: 1460kb

input:

140 30
3 127
4 52
15 89
18 42
22 138
26 90
27 36
36 68
52 134
55 22
61 99
62 45
66 27
67 83
74 119
7...

output:

147966206

result:

ok "147966206"

Test #7:

score: 5
Accepted
time: 0ms
memory: 1464kb

input:

200 1
70 187

output:

484627735

result:

ok "484627735"

Test #8:

score: 5
Accepted
time: 0ms
memory: 1464kb

input:

300 15
26 7
30 14
34 10
52 9
55 6
63 13
65 4
72 8
96 11
116 15
147 5
204 1
208 2
223 12
239 3

output:

162653895

result:

ok "162653895"

Test #9:

score: 5
Accepted
time: 5ms
memory: 1472kb

input:

500 2
163 375
163 300

output:

0

result:

ok "0"

Test #10:

score: 5
Accepted
time: 7ms
memory: 1476kb

input:

650 1
643 506

output:

173511339

result:

ok "173511339"

Test #11:

score: 5
Accepted
time: 6ms
memory: 1480kb

input:

850 40
25 9
38 36
43 10
62 30
68 38
127 40
132 33
159 19
164 28
221 23
226 37
250 17
251 1
307 18
32...

output:

134077016

result:

ok "134077016"

Test #12:

score: 5
Accepted
time: 10ms
memory: 1484kb

input:

1000 600
1 61
2 885
4 881
6 304
7 824
8 554
10 750
11 608
12 325
14 368
16 130
17 642
18 515
24 638
...

output:

715633965

result:

ok "715633965"

Test #13:

score: 5
Accepted
time: 20ms
memory: 1492kb

input:

1500 114
1 1265
56 1119
67 557
97 590
124 1092
126 859
137 647
140 714
142 612
158 186
171 975
182 4...

output:

726562104

result:

ok "726562104"

Test #14:

score: 5
Accepted
time: 33ms
memory: 1504kb

input:

2000 300
7 7
30 10
37 36
39 37
45 1
56 28
77 50
89 81
94 74
98 23
102 16
126 39
132 57
152 40
159 73...

output:

519422664

result:

ok "519422664"

Test #15:

score: 5
Accepted
time: 53ms
memory: 1520kb

input:

2500 1700
3 1
4 2
5 5
13 8
15 6
19 10
26 21
30 28
43 31
47 45
48 3
51 20
56 11
59 12
60 27
66 36
77 ...

output:

848793393

result:

ok "848793393"

Test #16:

score: 5
Accepted
time: 97ms
memory: 1528kb

input:

3000 15
225 13
253 1
374 14
593 11
836 2
1176 6
1276 12
1413 9
1843 10
1847 15
2007 7
2416 3
2418 5
...

output:

84073256

result:

ok "84073256"

Test #17:

score: 5
Accepted
time: 168ms
memory: 1560kb

input:

4200 1
3504 859

output:

360023162

result:

ok "360023162"

Test #18:

score: 5
Accepted
time: 156ms
memory: 1592kb

input:

5500 3600
3 3
8 6
9 9
15 5
18 12
29 21
35 10
43 30
44 31
48 37
70 58
78 32
79 61
80 78
81 68
87 36
8...

output:

184238535

result:

ok "184238535"

Test #19:

score: 5
Accepted
time: 184ms
memory: 1616kb

input:

6700 6666
1 4536
2 2092
3 5501
4 1935
5 954
6 4346
7 4636
8 6189
9 138
10 2762
11 4141
12 864
13 609...

output:

526789226

result:

ok "526789226"

Test #20:

score: 5
Accepted
time: 571ms
memory: 1640kb

input:

8000 30
256 28
393 23
718 15
896 21
957 8
1058 3
1182 11
1281 30
1520 14
1888 18
2030 26
2129 5
2251...

output:

907165767

result:

ok "907165767"