Codeforces Edu 109 A - D

A - Potion-making

其实就是相当于把一个未约分的数约分一下,然后分母位置就是我们需要操作次数。

#include <bits/stdc++.h>

using namespace std;

#define pb emplace_back
#define MP make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define lson rt<<1
#define rson rt<<1|1
#define CLOSE std::ios::sync_with_stdio(false)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-6;
int gcd(int a,int b) {
	return b == 0 ? a : gcd(b,a%b);
}
int main() {
	int T;scanf("%d",&T);
	while(T--) {
		int k; scanf("%d",&k);
		printf("%d\n",100 / (gcd(100,k)));
	}
	return 0;
}

B - Permutation Sort

题意:给定你一个 n n n的排列,你所可以做的操作是,每次选择一个长度不为 n n n的连续区间,对其内的元素进行重排,问你最少需要几次操作使得整个序列能够变得有序

思路:说了不能排整个区间但是我们可以排 n − 1 n-1 n1长度的区间,假如现在 p 1 = = 1 p_1 == 1 p1==1或者 p n = = n p_n == n pn==n,那么很明显,直接重排其余区间一次即可完成。所以我们就想办法把这两个数任意其中之一移到对应位置即可。
直接分类讨论即可
1. 1. 1.序列本来有序 -> 0
2. p 1 = = 1 ∣ ∣ p n = = n 2.p_1 == 1 || p_n == n 2.p1==1pn==n -> 1
3. 3. 3.从1的角度考虑, p 1 ! = 1 p_1 != 1 p1!=1,考虑把1换到前面,此时如果 p 1 ! = n p_1 != n p1!=n,输出3,否则输出2。

#include <bits/stdc++.h>

using namespace std;

#define pb emplace_back
#define MP make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define lson rt<<1
#define rson rt<<1|1
#define CLOSE std::ios::sync_with_stdio(false)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-6;
const int N = 100;
int a[N],n;

int main() {
	int T;scanf("%d",&T);
	while(T--) {
		scanf("%d",&n);
		int fg = 0,pos;
		for(int i = 1;i <= n;i ++) {
			scanf("%d",&a[i]);
			if(a[i] == 1) pos = i;
			if(a[i] != i) fg = 1;	
		}
		if(!fg) { printf("0\n"); continue; }
		if(a[1] == 1 || a[n] == n) { printf("1\n"); continue; }
		if(a[1] != 1) {
			if(a[n] == 1) {
				if(a[1] == n) printf("3\n");
				else printf("2\n");//只需要把n换到n即可
			}
			else {
				int fgg = 0;
				for(int i = pos + 1;i <= n;i ++) {
					if(a[i] != i) fgg = 1;
				}
				if(!fgg) printf("1\n");
				else printf("2\n");
			}
		}
	}
	return 0;
}
// 4 1 5 6 3 2
// 3 4 2 1 

C - Robot Collisions

题意:给定 n n n个机器人的起始坐标,保证不会出现起始坐标相同的机器人,然后给你每个机器人初始的运动方向,但是在位置0和位置m处会有两个挡板,机器人碰到之后运动方向会折返,速度始终保持为1,两个机器人在当且仅当坐标为整数的点碰撞之后会发生爆炸,爆炸后将不再影响其他机器人,问你对于每个机器人它的爆炸时间是多少,没有输出-1

思路:
涉及到弹回不断运动的状态,一定是有规律来解决问题的,题目要求了坐标碰撞的点一定是整数才会爆炸,而对于给定任意两个机器人之间的坐标,他们想要在整数点碰撞,一定必须奇偶性相同,因为我们做一个小变换,把所有朝一个方向运动的机器人,变为相向而行,这样最后相撞的地点一定是距离/2,保证了需要是个偶数。

所以我们就可以,分奇偶的去讨论,因为奇数和偶数时不可能在整数点碰撞的。将所有机器人按照 x x x从小到大排序,那么如果当前的机器人是 L L L,那么如果它之前有方向为 R R R的机器人,那么他俩一定会相撞,所以我们先用一个栈,把这种情况处理完,然后剩下的就是LLL…RRR…,LLL…,RRR…,这三种可能的情况,都是可以直接计算的,这里注意一下就是,如果按放入栈的顺序去做,RRR…这种情况没有问题的,但是LLL…这种情况我们需要的是从最左边开始依次计算,所以需要用一个新栈把顺序翻转一下。

最后如果剩下了一个L和一个R,再计算一次答案。
按这个方法奇偶分两次做一遍就可以了。

这道题想到了,会发生碰撞的坐标关系,其实就可以写了,就是分类可能稍微复杂些,所以过的人没那么多…

#include <bits/stdc++.h>

using namespace std;

