补题--11 Educational Codeforces Round 74 (Rated for Div. 2)

本文解析了Codeforces Round 74的五道题目,包括通过除以素数判断两数关系的A题,最少炸弹数量计算的B题,宝石数量与平台跳跃的C题,子串与回文串的D题,以及键盘布局与打字速度优化的E题。

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

Educational Codeforces Round 74 Rated for Div. 2


codeforce的比赛地址: https://codeforces.com/contest/1238
vj比赛网址: https://vjudge.net/contest/384245

A

题意:
a通过除以素数最后等于b输出yes。
思路:
所有的正数都可以看成是由1和所有的素数的倍数组成,所以只要这两数大小大于1即可。

#include <iostream>
using namespace std;
typedef long long ll;
int main() {
    int t;
    cin >> t;
    while(t--) {
        ll a, b;
        cin >> a >> b;
        if (a - b > 1) {
            cout << "YES" << endl;
        }
        else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

B

题意:
就是打怪兽,n个怪兽,然后r是移动的距离,xi是怪兽的距离,然后是扔出炸弹炸他们,如果怪兽与炸弹距离一致怪兽就死,如果小则左移r,大则右移r,问最少多少个炸弹。
思路:
我的最早思路:应该尽量使这些怪兽最后都在一起,就例如他们的距离是一致的,但是实行起来很麻烦。所以换一种思路就是把他们往一个方向干,但最后有可能会重叠就省一个。
官方代码:
思路是右面是最不理想的位置,所以往左干他们。

#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
const int N = 100 * 1000 + 13;
int n, r;
int a[N];
void solve() {
	scanf("%d%d", &n, &r);
	forn(i, n) scanf("%d", &a[i]);
	
	sort(a, a + n);
	n = unique(a, a + n) - a;
	
	int ans = 0;
	for (int i = n - 1; i >= 0; i--) 
		ans += (a[i] - ans * r > 0);

	printf("%d\n", ans);
}

int main() {
	int q;
	scanf("%d", &q);
	forn(i, q) solve();
}

C

题意:
一个游戏,首先你会在h高的悬崖上,然后会有n个移动平台的数目,然后每一次只可以跳以一个的距离跳,如果距离大于2就死了,游戏结束吗,但是有宝石可以改变其他移动平台的高度,问需要多少个宝石就可以通关。

#include<bits/stdc++.h>

using namespace std;

#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())

#define x first
#define y second

const int INF = int(1e9);

int h, n;
vector<int> p;

inline bool read() {
	if(!(cin >> h >> n))
		return false;
	p.resize(n);
	fore(i, 0, n)
		cin >> p[i];
	return true;
}

inline void solve() {
	int ans = 0;
	
	int lf = 0;
	fore(i, 0, n) {
		if (i > 0 && p[i - 1] > p[i] + 1) {
			if (lf > 0)
				ans += (i - lf) & 1;
			else
				ans += 1 - ((i - lf) & 1);
			lf = i;
		}
	}
	if (p[n - 1] > 1) {
		if (lf != 0)
			ans += (n - lf) & 1;
		else
			ans += 1 - ((n - lf) & 1);
	}
	
	cout << ans << endl;
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(15);
	
	int tc; cin >> tc;
	while(tc--) {
		read();
		solve();
	}
	return 0;
}

D

题意:
就是给定一个字符串,如果包含回文串,就说它是个好串。那么000给定一个字符串,求其中有多少个子串是好串且子串中每个字符都属于一个回文小串中。
代码网址

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e5+5;
const int INF = 0x3f3f3f3f;
int t,n,p[MAX],c[MAX],tot,ans;
vector<int> list1[MAX];
bool vis[MAX];
void dfs(int now){
	list1[tot].push_back(now);
	vis[now]=true;
	if(!vis[p[now]]) dfs(p[now]);
}
void solve(){
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n); ans=INF;
		for(int i=0;i<=n;++i) vis[i]=false,tot=0;
		for(int i=1;i<=n;++i) scanf("%d",&p[i]);
		for(int i=1;i<=n;++i) scanf("%d",&c[i]);
		for(int i=1;i<=n;++i)
			if(!vis[p[i]]){ list1[tot].clear();dfs(p[i]);++tot; }
		for(int i=0;i<tot;++i){
			int len = list1[i].size();
			for(int j=1;j<=len;++j){
				if(len%j==0){
					bool flag1 = false;
					int cnt = len/j;
					for(int s=0;s<j;++s){
						bool flag=true;
						int index=s,last=-1;
						for(int k=0;k<cnt;++k,(index+=j)%=len){
							if(last==-1) last = c[list1[i][index]];
							else if(last!=c[list1[i][index]]){ flag=false;break; }
						}
						if(flag){ ans=min(ans,j);flag1=true;break; }
					}
					if(flag1) break;
				}
			}
		}
		printf("%d\n",ans);
	}
}
int main(void)
{
	solve();
	return 0;
 } 

E

题意:
给定一个长n的密码字符串,其中包含m种字符(前m个小写字母)。求另一个键盘的字符串(每种字母一个),使其打出,密码串的速度最快,求最快速度。速度即两个不相同字母在键盘上的由一个字母的位置移动到另一个的步数。

#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
#include<utility>
#include<ostream>
#include<istream>
typedef long long ll;
using namespace std;
#define mod 1e9+7
#define PI acos(-1.0)
const ll maxn=1e5;
const int mx=(1<<20)+99;
ll n,m,ans,aans,b[30][30],dp[mx],cnt,sum;
string a;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    //int t;
    //cin>>t;
    //for(int o=1;o<=t;o++)
    //{
        cin>>n>>m>>a;
        memset(b,0,sizeof(b));
        for(int i=1;i<n;i++)
        {
            int l=a[i]-'a'+1,r=a[i-1]-'a'+1;
            b[l][r]++; b[r][l]++;
        }
        memset(dp,0x3f3f3f,sizeof(dp));
        dp[0]=0;
        for(int i=0;i<(1<<m);i++){
        for(int j=1;j<=m;j++)if((i&(1<<j-1))==0)
        {
            cnt=0,sum=0,ans=0;
            for(int k=1;k<=m;k++)if(i&(1<<k-1))
            {
                cnt++,sum+=b[j][k];
            }
            else
            ans+=b[j][k]*(j!=k);
            dp[i|(1<<j-1)]=min(dp[i|(1<<j-1)],dp[i]+(cnt+1)*(sum-ans));
        }
    }
    cout<<dp[(1<<m)-1]<<endl;
    //}
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值