(CodeForces) Round#408(Div.2) A,B,C,D

本文解析了两道算法竞赛题目:'BuyingAHouse' 和 'FindTheBone' 的代码实现。'BuyingAHouse' 使用暴力搜索策略寻找最优解,'FindTheBone' 则通过模拟策略在限制时间内找到目标。代码使用 C++ 编写,展示了如何处理输入数据并进行高效运算。

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

传送门

A. Buying A House

暴力一下就行

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int inf=-0x3f3f3f3f;
const int maxn=1e3+5;
int a[maxn];
int n,m,k;
int main(){
	cin>>n>>m>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	int res=INF;
	for(int i=m+1;i<=n;++i){
		if(a[i]!=0&&a[i]<=k){
			res=min(res,(i-m)*10);
			break;
		}
	}
	for(int i=m-1;i>=1;--i){
		if(a[i]!=0&&a[i]<=k){
			res=min(res,(m-i)*10);
		}
	}
	cout<<res<<endl;
	return 0;
}

B. Find The Bone

模拟题,本题用cin有TLE的风险,跑了1996ms,而用scanf只跑了300ms+,怕了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int inf=-0x3f3f3f3f;
const int maxn=2e6+5;
bool h[maxn];
int n,m,k,xx;
int main() {
	scanf("%d%d%d",&n,&m,&k);
	memset(h,false,sizeof(h));
	for(int i=1; i<=m; ++i) {
		scanf("%d",&xx);
		h[xx]=true;
	}
	int x,y;
	bool ff=false;
	int res=1;
	if(h[1])	ff=true;
	for(int i=1; i<=k; ++i) {
		scanf("%d%d",&x,&y);
		if(ff)	continue;
		if(x!=res&&y==res) {
			res=x;
		} else if(y!=res&&x==res) {
			res=y;
		}
		if(h[res]) {
			ff=true;
		}
	}
	cout<<res<<endl;
	return 0;
}

C,D 单独写一下

C. Bank Hacking

https://blog.youkuaiyun.com/TDD_Master/article/details/84947002

D.Police Stations

https://blog.youkuaiyun.com/TDD_Master/article/details/84949779

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值