CF1419D2

题面:https://codeforces.com/problemset/problem/1419/D2

我宣布一件事:我是傻逼!

大致题意:给定一个数列,若有一个数比左右相邻的数都小,则其对答案的贡献为1,允许随意重新排列,求最终答案的最大值

题解:易知,若最终答案为n,则存在至少n+1个值严格大于对答案有贡献的数(我们在这里取最小的n个值),故直接对数列进行排序即可

但是直接贪心排序后答案可能为错,应该找到最小的值比前一个MIN大且比后一个MIN大

我硬写了一个,ac了(大致思路是找出最可能出最优解的序列,再统计答案)

(代码比较丑,就当警示碑了bushi)

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<vector>
#include<string>
#include<map>
#include<cstdio>
#include<list>
#include<stack>
#include<unordered_set>
using namespace std;
using ll = int64_t;
ll T, n, m, k;
ll tmp, a[300005];
int main()
{
T = 1;
//cin >> T;
while(T--)
{
cin >> n;
for (ll i = 1; i <= n;i++)
{
cin >> a[i];
}
sort(a + 1, a + 1 + n);
m = (n - 1) / 2;
k = n - m;
vector<ll> MIN, MAX, ans;
for (ll i = 1; i <= n;i++)
{
if(i<=m)
{
MIN.push_back(a[i]);
}else
{
MAX.push_back(a[i]);
}
}
int flag = 1;
ll tot = 0, tot2 = 0, tot3 = 0;
vector<ll> no;
for (ll i = 1; i <= n;i++)
{
if(flag==1&&tot<MAX.size())
{
while(tot<MAX.size()&&((tot3<MIN.size()&&MAX[tot]<=MIN[tot3])||(ans.size()>0&&(MAX[tot]<=ans[ans.size()-1]))))
{
no.push_back(MAX[tot++]);
}
if(tot<MAX.size())
{
ans.push_back(MAX[tot++]);
}
flag = 0;
}else
{
if(tot3<MIN.size())
{
ans.push_back(MIN[tot3++]);
}else
{
if(tot<MAX.size())
ans.push_back(MAX[tot++]);
else
ans.push_back(no[tot2++]);
}
flag = 1;
}
}
while(tot2<no.size())
{
ans.push_back(no[tot2++]);
}
ll num = 0;
for (ll i = 1; i < n-1;i++)
{
if(ans[i]<ans[i-1]&&ans[i]<ans[i+1])
{
num++;
}
}
cout << num << "\n";
for (ll i = 0; i < n;i++)
{
cout << ans[i] << " ";
}
cout << "\n";
}
return 0;
}

只要将n为偶数时,MAX的位置向后移一位,我原本的代码也ac了..

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<vector>
#include<string>
#include<map>
#include<cstdio>
#include<list>
#include<stack>
#include<unordered_set>
using namespace std;
using ll = int64_t;
ll T, n, m, k;
ll tmp, a[300005];
int main()
{
T = 1;
//cin >> T;
while(T--)
{
cin >> n;
for (ll i = 1; i <= n;i++)
{
cin >> a[i];
}
sort(a + 1, a + 1 + n);
//m = (n - 1) / 2;
//k = n - m;
k = n / 2 + 1;
m = k - 1;
vector<ll> MIN, MAX, ans;
for (ll i = 1; i <= n;i++)
{
if(i<=m)
{
MIN.push_back(a[i]);
}else
{
MAX.push_back(a[i]);
}
}
int flag = 1;
ll tot = 0;
for (ll i = 1; i <= n;i++)
{
if(flag==1)
{
ans.push_back(MAX[tot++]);
flag = 0;
}else
{
if(tot<=MIN.size())
{
ans.push_back(MIN[tot - 1]);
}else
{
ans.push_back(MAX[tot++]);
}
flag = 1;
}
}
ll num = 0;
for (ll i = 1; i < n-1;i++)
{
if(ans[i]<ans[i-1]&&ans[i]<ans[i+1])
{
num++;
}
}
cout << num << "\n";
for (ll i = 0; i < n;i++)
{
cout << ans[i] << " ";
}
cout << "\n";
}
return 0;
}