题目列表
- A. 六一的礼物
- B. 挑战
- C. 游戏
- D. 涂色
- E. 区间
- F. 摘花
A. 六一的礼物(入门)
code
简单分支结构,代码如下。
#include <bits/stdc++.h>
#define endl '\n'
#define r0 return 0
#define inf (0x7fffffff)
#define maxn (+5)
#define int long long
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
int n;
signed main() {
cin >> n;
if (n < 10) {
cout << "A";
} else if (n < 20) {
cout << "B";
} else if (n < 40) {
cout << "C";
} else if (n < 50) {
cout << "D";
} else if (n < 80) {
cout << 'E';
}
cout << endl;
return 0;
}B. 挑战
code(字符串模拟)
简单模拟,注意取模运算。
#include<bits/stdc++.h>
#define endl '\n'
#define r0 return 0
#define inf (0x7fffffff)
#define maxn ( 100000 +5)
#define int long long
using namespace std;
inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}
int n;
signed main(){
cin >> n;
char ch[n+5];
cin >> ch;
for(int i=0;i<n;i++){
if('a'<=ch[i] && ch[i]<='z'){
cout << (char)(ch[i]+'A'-'a');
}
if('A'<=ch[i] && ch[i]<='Z'){
cout << (char)(ch[i]+'a'-'A');
}
if('0'<=ch[i] && ch[i]<='9'){
int x=ch[i]-'0',num=(i+1)%10;
//cout << ' ' << x << ' ';
x+=10-num;
//cout << "(" << x << ")";
x%=10;
cout << x;
}
}
return 0;
}C. 游戏
解法(模拟)
用桶记录每一个数字的出现次数,然后一遍一遍的把顺子挑出来删点,直到最后没有顺子。
#include<bits/stdc++.h>
#define endl '\n'
#define r0 return 0
#define inf (0x7fffffff)
#define maxn ( 1000 +5)
#define int long long
using namespace std;
inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}
int n,a[maxn],vis[maxn],minn=2147,maxnum=-1;
signed main(){
cin >> n;
for(int i=1;i<=n;i++){
cin >> a[i];
vis[a[i]]++;
maxnum=max(maxnum,a[i]);
}
int ans=0;
/*for(int i=1;i<=maxnum;i++){
cout << vis[i] << ' ';
}*/
while(1){
minn=-1;
for(int i=1;i<=maxnum;i++){
if(vis[i]>0){
if(vis[i+1]>0){
minn=i;break;
}else{
vis[i]=0;
}
}
}if(minn==-1){break;}
ans++;
for(int i=minn;vis[i]>0;i++){vis[i]--;}
//cout << ans << ' ';
}
cout << ans;
}D. 涂色
解法(数学)
组合数 为奇数当且仅当在二进制表示下, 的每一位都不大于 的对应位。即 (n & k) == k。
code
#include<bits/stdc++.h>
using namespace std;
int main(){
int N, M;
cin >> N >> M;
while(M--) {
int L, R;
cin >> L >> R;
int m = L - 1;
int n = R - 1;
if ((m & n) == n) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}E. 区间(没数据范围不敢做)
F. 摘花
解法(双指针)
双指针模板题,当不满足数量要求时,右指针右移,满足后让左指针右移使区间内白花数量最少,每次统计最小答案。
code
#include<bits/stdc++.h>
#define endl '\n'
#define r0 return 0
#define inf (0x3f)
#define maxn ( 10000000 +5)
#define int long long
using namespace std;
inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}
int n,x,y,a[maxn];
signed main(){
cin >> n >> x >> y;
for(int i=1;i<=n;i++){
cin >> a[i];
}
int l=1,r=1,xnum=0,ynum=0,wnum=0,ans=2147483647;
for(r=1;r<=n;r++){
if(a[r]==1){
xnum++;
}else if(a[r]==2){
ynum++;
}else if(a[r]==0){
wnum++;
}
while(l<=r && xnum>=x && ynum>=y){
ans=min(ans,wnum);
if(a[l]==1){
xnum--;
}else if(a[l]==2){
ynum--;
}else if(a[l]==0){
wnum--;
}
l++;
}
}
cout << ans;
}