题目地址:AtCoder Beginner Contest 284 - AtCoder
一个不知名大学生,江湖人称菜狗
original author: jacky Li
Email : 3435673055@qq.com
Time of completion:2023.1.8
Last edited: 2023.1.8
目录
题目地址:AtCoder Beginner Contest 284 - AtCoder
AtCoder Beginner Contest 284 A - E
A - Sequence of Strings
思路
参考代码
B - Multi Test Cases
思路
参考代码
C - Count Connected Components
思路
参考代码
D - Happy New Year 2023
思路
参考代码
E - Count Simple Paths
思路
参考代码
AtCoder Beginner Contest 284 A - E
A - Sequence of Strings
思路
建立一个字符串数组,依次读入,倒序输出就好。
参考代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }
const int N = 5e5+10, mod = 1e9+7, M = 1e6+5, K = 1e5+10, Z = 2e5+7;
using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI;
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;
string a[12];
void solve()
{
int n; cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
for(int i = n; i >= 1; i --) cout << a[i] << endl;
return;
}
signed main()
{
IOS; cin.tie(0); cout.tie(0);
int T = 1;
// cin >> T;
while( T -- ) solve();
return 0;
}
B - Multi Test Cases
思路
T组用例,用一个odd变量来数奇数个数,逐个输入逐个输出
参考代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }
const int N = 5e5+10, mod = 1e9+7, M = 1e6+5, K = 1e5+10, Z = 2e5+7;
using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI;
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;
void solve()
{
int n, odd = 0; cin >> n;
for(int i = 1; i <= n; i ++)
{
int a; cin >> a;
if(a & 1) odd ++;
}
cout << odd << endl;
}
signed main()
{
IOS; cin.tie(0); cout.tie(0);
int T = 1;
cin >> T;
while( T -- ) solve();
return 0;
}
C - Count Connected Components
思路
该题的输入:
5 3
1 2
1 3
4 5
输出:
2
该题是让查找连通块的个数,上面样例如下图所示。
所以运用并查集的板子可以直接求解
参考代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }
const int N = 5e5+10, mod = 1e9+7, M = 1e6+5, K = 2e5+10, Z = 1e5+7;
using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI;
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;
int p[K], st[K], ans;
void init()
{
for(int i = 1; i <= K - 5; i ++) p[i] = i;
}
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
void solve()
{
init();
int n, m; cin >> n >> m;
for(int i = 1; i <= m; i ++)
{
int a, b; cin >> a >> b;
p[find(b)] = find(a);
}
for(int i = 1; i <= n; i ++)
st[find(i)] = true;
for(int i = 1; i <= n; i ++)
if(st[i] == true) ans ++;
cout << ans << endl;
}
signed main()
{
IOS; cin.tie(0); cout.tie(0);
int T = 1;
// cin >> T;
while( T -- ) solve();
return 0;
}
D - Happy New Year 2023
思路
该题让寻找q^2 * p = n,而n的范围达到了9e18的大小,
由数学知识可得,p 和 q一定有一个值得范围小于9e18的三次跟即3e6,
而且题目中说明有唯一解,而且一定有解使题目变得简单许多,只需要对p、q同时判断,便可以
参考代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
//#define cmp [&](PII a, PII b){ return a.y < b.y; }
const int N = 5e5+10, mod = 1e9+7, M = 5e7+5, K = 2e5+10, Z = 1e5+7, X = 1.5 * 1e9;
using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI;
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;
int primes[M], cnt;
bool st[M];
void get_primes(int n)
{
for (int i = 2; i <= n; i ++ )
{
if (st[i]) continue;
primes[cnt ++ ] = i;
for (int j = i + i; j <= n; j += i)
st[j] = true;
}
}
void solve()
{
// 题目中说给的用例一定有解,且唯一解,使题目难度降低,因为读错题目写了两种
int n; cin >> n;
for(int i = 0; i < cnt; i ++)
{
int t = primes[i];
if(n % t == 0)
{
n = n / t;
if(n % t == 0)
{
int a = t, b = n / t;
cout << a << ' ' << b << endl;
break;
}
else
{
int a = sqrt(n), b = t; // 题目中给说了有唯一确定的值,前面没看见唯一确定的值。
cout << a << ' ' << b << endl;
break;
}
}
}
// int t = pow(n, 0.333333333333333333);
// int s = lower_bound(primes + 1, primes + cnt + 1, t) - (primes + 1);
// for(int i = s; i <= cnt; i ++)
// {
// if(n % (primes[i] * primes[i]) == 0)
// {cout << primes[i] << ' ' << (n / (primes[i] * primes[i])) << endl; break;}
// }
return;
}
signed main()
{
IOS; cin.tie(0); cout.tie(0);
get_primes(3e6);
int T = 1;
cin >> T;
while( T -- ) solve();
return 0;
}
E - Count Simple Paths
思路
dfs搜索,超过1e6的时候跳出,由于是递归不能使用return结束。要使用exit(0);
参考代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define IOS std::ios::sync_with_stdio(false)
#define inf 0x3f3f3f3f
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define int long long
#define x first
#define y second
#define cmp [&](PII a, PII b){ return a.y < b.y; }
const int N = 5e5+10, mod = 1e9+7, M = 5e7+5, K = 2e5+10, Z = 1e5+7, X = 1.5 * 1e9;
using namespace std;
typedef long long LL;
typedef priority_queue<int> PQI;
typedef priority_queue <int, vector<int>, greater<>> PQGI;
typedef pair<int, int> PII;
vector<vector<int>> adj(K);
vector<int> vis(K);
int ans;
int df = 0;
void dfs(int df, int x)
{
if(vis[x]) return;
ans ++;
if(ans == 1000000)
{
cout << ans << endl;
exit(0);
}
vis[x] = 1;
for(auto y : adj[x])
{
dfs(df, y);
}
vis[x] = 0;
}
void solve()
{
int n, m; cin >> n >> m;
for(int i = 1; i <= m; i ++)
{
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
// auto dfs = [&](auto dfs, int x)
// {
// if(vis[x]) return;
// ans ++;
// if(ans == 1000000)
// {
// cout << ans << endl;
// exit(0);
// }
// vis[x] = 1;
// for(auto y : adj[x])
// {
// dfs(dfs, y);
// }
// vis[x] = 0;
// };
dfs(df, 1);
cout << ans << endl;
}
signed main()
{
IOS; int T = 1;
cin.tie(nullptr);
cout.tie(nullptr);
// cin >> T;
while( T -- ) solve();
return 0;
}
作者有言
如果感觉博主讲的对您有用,请点个关注支持一下吧,将会对此类问题持续更新……