[Atcoder] ABC225

A-Distinct Strings

image

代码

#include<bits/stdc++.h>
using namespace std;

int main(){
	string str;
	cin >> str;
	if(str[0] == str[1] && str[1] == str[2]) cout << 1 << endl;
	else if(str[0] == str[2] || str[1] == str[2] || str[0] == str[1]) cout << 3 << endl;
	else cout << 6 << endl;
	
	return 0;
} 

B-Star or Not

image
image

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int t[N], a[N], b[N];
int main(){
	int n;
	cin >> n;
	for(int i = 1; i < n; i ++) {
		cin >> a[i] >> b[i];
		t[a[i]] ++;
		t[b[i]] ++;
	}
	for(int i = 1; i <= n; i ++) {
		if(t[i] == n - 1){
			cout << "Yes" << endl;	
			return 0;
		}
	}
	cout << "No" << endl;
	
	return 0;
}

C-Calendar Validator

image
image

思路

题目意思就是有10^100次方为行的7列数据作为A矩阵,判断B矩阵是否出现在A矩阵中。
求出B矩阵第一个数的应当的起始相对位置,判断B矩阵的列数是否超过A矩阵(这里要注意对第一个数求起始相对位置时要注意取值范围是1~7),接下来再进行遍历判断即可

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
typedef long long ll;
ll b[N][N];
int main(){
	int n, m;
	cin >> n >> m;
	ll sta = 0;
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= m; j ++) {
			cin >> b[i][j];
			if(i == 1 && j == 1) {
				if(b[i][j] <= 7) sta = b[i][j];
				else sta = b[i][j] - (b[i][j] / 7) * 7;
				if(sta == 0) sta = 7;
			} 
		}
	}
	//cout << sta << endl;
	if(m == 1 && n == 1) {
		cout << "Yes" << endl;
		return 0;
	}
	if((m + sta - 1) > 7) {
		//cout << 1 << endl;
		cout << "No" << endl;
		return 0;
	}
	bool flag = true;
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= m; j ++) {
			if(i == 1 && j != 1 && b[i][j] != b[i][j - 1] + 1) flag = false;
			else if(i != 1 && j == 1 && b[i][j] != b[i- 1][j] + 7) flag = false;
			else if(i != 1 && j != 1 && b[i][j] != b[i][j - 1] + 1) flag = false;
		}
	}
	if(flag == true) cout << "Yes" << endl;
	else cout << "No" << endl;
	
	return 0;
}

D-Play Train

image
image
image

思路

相当于是一个链表的处理,开一个二维数组表示当前数的前驱和后继,如果是1操作直接将x的后继指向y,将y的前驱指向x;如果是2操作,将x的后继为-1,y的前驱为-1;如果是3操作,通过dfs进行遍历,对前驱遍历,对后继也进行遍历

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> e1[N], e2[N], ans1, ans2;
int ne[N][3];
void dfs1(int x) {
	ans2.push_back(x);
	if(ne[x][1] != -1) {
		int y = ne[x][1];
		dfs1(y);
	}
}
void dfs2(int x) {
	int y = ne[x][2];
	if(y != -1) ans1.push_back(y), dfs2(y);
}
int main(){
	int n, q;
	cin >> n >> q;
	int op, x, y;
	memset(ne, -1, sizeof ne);
	while(q --) {
		cin >> op;
		if(op == 1) {
			cin >> x >> y;
			ne[x][1] = y;
			ne[y][2] = x;
		}
		else if(op == 2) {
			cin >> x >> y;
			ne[x][1] = -1;
			ne[y][2] = -1;
		}
		else {
			cin >> x;
			ans1.clear(), ans2.clear();
			dfs1(x);
			dfs2(x);
			cout << ans1.size() + ans2.size() << " ";
			for(int i = ans1.size() - 1; i >= 0; i --) {
				cout << ans1[i] << " ";
			}
			for(int i = 0; i < ans2.size(); i ++) {
				cout << ans2[i] << " ";
			}
			cout << endl;
		}
	}
	
	return 0;
} 

E-7

image
image

思路

题目大意是给出n个点,每个点(x, y) 与 (x - 1, y), (x, y - 1)组成一个“7”形,判断从原点出发最多能看到多少个“7”
每个点以两个斜率确定这个点的范围(求斜率的时候要用long double,不然会wa,long double精度更高),在输入时求出这两个斜率,再进行贪心

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
#define double long double
struct Node {
	double l, r;
}a[N];
bool cmp(Node x, Node y) {
	if(x.l == y.l) return x.r < y.r;
	return x.l < y.l;
}
int main(){
	int n;
	cin >> n;
	for(int i = 1; i <= n; i ++) {
		long long x, y;
		scanf("%lld%lld", &x, &y);
		a[i].l = ((double)(y) / (double)(x - 1.0));
		a[i].r = ((double)(y - 1.0) / (double)(x));
	}
	sort(a + 1, a + 1 + n, cmp);
	double si = a[1].l;
	long long ans = 1;
	for(int i = 2; i <= n; i ++) {
		if(a[i].r >= si) ans ++, si = a[i].l;
	}
	cout << ans << endl;
	
	return 0;
}

F-String Cards

image

思路

题目大意是给出n个字符串,求取k个字符串组成的最小字典序的字符串
通过dp来求解,dp[k]表示从前i个字符串里取k个的最小字符串

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100;
bool cmp(string a, string b) {
	return a + b > b + a;
}
int main(){
	int n, k;
	cin >> n >> k;
	string dp[N];
	string str[N];
	for(int i = 1; i <= n; i ++) {
		cin >> str[i];
	}
	for(int j = 1; j <= k; j ++) {
		dp[j] = '{';
	}
	sort(str + 1, str + 1 + n, cmp);
	//dp[1][1] = str[1];
	for(int i = 1; i <= n; i ++) {
		for(int j = i; j >= 1; j --) {
			dp[j] = min(dp[j], str[i] + dp[j - 1]);
		}
	}
	cout << dp[k] << endl;
	
	return 0;
}
posted @ 2021-11-01 22:09  行舟C  阅读(397)  评论(0)    收藏  举报