Codeforces Round #536 (Div. 2)

本文解析了CodeForces平台上的四道算法题,包括图案查找、食物订购模拟、数组组合优化及图遍历路径字典序最小化。通过具体代码示例,详细介绍了每道题目的解决思路和实现细节。

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

http://codeforces.com/contest/1106

A. Lunar New Year and Cross Counting:

在n*n的矩阵里找一个固定的图案,n很小,暴力就行。

#include<iostream>
#include<vector>
#include<string.h>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long
const int N = 500+2;
char mp[N][N];
int n;
bool checkx(int x,int y)
{
	if(x<0||x>=n||y<0||y>=n)	return false;
	if(mp[x][y]=='X') return true;
	return false;
}




int main()
{
	int num=0;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>mp[i];
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++){
			if(checkx(i,j)&&checkx(i-1,j-1)&&checkx(i-1,j+1)&&checkx(i+1,j+1)&&checkx(i+1,j-1))
				num++;
		}
	cout<<num;

}

 

B. Lunar New Year and Food Ordering

模拟。

 

#include<iostream>
#include<vector>
#include<string.h>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long
const LL N = 2e6+2;

LL to[N];
LL val[N];
LL num[N];
LL k[N],y[N];

bool cmp(LL x,LL y)
{
	if(val[x]!=val[y])
		return val[x]<val[y];
	return x<y;
}

int main()
{
	LL n,m;
	scanf("%lld %lld",&n,&m);
	for(LL i=1;i<=n;i++){
		scanf("%lld",&num[i]);
		to[i]=i;
	}
	for(LL i=1;i<=n;i++) scanf("%lld",&val[i]);
	for(LL i=1;i<=m;i++){
		scanf("%lld%lld",&k[i],&y[i]);
	}
	sort(to+1,to+1+n,cmp);
	LL now=1;
	for(LL i=1;i<=m;i++){
		LL cost=0;
			if(num[k[i]]>=y[i]){
				num[k[i]]-=y[i];
				cost=y[i]*val[k[i]];
				y[i]=0;
			}else{
				cost=num[k[i]]*val[k[i]];
				y[i]-=num[k[i]];
				num[k[i]]=0;
			}
			while(y[i]&&now<=n){
				if(num[to[now]]>=y[i]){
					num[to[now]]-=y[i];
					cost+=y[i]*val[to[now]];
					y[i]=0;
				}else{
					cost+=num[to[now]]*val[to[now]];
					y[i]-=num[to[now]];
					num[to[now]]=0;
					now++;
				}
			}
		if(now>n) printf("0\n");
		else
			printf("%lld\n",cost);
	}



}

 

 

C. Lunar New Year and Number Division:

把一些数组合起来。每个【组合数】的权值为他的平方。

组合数最少2个。

因为x^2随着x增大越来越大。

所以排序,最大和最小组合。

 

D. Lunar New Year and a Wander:

给你一张图,问你从1开始,经过的点形成的序列字典序最小是多少。可以重复走。

那么就很简单。把1能走到的点加入优先队列。

然后取top,把top能走到的点加入。重复即可。

 

#include<iostream>
#include<vector>
#include<string.h>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long
const LL N = 1e5+2;

int flag[N];
vector<int>G[N];
vector<int> ans;
priority_queue<int,vector<int>,greater<int> > q;
int n,m;
void haha(){
	q.push(1);
	flag[1]=1;
	while(!q.empty()){
		int u = q.top();
		q.pop();
		ans.push_back(u);
		for(int i=0;i<G[u].size();i++){
			int v=G[u][i];
			if(flag[v]==0){
				q.push(v);
				flag[v]=1;
			}
		}
	}
	for(int i=0;i<n;i++){
		printf("%d%c",ans[i],i==n-1?'\n':' ');
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	haha();


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值