首页 > 其他分享 >Codeforces Round 886 (Div. 4) D - H

Codeforces Round 886 (Div. 4) D - H

时间:2023-07-25 20:14:20浏览次数:48  
标签:vector 886 int void Codeforces num Div find dis

D. Balanced Round

Problem - D - Codeforces

双指针,贪心

题意:

​ 给出\(n\)个数,我们可以删去其中若干个数,使得删完后的数重新排列任意相邻的数之差绝对值不超过\(k\),输出最小删去数的个数

思路:

​ 很明显可以先将给出的数组进行排序。对于排序后的数组,我们可以将其分为若干个相邻差绝对值不超过\(k\)的子数组,对于每个子数组,很明显我们只能留下其中一个。考虑贪心选取其中长度最大的,若长度为\(len\),则答案为\(n - len\)。

void QAQ(){
    int n, k;
    cin >> n >> k;
    vector<int> num(n + 1);
    for(int i = 1; i <= n; i ++){
    	cin >> num[i];
	}
	sort(num.begin() + 1, num.end());
	int ans = 1, l = 1, r = 1;
	for(int i = 2; i <= n; i ++){
		if(abs(num[i] - num[i - 1]) <= k) r ++;
		else{
			ans = max(ans, r - l + 1);
			l = r + 1;
			r = l;
		}
	}
	ans = max(ans, r - l + 1);
	cout << n - ans << endl;
}

E. Cardboard for Pictures

Problem - E - Codeforces

题意:

​ 有\(n\)幅照片,每幅图片都裱在一块正方形硬纸板上,这样每幅图片的四边都有 \(w\)厘米的硬纸板边框。他总共用了\(c\)平方厘米的硬纸板。已知图片尺寸和数值\(c\),求出恰好满足要求的\(w\)

思路:

​ 二分查找答案。

​ 数据范围大要开__int128

void QAQ(){
    ll n, c;
	cin >> n >> c;
    vector<ll> num(n + 1);
	for(int i = 1; i <= n; i ++){
		cin >> num[i];
	}
	int l = 0, r = 1e9 + 10;
	auto check =[&] (int x) -> bool {
		int cnt = 0;
		for(int i = 1; i <= n; i ++){
			cnt += (num[i] + x * 2) * (num[i] + x * 2); 
			if(cnt >= c) return false;
		}
		return true;
	};
	while(l + 1 < r){
		int mid = l + r >> 1;
		if(check(mid)) l = mid;
		else r = mid;
	}
	cout << (ll)r << endl; 
}

F. We Were Both Children

Problem - F - Codeforces

题意:

​ 有\(n\)只青蛙,第\(i\)只青蛙每次跳\(a_i\)距离,我们可以挑选一个位置放置陷阱,使其能捕捉到的青蛙个数最多

思路:

​ 直接暴力统计每个青蛙会跳过的位置,取max即可,注意去重否则会t掉

void QAQ(){
	int n;
    cin >> n;
	vector<int> num(n + 1);
	map<int, int> mp;
	int ans = 0;
	for(int i = 1; i <= n; i ++){
		cin >> num[i];
	}
	sort(num.begin(), num.end());
	int k = 1;
	while(k <= n){
		int cnt = 1;
		while(k < n && num[k] == num[k + 1]){
			k ++;
			cnt ++;
		}
		for(int j = num[k]; j <= n; j += num[k]){
			if(j <= n){
				mp[j] += cnt;
				ans = max(ans, mp[j]);
			}
		}
		k ++;
	}
	cout << ans << endl;
}

G. The Morning Star

Problem - G - Codeforces

题意:

​ 有\(n\)个指针,放在不同位置,可以指向上下左右,左上,左下,右上,右下,问能找出多少对指针互相指向彼此

思路:

​ 分别用多个map记录x,y,x + y,x - y(分别代表上下,左右,左上右下,左下右上),对每个点记录求和即可。

