首页 > 其他分享 >AtCoder Beginner Contest 376

AtCoder Beginner Contest 376

时间:2024-10-23 20:09:56浏览次数:1  
标签:AtCoder Beginner int auto cin long res using 376

A - Candy Button

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;


using vi = vector<int>;
using pii = pair<int,int>;

i32 main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int n, c;
	cin >> n >> c;
	int res = 1, lst;
	cin >> lst;
	for(int i = 2, x; i <= n; i ++) {
		cin >> x;
		if(x < lst + c) continue;
		lst = x, res ++;
	}
	cout << res;
	return 0;
}

B - Hands on Ring (Easy)

这里要注意的就是移动过程中不能出现手跨越手的情况,因此我们要在选择不会跨越的情况。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;


using vi = vector<int>;
using pii = pair<int,int>;

i32 main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int n, q;
	cin >> n >> q;
	int l = 1, r = 2;
	int res = 0;
	while(q --) {
		string h;
		int t;
		cin >> h >> t;
		if(h == "L") {
			if(l == t) continue;
			if(min(t, l) <= r and r <= max(t, l))
				res += n - abs(t - l);
			else 
				res += abs(t - l);
			l = t;
		} else {
			if(r == t) continue;
			if(min(t, r) <= l and l <= max(t, r))
				res += n - abs(t - r);
			else 
				res += abs(t - r);
			r = t;
		}
	}
	cout << res;
	return 0;
}

C - Prepare Another Box

我们可以二分枚举买的盒子的大小,然后贪心的尝试匹配一下。怎么贪心?我们可以把盒子和物品都排个序即可。其中物品不用每次都排序,但盒子需要。其实盒子中\(N-1\)个盒子的相对位置是固定的。我们可以先把原始盒子排序,然后每次加入一个后再排序,这样的复杂度是不会跑满\(O(N\log N)\)的。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;


using vi = vector<int>;
using pii = pair<int,int>;

const int inf = INT_MAX / 2;

i32 main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int n;
	cin >> n;
	vi a(n), b(n - 1);
	for(auto &i : a) cin >> i;
	for(auto &i : b) cin >> i;
	ranges::sort(a);
	ranges::sort(b);

	auto check = [=](int x) {
		auto c = b;
		c.push_back(x);
		ranges::sort(c);
		for(int i = 0; i < n; i ++) 
			if(a[i] > c[i]) return false;
		return true;
	};

	int l = 0, r = inf, res = -1;
	while(l <= r) {
		int mid = (l + r) / 2;
		if(check(mid)) res = mid, r = mid - 1;
		else l = mid + 1;
	}
	cout << res;
	return 0;
}

当然了还有一种思路是,贪心的匹配较大的,找到第一个匹配不上的,此时就必须要购买一个,在验证剩下的是否都可以匹配上。

#include <bits/stdc++.h>

using namespace std;

int main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);

	int n;
	cin >> n;

	vector<int> a(n), b(n - 1);

	for(auto &i : a) cin >> i;
	for(auto &i : b) cin >> i;

	ranges::sort(a);
	ranges::sort(b);

	int x = n - 1, y = n - 2;

	int res = 0;
	while(x >= 0 and y >= 0) {
		if(b[y] >= a[x]) {
			x --, y --;
		} else {
			res = x;
			break;
		}
	}
	int f = 1;
	x --;
	while(x >= 0 and y >= 0){
		if(b[y] >= a[x]) {
			x --, y --;
		} else {
			f = 0;
			break;
		}
	}
	if(f) {
		cout << a[res];
	} else {
		cout << -1;
	}

}

D - Cycle

从 1 开始的 bfs 就好了。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;


using vi = vector<int>;
using pii = pair<int,int>;

const int inf = INT_MAX / 2;

vector<vi> e;

i32 main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int n, m;
	cin >> n >> m;

	vector<vi> e(n + 1);
	for(int x, y; m ; m --) {
		cin >> x >> y;
		e[x].push_back(y);
	}

	vi dep(n + 1, inf);
	dep[1] = 1;
	queue<int> q;
	q.push(1);
	while(not q.empty()) {
		int x = q.front();
		q.pop();
		for(auto y : e[x]) {
			if(y == 1) {
				cout << dep[x];
				return 0;
			}
			if(dep[y] != inf) continue;
			dep[y] = dep[x] + 1;
			q.push(y);
		}
	}
	cout << -1;
	return 0;
}

E - Max × Sum

考虑枚举一下\(\max\),贪心的最小的\(k\)个数即可。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;

#define int i64

using vi = vector<int>;
using pii = pair<int,int>;

const int inf = LLONG_MAX / 2;

