AtCoder Beginner Contest 278 A-E题解
A - Shift
题意
给一个长度为
思路
看
- 如果
,则全为0 - 否则,先输出最后面
个元素,再输出 个0。
Code
#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}
using ll = long long;
// #define int long long
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 210, M = 5010;
const int inf = 0x3f3f3f3f;
inline void _A_A_() {
#ifdef LOCAL
freopen("in.in", "r", stdin);
#endif
_u_u_;
int n, m;
cin >> n >>m;
vector<int>v (n);
for (int i = 0;i < n;i++) cin >> v[i];
if (m >= n) {
for (int i = 0;i < n;i++) {
cout<< 0 << " ";
}
}
else {
for (int i = m ;i < n;i++) {
cout << v[i] << " ";
}
for (int i = 0;i < m;i++) {
cout << 0 << " ";
}
}
cout << "\n";
}
B - Misjudge the Time
题意
定义 a confusing time 为:
将24小时制下的ab:cd
变成ac:bd
,如果ac:bd
也是合法的时间,则说明ab:cd
是a confusing time。
给一个24小时制的时间ab:cd
,如果ab:cd
是a confusing time,输出ab:cd
,否则输出下一个a confusing time。
思路
先从当前时间开始到23:59
判断是否有 a confusing time ,再从00:00
到当前时间判断是否存在 a confusing time 。
Code
#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}
using ll = long long;
// #define int long long
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 210, M = 5010;
const int inf = 0x3f3f3f3f;
bool check(int n, int m) {
if (n < 0 || n >= 24) return 0;
if (m < 0 || m >= 60) return 0;
return 1;
}
inline void _A_A_() {
#ifdef LOCAL
freopen("in.in", "r", stdin);
#endif
_u_u_;
int n, m;
cin >> n >> m;
for (int i = n;i <= 23;i++) {
if (i == n) {
for (int j= m;j <= 59;j++) {
int newi = (i / 10) * 10 + j /10;
int newj = (i % 10) * 10 + j % 10;
if (check(newi, newj)) {
cout << i << " " << j << "\n";
return;
}
}
}
else {
for (int j= 0;j <= 59;j++) {
int newi = i / 10 + j /10;
int newj = (i % 10) * 10 + j % 10;
if (check(newi, newj)) {
cout << i << " " << j << "\n";
return;
}
}
}
}
for (int i = 0;i <= n;i++) {
if (i == n) {
for (int j= 0;j < m;j++) {
int newi = i / 10 + j /10;
int newj = (i % 10) * 10 + j % 10;
if (check(newi, newj)) {
cout << i << " " << j << "\n";
return;
}
}
}
else {
for (int j= 0;j <= 59;j++) {
int newi = i / 10 + j /10;
int newj = (i % 10) * 10 + j % 10;
if (check(newi, newj)) {
cout << i << " " << j << "\n";
return;
}
}
}
}
}
C - FF
题意
有
- 操作一:A follow B
- 操作二:A unfollow B
- 操作三:问A与B是否相互follow
思路
使用map<pair<int,int>,bool> mp[{a,b}]
判断A是否follow B,依次操作即可。
Code
#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}
using ll = long long;
// #define int long long
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 210, M = 5010;
const int inf = 0x3f3f3f3f;
map<pair<int,int>,bool> mp;
inline void _A_A_() {
#ifdef LOCAL
freopen("in.in", "r", stdin);
#endif
_u_u_;
int n, q;
cin >> n >> q;
while (q -- ) {
int a, b, c;
cin >> a >> b >> c;
if (a == 1) {
mp[{b,c}] = 1;
}
else if (a == 2) {
mp[{b,c}] = 0;
}
else {
if (mp[{b,c}] && mp[{c,b}]) {
cout << "Yes\n";
}
else {
cout << "No\n";
}
}
}
}
D - All Assign Point Add
题意
有长度为
- 操作一:1 val,将数组a中所有值设置为val
- 操作二:2 id val, a[id] += val
- 操作三:3 id, 输出a[id]
思路
设置层次的概念,初始时层次为0,每次操作一后层次加一。
并且在a中每一个元素都带着一个层次,初始为0。
当操作二或三时,判断a[id]的层次是否和现在的层次相符,不相等时将a[id]的层次改为现在的层次,并将a[id]的值改为当前层次的初始值val,再进行相应操作。
Code
#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}
using ll = long long;
#define int long long
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 210, M = 5010;
const int inf = 0x3f3f3f3f;
inline void _A_A_() {
#ifdef LOCAL
freopen("in.in", "r", stdin);
#endif
_u_u_;
int n;
cin >> n;
vector<pair<int,int>>v(n, {0,0});
pair<int,int> now;
now.first = 0; // 当前的层次初始化为0
now.second = -1; // 当前的val初始化为-1
for (int i= 0;i < n;i++) {
cin >> v[i].first;
}
int q;
cin >> q;
int op;
while (q --) {
cin >> op;
if (op == 1) { // 操作一
now.first++; // 层次加一
cin >> now.second; // 输入当前层次的值val
}
else if (op == 2) {
int t, val;
cin >> t >> val;
t--;
if (v[t].second == now.first) { // 层次相符
v[t].first += val;
}
else { // 层次不相符
v[t].second = now.first; // 改为现在的层次
v[t].first = now.second + val;
}
}
else {
int t;
cin >> t;
t--;
if (v[t].second == now.first) // 层次相符
cout << v[t].first << "\n";
else { // 层次不相符
v[t].second = now.first; // 该为现在的层次
v[t].first = now.second;
cout << v[t].first << "\n";
}
}
}
}
E - Grid Filling
题意
给定
依次将大小为
思路
由于
Code
#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}
using ll = long long;
// #define int long long
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
// const int N = 210, M = 5010;
const int inf = 0x3f3f3f3f;
unordered_map<int,int> mp; // 有点多余,应该设置一个数组。
inline void _A_A_() {
#ifdef LOCAL
freopen("in.in", "r", stdin);
#endif
_u_u_;
int n, m, N, dx,dy;
cin >> n >> m >> N >> dx >> dy;
vector<vector<int>> v(n + 1, vector<int> (m + 1, 0));
vector<vector<vector<int>>> sum(N + 1, vector<vector<int>> (n + 1, vector<int> (m + 1, 0))); // N个二维前缀和
for (int i= 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cin >> v[i][j];
mp[v[i][j]]++;
sum[v[i][j]][i][j]++;
}
}
for (int val = 1;val <=N;val++) {
for (int i = 1;i <= n;++i) {
for (int j = 1;j <= m;j++) {
sum[val][i][j] += sum[val][i - 1][j] + sum[val][i][j - 1] - sum[val][i - 1][j - 1]; // 求二维前缀和
}
}
}
for (int i = 1;i + dx - 1 <= n;i++) {
for (int j = 1;j + dy - 1 <= m;j++) { // 对每一个dx * dy的区域
int ans = 0;
for (int val = 1;val <= N;val++) {
int t_sum = sum[val][i + dx -1][j + dy - 1] + sum[val][i - 1][j - 1] - sum[val][i - 1][j + dy -1] - sum[val][i + dx -1][j - 1]; // 求出在被覆盖的区域中val的个数
if (mp[val] - t_sum) ans++; // 如果不是所有的val都被覆盖,答案加一
}
cout << ans << " ";
}
cout << "\n";
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?