AtCoder Beginner Contest 292(补题代码)

AtCoder Beginner Contest 292(补题代码)

比赛跳转

A - CAPS LOCK

// Problem: A - CAPS LOCK
// Contest: AtCoder - AtCoder Beginner Contest 292
// URL: https://atcoder.jp/contests/abc292/tasks/abc292_a
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<string>
#include<iostream>
using namespace std;

int main()
{
	string str;
	cin >> str;
	for(int i = 0; i < str.size();i++) putchar(toupper(str[i]));
	return 0;
}

B - Yellow and Red Card

// Problem: B - Yellow and Red Card
// Contest: AtCoder - AtCoder Beginner Contest 292
// URL: https://atcoder.jp/contests/abc292/tasks/abc292_b
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, q;
	cin >> n >> q;
	vector<int> red(n + 1);
	vector<int> yellow(n + 1);
	int op, x;
	for(int i = 0 ; i < q; i++)
	{
		cin >> op >> x;
		if(op == 1) 
		{
			if(red[x]) continue;
			else
			{
				if(yellow[x] == 2) continue;
				yellow[x]++;
			}
		}
		else if(op == 2)
		{
			red[x]++;
		}
		else
		{
			if(!red[x] && yellow[x] <= 1)
			{
				cout << "No\n";
			}
			else
			{
				cout << "Yes\n";
			}
		}
	}
	
	
	return 0;
}

C - Four Variables

  • 思维模拟
// Problem: C - Four Variables
// Contest: AtCoder - AtCoder Beginner Contest 292
// URL: https://atcoder.jp/contests/abc292/tasks/abc292_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

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

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n;
	cin >> n;
	vector<int> p(n, 0);
	for(int i = 1; i * i < n;i++)
	{
		for(int j = i; i * j < n; j++)
		{
			p[i * j] += (i == j) ? 1 : 2; 
		}
	}
	
	int ans = 0;
	for(int i = 1; i < n; i++)
	{
		ans += p[i] * p[n - i];
	}	
	
	cout << ans << "\n";
	return 0;
}

D - Unicyclic Components

  • 并查集,需要维护点和边的数量
// Problem: D - Unicyclic Components
// Contest: AtCoder - AtCoder Beginner Contest 292
// URL: https://atcoder.jp/contests/abc292/tasks/abc292_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
const int N = 2e5 + 10;
int cnt;
int n, m;
struct DSU
{
	vector<int> f,siz, edge;
	DSU(int n) : f(n), siz(n, 1), edge(n){iota(f.begin(), f.end(), 0);}
	int leader(int x)
	{
		if(x != f[x])
		{
			int t = leader(f[x]);    
			// dist[x] += dist[f[x]];//至多加一次 
			f[x] = t;
		}
		return f[x];
	}
	bool same(int u, int v)
	{
		return leader(u) == leader(v);
	}
	bool merge(int u, int v)
	{
		u = leader(u);
		v = leader(v);
		if(u == v)  
		{
			edge[v]++;
			return false;
		}
		f[u] = v;
		siz[v] += siz[u];
		// dist[u] = dis;//根节点下发。
		edge[v] += edge[u];
		edge[v]++;
		return true;
	}
	
	int size(int x)
	{
		return siz[leader(x)];
	}
	int distance(int x)
	{
		return edge[leader(x)];
	}
};


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> n >> m;
	DSU dsu(N);
	for(int i = 0; i < m; i++)
	{
		int u, v;
		cin >> u >> v;
		u--, v--;
		dsu.merge(u, v);
	}
	
	int flag = 1;
	for(int i = 0; i <  n; i++)
	{
		if(i == dsu.leader(i))
		{
			 // 没有理解维护距离的并查集
			 if(dsu.size(i) != dsu.distance(i)) 
			 {
			 	flag = 0;
			 	break;
			 }
		}
	}
	if(flag) cout << "Yes\n";
	else cout << "No\n";
	
	
	
	return 0;
}

//超时做法
// #include<iostream>
// #include<vector>
// #include<map>
// using namespace std;
// 
// const int N = 2e5 + 10;
// vector<int> e[N];
// int n, m;
// map<int, map<int, int> > g;
// bool flag;
// bool vis[N];
// int cnt, w;
// //超时做法
// void dfs(int u)
// {
	// if(!vis[u]) cnt++;
	// vis[u] = 1;
	// if(g[u][u]) 
	// {
		// w += g[u][u];
		// g[u][u] = 0;
	// }
	// // cout << u << " " << w << "\n";
	// for(int i = 0; i < e[u].size(); i++)
	// {
		// int v = e[u][i];
		// if(g[u][v])
		// {
			// w++;
			// g[u][v]--;
			// g[v][u]--;
		// }
		// else continue;
		// dfs(v);
	// }
// }
// int main()
// {
	// ios::sync_with_stdio(false);
	// cin.tie(nullptr);