void solve(){
	int n, k;
	cin >> n >> k;
	vector<pii> p(n);
	for(auto &[a, b] : p) cin >> a;
	for(auto &[a, b] : p) cin >> b;

	ranges::sort(p);
	
	int sum = 0;
	priority_queue<int> heap;
	for(int i = 0; i < k - 1; i ++) {
		heap.push(p[i].second);
		sum += p[i].second;
	}
	int res = inf;
	for(int i = k - 1; i < n; i ++) {
		heap.push(p[i].second);
		sum += p[i].second;
		if(heap.size() > k) {
			sum -= heap.top();
			heap.pop();
		}
		res = min(res, sum * p[i].first);
	}
	cout << res << "\n";
}

i32 main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int T;
	cin >> T;
	while(T --)
		solve();

	return 0;
}

标签:AtCoder,Beginner,int,auto,cin,long,res,using,376
From: https://www.cnblogs.com/PHarr/p/18498276

相关文章

  • AtCoder DP Contest 速通指南
    题单链接这是AT之前办的一场DP专题,里面都是很经典的问题,可以帮助大家复习DP的套路,个人感觉对于巩固基础来说质量很高,建议大家去去联系一下,尽量不要看题解。本博客只讨论了绿色及以上难度的题目,下面是我的题解。ICoins设\(f_{i,j}\)表示扔到了第\(i\)个,有\(j\)个......
  • Django for beginner for windows
    Setupdjangoprojectstep1:createafolderforprojectandswitchtothefolderinterminal.step2:createavirtualenvironment:python-mvenvvirtual_environment_namestep3:activatethevirtualenvironment:virtual_environment_name\Scripts\acti......
  • AtCoder Beginner Contest 376
    A-CandyButton题意按一次按钮得到一块糖,条件是这次按按钮的时间间隔上次得到糖的时间\(\gec\)。现在按\(n\)次按钮,已知第\(i\)次按按钮时间为\(t_i\),求得到的糖果数。思路模拟。代码点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineintlong......
  • AtCoder Beginner Contest 369 - VP记录
    A-369样例已经包括了所有的情况(真良心)。点击查看代码#include<cstdio>#include<algorithm>usingnamespacestd;intmain(){ inta,b;scanf("%d%d",&a,&b); intans=0; if(a>b)swap(a,b); if(a==b)ans=1; elseif((b-a)&1)ans=2; else......
  • AtCoder Beginner Contest 376
    AtCoderBeginnerContest376A-CandyButton有一个人按若干次按钮,如果距离上次得分的时间超过\(C\),那么就会获得一颗糖。给出这个人按按钮的时刻,回答最终会获得有多少糖。模拟题#include<iostream>#include<cstdio>usingnamespacestd;intn,c,a,ans;intmain(){......
  • 题解:AT_abc376_c [ABC376C] Prepare Another Box
    很好的一道二分答案题。听说CSP考前写tj可以让rp+=inf?注:下文中\(w\)指物品重量序列,\(x\)指箱子容量序列。先问个问题:为什么我上来就敢肯定这是个二分答案题?或者说,单调性在哪儿?非常简单:如果一个盒子的容量越大,能装下的东西就更多(废话)。那么如果\(v\)不够用,可以扩......
  • 题解:AT_abc376_c [ABC376C] Prepare Another Box
    这道题要求把\(a\)数组和\(b\)数组一一匹配,且要求无法匹配的数量最多为一,并且这个无法匹配的元素最小。可以注意到我们把两个数组排序以后一一对应以后如果出现一个无法匹配的元素,那么这一定就是答案。但是如果我们从小到大枚举,会发现最后剩下的元素不一定最小,所以我们选择......
  • abc376C Prepare Another Box
    有N个玩具,大小分别为A[i];另外有N-1个盒子,大小分别为B[i]。现要再买一个盒子,把所有玩具装到盒子里,要求每个玩具都装一个盒子,并且玩具大小不超过盒子大小。问买的盒子至少为多大?如果无法满足,输出-1。2<=N<=2E5,1<=A[i],B[i]<=1E9分析:将玩具按从大到小排序再依次处理,每次用不小于......
  • abc376D Cycle
    有N个顶点M条边的简单有向图,求包含1号点的最小环的顶点数,如果不存在,输出-1。2<=N<=2E5,1<=M<=2E5分析:bfs求最小环。#include<bits/stdc++.h>usingi64=longlong;voidsolve(){ intN,M; std::cin>>N>>M; std::vector<std::vector<int>>adj(N); fo......
  • abc376E Max x Sum
    有序列A[N]和B[N],选出一组大小为K的下标,让A[i]的最大值乘以(B[i]之和)的结果最小,求最小值。1<=T<=2E5,1<=K<=N<=2E5,1<=A[i],B[i]<=1E6分析:因为A[i]跟B[i]要同步选,因此对下标排序,然后枚举每个A[i]作为最大值,从B[i]中选出最小的K个求和,得到结果,B[i]之和可以用堆来维护。#inclu......