【CF补题】【ABC】Educational Codeforces Round 125 (Rated for Div. 2) C++代码

A. Integer Moves

【题目】只能横向或纵向移动,输出从点 (0,0) 移动到点 (x,y) 所需的最少操作次数。

【解答】长度不限,所以答案只有3种可能,0、1、2。其中如果距离是整数则1,否则我们只要右移x,上移y就是2步。

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
const int maxn=200020;
const int mo=1e9+7;
int x,y;
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int t;
    cin>>t;
	while(t--)
	{
		cin>>x>>y;
		if(x==0 &&y==0) {
			cout<<0<<endl;
		}
		else if(x==0 || y==0)
		{
			cout<<1<<endl; 
		}
		else
		{
			float a;
			a= sqrt(x*x+y*y);
			int k=sqrt(x*x+y*y);
			if(fabs(a-k)<1e-7)cout<<1<<endl;
			else cout<<2<<endl;			
		}
	 } 
    return 0;
}

——————————————————————————————————————————

B. XY Sequence

【题目】a[0]=0

要满足a[i]<=B,求a[0~n]的最大值

【解答】模拟贪心,当且仅当采用+操作过大时才使用-操作

#include<bits/stdc++.h>
using namespace std;
long long a, ans;
int n,b,x,y;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> b >> x >> y;
		a = 0;
		ans = 0;
		int mx=b-x;
		for (int i = 1; i <= n; i++)
		{
			if (a <= mx)a = a + x; else a = a - y;
			ans += a;
		}
		cout << ans << '\n';
	}
	return 0;
}

 ——————————————————————————————————————————

C.Bracket Sequence Deletion

【题目】

 【解答】首先排除暴力。在判断回文时要注意()不是回文!  ))才是!

然后观察发现,()是符合常规的,((,))这两个都是回文串,那么只有在出现)(时需要判断。

然后,当开头是)(时,只要后面再出现一个),形成1000...0001,又是一个回文串,可以全部删除。

那么这题就很明朗了。如果开头是(那么前缀全部删除,开头是)就压栈等到下一个)后全部清除,用vector

ps:题中说了“最短前缀”所以不用考虑出现100010001这种情况

#include<bits/stdc++.h>
using namespace std;
long long a, ans;
int n,b,x,y;
string s;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		vector<char>v;
		ans = 0;
		cin >> n >> s;
		for (int i = 0; i < n; i++)
		{
			v.push_back(s[i]);
			if (v.size() > 1)
			{
				if (*(v.begin()) == '(' || *(v.begin()) == v.back())
				{
					v.clear();
					ans++;
				}
			}
		}
		cout << ans << ' ' << v.size() << '\n';
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值