Codeforces #690 Div. 3 ABCD 解题报告
手痒补了一下水题,来发一篇题解!
A. Favorite Sequence
题解:
给出一串数字,按要求重新排序
【左边第一个】 【右边第一个】 【左边第二个】 【右边第二个】 … 以此类推。
解法:双指针,判断条件是i < j
但是如果总长度是奇数的话,那么最后还要输出 a[n / 2 + 1] 这个数字。
详情请看代码: 👇👇👇👇
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
//最大公约数
int gcd(int x,int y) {
if(x<y) swap(x,y);//很多人会遗忘,大数在前小数在后
//递归终止条件千万不要漏了,辗转相除法
return x % y ? gcd(y, x % y) : y;
}
//计算x和y的最小公倍数
int lcm(int x,int y) {
return x * y / gcd(x, y);//使用公式
}
int ksm(int a, int b, int mod) { int s = 1; while(b) {if(b&1) s=s*a%mod;a=a*a%mod;b>>=1;}return s;}
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
const int N = 310;
int a[N];
signed main(){
int t = read();
while (t--) {
int n = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
}
for (int i = 1, j = n; i < j; ++i, --j) {
printf("%lld ", a[i]);
printf("%lld ", a[j]);
}
if (n % 2) printf("%lld", a[n / 2 + 1]);
printf("\n");
}
return 0;
}
B.Last Year’s Substring
题解:
给定一个字符串,判断该字符串是否满足,只消掉任意一段字串,就可以得到 “2020” 这个字符串。只能操作一次。所以总共只有5种情况是YES
- 2020 …
- 202 … 0
- 20 … 20
- 2 … 020
- … 2020
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
//最大公约数
int gcd(int x,int y) {
if(x<y) swap(x,y);//很多人会遗忘,大数在前小数在后
//递归终止条件千万不要漏了,辗转相除法
return x % y ? gcd(y, x % y) : y;
}
//计算x和y的最小公倍数
int lcm(int x,int y) {
return x * y / gcd(x, y);//使用公式
}
int ksm(int a, int b, int mod) { int s = 1; while(b) {if(b&1) s=s*a%mod;a=a*a%mod;b>>=1;}return s;}
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
const int N = 210;
char a[N];
signed main()
{
int t = read();
while (t--) {
int n = read();
for (int i = 1; i <= n; ++i) {
scanf("%c", &a[i]);
}
if (a[1] == '2' && a[2] == '0' && a[3] == '2' && a[4] == '0') printf("YES\n");
else if (a[1] == '2' && a[2] == '0' && a[3] == '2' && a[n] == '0') printf("YES\n");
else if (a[1] == '2' && a[2] == '0' && a[n - 1] == '2' && a[n] == '0') printf("YES\n");
else if (a[1] == '2' && a[n - 2] == '0' && a[n - 1] == '2' && a[n] == '0') printf("YES\n");
else if (a[n - 3] == '2' && a[n - 2] == '0' && a[n - 1] == '2' && a[n] == '0') printf("YES\n");
else printf("NO\n");
}
return 0;
}
C. Unique Number
题意:给定一个正数x,求最小的正整数,且所有数字的和等于x,并且所有数字都不相同(唯一)。
题解:从右往左,能取最大就取最大。注意:超过45就不可以了,因为 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MOD 1e9 + 7
using namespace std;
int read()
{
int w = 1, s = 0;
char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';ch = getchar(); }
return s * w;
}
//最大公约数
int gcd(int x,int y) {
if(x<y) swap(x,y);//很多人会遗忘,大数在前小数在后
//递归终止条件千万不要漏了,辗转相除法
return x % y ? gcd(y, x % y) : y;
}
//计算x和y的最小公倍数
int lcm(int x,int y) {
return x * y / gcd(x, y);//使用公式
}
int ksm(int a, int b, int mod) { int s = 1; while(b) {if(b&1) s=s*a%mod;a=a*a%mod;b>>=1;}return s;}
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
const int N = 210;
char a[N];
signed main()
{
int t = read();
while (t--) {
int n = read();
if (n > 45) {
printf("-1\n");
continue;
}
if (n <= 9) printf("%lld\n", n);
else if (n > 9 && n <= 17) printf("%lld9\n", n - 9);
else if (n > 17 && n <= 24) printf("%lld89\n", n - 17);
else if (n > 24 && n <= 30) printf("%lld789\n", n - 24);
else if (n > 30 && n <= 35) printf("%lld6789\n", n - 30);
else if (n > 35 && n <= 39) printf("%lld56789\n", n - 35);
else if (n > 39 && n <= 42) printf("%lld456789\n", n - 39);
else if (n > 42 && n <= 44) printf("%lld3456789\n", n - 42);
else printf("123456789\n");
}
return 0;
}