void QAQ(){
	int n;
	cin >> n;
	map<int, int> dia, inv, xx, yy;
	vector<pll> s;
	for(int i = 1; i <= n; i ++){
		int a, b;
		cin >> a >> b;
		s.push_back({a, b});
		xx[a] ++;
		yy[b] ++;
		dia[a - b] ++;
		inv[a + b] ++;
	}
	int ans = 0;
	for(auto it : s){
		int x, y;
		tie(x, y) = it;
		ans += xx[x] - 1;
		ans += yy[y] - 1;
		ans += dia[x - y] - 1;
		ans += inv[x + y] - 1;  
	}
	cout << ans << endl;
	
}

H. The Third Letter

Problem - H - Codeforces

题意:

​ 共有\(n\)个人,给出\(m\)组距离关系,\(a_i,b_i,d_i\),若\(d_i\)大于\(0\)代表\(a_i\)在\(b_i\)前\(d_i\)距离的位置,小于\(0\)同理,问是否所有关系都能被满足。

思路:

​ 有两种不同思路,一种是利用带权并查集记录两点间的关系,在线查询和合并;第二种是建图,对每个连通块dfs查看是否存在距离冲突。

带权并查集

void QAQ(){
    int n, m;
    cin >> n >> m;
    vector<int> fa(n + 1), dis(n + 1);
    iota(fa.begin(), fa.end(), 0);
//===========================================================================
    auto find = [&] (auto find, int x) -> int{
        // cerr << x << " " << fa[x] << endl;
        if(fa[x] != x){
            int t = find(find, fa[x]);
            dis[x] = dis[fa[x]] + dis[x];
            fa[x] = t;
        }
        return fa[x];
    };
    auto add = [&] (int u, int v, int c) -> void {
        int uu = find(find, u), vv = find(find, v);
        fa[uu] = vv;
        dis[uu] = dis[v] - dis[u] - c;
    };
//===========================================================================
    bool flag = true;
    for(int i = 1; i <= m; i ++){
        int u, v, c;
        cin >> v >> u >> c;
        int uu = find(find, u), vv = find(find, v);
        if(uu == vv){
            if(dis[v] - dis[u] != c){
                flag = false;
            }
        } 
        else{
            add(u, v, c);
        }
    }
    if(flag){
        cout << "YES" << endl;
    }
    else{
        cout << "NO" << endl;
    }
}

建图dfs

void QAQ(){
    int n, m;
    cin >> n >> m;
    vector<int> vis(n + 1), dis(n + 1);
//===========================================================================
    struct type{
        int nxt, to, w;
    };
    vector<type> e(m * 2 + 1);
    vector<int> h(n + 1);
    int idx = 0;

    auto add = [&] (int u, int v, int w) -> void {
        idx ++;
        e[idx].to = v;
        e[idx].w = w;
        e[idx].nxt = h[u];
        h[u] = idx;
    };
    
    for(int i = 1; i <= m; i ++){
        int u, v, w;
        cin >> v >> u >> w;
        add(u, v, w);
        add(v, u, -w);
    }
//===========================================================================
    bool flag = true;
    auto dfs = [&](auto dfs, int u) -> void {
        vis[u] = true;
        for(int i = h[u]; i; i = e[i].nxt){
            int v = e[i].to, w = e[i].w;
            if(vis[v]){
                if(dis[u] + w != dis[v]) flag = false;
            }
            else{
                dis[v] = dis[u] + w;
                dfs(dfs, v);
            }
        }
    };

    for(int i = 1; i <= n; i ++){
        if(!vis[i]){
            dfs(dfs, i);
        }
        if(!flag) break;
    }
//===========================================================================
    if(flag) cout << "YES" << endl;
    else cout << "NO" << endl;
}

标签:vector,886,int,void,Codeforces,num,Div,find,dis
From: https://www.cnblogs.com/woods4325/p/17580873.html