// 	
	// cin >> n >> m;
	// for(int i = 0 ; i < m; i++)
	// {
		// int u, v;
		// cin >> u >> v;
		// e[u].push_back(v);
		// e[v].push_back(u);
		// if(u == v) 
		// {
			// g[u][v]++;
			// continue;
		// }
		// g[u][v]++;
		// g[v][u]++;
	// }
// 	
	// for(int i = 1; i <= n; i++)
	// {
		// cnt = 0, w = 0;
		// if(!vis[i])
		// {
			// dfs(i);
			// if(cnt != w) 
			// {
				// // cout << i << " " << cnt << " " << w <<"\n";
				// flag = 1;
				// break;
			// } 
		// }
	// }
// 	
	// if(flag) cout << "No\n";
	// else cout << "Yes\n";
// 	
	// return 0;
// }

E - Transitivity

  • 读懂题意
  • 比如说2到3,然后3到1,所以2和1必须有一个直接相连的有向边
  • 注意行添加进来的可能会形成新的三个连通的,比如样例
  • 一开始2 → 4 → 3 →1 由于2能到4,4能到3,所以2到3必须连一条有向边,但是此时2→3,3→1,所以2必须也都连向1。
  • 可以一个点一个点的处理,不同点的处理互不影响
  • 用队列存储下这个点可以到达(连通)的所有点,用direct数组存储之前有连线的点。
  • 用vis存储遍历过的点,一旦之前遍历过,说明已经满足题目要求,比如

E.png

// Problem: E - Transitivity
// Contest: AtCoder - AtCoder Beginner Contest 292
// URL: https://atcoder.jp/contests/abc292/tasks/abc292_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

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

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n, m;
	cin >> n >> m;
	vector<vector<int> > e(n + 1);
	for(int i = 0; i < m; i++)
	{
		int u, v;
		cin >> u >> v;
		e[u].push_back(v);
	}
	
	int ans = 0;
	for(int u = 1; u <= n; u++)
	{
		vector<bool> direct(n + 1), vis(n + 1);
		direct[u] = 1;
		for(int v : e[u]) direct[v] = 1;
		deque<int> q = {u};
		vis[u] = 1;
		while(q.size())
		{
			int v = q.back();
			q.pop_back();
			if(!direct[v]) ans++;
			for(int w : e[v])
			{
				if(!vis[w]) vis[w] = 1, q.push_back(w);
			}
		}
	}
	
	cout << ans << "\n";
	
	return 0;
}

F - Regular Triangle Inside a Rectangle

  • 经典浮点二分
  • 多做题,培养一些思维
  • 二分角度(0~30度)
// Problem: F - Regular Triangle Inside a Rectangle
// Contest: AtCoder - AtCoder Beginner Contest 292
// URL: https://atcoder.jp/contests/abc292/tasks/abc292_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//经典二分

#include<bits/stdc++.h>
#define pi acos(-1)
using namespace std;

double f(double x, double y)
{
	return (x / cos(y / 180.0 * pi));
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	double a, b;
	cin >> a >> b;
	if(a > b) swap(a, b);
	double l = 0.0, r = 30.0;
	while(abs(r - l) > 1e-11)
	{
		double mid = (l + r) / 2.0;
		if(f(a, mid) < f(b, 30 - mid) ) l = mid;
		else r = mid;
	}
	cout << fixed << setprecision(11) << f(a, l) << "\n";
	return 0;
}

G - Count Strictly Increasing Sequences(NO SOLVE)

DP有点复杂,不是很明白

  • 等到学到一定水平,再回来复习吧
  • 比较好的代码
  • 先浅浅了解一下atcoder中的函数
  • modint表示对int型数据进行取余的函数
  • 声明:using Modint = atcoder::static_modint<998244353>;
  • 定义:\(Modint dp[41][11][41][41]\);
  • 最后用.val()显示结果
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <atcoder/modint>
using namespace std;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;

i64 GCD(i64 a, i64 b){ return b ? GCD(b,a%b) : a; }

using Modint = atcoder::static_modint<998244353>;

Modint dp[41][11][41][41];

int main(){
    int N, M; cin >> N >> M;
    vector<string> S(N); rep(i,N) cin >> S[i];
    rep(i,N) dp[M][10][i][i+1] = 1;
    rep(i,N+1) dp[M][10][i][i] = 1;
    for(int j=M-1; j>=0; j--){
        rep(i,N+1) dp[j][0][i][i] = 1;
        for(int d=0; d<10; d++){
            for(int l=0; l<=N; l++){
                for(int r=l; r<=N; r++){
                    if(l != r && S[r-1][j] != '0' + d && S[r-1][j] != '?') break;
                    for(int s=0; s<=l; s++) dp[j][d+1][s][r] += dp[j][d][s][l] * dp[j+1][10][l][r];
                }
            }
        }
    }
    Modint ans = dp[0][10][0][N];
    cout << ans.val() << endl;
    return 0;
}



struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;

Ex - Rating Estimator(线段树,NO SOLVE)

  • 基础还没打牢固
  • 之后学扎实再来补上。
posted @ 2023-03-08 15:03  哲远甄骏  阅读(66)  评论(0)    收藏  举报