[Codeforces] 592 div2 A B D E

本文精选了五道算法竞赛题目,包括最优化路径选择、树的染色问题、序列操作优化等,通过深入解析题目的核心算法思想,提供了一种高效的问题解决策略。文章涵盖了动态规划、贪心算法、深度优先搜索、二分查找等多种算法技巧,旨在帮助读者提升算法理解和应用能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - Pens and Pencils

 emm题意忘了

B - Rooms and Staircases

 有两层房子,每层有k间 每间要么是0要么是1 0只能左右, 1可以上下 问最多能走多少间。

不用考虑上下左右来回走,枚举每个转折上面走最大下面走最大更新极值即可,来回走必然不如这样优

/*
    Zeolim - An AC a day keeps the bug away
*/
  
//#pragma GCC optimize(2)
//#pragma GCC ("-W1,--stack=128000000")
#include <bits/stdc++.h>
using namespace std;
#define mp(x, y) make_pair(x, y)
#define fr(x, y, z) for(int x = y; x < z; ++x)
#define pb(x) push_back(x)
#define mem(x, y) memset(x, y, sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair <int, int> pii;
typedef std::vector <int> vi;
//typedef __int128 ill;
const ld PI = acos(-1.0);
const ld E = exp(1.0);
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const ull P = 13331;
const int MAXN = 2e6 + 10;
 
int dp[MAXN][2] = {0};
 
int main()
{ 
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("1.txt", "r", stdin);
 
	int T;
	
	cin >> T;
	
	while(T--)
	{
		int n;
		cin >> n;
		int ans = 0;
		string s;
		cin >> s;
		if(s.find('1') == string::npos)
			cout << n << '\n';
			
		else
		{
			int cnt = 0;
			int fst = INF;
			int lst = -INF;
			for(int i = 0; i < n; ++i)
			{
				if(s[i] == '1')
				{
					++cnt;
					fst = min(fst, i);
					lst = max(lst, i);
				}
			}
			
			++fst, ++lst;
			int ans = 0;
			
			ans = max(ans, n - fst + 1);
			ans = max(ans, fst);
			ans = max(ans, n - lst + 1);
			ans = max(ans, lst);
			
			cout << ans * 2 << '\n';
		}
		
	}
    
    return 0;
}
D - Paint the Tree

 

给定一颗树,相邻三个节点颜色不能涂一样,每个节点可以涂三个颜色,不同颜色的权值给定

首先发现,树深度大于2必然有矛盾,所以这个树就是链,然后在链上dp就行

其实不用dp 直接枚举开始两个节点的情况暴力跑6遍就行 因为限定前两个以后后面情况是唯一的

/*
    Zeolim - An AC a day keeps the bug away
*/
  
//#pragma GCC optimize(2)
//#pragma GCC ("-W1,--stack=128000000")
#include <bits/stdc++.h>
using namespace std;
#define mp(x, y) make_pair(x, y)
#define fr(x, y, z) for(int x = y; x < z; ++x)
#define pb(x) push_back(x)
#define mem(x, y) memset(x, y, sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair <int, int> pii;
typedef std::vector <int> vi;
//typedef __int128 ill;
const ld PI = acos(-1.0);
const ld E = exp(1.0);
const ll INF = 0x3f3f3f3f3f3f3f;
const ll MOD = 1e9 + 7;
const ull P = 13331;
const int MAXN = 1e6 + 10;
 
vector <int> edge[MAXN];
ll v[MAXN][3];
 
vector <int> ans, t;
 
ll rs = 0, rans = INF;
 
bitset <MAXN> used;
 
void dfs(int now, int fa, int lfa)
{
	used[now] = 1;
	for(int i = 0; i < 3; ++i)
	{
		if(i != t[fa] && i != t[lfa])
		{
			t[now] = i;
			rs += v[now][i];
		}	
	}
	for(auto to : edge[now])
	{
		if(!used[to])
			dfs(to, now, fa);
	}
}
 
void gao(int beg, int x, int y)
{
	used.reset();
	rs = 0;
    t[beg] = x;
    int bto = edge[beg][0];
    t[bto] = y;
    rs = v[beg][x] + v[bto][y];
    used[beg] = 1;
    used[bto] = 1;
    for(auto to : edge[bto])
    {
    	if(!used[to])
    	{
    		dfs(to, bto, beg);
		}
	}
	if(rs < rans)
	{
		rans = rs;
		ans = t;
	}
}
 
int main()
{ 
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("1.txt", "r", stdin);
 
	int n;
	
	cin >> n;
	
	for(int i = 1; i <= n; ++i)
		cin >> v[i][0];
	
	for(int i = 1; i <= n; ++i)
		cin >> v[i][1];
		
	for(int i = 1; i <= n; ++i)
		cin >> v[i][2];
		
	for(int i = 1; i < n; ++i)
	{
		int x, y;
		cin >> x >> y;
		edge[x].pb(y);
		edge[y].pb(x);
	}
	
	int beg = 0;
	for(int i = 1; i <= n; ++i)
	{
		if(edge[i].size() > 2)
		{
			cout << "-1\n";
			return 0;
		}
		if(edge[i].size() == 1)
			beg = i;
	}
 
    ans.resize(n + 1);
    t.resize(n + 1);
    
    gao(beg, 0, 1);
    gao(beg, 1, 0);
    gao(beg, 1, 2);
    gao(beg, 2, 1);
    gao(beg, 0, 2);
    gao(beg, 2, 0);
    
    cout << rans << '\n';
    
    for(int i = 1; i <= n; ++i)
    	cout << ans[i] + 1 << ' ';
    
    return 0;
}

 

