codeforce 1650 ABCD

A Deletions of Two Adjacent Letters

题意:给定一个字符串s和一个字符c,问经过任意次操作能否将s变成c,操作为每次选中字符串中相邻两个字符进行删除
思路:从操作的特点入手,因为最终只剩下字符c,说明c在字符串s中左边的字符和右边的字符都被删除,又因为每次操作是选中相邻的两个字符进行删除,说明在字符串s中c所在位置左边和右边的字串长度都为偶数,遍历字符串找到所有c进行判断,有一个满足则满足
代码
#include <bits/stdc++.h>
using namespace std;
int main ()
{
	int t; cin >> t;
	while (t --)
	{
		string s; char c;
		int left = 0, right = 0;
		cin >> s >> c;
		int flag = 0;
		for (int i = 0; i < s.length(); i ++)
		{
			if (s[i] == c)
			{
				left = i;
				right = s.length () - i - 1;
				if (left % 2 == 0 && right % 2 == 0)
				{
					flag = 1;
					break;
				}
			}	
		}
		if (flag) cout << "YES" << endl;
		else cout << "NO" << endl;	
	}
	return 0;
} 

B DIV + MOD

题意:给定区间[l,r]和a,求x / a + x % a 的最大值,l <= x <= r
思路:对于式子x/a+x%a,x越大x/a是单调不减的,而x%a是从0~a-1循环,因此要从x%a入手,因为x%a要想最大的话x应该为a - 1,所以可以设x=ka+a-1,找到最大的k即可求出答案,因为ka+a-1<=r则k=(r+1-a)/a,则带入x即可求出答案,但是这个数不一定满足左边界(因为这里贪心使取模最大用的a-1,但是在区间中的数不满足x=k*a+a-1),因此要进行特判,如果不满足左边界的话求一下l/a,r/a,如果这两个值相等说明区间中x/a都是相同的,因此x取r得到最大值,如果不相等,再进行比较,详情见代码。
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;

int main ()
{
	int t; scanf ("%d", &t);
	while (t --)
	{
		LL l, r, a;
		scanf ("%lld%lld%lld", &l, &r, &a);
		LL zero = 0;
		LL k = (r + 1 - a) / a;
		LL sum = k * a + a - 1;
		LL ans = sum / a + sum % a;
		if (sum >= l && sum <= r)
			printf ("%lld\n", ans);
		else
		{
			LL mod1 = l / a, mod2 = r / a;
			if (mod1 == mod2)
				printf ("%lld\n", r / a + r % a);
			else
				printf ("%lld\n", max (r /a + r % a, a * (mod2 - 1) + a - 1));
		}
	}
} 

C. Weight of the System of Nested Segments

题意:一维坐标系中给定m个点,每个点都有一个权重w,找一个含有n个区间的嵌套区间使n个区间的端点权重和最小,嵌套区间:对于[l1,r1] [l2,r2],1≤i<j≤n, li<lj<rj<ri,端点不会重合。
思路:因为区间每个端点不会重合,此题就转化成了找2*n个点使权重最小,然后从两端向中间输出区间即可。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long LL;
struct node{
	int x, w, id;
	
}q[N];
 
bool cmp1 (struct node &a1, struct node &a2)
{
    return a1.w < a2.w;
}
 
bool cmp2 (struct node &a1, struct node &a2)
{
    return a1.x < a2.x;
}
 
 
int main ()
{
	int t; cin >> t;
	while (t --)
	{
		int n, m;
		cin >> n >> m;
		for (int i = 1; i <= m; i ++)
		{
		    cin >> q[i].x >> q[i].w;
		    q[i].id = i;
		}
		sort (q + 1, q + 1 + m, cmp1);	//按权值排序找到2*n个最小值 
		LL sum = 0;
		for (int i = 1; i <= 2 * n; i ++)
			sum += q[i].w;
		cout << sum << endl;
		sort (q + 1, q + 1 + 2 * n, cmp2);	//按位置进行排序,输出编号 
		int l = 1, r = 2 * n;
		while (l < r)
		{
		    cout << q[l].id << " " << q[r].id << endl;
		    l ++; r --;
		}
		cout << endl;
	}
	return 0; 
}

D Twist the Permutation

题意:给定一个排列数组即从1~n,a[i]=i,进行n次操作,第i次操作为前i个数进行旋转右移,求给定最终状态,每个位置的右移的大小。
思路:仍从操作特点入手,第i次对前i个数进行旋转右移,可以发现,第n个数一定是第n次操作确定的,那第i个数一定是第i次操作确定的,因此可以倒过来进行操作的还原。
#include <bits/stdc++.h>
using namespace std;

const int N = 2010;
int a[N], b[N], ans[N];
int n;

int main ()
{
	int t; cin >> t;
	while (t --)
	{
		cin >> n;
		for (int i = 1; i <= n; i ++)
		{
			cin >> a[i];
			b[a[i]] = i;
		}
		
		for (int i = n; i >= 1; i --)
		{
			ans[i] = b[i] % i;
			for (int j = 1; j <= n; j ++)
			{
				b[j] = (b[j] - ans[i] + i) % i;
				if (b[j] == 0) b[j] = i;
			}
		} 
		for (int i = 1; i <= n; i ++)
			cout << ans[i] << " ";
		puts("");
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值