codeforces 886 G 、H
G
当斜率为1时,我们可以推导出x1 + y1 = x2 + y2; 斜率为-1时我们可以推导出x1 - y1 = x2 -y2;这样我我们可以使用map来记录一下x+y 和x - y和x和y
如果有三个点,答案就是3+2+1 再 ×2,四个点就是 4+3+2+1,再×2
#include <bits/stdc++.h>
using namespace std;
using ll =long long;
const int N =2e5+9;
int x[N] ,y[N];
map<int,int>mpx,mpy,mp1,mp2;
ll jie(int x){
ll res =0;
x -=1;
while(x ){
res +=x;
x -=1;
}
return res;
}
void solve(){
mpx.clear();
mpy.clear();
mp1.clear();
mp2.clear();
int n;cin>>n;
ll ans =0;
for(int i=1;i<=n;++i){
int x , y;cin>>x>>y;
mpx[x]++;
mpy[y]++;
mp1[y-x]++;
mp2[x+y]++;
}
for(auto it : mpx) if(it.second >=2)ans +=jie(it.second);
for(auto it : mpy)if(it.second >=2)ans +=jie(it.second);
for(auto it : mp1)if(it.second >=2)ans +=jie(it.second);
for(auto it : mp2)if(it.second >=2)ans +=jie(it.second);
cout<<ans*2<<'\n';
}
int main(){
int _;cin>>_;
while(_--)solve();
return 0;
}
H(带权并查集)
这题可以使用带权并查集来做
带权并查集的合并,要同时维护dis数组
void emerge(int x , int y,int val){
int tx =root(x) , ty = root(y);
if(tx == ty)return;
pre[tx] = ty;
dis[tx] = dis[y] + val -dis[x];
}
根的查找
int root(int x){
if(x != pre[x]){
int t =root(pre[x]);
dis[x] +=dis[pre[x]]'
pre[x] = t;
return pre[x];
}
return x;
}
整体的代码就是
#include <bits/stdc++.h>
using namespace std;
using ll =long long;
const int N =4e5+9;
ll pre[N] , dis[N];
ll root(ll x){
if(x != pre[x]){
int t = root(pre[x]);
dis[x] += dis[pre[x]];
pre[x] =t;
return pre[x];
}
return x;
}
void merge(int x ,int y ,int d){
ll fx = root(x) ,fy = root(y);
pre[fx] = fy;
dis[fx] = d+dis[y] -dis[x];
}
void solve(){
int n,m;cin>>n>>m;
bool f =true;
for(int i =1;i<=n;++i){
pre[i] =i;
dis[i] =0;
}
while(m--){
ll a,b,c;
cin>>a>>b>>c;
if(root(a) != root(b)){
merge(a , b ,c);
}
else if(f && dis[a] - dis[b] != c){
f =false;
cout<<"NO"<<'\n';
}
}
if(f)cout<<"YES"<<'\n';
}
int main(){
ios::sync_with_stdio(),cout.tie(0),cin.tie(0);
int _;cin>>_;
while(_--)solve();
return 0;
}
并查集两练
1、修复公路(并查集 ,贪心)
传送门:P1111 修复公路 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
这道题首先使用结构体 , 并且对时间从小到大进行排序,然后先从排完序后的第一个开始,如果两个村庄之间没通路,就把他们合并,然后村庄数减一,直到最终村庄数等于1,我们输出答案
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
int pre[N];
int n,m;
struct cun{
int x, y ,t;
}a[N];
int cmp(cun a , cun b){
return a.t < b.t;
}
int root(int x){
return pre[x] =(pre[x] == x ? x : root(pre[x]));
}
void merge(int x ,int y){
x = root(x) , y =root(y);
if(x == y)return;
pre[x] =y;
}
int main(){
ios::sync_with_stdio(),cout.tie(0),cin.tie(0);
cin>>n>>m;
for(int i =1;i<=n;++i)pre[i] = i;
for(int i =1;i<=m;++i){
cin>>a[i].x>>a[i].y>>a[i].t;
}
sort(a+1 , a+1+m , cmp);
int cnt = n;
for(int i =1;i<=m;++i){
if(root(a[i].x) != root(a[i].y)){
cnt--;
merge(a[i].x , a[i].y);
}
if(cnt == 1){
cout<<a[i].t;
return 0;
}
}
cout<<-1;
return 0;
}
2、关押罪犯(并查集,贪心,二分)
这是我写过的第一道绿题,在此记录。
这题采用贪心的做法,把怒气值最大的放在前面,然后开一个数组用来存放敌人,我们要把敌对的人分开,每一个监狱用一个并查集记录犯人的关系,如果当遍历到某个敌对的人,将他们分开时,他们 的其中一个,跟监狱里的人有关系,那么就得到答案了
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int q[N] , pre[N];
struct p{
int x , y , z;
}s[N];
int cmp(p x , p y){
return x.z > y.z;
}
int root(int x){
return pre[x] =(pre[x] == x ? x:root(pre[x]));
}
void merge (int x ,int y){
x = root(x) , y = root(y);
if(x == y)return;
pre[x] = y;
}
int main(){
ios::sync_with_stdio(),cout.tie(0),cin.tie(0);
int n,m;cin>>n>>m;
for(int i =1;i<=n;++i)pre[i] = i;
for(int i =1;i<=m;++i)cin>>s[i].x>>s[i].y>>s[i].z;
sort(s+1 , s+1+m , cmp);
for(int i =1;i<=m;++i){
if(root(s[i].x) == root(s[i].y)){
cout<<s[i].z;
return 0;
}
if(!q[s[i].x])q[s[i].x] =s[i].y;
else merge(q[s[i].x] , s[i].y);
if(!q[s[i].y])q[s[i].y] = s[i].x;
else merge(q[s[i].y] , s[i].x);
}
cout<<0;
return 0;
}