E - Minimizing Difference

题意:给定一个序列和一个k,你可以操作至多k次,每次操作可以将任意一个数字加1也可以将任意一个数字减1 

求操作后序列最大值减最小值差值最小;

思路:首先考虑将数组中个某个值当作上界或下界,再分别二分查找以当前值为上界/下界所能达到的最优值,更新答案即可

对于每次check,小于左边界的所有值必须要加左边界,大于二分右边界的都要减到右边界,可以在序列中二分位置+前缀和快速计算

复杂度O(nloglogn)

/*
    Zeolim - An AC a day keeps the bug away
*/
 
//#pragma GCC optimize(2)
//#pragma GCC ("-W1,--stack=128000000")
#include <bits/stdc++.h>
using namespace std;
#define mp(x, y) make_pair(x, y)
#define fr(x, y, z) for(int x = y; x < z; ++x)
#define pb(x) push_back(x)
#define mem(x, y) memset(x, y, sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair <int, int> pii;
typedef std::vector <int> vi;
//typedef __int128 ill;
const ld PI = acos(-1.0);
const ld E = exp(1.0);
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 386910137;
const ull P = 13331; 
const int MAXN = 1e6 + 10;
 
ll n, k;
ll arr[MAXN] = {0}, pre[MAXN] = {0}, rpre[MAXN] = {0};
ll rk;
bool check(ll val)
{
	int pos = lower_bound(arr, arr + n, val) - arr;
	ll rx = rpre[pos] - val * (n - pos);
	return rx <= rk;
}
bool check1(ll val)
{
	if(arr[0] >= val)
		return 1;
	int pos = lower_bound(arr, arr + n, val) - arr;
	while(arr[pos] >= val) --pos;
	ll rx = val * (pos + 1) - pre[pos];
	return rx <= rk;
}
 
int main()
{  
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen("d:\out.txt","w",stdout);
    //freopen("d:\in.txt","r",stdin);
    
    cin >> n >> k;
    
    for(ll i = 0; i < n; ++i)
    	cin >> arr[i];
    	
   	sort(arr, arr + n);
   	
   	pre[0] = arr[0];
   	
	for(ll i = 1; i < n; ++i)
		pre[i] = pre[i - 1] + arr[i];
	
	rpre[n - 1] = arr[n - 1];
	
	for(ll i = n - 2; i >= 0; --i)
		rpre[i] = rpre[i + 1] + arr[i];
		
	ll ans = INF;
	
	for(ll i = 0; i < n; ++i)
	{
		ll rval = arr[i] * (i + 1);
		ll rx = rval - pre[i];
		if(rx > k) break;
		rk = k - rx;
		ll fst = arr[i], lst = arr[n - 1], mid;
		while(fst <= lst)
		{
			mid = (fst + lst) / 2;
			if(check(mid))
			{
				ans = min(ans, mid - arr[i]);
				lst = mid - 1;
			}
			else
			{
				fst = mid + 1;
			}
		}
	}
	
	for(ll i = n - 1; i >= 0; --i)
	{
		ll rval = arr[i] * (n - i);
		ll rx = rpre[i] - rval;
		if(rx > k) break;
		rk = k - rx;
		ll fst = 0, lst = arr[i], mid;
		while(fst <= lst)
		{
			mid = (fst + lst) / 2;
			if(check1(mid))
			{
				ans = min(ans, arr[i] - mid);
				fst = mid + 1;
			}
			else
			{
				lst = mid - 1;
			}
		}
	}
	
	cout << ans << '\n';
	
    return 0;
}

 

### Codeforces Div. 2 比赛介绍 Codeforces 是一个在线编程竞赛平台,其中Div. 2比赛面向的是那些评分低于2100分的参赛者[^1]。这类比赛旨在挑战并提升程序员解决问题的能力和技术水平。 ### 参与方式 为了参加Codeforces Div. 2的比赛,参与者首先需要注册账号,并确保个人评级满足参与条件。每次比赛前会有一个虚拟房间分配过程,在此期间选手可以选择加入特定的房间或接受随机安排。比赛通常持续两小时,其间可以尝试解决多个不同难度级别的算法问题。 ### 题目难度 Codeforces Div. 2 的题目按照难度分为几个等级,一般情况下: - **A类题**:相对容易入门级的问题,适合新手练习基础逻辑思维和编码技巧。 - **B类题**:稍微复杂一点的任务,可能涉及到更深入的数据结构应用或是简单的动态规划概念。 - **C/D类题**:这些属于中等到较难程度的问题,往往要求更高的抽象能力和创造性解法设计能力。 - **E/F及以上类别**:非常具有挑战性的高级别难题,不仅考验全面的知识体系掌握情况还涉及到了尖端的研究成果运用[^2]。 ```python # 示例代码用于展示如何连接到Codeforces API获取即将举行的赛事列表 import requests def get_upcoming_contests(): url = "https://codeforces.com/api/contest.list?gym=false" response = requests.get(url).json() upcoming_contests = [] for contest in response['result']: if 'DIV_2' in contest['name'].upper() and contest['phase'] == 'BEFORE': upcoming_contests.append(contest) return upcoming_contests ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值