三班补题2

· · 个人记录

T639096 落落的去养植物

这道题直接模拟就可以了,每次加 2^i 就可以A了

#include<bits/stdc++.h>
#define int long long 
using namespace std;
int n;
int sum=0;
signed main(){
    cin>>n;
    for(int i=1;;++i){
        int cnt=1;
        for(int j=2;j<=i;++j){
            cnt*=2;
        }
//      cout<<cnt<<"\n";
        sum+=cnt;
        if(sum>n){
//          cout<<sum<<"\n";
            cout<<i<<"\n";
            return 0;
        }
    }
    return 0;
} 

T638488 数字存储

这道题直接用暴力写,循环 1 到 n,每次将 i 二进制的数字数量加到总值上,就可以了。

#include<bits/stdc++.h>

using namespace std;
/*
0 0 1
1 1 1

2 10 2
3 11 2

4 100 3
5 101 3
6 110 3
7 111 3

8 1000 4
9 1001 4
10 1010 4
11 1011 4
12 1100 4
13 1101 4
14 1110 4
15 1111 4

16 10000 5
17 10001 5
18 10010 5
19 10010 5
20 10011 6
*/
int n;
signed main(){
    cin>>n;
    int cnt=0;
    for(int i=1;i<=n;++i){
        int a=i;
        while(a){
            cnt++;
            a/=2;
        }
    }
    cout<<cnt+1<<"\n";
    return 0;
}

T638482 小梦的铁索连环

我考试时写的是直接将另一搜船的战力值加到上一搜船的战力值上。最后直接求每艘船中最大值输出就可以拿到 40 分。

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int a[N],n,m;
int maxx=-1e9;
int b[N];
signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;++i){
        cin>>a[i];
        b[i]=a[i];
    }
    for(int i=1;i<=m;++i){
        int x,y;
        cin>>x>>y;
        b[x]+=a[y];
    }
    for(int i=1;i<=n;++i){
        maxx=max(maxx,b[i]);
    }
    cout<<maxx<<"\n";
    return 0;
} 

上一个代码少了很多特判,而且每次将两艘战船连接的方式也不对,要用 map 来标记这两艘战船已经连接起来了,maxx 要在每次枚举上都要 max 一遍。因为题目上说把这些战船想象在一个平面图上,但是每个战船连接的线不可以相交。

由图可知:一个特种兵团最多有 4 艘战船,否则铁链便会相交,所以用四重循环。

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int a[N],n,m;
map<int,map<int,bool> >mp;
vector<int>q[N];
int maxx=-1e9;
signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    for(int i=1;i<=m;++i){
        int x,y;
        cin>>x>>y;
        mp[x][y]=true;
        mp[y][x]=true;
        if(x>y)swap(x,y);
        q[x].push_back(y);
    }
    for(int i=1;i<=n;++i){
        for(int j=0;j<q[i].size();++j){
            maxx=max(maxx,a[i]+a[q[i][j]]);
            for(int l=j+1;l<q[i].size();++l){
                if(mp[q[i][j]][q[i][l]]==false)continue;
                maxx=max(maxx,a[i]+a[q[i][j]]+a[q[i][l]]);
                for(int r=l+1;r<q[i].size();++r){
                    if(mp[q[i][r]][q[i][l]]==false||mp[q[i][r]][q[i][j]]==false)continue;
                    maxx=max(maxx,a[i]+a[q[i][j]]+a[q[i][l]]+a[q[i][r]]);
                }
            }
        }
    }
    cout<<maxx<<"\n";
    return 0;
}