codeforces 排位赛3

本文介绍了codeforces平台上的三道编程竞赛题目,涉及wormhole排序、赛跑策略和文字处理器的自动换行算法。文章详细分析了每道题目,并给出了代码实现。对于wormhole排序,重点在于找到最小宽度的虫洞;赛跑问题讨论了在不同速度限制下完成赛跑的最短时间;文字处理器部分,解释了如何按照字符限制进行文本换行。
摘要由CSDN通过智能技术生成

传送门

题面

A. Wormhole Sort
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John’s cows have grown tired of his daily request that they sort themselves before leaving the barn each morning. They have just completed their PhDs in quantum physics, and are ready to speed things up a bit.

This morning, as usual, Farmer John’s N cows (1≤N≤105), conveniently numbered 1…N, are scattered throughout the barn at N distinct locations, also numbered 1…N, such that cow i is at location pi. But this morning there are also M wormholes (1≤M≤105), numbered 1…M, where wormhole i bidirectionally connects location ai with location bi, and has a width wi (1≤ai,bi≤N,ai≠bi,1≤wi≤109).

At any point in time, two cows located at opposite ends of a wormhole may choose to simultaneously swap places through the wormhole. The cows must perform such swaps until cow i is at location i for 1≤i≤N.

The cows are not eager to get squished by the wormholes. Help them maximize the width of the least wide wormhole which they must use to sort themselves. It is guaranteed that it is possible for the cows to sort themselves.

Input
The first line contains two integers, N and M.

The second line contains the N integers p1,p2,…,pN. It is guaranteed that p is a permutation of 1…N.
For each i between 1 and M, line i+2 contains the integers ai, bi, and wi.

Output
A single integer: the maximum minimal wormhole width which a cow must squish itself into during the sorting process. If the cows do not need any wormholes to sort themselves, output −1.

Examples
inputCopy
4 4
3 2 1 4
1 2 9
1 3 7
2 3 10
2 4 3
outputCopy
9
inputCopy
4 1
1 2 3 4
4 2 13
outputCopy
-1
Note
The first example is one possible way to sort the cows using only wormholes of width at least 9:

Cow 1 and cow 2 swap positions using the third wormhole. Cow 1 and cow 3 swap positions using the first wormhole. Cow 2 and cow 3 swap positions using the third wormhole.

The second example is no wormholes are needed to sort the cows.

分析

首先要知道,连通块内部相互可达,可以经过虫洞交换
遍历order数组,若某个数与其序号不同,即 order [i] != i
则说明 i 和 order[i] 这两个位置都需要交换,标记一下,并记数
把边按宽度从大到小排序
用并查集维护一个连通块,不断把边加入该连通块,当连通块内有效元素个数==cnt时,该宽度就是最小宽度
其中,根节点的权值代表集合个数,每次合并,把两个根的权值相加,赋给新的根

代码

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

const int INF=1e9;
const int MAX_N=1e5+5;
int order[MAX_N],Y[MAX_N],fa[MAX_N],n,m;

struct edge{
    int x,y,wid; }a[MAX_N];
bool cmp(edge a,edge b){
    return a.wid>b.wid; }

void init(){
    for(int i=1;i<=n;i++) fa[i]=i; }
int get(int x){
    return x==fa[x]? x:fa[x]=get(fa[x]); }

int main()
{
   
	scanf("%d%d",&n,&m); init();			//连通块内部相互可达 可经过虫洞交换  
	int cnt=0,ans=-1;
	for(int i=1;i<=n;i++) {
    
		scanf("%d",&order[i]); 
		if(i!=order[i]) {
   
			if(!Y[i]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值