5.14~5.18

逆序对

最简单的求逆序对的方法:冒泡排序,时间复杂度$ O(n^2) $

直接给无优化的代码,不解释了:

#include <bits/stdc++.h>
using namespace std;

int a[10] = {3,5,7,9,2,1,4,6,8,10};

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n = 10, cnt = 0;
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            if(a[j] < a[i]){
                swap(a[i],a[j]);
                cnt++;
            }
    cout << cnt << '\n';
    return 0;
}

更好的求法:树状数组(逆序对+离散化)

也是好久没有接触树状数组了,最近一次出现树状数组还是德叔在重庆赛上秒H题

用树状数组求逆序对的个数,需要用到一个技巧:把数字看成树状数组的下标

例如,序列{5,4,2,6,3,1},对应a[5]、a[4]、a[2]、a[6]、a[3]、a[1]

每处理一个数字,树状数组下标对应的元素数值加1,统计前缀和,就是逆序对的数量

倒序处理和正序处理都可以,我一般用倒序处理

倒序:

用树状数组倒叙处理序列,当前数字的前一个数的前缀和,即为以该数为较大数的逆序对的个数。

例如,还是序列{5,2,4,6,3,1},倒叙处理:

数字1,把a[1]+1,计算a[1]的前缀和sum(0),逆序对数量ans += sum(1-1) = 0

数字3,把a[3]+1,计算a[3]的前缀和sum(2),逆序对数量ans += sum(3-1) = 1

数字6,把a[6]+1,计算a[6]的前缀和sum(5),逆序对数量ans += sum(6-1) = 3

......

此外,还需要用到离散化来处理空间问题。

离散化就是把原来的数字用它们的相对大小替代,而它们的顺序仍然不变,不影响逆序对的计算。例如,{1,20000,10,30,890000000},离散化后变为 {1,4,2,3,5},新旧序列的逆序对数量是一样的

以一道模板题为例,洛谷P1908

#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
using namespace std;
using ll = long long;
const int maxn = 5e5+5;

int tree[maxn],dc[maxn],n;
struct node{
	int val,index;
}a[maxn];

bool cpa(node x,node y){
	if(x.val == y.val) return x.index < y.index;
	return x.val < y.val;
}

void update(int x,int d){
	while(x <= n){
		tree[x] += d;
		x += lowbit(x);
	}
}

int sum(int x){
	int ans = 0;
	while(x > 0){
		ans += tree[x];
		x -= lowbit(x);
	}
	return ans;
}

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> a[i].val;
		a[i].index = i;
	}
	sort(a+1,a+n+1,cpa);
	for(int i=1;i<=n;i++) dc[a[i].index] = i;
	ll cnt = 0;
	for(int i=n;i>=1;i--){
		update(dc[i],1);
		cnt += sum(dc[i]-1);
	}
	cout << cnt << '\n';
	return 0;
}

牛客周赛91

E

开始看错题了,以为可以无限次操作,那难度就飙升了

后来发现只有一次操作,那没事了,不过考代码细节

三种情况:

1.全为0,ok

2.只有某两行或某两列为1,其它都为0

3.某个中心为0,以它为中心的十字架上的字符都为1

可以通过讨论字符1的个数来做,待续

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3+3;

char a[maxn][maxn];

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t; cin >> t;
	while(t--){
		int n,m,row[maxn]={0},col[maxn]={0}; cin >> n >> m;
		memset(row,0,sizeof(row));
		memset(col,0,sizeof(col));
		bool cd1 = 1, pd = 1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin >> a[i][j];
				if(cd1 && a[i][j]-'0') cd1 = 0;
			}
		}
		if(cd1) {cout << "YES\n"; continue;}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(a[i][j] == '1') row[i]++;
			}
		}
		for(int j=1;j<=m;j++){
			for(int i=1;i<=n;i++){
				if(a[i][j] == '1') col[j]++;
			}
		}
		int hang=0,lie=0;
		for(int i=1;i<=n;i++){
			if(row[i] == m) hang++;
			if(row[i]!=0 && row[i]!=m) {hang=0;break;}
		}
		if(hang == 2) {cout << "YES\n";continue;}
		for(int j=1;j<=m;j++){
			if(col[j] == n) lie++;
			if(col[j] != n && col[j] != 0) {lie=0;break;}
		}
		if(lie == 2) {cout << "YES\n";continue;}
		for(int i=1;i<=n && pd;i++)
			for(int j=1;j<=m;j++)
				if(a[i][j] == '0' && row[i]==m-1 && col[j]==n-1){
					cout << "YES\n";
					pd = 0;
					break;
				}
		if(pd) cout << "NO\n";
	}
	return 0;
}

D

相当于求连续段的个数,注意去重,还有边界特判(恼火*******)

写完报错半天看不出来错哪,查了才发现用&[x,y]遍历map是C++17的特性*******

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;

map <int,int> ma;

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t; cin >> t;
	while(t--){
		ma.clear();
		int n; cin >> n;
		for(int i=1;i<=n;i++){
			int c; cin >> c;
			ma[c]++;
		}
		int cnt = 0;
		for(auto &it:ma){
			if(!ma.count(it.first-1)){
				cnt++;
				if(!ma.count(it.first+1))
					cnt += it.second-1;
			}
		}
		cout << cnt-1 << '\n';
	}
	return 0;
}

C

求最大逆序对之和

贪心维护最大前缀值,然后倒序判断

为什么这样做?

从最后一个数开始倒序处理,当前数(现在是最后一个数)作为逆序对(x,y)的右数y,要使得逆序对之和最大,那么左数x就要尽可能大

因此要维护最大前缀值,这个最大前缀值mp[i]就是[1,i]内的最大数,用来作为左数

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e5+5;

int a[maxn],mp[maxn];

signed main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t; cin >> t;
	while(t--){
		memset(a,0,sizeof(a));
		memset(mp,0,sizeof(mp));
		int n; cin >> n;
		for(int i=1;i<=n;i++){
			cin >> a[i];
			mp[i] = max(mp[i-1],a[i]);
		}
		int ans = 0;
		for(int i=n;i>=1;i--)
			if(mp[i-1] > a[i])
				ans = max(ans,mp[i-1]+a[i]);
		cout << ans << '\n';
	}
	return 0;
}

A、B

签到

#include <bits/stdc++.h>
using namespace std;

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	string t = "while",s;
	cin >> s;
	int ans = 0;
	for(int i=0;i<s.size();i++)
		if(t[i] != s[i]) ans++;
	cout << ans << '\n';
	return 0;
}
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5+5;
int a[maxn]{},sum[maxn];

signed main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n;
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		sum[i] = sum[i-1] + a[i];
	}
	int ans = 0;
	for(int i=1;i<=min(10LL,n);i++)
		ans = max(ans,sum[i]);
	for(int i=11;i<=n;i++)
		ans = max(ans,sum[i]-sum[i-10]);
	cout << ans << '\n';
	return 0;
}
posted @ 2025-06-14 12:29  HLAIA  阅读(11)  评论(0)    收藏  举报