title: 261107周六上午 - 排序杂题 author: wu_sir date: 2026-11-07 organization: 河源高级中学 header: title————{title} ———— {author}(organization)footer:起始于:{organization}) footer: 起始于:{date} - - - - - - 第 pageNo页/共{pageNo} 页 / 共 {pageCount} 页 - - - - - - 终止于:${today} keywords:

  • 排序

排序杂题

#C1132. 修理牛棚

code
const int N = 2e5 + 9, inf = 1e9 + 9;
int a[N];

void solved() {
	int n, m;	cin >> n >> m;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	sort(a + 1, a + n + 1);
	int s = a[n] - a[1] + 1;	// 用一块木板时候的总长
	for(int i = 1; i <= n - 1; i ++) a[i] = a[i + 1] - a[i] - 1;
	sort(a + 1, a + n - 1 + 1);
	for(int i = 1; i <= min(n - 1, m - 1); i ++) s -= a[n - i];
	cout << s << endl;
}

#C1149. 重排成为最小字符串 [NOIP2023]

code
const int N = 2e3 + 9, inf = 1e9 + 9;
string s[N], f[N];	 // s[i] 表示字符串 i 的最小重排字典序,f[i] 表示最大重排字典序
string pre[N], nex[N];

void solved() {
	int n;	cin >> n;
	for(int i = 1; i <= n; i ++) {
		cin >> s[i];
		sort(s[i].begin(), s[i].end());
		f[i] = s[i];
		reverse(f[i].begin(), f[i].end());
	}
	pre[1] = f[1];
	for(int i = 2; i <= n; i ++) pre[i] = min(pre[i - 1], f[i]);
	nex[n] = f[n];
	for(int i = n - 1; i >= 1; i --) nex[i] = min(nex[i + 1], f[i]);
	int cnt = 0;
	for(int i = 1; i <= n; i ++) {
		if(i != 1 && s[i] >= pre[i - 1]) continue;
		if(i != n && s[i] >= nex[i + 1]) continue;
		cnt ++;
	}
	cout << cnt << endl;
}

#C1151. 二排列交换统计

code
const int N = 2e5 + 9, inf = 1e9 + 9;
int a[N], b[N], p[N];
bool vis[N];

void solved() {
	int n;	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	for(int i = 1; i <= n; i ++) cin >> b[i];
	for(int i = 1; i <= n; i ++) p[a[i]] = b[i];
	int ans = 1;
	for(int i = 1; i <= n; i ++) {
		if(vis[i] == true) continue;
		ans = ans * 2 % 998244353;
		int now = i;
		// vis[now] = true;
		while(vis[p[now]] == false) {
			now = p[now];
			vis[now] = true;
		}
	}
	cout << ans << endl;
	for(int i = 1; i <= n; i ++) vis[i] = false;
}

#C1156. 最近的一对相同元素

code
const int N = 2e5 + 9, inf = 1e9 + 9;
struct node {
	int w, id;
} no[N];

bool operator< (node a, node b) {
	if(a.w != b.w) return a.w < b.w;
	return a.id < b.id;
}
void solved() {
	int n;	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> no[i].w, no[i].id = i;
	sort(no + 1, no + n + 1);
	int d = inf, ans = -1;
	for(int i = 1; i <= n - 1; i ++) {
		if(no[i].w != no[i + 1].w) continue;
		int now = no[i + 1].id - no[i].id;
		if(now < d) d = now, ans = no[i].w;
	}
	if(ans == -1) cout << "No Result" << endl;
	else cout << ans << endl;
}

#C1161. 五照片重新定位

code
const int N = 4E5 + 9, inf = 1e9 + 9;
int a[N], p[6][N];

bool cmp(int x, int y) {
	int cnt = 0;
	for(int i = 1; i <= 5; i ++) cnt += p[i][x] < p[i][y];
	return cnt >= 3;
}
void solved() {
	int n;	cin >> n;
	for(int i = 1; i <= 5; i ++) {
		for(int j = 1; j <= n; j ++) cin >> a[j], p[i][a[j]] = j;
	}
	// for(int i = 1; i <= n; i ++) a[i] = i;
	sort(a + 1, a + n + 1, cmp);
	for(int i = 1; i <= n; i ++) cout << a[i] << ' ';
	cout << endl;
}

0 条评论

目前还没有评论...