文章目录
A. Marathon

打卡题
#include <bits/stdc++.h>
#define pt2(x,y) cout <<(x)<<"--"<<(y)<<endl;
#define pt1(x) cout <<"#"<<(x)<<"#"<<endl;
#define hh cout << "\n"
#define rep(i,a,n) for(int i = (a); i <= int(n); i++)
#define per(i,a,n) for(int i = (n); i >= int(a); i--)
#define fi first;
#define se second;
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double DB;
const int N=1e6+10,M=1e6+10,INF=0x3f3f3f3f;
int t=1;
void solve()
{
int a,b,c,d;
cin >> a >> b >> c >> d;
int ans=0;
if(b>a) ans++;
if(c>a) ans++;
if(d>a) ans++;
cout << ans <<endl;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> t;
while(t--)
{
solve();
}
return 0;
}
B. All Distinct

分奇数偶数,奇数可以自己解决最后只剩一个,偶数个数2个配对消,向下取整
#include <bits/stdc++.h>
#define pt2(x,y) cout <<(x)<<"--"<<(y)<<endl;
#define pt1(x) cout <<"#"<<(x)<<"#"<<endl;
#define hh cout << "\n"
#define rep(i,a,n) for(int i = (a); i <= int(n); i++)
#define per(i,a,n) for(int i = (n); i >= int(a); i--)
#define fi first;
#define se second;
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double DB;
const int N=1e6+10,M=1e6+10,INF=0x3f3f3f3f;
int t=1;
unordered_map<int,int> h;
int cnt;
int find(int x)
{
if(h.count(x)) return h[x];
return h[x]=cnt++;
}
void solve()
{
h.clear();
cnt=1;
int n;
int st[100]={0};
cin >> n;
int ans=0;
rep(i,1,n)
{
int x;
cin >> x;
st[find(x)]++;
}
int ji=0,ou=0;
rep(i,1,50)
{
if(st[i]==0) continue;
if(st[i]%2==0) ou++;
else ji++;
}
ans=ji+(ou/2)*2;
cout << ans<<endl;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> t;
while(t--)
{
solve();
}
return 0;
}
C. Where’s the Bishop?

找前一行有2个当前这行只有一个的位置
#include <bits/stdc++.h>
#define pt2(x,y) cout <<(x)<<"--"<<(y)<<endl;
#define pt1(x) cout <<"#"<<(x)<<"#"<<endl;
#define hh cout << "\n"
#define rep(i,a,n) for(int i = (a); i <= int(n); i++)
#define per(i,a,n) for(int i = (n); i >= int(a); i--)
#define fi first;
#define se second;
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double DB;
const int N=1e6+10,M=1e6+10,INF=0x3f3f3f3f;
int t=1;
char g[10][10];
void solve()
{
rep(i,0,7) cin >> g[i];
int last=0;
rep(i,0,7)
{
int res=0;
int x,y;
rep(j,0,7)
{
if(g[i][j]=='#')
{
res++;
x=i,y=j;
}
}
if(res==1&& last==2)
{
cout << x+1<<" "<<y+1<<endl;
break;
}
last=res;
}
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> t;
while(t--)
{
solve();
}
return 0;
}
D. The Clock

简单的模拟题
#include <bits/stdc++.h>
#define pt2(x,y) cout <<(x)<<"--"<<(y)<<endl;
#define pt1(x) cout <<"#"<<(x)<<"#"<<endl;
#define hh cout << "\n"
#define rep(i,a,n) for(int i = (a); i <= int(n); i++)
#define per(i,a,n) for(int i = (n); i >= int(a); i--)
#define fi first;
#define se second;
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double DB;
const int N=1e6+10,M=1e6+10,INF=0x3f3f3f3f;
int t=1;
bool check(int i,int j)
{
int i1=i%10,i2=i/10;
int j1=j%10,j2=j/10;
if(j2==i1 && j1 == i2) return true;
return false;
}
void change(int &h,int &m,int x)
{
int dh = x/60;
int dm = x%60;
h=dh+h;
m=dm+m;
if(m>=60)
{
m=m%60;
h++;
}
if(h>=24)
{
h=h%24;
}
}
void solve()
{
int h,m,x;
scanf("%d:%d",&h,&m);
scanf("%d",&x);
int ans=0;
int ch=h,cm=m;
bool one=true;
while(one||h!=ch || m !=cm)
{
one=false;
change(h,m,x);
if(check(h,m)) ans++;
}
cout << ans<<endl;
}
int main()
{
// cin.tie(0);
// cout.tie(0);
// ios::sync_with_stdio(0);
cin >> t;
while(t--)
{
solve();
}
return 0;
}
E. Binary Deque

