0%

思路讲解

BIT+频数数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
inline ll query(ll x){
ll idx=li[x];
ll res=0;
// x是在后面的数,查找前面的全部比它大的数
// 当然,直接查找全部比它大的数比较难
// 所以返回的是所有比它<=的数的数量
while(idx>0){
res+=tr[idx];
idx-=lowbit(idx);
}
return res;
}

cnt+=std::max((i-1-query(A[i])),0LL);

AC代码

https://www.luogu.com.cn/record/197653847

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 <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>(5e5)+10;
ll N,T,A[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){
// tr[li[x]]+=1;
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;
}

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]);
}
ll idx=0;
for(std::set<ll>::iterator it=ali.begin();it!=ali.end();it++){
li[*it]=++idx;
}
ll cnt=0;
for(int i=1;i<=N;++i){
cnt+=std::max((i-1-query(A[i])),0LL);
add(A[i]);
}
std::cout<<cnt<<"\n";
return 0;
}
/*
TLE https://www.luogu.com.cn/record/197653726
6
5 4 2 6 3 1
AC https://www.luogu.com.cn/record/197653847
*/

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

TLE https://www.luogu.com.cn/record/197653726

#include <unordered_map> 这个还是比map要快一点,省了300多ms

思路讲解

AC代码

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

思路讲解

答案一定形如跳跳跳到最后一题,然后把剩下的题全做了。

那这个跳到最后一题的过程一定要最小化,所以就是最短路啦

那么要最小化什么那?损失的分数,那么要最小化的就是权,或者说是边权

当然,这个人说的有点瑕疵,起始在跳到最后一题的过程中有两种跳法,一种是跳到B[i],一种是跳到i-1

1
2
3
4
pq.push({B[s],Dis[s]+A[s]});
// 还要考虑往回走的情况
if(s>1)
pq.push({s-1,Dis[s]});

还有一种思路,用线段树或者树状数组

AC代码

https://codeforces.com/contest/2024/submission/300344376

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
#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>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,2> arr;
const ll MAXN=static_cast<ll>(4e5)+10,MAXDIS=1e17+7;
ll N,T,B[MAXN],A[MAXN],sumA[MAXN],Dis[MAXN];
//std::vector<ll> g[MAXN];
struct cmp{
bool operator()(arr a,arr b){
if(a[1]!=b[1]){
return a[1]>b[1];
}
return false;
}
};
bool vis[MAXN];
inline void init(){
for(int i=0;i<=N+7;++i){
vis[i]=false;
// 初始化Dis数组为无限大
Dis[i]=MAXDIS;
sumA[i]=0;
}
Dis[1]=0;
}

void solve(){
std::cin>>N;
init();
for(int i=1;i<=N;++i){
std::cin>>A[i];
sumA[i]=sumA[i-1]+A[i];
}
std::priority_queue<arr,std::vector<arr>, cmp> pq;
for(int i=1;i<=N;++i){
std::cin>>B[i];
}
pq.push({1,0});
while(!pq.empty()){
//0 表示起始点 1 表示该点Dis值
ll s=pq.top()[0],dis=pq.top()[1];
pq.pop();
if(vis[s])
continue;
// 变为已确定最短路径点
vis[s]=true;
Dis[s]=dis;
pq.push({B[s],Dis[s]+A[s]});
// 还要考虑往回走的情况
if(s>1)
pq.push({s-1,Dis[s]});
}
ll ans=0;
for(int i=1;i<=N;++i){
ans=std::max(ans,sumA[i]-Dis[i]);
}
std::cout<<ans<<"\n";
return;
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>T;
while (T--) {
solve();
}
return 0;
}
/*
AC https://codeforces.com/contest/2024/submission/300344376
*/

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

这种一定要加一个限制,避免出现负数,0之类的不存在点进入的情况

1
2
3
// 还要考虑往回走的情况
if(s>1)
pq.push({s-1,Dis[s]});

https://codeforces.com/contest/2024/submission/300344292

