【CF补题】【ABC】Educational Codeforces Round 125

【CF补题】【ABC】Educational Codeforces Round 125 ,第1张

A. Integer Moves

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

【解答】长度不限,所以答案只有3种可能,0、1、2。

其中如果距离是整数则1,否则我们只要右移x,上移y就是2步。

#include 
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<

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

B. XY Sequence

【题目】a[0]=0

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

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

#include
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
using namespace std;
long long a, ans;
int n,b,x,y;
string s;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		vectorv;
		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;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/662276.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-18
下一篇 2022-04-18

发表评论

登录后才能评论

评论列表(0条)