2024牛客多校4

G.Horse Drinks Water

题意:
给定两个点坐标,求一个点到轴,再到另外一点的最短距离。

题解:
就是经典的将军饮马问题。一个点的对称点,与另外一个点相连。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef __int128 i128;
typedef long long ll;
typedef double db;

const db PI = acos(-1);
typedef array<ll, 2> PII; // vector<PII> a(n+1);
const ll inf = 2e18 + 10;
const int mod = 998244353;
const int MAX = 2e5 + 10;
bool multi = 1;

void Solve() {
    db x1, x2, y1, y2; scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
    db ans1 = sqrt((x1 + x2) * (x1 + x2) + abs(y1 - y2) * abs(y1 - y2));
    db ans2 = sqrt(abs(x1 - x2) * abs((x1 - x2)) + (y1 + y2) * (y1 + y2)); 
    printf("%.10lf\n", min(ans1, ans2));    
}


signed main() {
//     ios::sync_with_stdio(false);
//     cin.tie(0); cout.tie(0);
    ll T = 1;
    if(multi) cin >> T;
    while(T -- ) {
        Solve();
    }
    return 0;
}

H.Yet Another Origami Problem

题意:
给定一个数组,可以有两种操作
· 选一个元素 a p a_p ap,使所有 a i < a p a_i < a_p ai<ap的元素变为 a i + 2 ( a p − a i ) a_i + 2(a_p-a_i) ai+2(apai)
· 选一个元素 a p a_p ap,使所有 a i > a p a_i > a_p ai>ap的元素变为 a i − 2 ( a p − a i ) a_i - 2(a_p-a_i) ai2(apai)
不限操作次数,问最后的极值差是多少。

题解:
排序后,求相邻元素差的gcd就是答案,n=1特判。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef __int128 i128;
typedef long long ll;
typedef double db;

const db PI = acos(-1);
typedef array<ll, 2> PII; // vector<PII> a(n+1);
const ll inf = 2e18 + 10;
const int mod = 998244353;
const int MAX = 2e5 + 10;
bool multi = 1;

void Solve() {
    ll n; cin >> n;
    vector<ll> a(n + 1);
    for(ll i = 1; i <= n; i ++ ) cin >> a[i];
    sort(a.begin() + 1, a.end());
    ll g = 0;
    for(ll i = 1; i < n; i ++ ) {
        if(g == 0) g = a[i + 1] - a[i];
        else g = __gcd(g, a[i + 1] - a[i]); 
    }
    cout << g << "\n";
}


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    ll T = 1;
    if(multi) cin >> T;
    while(T -- ) {
        Solve();
    }
    return 0;
}

I.Friends

题意:
n个人,有m对好朋友,假如 [ l , r ] [l,r] [l,r]中相互都是好朋友,那 [ l , r ] [l,r] [l,r]就是好区间。
求好区间的个数.

题解:
如果 [ l , r ] [l,r] [l,r]是好区间,那要第 r + 1 r+1 r+1个人和 [ l , r ] [l,r] [l,r]的人都是好朋友, [ l , r + 1 ] [l,r+1] [l,r+1]才是好区间。如果在其中一个人断了,那 l l l就从断开的下一个人开始。
时间给了3s,m对好朋友,暴力检验就可以了.

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e6+6;
pair<int,int>a[N];
map<pair<int,int>,int> mp;
int p[N];

void solve()
{
	int n,m,x,y,u,v;
	cin >> n>>m;
	for(int i=1;i<=n;i++){
		p[i]=i;
	}
	
	for(int i=1;i<=m;i++){
		cin>>u>>v;
		if(u>v)swap(u,v);
		mp[{u,v}]=1;
		a[i]={u,v};
	}
	int l,r,ans=0;
	l=1,r=1;
	for(int i=2;i<=n;i++)
	{
		int f=1,x=l,y=r;
		for(int j=x;j<=y;j++){
			if(!mp[{p[j],i}]){
				f=0;
				l=j+1;
				r=i;
			}
		}
		r=i;
			ans+=(r-l);
		
	}
	
	cout<<ans+n<<'\n';
}

signed main()
{
    int T=1;

//    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

C.Sort4

题意:
给定一个序列,每次可以最多选 4 4 4个元素随意交换,问把序列排序好的最少操作次数。

题解:
一个被打乱的序列,把 i i i -> p i p_i pi作为边,一定会有多个环。
一个len=3,4的环可以一次排好
两个 len = 2的环可以一次排好
对于len> 4的环,每次操作可以排好其中三个元素,环长变为− 3,因此记录有多少个环最终长度为 2,将其两两合并处理即可。

代码:

#include<iostream>
#include<cstring>
using namespace std;
const int N=1e6+10;
int a[N],vis[N];
int bfs(int u){
    if(vis[u]) return 0;
    vis[u]=1;
    return bfs(a[u])+1;
}
void solve(){
    int n;cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        vis[i]=0;
    }
    int ans=0,s1=0;
    for(int i=1;i<=n;i++){
        int len=bfs(a[i]);
        ans+=len/3;
        if(len%3==2) s1++;
    }
    ans+=s1+1>>1;
    cout<<ans<<endl;
}
int main(){
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

A.LCT

题意:
给定一颗有根树,每次询问前 i i i条边组成的森林中,以节点 c c c为根的树的深度。每次询问的节点 c c c都是根节点。

题解:
每个节点都要一个深度,当连接到u,v时。
f ( r o o t u ) = m a x ( f ( r o o t u ) , d e e p u + f ( v ) + 1 ) f(root_u)=max (f(root_u),deep_u+f(v)+1) f(rootu)=max(f(rootu),deepu+f(v)+1)
用并查集维护每个节点的根节点。
先把 n − 1 n-1 n1条边记录下来,知道最后每个节点最后的深度。 每次加边后,更新节点的高度,那高度-深度就是答案。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+6;

int d[N],h[N];
int a[N],b[N],cc[N],fa[N];

vector <int>g[N];

void dfs(int u,int pre)
{
	d[u]=d[pre]+1;
	for(auto i:g[u]){
		if(i==pre)continue;
		dfs(i,u);
	}
}

int find(int x){
	if(x==fa[x])return x;
	return fa[x]=find(fa[x]);
}
void solve()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
	for(int i=1;i<n;i++){
		cin>>a[i]>>b[i]>>cc[i];
		g[a[i]].push_back(b[i]);
		g[b[i]].push_back(a[i]);
	}
	dfs(cc[n-1],0);
	for(int i=1;i<=n;i++)h[i]=d[i];
	
	
	int u,v,c,x,y;
	for(int i=1;i<n;i++){
		u=a[i],v=b[i],c=cc[i];
		x=find(u);
		y=find(v);
		fa[y]=fa[x];
		h[x]=max(h[x],h[y]);
		
		if(i!=1)cout<<' ';
		cout<<h[c]-d[c];
	}
	for(int i=1;i<=n;i++)g[i].clear();
	cout<<'\n';
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值