2024 蓝桥杯模拟赛3(div1+div2)

题目

A.

暴力枚举

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=2e5+10;
#define inf 0x3f3f3f3f


void solve() {
    int n,k;cin>>n>>k;
    vector<int>a(n+1);
    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(a[i]*a[j]<=k)ans++;
        }
    }
    cout<<ans;
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

B.

先按一周的算,剩下不足一周的再暴力
注意数据范围很大,用int128

#include <bits/stdc++.h>
using namespace std;

#define int __int128
const int N=2e5+10;
#define inf 0x3f3f3f3f

inline void read(__int128 &X)
{
    X = 0;
    int w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    if (w) X = -X;
}

void print(__int128 x)
{
    if (!x) return ;
    if (x < 0) putchar('-'),x = -x;
    print(x / 10);
    putchar(x % 10 + '0');
}
void solve() {
    int a,b,n;read(a);read(b);read(n);
    int tmp=a*5+b*2;
    int l=1,r=1e18,mid;
    int t=0;
  //  cout<<a<<' '<<b<<' '<<n<<"&&&"<<'\n';
    while(l<=r){
        mid=(l+r)/2;
        if(mid*tmp<=n){
            l=mid+1;
            t=mid;
         //   cout<<"mid*tmp="<<mid*tmp<<'\n';
        }
        else r=mid-1;
    }
   // cout<<"t="<<t<<'\n';
   // cout<<"t*tmp="<<t*tmp<<'\n';
    if(t*tmp==n){
        t*=7;
        print(t);
        return ;
    }
    int ans=t*7;
  //  cout<<"n="<<n<<'\n';

    n-=t*tmp;
    //cout<<"n="<<n<<'\n';
    int tt=0;
    for(int i=1;i<=7;i++){
        if(i>=1&&i<=5){
            tt+=a;
//            cout<<"n="<<n<<'\n';
            if(tt>=n){
                ans+=i;
                print(ans);
                return ;
            }
        }else{
            tt+=b;
            if(tt>=n){
                ans+=i;
                print(ans);
                return ;
            }
        }
    }
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

C.

分奇数行和偶数行
奇数行是正常排列,偶数行是反着排
先算出m和n的位置再求横竖距离差

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=2e5+10;
#define inf 0x3f3f3f3f


void solve() {
    int w,m,n;cin>>w>>m>>n;
    int up,down,l,r;
    if(m%w==0)up=m/w;
    else up=m/w+1;
    if(n%w==0)down=n/w;
    else down=n/w+1;
    if(up&1)l=m-(up-1)*w;
    else l=w-(m-(up-1)*w)+1;
    if(down&1)r=n-(down-1)*w;
    else r=w-(n-(down-1)*w)+1;
    cout<<(abs(up-down)+abs(l-r));
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

D.

可以先倒退求0年的天干地支年,这样好处理许多
然后年份模上60,再暴力

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=2e5+10;
#define inf 0x3f3f3f3f

vector<string>tg,dz;
void solve() {
    tg.push_back("jia");tg.push_back("yi");tg.push_back("bing");tg.push_back("ding");
    tg.push_back("wu");tg.push_back("ji");tg.push_back("geng");tg.push_back("xin");
    tg.push_back("ren");tg.push_back("gui");
    dz.push_back("zi");dz.push_back("chou");dz.push_back("yin");dz.push_back("mao");
    dz.push_back("chen");dz.push_back("si");dz.push_back("wu");dz.push_back("wei");
    dz.push_back("shen");dz.push_back("you");dz.push_back("xu");dz.push_back("hai");
    int n;cin>>n;
    n%=60;
    for(int i=6,j=0;j<=n;i++,j++){
        if(j==n) {
            int ii = i % 10;
            cout<<tg[ii];
            break;
        }
    }
    for(int i=8,j=0;j<=n;i++,j++){
        if(j==n) {
            int ii = i % 12;
            cout<<dz[ii];
            break;
        }
    }
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

E.

三层暴力循环,最后一层可以直接判

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=2e5+10;
#define inf 0x3f3f3f3f

void solve() {
    int n;cin>>n;
    for(int i=0;i*i<=n;i++){
        for(int j=0;i*i+j*j<=n;j++){
            for(int k=0;i*i+j*j+k*k<=n;k++){
                int tmp=sqrt(n-i*i-j*j-k*k);
                if(tmp>=0){
                    if(tmp*tmp==n-i*i-j*j-k*k){
                        cout<<i<<' '<<j<<' '<<k<<' '<<tmp;
                        return ;
                    }
                }
            }
        }
    }
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

F.

双指针

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=2e5+10;
#define inf 0x3f3f3f3f

void solve() {
    string s,t;cin>>s>>t;
    int j=0;
    for(int i=0;i<s.size()&&j<t.size();i++){
        if(s[i]==t[j])j++;
    }
    cout<<j;
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

G.

跟方格其实无关,直接判图有几个连通块

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e6+10;
#define inf 0x3f3f3f3f

int m,n,k;
vector<int>g[N];
vector<int>vis(N);
void dfs(int u){
    vis[u]++;
    for(int i=0;i<g[u].size();i++){
        int y=g[u][i];
        if(vis[y])continue;
        vis[y]++;
        dfs(y);
    }
}
void solve() {
    cin>>m>>n>>k;
    for(int i=0,a,b;i<k;i++){
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int ans=0;
    for(int i=1;i<=n*m;i++){
        if(vis[i])continue;
        ans++;
        dfs(i);
    }
    cout<<ans;
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

H.

可以确定一定要用n/2个编号
记录下当前编号用了2次及以上的数量a,和只用了一次的数量b
说明还有n/2-a个不满足条件,设为c
若c小等于b,那么只要改动c次即可(让c对两两配对)
若c大于b,那么改动b次后,还要改动2*(c-b)次(满足两两配对)

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e6+10;
#define inf 0x3f3f3f3f

void solve() {
    int n;cin>>n;
    map<int,int>mp;
    for(int i=0,x;i<n;i++){
        cin>>x;
        mp[x]++;
    }
    int ans=0,tmp=0;
    for(auto t:mp){
        if(t.second>=2)ans++;
        else tmp++;
    }
    if(ans==n/2){
        cout<<"0\n";return ;
    }
    int t=n/2-ans;
    if(t<=tmp){
        cout<<t<<'\n';
        return ;
    }else{
        cout<<(tmp+(t-tmp)*2)<<'\n';
        return ;
    }
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

H.

先读错题意了,wa了几发
暴力跑每一条起点到终点的路径,记录下路径上的点背经过的次数,
若次数与总路径相同,说明它是关键点

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e3+10;
#define inf 0x3f3f3f3f

int n,m;
int cnt[N],path[N],vis[N];
vector<int>g[N];
int total;
int st,e;
void dfs(int u,int en,int id){
    if(u==en){
        for(int i=0;i<id-1;i++){
            cnt[path[i]]++;
        }
        total++;
        return ;
    }
    for(int i=0;i<g[u].size();i++){
        int y=g[u][i];
        if(!vis[y]){
            vis[y]=true;
            path[id]=y;
            dfs(y,en,id+1);
            vis[y]=false;
        }
    }
}
void solve() {
    cin>>n>>m;
    for(int i=1;i<N;i++){
        vis[i]=0;cnt[i]=0;g[i].clear();total=0;
    }
    for(int i=0,u,v;i<m;i++){
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>st>>e;
    dfs(st,e,0);
    if(total==0){
        cout<<-1;return ;
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        if(cnt[i]==total)ans++;
    }
    cout<<ans;
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

I.

找规律

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e3+10;
#define inf 0x3f3f3f3f
const int mod=1e9+7;
void solve() {
    int n;cin>>n;
    int ans=0;
    for(int i=1;i<=n;i++){
        ans=(ans+(i*(n-i)*(n-i))%mod)%mod;
    }
    cout<<ans;
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

J.

数据范围小,可以暴力跑

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e3+10;
#define inf 0x3f3f3f3f
const int mod=1e9+7;
int dp[5010][5010];
void solve() {
    string s;cin>>s;
    int r=s.size()-1;
    int ans=0;
    for(int i=r;i>=0;i--){
        for(int j=i+1;j<=r;j++){
            if(s[i]>s[j])dp[i][j]=1;
            else if(s[i]<s[j])dp[i][j]=0;
            else dp[i][j]=dp[i+1][j-1];
            if(dp[i][j]==1)ans++;
        }
    }
    cout<<ans;
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}
posted @ 2024-02-26 17:55  WW爆米花  阅读(55)  评论(0编辑  收藏  举报