2022有道小图灵信息学水平测试(二)题解
比赛地址
K
看看有多少个数小于等于 \(a_k\) 即可
#include<bits/stdc++.h>
using namespace std;
//#define int long long
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<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 1010
int n, m, i, j, k, T;
int a[N];
signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read(); k=read();
for(i=1; i<=n; ++i) a[i]=read();
for(i=1; i<=n; ++i) if(a[i]<=a[k]) ++j;
printf("%d", j);
return 0;
}
L
显然,0全丢前面,1全丢后面
#include<bits/stdc++.h>
using namespace std;
//#define int long long
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<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 100010
int n, m, i, j, k, T;
char a[N];
signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read();
scanf("%s", a+1);
// for(i=1; <=n; ++i) a[i]=read();
sort(a+1, a+n+1);
for(i=1; i<=n; ++i) printf("%c", a[i]);
return 0;
}
M
用个map先存起来,最后判断在不在里面即可
#include<bits/stdc++.h>
using namespace std;
//#define int long long
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<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
//#define N
int n, m, i, j, k, T;
signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
T=read();
while(T--)
{
map<string, int>mp;
string s, c;
n=read();
for(i=1; i<=n; ++i)
{
cin>>s; mp[s]=1;
c+=s[0];
}
printf(mp[c] ? "yes\n" : "no\n");
}
return 0;
}
N
DP,设 \(dp(i,0/1)\) 代表前 \(i\) 枚硬币里最后一枚是反面/正面朝上的方案数
\[\Large dp(i,0)=dp(i-1,1)\\\Large dp(i,1)=dp(i-1, 0)+dp(i-1, 1)
\]
#include<bits/stdc++.h>
using namespace std;
#define int long long
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<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 40
int n, m, i, j, k, T;
int dp[N][2];
signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read();
dp[1][1]=dp[1][0]=1;
for(i=2; i<=n; ++i)
{
dp[i][1]=dp[i-1][1]+dp[i-1][0];
dp[i][0]=dp[i-1][1];
}
printf("%lld", dp[n][0]+dp[n][1]);
return 0;
}
O
按题意所说排序即可
#include<bits/stdc++.h>
using namespace std;
//#define int long long
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<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 100010
struct node
{
int x, y, a, b, c;
string s;
}a[N];
int n, m, i, j, k, T;
bool cmp(node x, node y)
{
if(x.a==y.a) {
if(x.b==y.b) {
if(x.c==y.c) return x.s<y.s;
return x.c<y.c;
}
return x.b<y.b;
}
return x.a<y.a;
}
signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read();
for(i=1; i<=n; ++i)
{
m=read();
for(j=1; j<=m; ++j)
{
a[++k].x=i; a[k].y=j;
cin>>a[k].s;
a[k].a=read(); a[k].b=read(); a[k].c=read();
}
}
sort(a+1, a+k+1, cmp);
for(i=1; i<=k; ++i)
cout<<a[i].s, printf(" %d %d\n", a[i].x, a[i].y);
return 0;
}
本文来自博客园,作者:zhangtingxi,转载请注明原文链接:https://www.cnblogs.com/zhangtingxi/p/16532563.html