相关文章

  • CodeForces 725F Family Photos
    洛谷传送门CF传送门不错的贪心题。我们考虑一对照片只有一张的情况。那么先后手会按照\(a+b\)降序取。因为若\(a_i+b_i\gea_j+b_j\),变形得\(a_i-b_j\gea_j-b_i\)且\(b_i-a_j\geb_j-a_i\),所以对于双方先取\(i\)一定是不劣的。回到原题,如果\(a_{i......
  • Codeforces Round 887 (Div. 2) A-D
    CodeforcesRound887(Div.2)A.Desorting题意:给出一个数组,可以进行任意次以下操作:选择一个i对于数组中的a[1],a[2],...,a[i]全部+1a[i+1]...a[n]全部-1,问最小使得数组变得无序的操作是多少次Solution直接找相邻的两个数的最小的差值即可voidsolve(){ intn;cin>>n......
  • Educational Codeforces Round 71 (Rated for Div. 2)
    EducationalCodeforcesRound71(RatedforDiv.2)A-ThereAreTwoTypesOfBurgers思路:价格高的优先取#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128typedefpair<int,int>PII;typedefpair<string,int&......
  • 题解 Codeforces Round 887 (Div 1+Div 2) / CF1853AB,CF1852ABCD
    下大分!悲!Div1只过了1A!!!但还是补完整场Div2吧。A.Desortinghttps://codeforces.com/problemset/problem/1853/Aproblem用操作:\([1,i]++,[i+1,n]--\),使得数组不单调不降,求最小操作次数。\(n\leq10^5\)。solution操作等同于在差分数组上选出\(i\),做\(c_1:=c_1+1,c_i:......
  • Codeforces Round #887 Div.2 A-E
    CodeforcesRound#887Div.2一定要手玩哦前言:一定要手玩,一定要手玩!我今早一手玩发现C很TM简单,如果赛时我能切C说不定直接上1800.。。。时隔多年,我的CodeforcesRating(1718)再次超越了@cqbzlhy(1674)!!!赛时错误:B题出得太慢了->劣于pzj半小时。%%%pzjC没有手玩,瞪眼半天......
  • UESTC 2023 Summer Training #13 Div.2
    Preface开始裸泳咯这个A题给我写的头皮发麻,后面发现我就是个智障儿童比赛的时候E题想了半天感觉天皇老子来了也是\(\frac{1}{n^2}\),赛后发现我是小丑感觉中间做J的时候因为看错题目浪费了很长时间,不过再给一个小时思博题该不会还是不会A.PainttheMiddle比赛的时候一眼贪......
  • Codeforces Round #885 数学专场
    妙,我只能说妙。今天补DEF发现除了F诡秘的杨辉三角,我都能独立做出来。但为什么我感觉DE难度不如C!!!!A题意:一个人站在\((x,y)\)处,而其他人分别在\((x_1,y_1)\dots(x_n,y_n)\),每一次这个人先移动一步到上下左右四个格子,然后其他\(n\)个人再移动一步,求是否永远这个人与其他人不......
  • Codeforces Round 887 (Div. 2)记录
    A.Desorting如果有$a_1\leqa_2\leq\ldots\leqa_{n-1}\leqa_n$,则称长度为$n$的数组$a$已排序。Ntarsis有一个长度为$n$的数组$a$。他可以对数组进行一种操作(0次或多次):-选择一个索引$i$($1\leqi\leqn-1$)。-将$1$加到$a_1,a_2,\ldots,a_i$。-用$a_{......
  • Codeforces Round 887(Div 2)(A-C)
    A.Desorting题目里说的无序是指后面的一个数大于前面一个数,所以只要有一个a[i+1]-a[i]<0就说明已经符合题目的无序要求了,不需要改变即可,即输出0如果有该序列是非严格递增的根据题目所说的改变就只需要求出最小的差值即可,最后用最小的差值除以2(因为每次可以让选中的部分之前的......
  • 【题解】Imbalanced Arrays - Codeforces 1852B
    出处:CodeforcesRound887链接:https://codeforces.com/problemset/problem/1852/B题目大意:给定一个包含\(n\)个非负整数的频次序列\(f\)。构造任意一个等长的整数序列\(b\),要求①\(b\in[-n,n]\)but$b\neq0$②\(b\)中不存在相反数③对于每个坐标\(i\)......