0%

思路讲解

赛时就做出来了,存档一下

好像有更简单的做法(倒过来想),但还是算了

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=static_cast<ll>(1e6)+117;

ll N,T,A[MAXN],Ans[MAXN],tr[MAXN];
//std::set<ll> ali;
//// 数->第几个
//std::unordered_map<ll,ll> li;

inline ll lowbit(ll x){
return x&(-x);
}

inline void add(ll x){
ll idx=x+1;
while (idx<=MAXN-10) {
tr[idx]+=1;
idx+=lowbit(idx);
}
}

inline ll query(ll x){
ll idx=x+1;
ll res=0;
// x是在后面的数,查找前面的全部比它大的数
// 当然,直接查找全部比它大的数比较难
// 所以返回的是所有比它<=的数的数量
while(idx>0){
res+=tr[idx];
idx-=lowbit(idx);
}
return res;
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>N;
for(int i=1;i<=N;++i){
std::cin>>A[i];
// ali.insert(A[i]);
}
// for(std::set<ll>::iterator it=ali.begin();it!=ali.end();it++){
// li[*it]=++idx;
// }
for(int i=1;i<=N;++i){
// 在这之前的成年人,我们看他们能不能给到第i年
ll lowerThanI=query(i-1);
add(A[i]+i-1-lowerThanI+i);
// 最初有多少宝石
Ans[i]=A[i]+i-1-lowerThanI;
}
for(int i=1;i<=N;++i){
Ans[i]=std::max(Ans[i]-(N-i),0LL);
std::cout<<Ans[i]<<" ";
}
std::cout<<"\n";
return 0;
}
// AC https://atcoder.jp/contests/abc388/submissions/61585347

心路历程(WA,TLE,MLE……)

思路讲解

和这题比较像,当然牛客的要简单一点

image

参考题解

image

我们来重点解释一下这个式子,其实我们最需要解决的就是可能两个数我要一个数,但是问题是我一个数不可能掰成两个数用。

https://www.luogu.com.cn/article/91cyarz3 当然下面这个最清楚

image

AC代码

心路历程(WA,TLE,MLE……)

思路讲解

如何使用树状数组代替平衡树

其实我主要不会的是怎么查前驱后继

想复杂了,这个其实是用二分做的。

那check函数怎么写呢?

不难发现一个性质,就是如果我要组成k个对
那么一定可以由k个最大的数作底座(当然你想反过来也可以)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
inline bool check(ll k){
// 不难发现一个性质,就是如果我要组成k个数
// 那么一定可以由k个最大的数作底座(当然你想反过来也可以)
// 于是我们只要检查这样可不可以组成就行了
int j=N;
ll res=0;
for(int i=N-k;i>=1 && j>N-k;--i){
if(A[j]>=A[i]*2){
++res;
j-=1;
}
}
if(res>=k){
return true;
}else{
return false;
}
}

其实我之前的做法的问题就是“内讧”,自己抢自己的。

AC代码

https://atcoder.jp/contests/abc388/submissions/61633977

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=static_cast<ll>(5e5)+117;

ll N,T,A[MAXN];

inline bool check(ll k){
// 不难发现一个性质,就是如果我要组成k个数
// 那么一定可以由k个最大的数作底座(当然你想反过来也可以)
// 于是我们只要检查这样可不可以组成就行了
int j=N;
ll res=0;
for(int i=N-k;i>=1 && j>N-k;--i){
if(A[j]>=A[i]*2){
++res;
j-=1;
}
}
if(res>=k){
return true;
}else{
return false;
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>N;
for(int i=1;i<=N;++i){
std::cin>>A[i];
}
ll l=0,r=N/2;
while (l<r) {
ll mid=l+r+1>>1;
if(check(mid)){
l=mid;
}else{
r=mid-1;
}
}
std::cout<<l<<"\n";
return 0;
}
/*
AC https://atcoder.jp/contests/abc388/submissions/61633977
10
1 1 1 1 3 3 3 6 6 6

*/

心路历程(WA,TLE,MLE……)

用这个multiset可以实现查前继后驱

但事实证明贪心的选择A以及A*2的后驱还是有问题

WA https://atcoder.jp/contests/abc388/submissions/61623225

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=static_cast<ll>(5e5)+117;
std::multiset<ll> tr;

ll N,T,A[MAXN];

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>N;
for(int i=1;i<=N;++i){
std::cin>>A[i];
tr.insert(A[i]);
}
ll ans=0;
for(int i=1;i<=N;++i){
std::multiset<ll>::iterator its=tr.find(A[i]);
// 没有找到我们continue
if(its==tr.end())
continue;
// 起到一个tag的作用
tr.insert(A[i]*2);
std::multiset<ll>::iterator it=tr.find(A[i]*2);
std::multiset<ll>::iterator trEnd=std::prev(tr.end());
if(it==trEnd){
tr.erase(it);
continue;
}
std::multiset<ll>::iterator nextIt = std::next(it);
tr.erase(its);
tr.erase(it);
tr.erase(nextIt);
++ans;
}
std::cout<<ans<<"\n";
return 0;
}
/*
10
1 1 1 1 3 3 3 6 6 6

8
2 4 6 12 100 101 102 103


*/

