Codeforces Round #739 (Div. 3)(编辑距离+数位dp)

本文介绍了三道不同的代码竞赛题目,涉及输入处理、整数数组操作和几何计算。第一题处理输入并输出数组最后一个元素;第二题解决两个数之间的最短距离计算;第三题涉及寻找平方根的整数部分。每道题均提供了解题思路和代码实现。

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

a

int n;
int arr[N];
 
void solve()
{
	cin >> n;
	cout << arr[n] << endl;
}
 
int main()
{
 
	ios::sync_with_stdio(false);cin.tie(0);
	int x = 1;
	rep(i,1,100000)
	{
		if(i%3==0 || i%10==3) continue;
		else arr[x++]=i;
	}
	int Case;cin >> Case;
	while(Case--)
		solve();
	return 0;
}

b

找规律就行,

// Problem: B. Who's Opposite?
// Contest: Codeforces - Codeforces Round #739 (Div. 3)
// URL: https://codeforces.com/contest/1560/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
using namespace std;

#define rep(i,l,r) for(int i = l; i <= r; i++)
const int N=1e5+100;
int n;
int arr[N];

void solve()
{
	int a, b, c;
	cin >> a >> b >> c;
	int cc = abs(a-b);
	int ans = - 1;
	if(c - cc > 0) ans = c - cc;
	if(c + cc <= 2*cc) ans = c + cc;
	if(c > 2*cc) 
	{
		cout << -1 << endl;
		return ;
	}
	if(cc < min(a,b))  // 注意这里是小于
	{
		cout << -1 << endl;
		return ;
	}
	cout << ans << endl;
}

int main()
{

	ios::sync_with_stdio(false);cin.tie(0);
	int Case;cin >> Case;
	while(Case--)
		solve();
	return 0;
}

c

// Problem: C. Infinity Table
// Contest: Codeforces - Codeforces Round #739 (Div. 3)
// URL: https://codeforces.com/contest/1560/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
using namespace std;
#define lowbit(x) (x&-x)
#define pf(a) printf("%d\n",a)
#define mem(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout << #x << " = " << x << endl
#define rep(i,l,r) for(int i = l; i <= r; i++)
#define fep(i,a,b) for(int i=b;i>=a;--i)
typedef long long ll;
typedef unsigned long long ull;

const int inf=0x3f3f3f3f;
const int N=1e5+100;
int n;
int arr[N];

void solve()
{
	ll k;
	cin >> k;
	ll cc = 0, r = 0;
	ll c;
	if(k==1) 
	{
		cout << 1 << " " << 1 << endl;
		return ;
	}
	for(int i=1;i<=N;i++)
	{
		if(i*i <= k && k <= (i+1)*(i+1))
		{
			c=i+1;
			break;
		}
	}
	if( k >= c*c-c+1) 
	{
		cc = c;
		r = c*c - k + 1;
	}
	else {
		r = c;
		cc = c - (c*c-c+1 - k);
	}
	
	cout << cc << " " << r << endl;
}

int main()
{

	ios::sync_with_stdio(false);cin.tie(0);
	int Case;cin >> Case;
	while(Case--)
		solve();
	return 0;
}

d字符串匹配

// Problem: D. Make a Power of Two
// Contest: Codeforces - Codeforces Round #739 (Div. 3)
// URL: https://codeforces.com/contest/1560/problem/D#
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
using namespace std;
#define lowbit(x) (x&-x)
#define pf(a) printf("%d\n",a)
#define mem(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout << #x << " = " << x << endl
#define rep(i,l,r) for(int i = l; i <= r; i++)
#define fep(i,a,b) for(int i=b;i>=a;--i)
#define ll long long
ll str[50];


ll pow_mod(ll a, ll k)
{
    ll ans = 1;
    while(k)
    {
        if(k % 2)
            ans *= a;
        a *= a;
        k /= 2;
    }
    return ans;
}

int calc(string a, string b) 
{
    int n = a.size();
    int m = b.size();
    int i = 0, j = 0;
    int ret = 0;
    while(true) {
        while(i < n && a[i] != b[j]) i++;
        if(i == n) break;
        if(a[i] == b[j]) {
            ret++;
            i++;
            j++;
        }
        if(j == m) break;
    }
    return m + n - 2 * ret;
 
}
void solve()
{
	int n;
	cin >> n;
	
	int ans = 500;
	
	rep(i,0,60)
		ans = min(ans, calc(to_string(n),to_string(str[i])));

	cout << ans << endl;
}

int main()
{
	for(ll i = 0; i <= 60; i++)
		str[i] = pow_mod((ll)2,i);
	
	int Case;cin >> Case;
	while(Case--)
		solve();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值