Codeforces Round 974 (Div. 3) D,H,G

D. Robert Hood and Mrs Hood

给定m个区间,q个查询,每次给出一个区间,求给定区间和m个区间的交集个数。

使用前缀和预处理,记录以左端点开头的和以右端点结尾的区间个数即可。

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 5;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef array<ll, 4> ar;
typedef array<ll, 3> a3;
int mod = 1e9+7;
const int maxv = 4e6 + 5;
// #define endl "\n"


void solve()
{
	int n,d,k;
	cin>>n>>d>>k;
	vector<pll> seg(k);
	for(auto &[l,r]: seg) cin>>l>>r;
	// sort(seg.begin(),seg.end());
	vector<int> l(n+5),r(n+5);
	for(auto &[a,b] :seg) l[a]++,r[b]++;
	for(int i=1;i<=n;i++) l[i]+=l[i-1],r[i]+=r[i-1];
	int mx=0,mn=1e9;
	int pos1=0,pos2=0;
	for(int i=1;i+d-1<=n;i++){
		int st=i,ed=i+d-1;
		int res=l[ed]-r[st-1];
		if(res>mx){
			mx=res,pos1=i;
		}
		if(res<mn) mn=res,pos2=i;
	}
	cout<<pos1<<" "<<pos2<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	t=1;
	cin>>t;
	while(t--){
		solve();
	}
    system("pause");
    return 0;
}

E. Rendez-vous de Marian et Robin

分层图,对于这种同时出发相遇求最小时间的跑两边最短路,然后枚举相遇点即可。

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;
const int N = 5e5 + 5;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef array<ll, 4> ar;
typedef array<ll, 3> a3;
int mod = 1e9+7;
const int maxv = 4e6 + 5;
#define endl "\n"


vector<pll> e[N];

void add(ll u,ll v,ll w)
{
	e[u].push_back({v,w});
}

int n,m,h;
ll ds[N],dt[N];

void dijk(int s,int t)
{
	priority_queue<pll,vector<pll>,greater<pll> > q;
	q.push({0,s});
	// memset(ds,0x3f,sizeof ds);
	for(int i=1;i<=2*n;i++) ds[i]=1e18;
	ds[s]=0;
	vector<int> st(2*n+5);
	while(!q.empty()){
		auto [dis,u]=q.top();
		q.pop();
		if(st[u]) continue;
		st[u]=1;
		for(auto [v,w] :e[u]){
			if(ds[v]>ds[u]+w){
				ds[v]=ds[u]+w;
				q.push({ds[v],v});
			}
		}
	}
}

void dijk1(int s,int t)
{
	priority_queue<pll,vector<pll>,greater<pll> > q;
	q.push({0,s});
	// memset(dt,0x3f,sizeof dt);
	for(int i=1;i<=2*n;i++) dt[i]=1e18;
	dt[s]=0;
	vector<int> st(2*n+5);
	while(!q.empty()){
		auto [dis,u]=q.top();
		q.pop();
		if(st[u]) continue;
		st[u]=1;
		for(auto [v,w] :e[u]){
			if(dt[v]>dt[u]+w){
				dt[v]=dt[u]+w;
				q.push({dt[v],v});
			}
		}
	}
}

void solve()
{
	cin>>n>>m>>h;
	vector<int> s(n+5);
	for(int i=1;i<=2*n;i++) e[i].clear();
	for(int i=0;i<h;i++){
		int x;
		cin>>x;
		s[x]=1;
	}
	for(int i=1;i<=n;i++){
		if(s[i]) add(i,i+n,0),add(i+n,i,0);
		else add(i,i+n,1e18),add(i+n,i,0);
	}
	for(int i=1;i<=m;i++){
		ll u,v,w;
		cin>>u>>v>>w;
		add(u,v,w),add(v,u,w);
		add(u+n,v+n,w/2),add(v+n,u+n,w/2);
	}
	dijk(1,n),dijk1(n,1);
	ll ans=1e18;
	for(int i=1;i<=n;i++){
		// if(i==1){
		// 	ans=min(ans,dt[i]);
		// 	continue;
		// }
		// if(i==n){
		// 	ans=min(ans,ds[i]);
		// 	continue;
		// }
		if(ds[i]<1e18&&dt[i]<1e18){
			ans=min(ans,max(ds[i],dt[i]));
		}
	}
	if(ans>=1e17) cout<<-1<<endl;
	else cout<<ans<<endl;


}