思路讲解

无向图判环模版题

无向图判环+贪心选择短边,这就是kruskal算法

AC代码

https://vjudge.net/solution/57384651

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=110;
ll fa[110];

ll N;
struct cmp{
bool operator()(arr a,arr b){
if(a[2]!=b[2])
return a[2]>b[2];
return false;
}
};

inline void init(){
for(int i=0;i<N+7;++i)
fa[i]=i;
}

inline ll find(ll x){
if(fa[x]==x)
return x;
fa[x]=find(fa[x]);
return fa[x];
}

void solve(){
init();
std::priority_queue<arr,std::vector<arr>,cmp> pq;
for(int i=1;i<=N*(N-1)/2;++i){
ll a,b,c;
std::cin>>a>>b>>c;
pq.push({a,b,c});
}
ll ans=0;
while (!pq.empty()) {
ll a=pq.top()[0],b=pq.top()[1],c=pq.top()[2];
pq.pop();
if(find(a)!=find(b)){
fa[find(a)]=find(b);
ans+=c;
}
}
std::cout<<ans<<"\n";
return;
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
while (std::cin>>N) {
if(N==0){
break;
}
solve();
}
return 0;
}
/*
AC https://vjudge.net/solution/57384651
3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0

*/

心路历程(WA,TLE,MLE……)

思路讲解

P1908 逆序对 的升级版,加了下面这句话

He is allowed to swap two adjacent numbers for no more than k times.

1
cnt=std::max(cnt-K,0LL);

那为什么只要加了这句话就AC了呢?交换两个相邻元素就可以消除逆序对?

首先这个逆序对有两种情况,一种是相邻的逆序对,一种是不相邻的逆序对,比如以下这个

2.....12. . . . .1

中间只能填2,所以任何所谓的超距作用超距作用其实中间的所有数必然和那个1‘1’是逆序对关系,所以所有逆序对关系都可以转化为相邻逆序对关系。

AC代码

https://vjudge.net/solution/57377152

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
95
96
97
98
99
100
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>
#include <unordered_map>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=static_cast<ll>(1e5)+10;
std::set<ll> ali;
// 数->第几个
std::unordered_map<ll,ll> li;
ll N,T,A[MAXN],tr[MAXN],K;

inline ll lowbit(ll x){
return x&(-x);
}

inline void add(ll x){
ll idx=li[x];
while (idx<=N) {
tr[idx]+=1;
idx+=lowbit(idx);
}
}

inline ll query(ll x){
ll idx=li[x];
ll res=0;
// x是在后面的数,查找前面的全部比它大的数
// 当然,直接查找全部比它大的数比较难
// 所以返回的是所有比它<=的数的数量
while(idx>0){
res+=tr[idx];
idx-=lowbit(idx);
}
return res;
}

inline void init(){
ali.clear();
li.clear();
for(int i=0;i<=N+5;++i)
tr[i]=0;
}

inline void solve(){
init();
for(int i=1;i<=N;++i){
std::cin>>A[i];
ali.insert(A[i]);
}
ll idx=0;
for(std::set<ll>::iterator it=ali.begin();it!=ali.end();it++){
li[*it]=++idx;
}
ll cnt=0,op=0; // op即操作数
for(int i=1;i<=N;++i){
ll lcnt=std::max((i-1-query(A[i])),0LL);
cnt+=lcnt;
add(A[i]);
}
cnt=std::max(cnt-K,0LL);
std::cout<<cnt<<"\n";
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
while (std::cin>>N>>K) {
solve();
}
return 0;
}
/*
AC https://vjudge.net/solution/57377152
3 1
2 2 1
3 0
2 2 1
EOF

8 4
12 33 1 77 91 11 22 71
EOF

*/

心路历程(WA,TLE,MLE……)