这个MAXDIS设太小了,这种设大一点比较好

1
const ll MAXN=static_cast<ll>(4e5)+10,MAXDIS=1e17+7;

思路讲解

?即是有多少个数不参与这个移动

image

当然这个公式只有在特定条件下才能用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(K==0){
if(First>=N){
std::cout<< N<<"\n";
}else if(First>=N-2){
// 不用移动N次,因为N有一个数可以作为不动数
std::cout<< N-1<<"\n";
}else{
std::cout<<-1<<"\n";
}
}else{
if(First>=N){
std::cout<< N<<"\n";
}else{
// res即是上图的?
ll res=(N-First-1)/K+1;
std::cout<< N-res<<"\n";
}
}

AC代码

https://codeforces.com/contest/2028/submission/300282002

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
#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>

typedef long long ll;
typedef std::pair<ll,ll> pll;
const ll MAXN=static_cast<ll>(2e5)+10;
ll N,T,K,First;

void solve(){
// 项数,公差以及首项
std::cin>>N>>K>>First;
if(K==0){
if(First>=N){
std::cout<< N<<"\n";
}else if(First>=N-2){
// 不用移动N次,因为N有一个数可以作为不动数
std::cout<< N-1<<"\n";
}else{
std::cout<<-1<<"\n";
}
}else{
if(First>=N){
std::cout<< N<<"\n";
}else{
ll res=(N-First-1)/K+1;
std::cout<< N-res<<"\n";
}
}
return;
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>T;
while (T--) {
solve();
}
return 0;
}
/*
AC https://codeforces.com/contest/2028/submission/300282002
1
10 10 10

9

1
1 0 0

1
1 1 2

*/

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

思路讲解

我看题解说,注意到,每个块最多被move back一次,那为什么最多被移动一次那?

我个人的理解是因为我们是可以随意选择move back的顺序,所以我们可以让我们的move back自动搞为一个单调的。

其实这个题解主要还是坚定了我的信心,让我知道我做法的时间复杂度是没问题的,我前面想复杂了。

总体上的思路是用数据结构优化模拟法,当然发现加粗(每个块最多被move back一次)是非常重要的,否则很容易想复杂。

核心是这段程序

1
2
3
4
5
6
7
8
9
10
11
12
13
while (!pq.empty()) {
pll temp=pq.top();
pq.pop();
if(temp.second>needBack){
// 我们发现这个不要后移,那就不后移
Ans[++idx]=temp.first;
needBack=temp.second;
}else{
// 如果我们发现这个要后移,那我们就模拟后移
pq.push({temp.first+1,N+back});
back+=1;
}
}

AC代码

https://codeforces.com/contest/2047/submission/300222017

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
#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>

typedef long long ll;
typedef std::pair<ll,ll> pll;
const ll MAXN=static_cast<ll>(1e5)+10;
ll N,T,A[MAXN],Ans[MAXN];
struct cmp{
bool operator()(pll a,pll b){
if(a.first!=b.first){
return a.first>b.first;
}
if(a.second!=b.second){
return a.second>b.second;
}
return false;
}
};

void solve(){
std::cin>>N;
// 里面装的东西,容器,以及比较规则
std::priority_queue<pll,std::vector<pll>,cmp > pq;
// needBack指的是这之前的数都要往后移
ll needBack=0;
for(int i=1;i<=N;++i){
std::cin>>A[i];
pq.push({A[i],i});
}
// back是指被移到后面的哪个位置(即移到原来的N+back)
ll idx=0,back=1;
// 每一个位置最多被移动一次,所以复杂度不会太高。
while (!pq.empty()) {
pll temp=pq.top();
pq.pop();
if(temp.second>needBack){
Ans[++idx]=temp.first;
needBack=temp.second;
}else{
pq.push({temp.first+1,N+back});
back+=1;
}
}
for(int i=1;i<=N;++i){
std::cout<<Ans[i]<<" ";
}
std::cout<<"\n";
return;
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>T;
while (T--) {
solve();
}
return 0;
}

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