前缀和加枚举区间
#include <bits/stdc++.h>
#define pt2(x,y) cout <<(x)<<"--"<<(y)<<endl;
#define pt1(x) cout <<"#"<<(x)<<"#"<<endl;
#define hh cout << "\n"
#define rep(i,a,n) for(int i = (a); i <= int(n); i++)
#define per(i,a,n) for(int i = (n); i >= int(a); i--)
#define fi first;
#define se second;
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double DB;
const int N=1e6+10,M=1e6+10,INF=0x3f3f3f3f;
int t=1;
int a[N],b[N];
void solve()
{
int n,m;
cin >> n >> m;
rep(i,1,n)
{
int x;
cin >> x;
a[i]=a[i-1]+x;
}
if(a[n]<m)
{
cout <<-1<<endl;
return;
}
rep(i,1,n) b[i]=-1; // b表示值为i的数第一次出现的位置
b[0]=0;
int ans=n;
rep(r,1,n)
{
if(a[r]-m >= 0 && b[a[r]-m]!=-1) ans=min(ans,(n-(r-b[a[r]-m])));
if(b[a[r]]==-1) b[a[r]]=r;
}
cout << ans<<endl;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> t;
while(t--)
{
solve();
}
return 0;
}
F. 3SUM

和的最后一位只与每一个数的最后一个数有关,打表找规律,分类讨论
#include <bits/stdc++.h>
#define pt2(x,y) cout <<(x)<<"--"<<(y)<<endl;
#define pt1(x) cout <<"#"<<(x)<<"#"<<endl;
#define hh cout << "\n"
#define rep(i,a,n) for(int i = (a); i <= int(n); i++)
#define per(i,a,n) for(int i = (n); i >= int(a); i--)
#define fi first;
#define se second;
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double DB;
const int N=1e6+10,M=1e6+10,INF=0x3f3f3f3f;
int t=1;
void solve()
{
int st[15]={0};
int n;
cin >> n;
rep(i,1,n)
{
int x;
cin >> x;
st[x%10]++;
}
if(st[1]>=3)
{
cout <<"YES\n";
return ;
}
rep(i,0,9)
{
if(st[i]>=2)
{
rep(j,0,9)
if((2*i+j)%10==3)
{
if(st[j]==0 || i==j) continue;
cout <<"YES\n";
return;
}
}
}
rep(i,0,9) rep(j,i+1,9) rep(k,j+1,9)
{
if(st[i]==0||st[j]==0||st[k]==0) continue;
if((i+j+k)%10==3)
{
cout <<"YES\n";
return ;
}
}
cout << "NO\n";
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> t;
while(t--)
{
solve();
}
return 0;
}
G. 2^Sort

枚举左端点向右是否都满足条件
#include <bits/stdc++.h>
#define pt2(x,y) cout <<(x)<<"--"<<(y)<<endl;
#define pt1(x) cout <<"#"<<(x)<<"#"<<endl;
#define hh cout << "\n"
#define rep(i,a,n) for(int i = (a); i <= int(n); i++)
#define per(i,a,n) for(int i = (n); i >= int(a); i--)
#define fi first;
#define se second;
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double DB;
const int N=1e6+10,M=1e6+10,INF=0x3f3f3f3f;
int t=1;
int a[N],b[N];
void solve()
{
int n,k;
cin >> n >> k;
rep(i,1,n) cin >> a[i];
int ans=0;
rep(i,1,n-1)
{
if(a[i]<2*a[i+1]) b[i]=b[i-1]+1;
else b[i]=0;
if(b[i]>=k) ans++;
}
cout << ans<<endl;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> t;
while(t--)
{
solve();
}
return 0;
}
本文探讨了五种编程问题:马拉松算法的计数,元素唯一性的处理,寻找象棋棋子位置,时钟操作模拟,以及二进制双端队列操作。通过实例展示了如何解决这些与数据结构、算法和逻辑思维相关的问题。
491

被折叠的 条评论
为什么被折叠?



