题目列表
说明
好久没打蓝桥杯的比赛,回来试试水,就开了第1、2、3一共三个题,第4题可惜了。
1.thanks,mom【算法赛】
思路:
没什么好说的,但是当时比赛刚开始服务器有问题,基本提交的全WA了。
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
using ull = unsigned long long;
const int N = 1e5+5,mod = 1e9+7,INF = 0x3f3f3f3f;
void solve(){
cout << "thanks,mom";
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T = 1;
while(T--){
solve();
}
return 0;
}
2.霓虹【算法赛】
说明
这个题,没啥好特别的想法,我想的是把'0'到'9'的每一笔画是否出现置成0和1(有该笔画为1,否则为0),然后遍历两个字符串比较。
参考代码
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 1e5+5,mod = 1e9+7,INF = 0x3f3f3f3f;
int cnt[10][7]={{1,1,1,0,1,1,1},{0,0,1,0,0,1,0},{1,0,1,1,1,0,1},
{1,0,1,1,0,1,1},{0,1,1,1,0,1,0},{1,1,0,1,0,1,1},{1,1,0,1,1,1,1},
{1,0,1,0,0,1,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
void solve(){
string s1,s2;
cin >> s1 >> s2;
int ans = 0;
for(int i = 0;i<s1.size();i++){
char c1=s1[i],c2=s2[i];
if(c1==c2) continue;
else{
for(int j = 0;j<7;j++){
if(cnt[c1-'0'][j]!=cnt[c2-'0'][j]) ans++;
}
}
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T = 1;
while(T--){
solve();
}
return 0;
}
3. 奇偶排序【算法赛】
思路
没啥好说的,存进数组后sort()一下,自己写一个cmp()比较函数即可
参考题解
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 1e3+5,mod = 1e9+7,INF = 0x3f3f3f3f;
int a[N];
int n;
bool cmp(int a1,int a2){
if(a1%2==1&&a2%2==1) return a1<a2;
else if(a1%2==0&&a2%2==0) return a1<a2;
else if(a1%2==0&&a2%2==1) return false;
else if(a1%2==1&&a2%2==0) return true;
}
void solve(){
cin >> n;
for(int i = 0;i<n;i++) cin >> a[i];
sort(a,a+n,cmp);
for(int i = 0;i<n;i++) cout << a[i] << " \n"[i==n-1];
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T = 1;
while(T--){
solve();
}
return 0;
}
4. 可结合的元素对【算法赛】
说明
最简单的方法是写两个循环暴力跑,我也是这么做的,但是当时比赛环境可能有问题,一直过不去,比赛完了重新提交就AC了;用map或者onordered_map也可以跑出来,不过当时比赛的时候没想到(其中用unordered_map速度最快,map速度最慢)
参考题解1
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e5+5;
int n;
int a[N];
int lowbit(int x){return x&(-x);}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
for(int i = 1;i<=n;i++) cin >> a[i];
int ans = 0;
for(int i = 1;i<=n;i++){
for(int j = i+1;j<=n;j++){
if(lowbit(a[i]+a[j])==a[i]+a[j]) ans++;
}
}
cout << ans;
return 0;
}
参考题解2
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 1e5+5,mod = 1e9+7,INF = 0x3f3f3f3f;
map<ll,ll> mp;
void solve(){
int n;cin >> n;
ll ans = 0;
for(int i = 1;i<=n;i++){
ll x;cin >> x;
for(int j = 0;j<32;j++){
ll y = 1LL<<j;
ans+=mp[y-x];
}
mp[x]++;
}
cout << ans;
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T = 1;
while(T--){
solve();
}
return 0;
}
参考题解3
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 1e5+5,mod = 1e9+7,INF = 0x3f3f3f3f;
unordered_map<ll,ll> mp;
void solve(){
int n;cin >> n;
ll ans = 0;
for(int i = 1;i<=n;i++){
ll x;cin >> x;
for(int j = 0;j<32;j++){
ll y = 1LL<<j;
ans+=mp[y-x];
}
mp[x]++;
}
cout << ans;
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T = 1;
while(T--){
solve();
}
return 0;
}
5. 兽之泪【算法赛】
思路
用堆实现每次取到的都是最小的,但是前k-1个要考虑是否是第一次打败的问题,以及考虑是否要打第k个怪物的问题
参考题解
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
using PII = pair<int,int>;
const int N = 2e5+5;
int k,n;
PII a[N];
priority_queue<PII,vector<PII>,greater<PII> > heap1;
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> k >> n;
for(int i = 1;i<=k;i++){
int x,y;cin >> x >> y;
a[i]={x,y};
}
for(int i = 1;i<=k-1;i++){
PII t1 = {a[i].first,i};
heap1.push(t1);
}
int ans1 = 0;
for(int i = 1;i<=n;i++){
auto t=heap1.top();
ans1+=t.first;
if(t.second!=-1){
heap1.pop();
PII t1 = {a[t.second].second,-1LL};
heap1.push(t1);
}
}
int ans2 = 0;
priority_queue<PII,vector<PII>,greater<PII> > heap2;
for(int i = 1;i<=k;i++){
ans2+=a[i].first;
heap2.push({a[i].second,-1LL});
n--;
}
for(int i = 1;i<=n;i++){
auto t1 = heap2.top();
ans2+=t1.first;
}
cout << min(ans1,ans2);
return 0;
}
6. 矩阵X【算法赛】
说明
这个题用二维前缀和加滑动窗口来写最便捷
参考题解(从大佬那copy来的)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define pp pair<int,int>
const int N=1e6+10;
const int M=(1ll<<31)-1;
int n,m,x,y,k,z,o;
const int mo=1e9+7;
struct w{
int x,y;
}a[N];
void sovel()
{
cin>>n>>m>>k>>o;
vector<vector<int> > v(n+1,vector<int>(m+1));
vector<vector<int> > sum(n+1,vector<int>(m+1));
vector<vector<int> > dpa(n+1,vector<int>(m+1));
vector<vector<int> > dpb(n+1,vector<int>(m+1));
auto getsum=[&](int x,int y){
if(x<k||y<o)
{
return 0ll;
}
return sum[x][y]-sum[x-k][y]-sum[x][y-o]+sum[x-k][y-o];
};
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>v[i][j];
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+v[i][j];
}
}
for(int j=1;j<=m;j++)
{
int l=0,r=-1;
for(int i=1;i<=n;i++)
{
x=v[i][j];
while(l<=r&&a[r].x<=x)
{
r--;
}
r++;
a[r]={x,i};
while(i-a[l].y+1>k)
{
l++;
}
dpa[i][j]=a[l].x;
}
}
for(int i=k;i<=n;i++)
{
int l=0,r=-1;
for(int j=1;j<=m;j++)
{
x=dpa[i][j];
while(l<=r&&a[r].x<=x)
{
r--;
}
r++;
a[r]={x,j};
while(j-a[l].y+1>o)
{
l++;
}
dpb[i][j]=a[l].x;
}
}
int manx=0;
for(int j=1;j<=m;j++)
{
int l=0,r=-1;
for(int i=1;i<=n;i++)
{
x=-v[i][j];
while(l<=r&&a[r].x<=x)
{
r--;
}
r++;
a[r]={x,i};
while(i-a[l].y+1>k)
{
l++;
}
dpa[i][j]=a[l].x;
}
}
for(int i=k;i<=n;i++)
{
int l=0,r=-1;
for(int j=1;j<=m;j++)
{
x=dpa[i][j];
while(l<=r&&a[r].x<=x)
{
r--;
}
r++;
a[r]={x,j};
while(j-a[l].y+1>o)
{
l++;
}
manx=max(manx,(dpb[i][j]+a[l].x)*getsum(i,j));
}
}
cout<<manx;
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
while(t--)
{
sovel();
}
}