int main()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	t=1;
	cin>>t;
	while(t--){
		solve();
	}
    system("pause");
    return 0;
}

H. Robin Hood Archery

本质上是求区间内数的出现次数是否均为偶数次,莫队即可。

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;
const int N = 5e5 + 5;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef array<ll, 4> ar;
typedef array<ll, 3> a3;
int mod = 1e9+7;
const int maxv = 4e6 + 5;
#define endl "\n"

int sq;
struct query // 把询问以结构体方式保存
{
    int l, r, id;
    bool operator<(const query &o) const // 重载<运算符,奇偶化排序
    {
        // 这里只需要知道每个元素归属哪个块,而块的大小都是sqrt(n),所以可以直接用l/sq
        if (l / sq != o.l / sq) 
            return l < o.l;
        if (l / sq & 1)
            return r < o.r;
        return r > o.r;
    }
} Q[N];
int a[N], ans[N], cnt[maxv], cur, l = 1, r = 0,res;
inline void add(int p)
{
    if (cnt[a[p]] == 0)
        cur++;
    cnt[a[p]]++;
	if(cnt[a[p]]%2==0) res++;
	else res--;
}
inline void del(int p)
{
    cnt[a[p]]--;
    if (cnt[a[p]] == 0)
        cur--;
	if(cnt[a[p]]%2==0) res++;
	else res--;
}
void solve()
{
	int n,q;
	res=cur=0;
	l=1,r=0;
	cin>>n>>q;
    sq = sqrt(n);
    for (int i = 1; i <= n; ++i)
        cin>>a[i],cnt[a[i]]=0;
    for (int i = 0; i < q; ++i)
        cin>>Q[i].l>>Q[i].r,Q[i].id=i;
    sort(Q, Q + q); // 排序
    for (int i = 0; i < q; ++i)
    {
        while (l > Q[i].l)
            add(--l);
        while (r < Q[i].r)
            add(++r);
        while (l < Q[i].l)
            del(l++);
        while (r > Q[i].r)
            del(r--);
        ans[Q[i].id] = res; // 储存答案
    }
    for (int i = 0; i < q; ++i)
        if(ans[i]==0) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	t=1;
	cin>>t;
	while(t--){
		solve();
	}
    system("pause");
    return 0;
}
相关推荐
科研小白_d.s3 分钟前
冒泡排序原理及python代码
数据结构·python·算法
可别是个可爱鬼4 分钟前
代码随想录 -- 回溯 -- 子集
数据结构·算法·leetcode
水木流年追梦1 小时前
【shell脚本5】Shell脚本学习--条件控制
人工智能·python·深度学习·学习·算法·机器学习
且行且知1 小时前
`#include <vector>`
c++·算法
saberyydsicloud2 小时前
C++(9.24)
开发语言·c++·算法
重生之我是数学王子2 小时前
Leetcode 1039. 多边形三角形剖分的最低得分 枚举型区间dp C++实现
c++·算法·leetcode·深度优先·动态规划
Ciderw2 小时前
leetcode155.最小栈,两个栈
数据结构·c++·算法·leetcode·面试·职场和发展
大二转专业2 小时前
408算法题leetcode--第11天
考研·算法·leetcode
Ciderw2 小时前
LeetCode 257. 二叉树的所有路径,dfs
数据结构·c++·算法·leetcode·面试·深度优先
tjl521314_212 小时前
L3 逻辑回归
算法·机器学习·逻辑回归