#define pb emplace_back
#define MP make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define lson rt<<1
#define rson rt<<1|1
#define CLOSE std::ios::sync_with_stdio(false)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-6;
const int N = 3e5 + 10;
//思维点 其实就在 两个机器人只有起始坐标奇偶性相同时 才可能碰撞
//这个可以直接推x - t的式子 或者 分类讨论一下 就能得到
//就是模拟稍微麻烦点 思维点不高 代码点稍高 所以可能过的人没那么多
struct node {
	int id,x;
	char dir;
}rot[N];
int n,m;
std::vector<node>odd,even;
std::stack<int>stl,str,tl,tr;
int ans[N];
void clear() {
	while(!stl.empty()) stl.pop();
	while(!str.empty()) str.pop();
	while(!tl.empty()) tl.pop();
	while(!tr.empty()) tr.pop();
}
void work(vector<node>&v) {
	clear();
	for(int i = 0;i < sz(v);i ++) {
		if(v[i].dir == 'L') {
			if(!str.empty()) {
				int u1 = v[i].id,u2 = v[str.top()].id;
				ans[u1] = ans[u2] = (rot[u1].x - rot[u2].x) / 2;
				str.pop();
			}
			else stl.push(i);
		}
		else str.push(i);
	}
	while(!stl.empty()) {//向左走 需要把顺序倒过来一下
		tl.push(stl.top()); stl.pop();
	}
	while(sz(tl) >= 2) {
		int u1 = v[tl.top()].id; tl.pop();
		int u2 = v[tl.top()].id; tl.pop();
		ans[u1] = ans[u2] = (rot[u1].x + rot[u2].x) / 2;
	}

	while(sz(str) >= 2) {
		int u1 = v[str.top()].id; str.pop();
		int u2 = v[str.top()].id; str.pop();
		ans[u1] = ans[u2] = m - (rot[u1].x + rot[u2].x) / 2; 
	}
	if(sz(tl) && sz(str)) {
		int l = v[tl.top()].id,r = v[str.top()].id;
		if(rot[l].x < rot[r].x) {
			ans[l] = ans[r] = m + (rot[l].x - rot[r].x) / 2;
		}
		else {
			ans[l] = ans[r] = (rot[l].x - rot[r].x) / 2;
		}
	}
}

int main() {
	int T;scanf("%d",&T);
	while(T--) {
		odd.clear(); even.clear();
		scanf("%d%d",&n,&m);
		for(int i = 1;i <= n;i ++) scanf("%d",&rot[i].x);
		for(int i = 1;i <= n;i ++) {
			cin >> rot[i].dir;
			rot[i].id = i;
		}
		for(int i = 1;i <= n;i ++) {
			if(rot[i].x & 1) odd.pb(rot[i]);
			else even.pb(rot[i]);
		}
		//奇偶分开讨论 互不影响
		sort(odd.begin(),odd.end(),[](node a,node b){
			return a.x < b.x;
		});
		sort(even.begin(),even.end(),[](node a,node b){
			return a.x < b.x;
		});

		work(odd);
		work(even);

		for(int i = 1;i <= n;i ++) {
			if(!ans[i]) printf("-1 ");
			else printf("%d ",ans[i]);
			ans[i] = 0;
		}
		puts("");
	}
	return 0;
}

D - Armchairs

匹配问题的 d p dp dp,设 d p [ i ] [ j ] dp[i][j] dp[i][j]表示前 i i i个人,已经把 j j j个1匹配成功的最小花费。
而且按顺序匹配 0 0 0 1 1 1是能够保证正确性的,所以说我们顺序的匹配前 i i i个位置中的 j j j个1,这是可以保证正确性的。
d p [ i ] [ j ] = d p [ i − 1 ] [ j ] dp[i][j] = dp[i-1][j] dp[i][j]=dp[i1][j],对应这个位置无法放1。如果对应的 a i = = 0 a_i == 0 ai==0,也就是这个位置可以调整, d p [ i ] [ j ] = d p [ i − 1 ] [ j − 1 ] + d i s ( i , j ) dp[i][j] = dp[i-1][j-1] + dis(i,j) dp[i][j]=dp[i1][j1]+dis(i,j)对应这个位置调整一个1。

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define eb emplace_back
#define MP make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define lson rt<<1
#define rson rt<<1|1
#define CLOSE std::ios::sync_with_stdio(false)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-6;
const int N = 5010;
int a[N],n,dp[N][N];
//匹配问题的DP
//假设一个初始位置序列x1,x2,x3... 结束位置y1,y2,y3....都是有序的那么最后一定是按照顺序
//x1-y1,x2-y2,...xn - yn这样是最优的
//所以当我们当我们确定出前几个已经被占据的位置后 剩下最优的位置一定出现在后面
//设dp[i][j]表示 前i个数中已经已经匹配了j个1的最优值 因为要保证j是有序地 所以只需要在一个单独按顺序存放的数组中去选择即可
std::vector<int>v1;

int main() {
	scanf("%d",&n);
	for(int i = 1;i <= n;i ++) {
		scanf("%d",&a[i]);
		if(a[i] == 1) v1.eb(i);
	}
	memset(dp,INF,sizeof(dp));
	// dp[0][0] = 0;
	// for(int i = 0;i <= n;i ++) dp[0][i] = 0;
	for(int i = 0;i <= n;i ++) dp[i][0] = 0;
	for(int i = 1;i <= n;i ++) {
		for(int j = 1;j <= sz(v1);j ++) {//保证了有序性
			dp[i][j] = min(dp[i][j],dp[i-1][j]);//这个位置不能匹配
			if(a[i] == 0) {
				dp[i][j] = min(dp[i][j],dp[i-1][j-1] + abs(i - v1[j-1]));//
				// cout << i << ' ' << j << ' ' << dp[i][j] << '\n';
			}
		}
	}
	printf("%d\n",dp[n][sz(v1)]);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值