Educational Codeforces Round 118 (Rated for Div. 2)

这篇博客包含了四个不同的代码片段,分别涉及字符串比较、求解模运算中的最小x值、利用二分查找解决单调性问题以及计算MEX序列。每个片段展示了在C++中处理这些问题的不同算法和技巧。

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

A - Long Comparison
B - Absent Remainder
C - Poisoned Dagger
D - MEX Sequences
E - Crazy Robot

拿字符串比较

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <stack>
#include <set>
#define mid (l+r>>1)
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 2e6+10, mod = 1e9 + 7;
void mull(int &a, LL b){a = a*b%mod;return ;}
void add(int &a, LL b){a = (a+b)%mod;return ;}


int main() 
{	
	int t;
	scanf("%d", &t);
	while(t --)
	{
		string a, b;
		int c, d, f = 1;
		cin>>a>>c>>b>>d;
		while(a.size()<b.size()&&c)a+='0', c--;
		
		while(a.size()>b.size()&&d)b+='0', d--;
		
		int la = a.size()+c, lb = b.size()+d;
		if(la < lb) f = -1;
		else if(la == lb)
		{
			if(a == b)f = 0;
			else if(a > b)f = 1;
			else f = -1;
		}
		if(f == 1)puts(">");
		else if(f == -1)puts("<");
		else puts("=");
	 } 
	return 0;
}

y   m o d   x = t ,   i f y > x , t < x y\,mod\,x = t, \,if y > x,t < x ymodx=t,ify>x,t<x,那么x取最小值

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <stack>
#include <set>
#define mid (l+r>>1)
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 2e6+10, mod = 1e9 + 7;
void mull(int &a, LL b){a = a*b%mod;return ;}
void add(int &a, LL b){a = (a+b)%mod;return ;}

int a[N];
int main() 
{	
	int t;
	scanf("%d", &t);
	while(t --)
	{
		int n;
		scanf("%d", &n);
		for(int i = 0;i < n;i ++)scanf("%d", a+i);
		sort(a, a+n);
		int len = n/2, x = 1;
		while(len --)
			printf("%d %d\n", a[x++], a[0]);	
	} 
	return 0;
}

答案具有单调性,那么可以二分 n l o g   n nlog\,n nlogn

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <stack>
#include <set>
#define mid (l+r>>1)
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 2e6+10, mod = 1e9 + 7;
void mull(int &a, LL b){a = a*b%mod;return ;}
void add(int &a, LL b){a = (a+b)%mod;return ;}

int a[N], n;
LL h;

bool ch(LL x)
{
	LL sum = 0, tail = 0;
	for(int i = 1;i <= n;tail = max(tail, a[i]+x), i ++)
		tail < a[i] ? sum += x : sum += a[i]+x-tail;
	return sum >= h;
}

int main() 
{	
	int t;
	scanf("%d", &t);
	while(t --)
	{
		scanf("%d%lld", &n, &h);
		for(int i = 1;i <= n;i ++)scanf("%d", a+i);
		LL l = 1, r = 1e18;
		while(l < r)
		{
			if(ch(mid))r = mid;
			else l = mid+1;
		}
		cout<<l<<endl;
	} 
	return 0;
}

d p , d p [ i ] [ 0 / 1 ] 表 示 m e x = i , m a x = i − 1   o r   i + 1 dp,dp[i][0/1] 表示mex = i,max = i-1\,or\,i+1 dp,dp[i][0/1]mex=imax=i1ori+1 然后开始疯狂的推式子了, a i 会 和 d p [ a i − 1 ] , d p [ a i ] , d p [ a i + 1 ] a_i会和dp[a_i-1],dp[a_i],dp[a_i+1] aidp[ai1],dp[ai],dp[ai+1] 有影响,我把所有的数字都右移了1,因为不移1的话初始状态是 d p [ − 1 ] [ 1 ] = 1 dp[-1][1]=1 dp[1][1]=1

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <stack>
#include <set>
#define mid (l+r>>1)
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 2e6+10, mod = 998244353;
void mull(int &a, LL b){a = a*b%mod;return ;}
void add(int &a, LL b){a = (a+b)%mod;return ;}

int in[N], dp[N][2];

int main() 
{	
	int t;
	scanf("%d", &t);
	while(t --)
	{
		int n;
		scanf("%d", &n);
		int ans = 0;
		dp[1][0] = 1;
		for(int i = 1;i <= n;i ++)
		{
			int x;
			scanf("%d", &x); ++x;
			add(ans, (LL)dp[x+1][0]+dp[x+1][1]+dp[x][0]+dp[x-1][1]+dp[x-1][0]);
			
			add(dp[x-1][1], dp[x-1][1] + dp[x-1][0]);
			mull(dp[x+1][1], 2);
			add(dp[x+1][0], dp[x+1][0] + dp[x][0]);
		}
		for(int i = 1;i <= n+1;i ++)
			dp[i][0] = dp[i][1] = 0; 
		cout<<ans<<endl;
	} 
	return 0;
}

这题想简单了,哎。要边遍历边修改

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#define mid (l+r>>1)
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 1e6+10, mod = 998244353;
void mull(int &a, LL b){a = a*b%mod;return ;}
void add(int &a, LL b){a = (a+b)%mod;return ;}

int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

int main() 
{	
	int t;
	scanf("%d", &t);
	while(t --)
	{
		int n, m, x, y;
		scanf("%d%d", &n, &m);
		char a[n+1][m+1] ;   
		for(int i = 1;i <= n;i ++)
		{
			scanf("%s", a[i]+1);
			for(int j = 1;j <= m;j ++)
				if(a[i][j] == 'L')
					x = i, y = j;
		}
		queue<PII>q;
		while(!q.empty())q.pop();	
		for(int i = 0;i < 4;i ++)
			q.push({x+dx[i], y+dy[i]});
		a[x][y] = '+';
		while(!q.empty())
		{
			int x = q.front().first, y = q.front().second, l = 0, r = 0;
			q.pop();
			if(!x || x>n || !y || y>m || a[x][y] != '.') continue;
			
			for(int i = 0;i < 4;i ++)
			{
				int ll = x+dx[i], rr = y+dy[i];
				if(!ll || ll>n ||!rr || rr>m)continue;
				l += a[x+dx[i]][y+dy[i]] == '.', r += a[x+dx[i]][y+dy[i]] == '+';
			}
				
			if(!r || l > 1 )continue;
			a[x][y] = '+';
			for(int i = 0;i < 4;i ++)
				q.push({x+dx[i], y+dy[i]});
		}		
		a[x][y] = 'L';
		for(int i = 1;i <= n;i ++)
			printf("%s\n",a[i]+1);
	} 
	return 0;
}
//#....
//..##L
//...#.
//.....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李昌荣。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值