8.21 总结

· · 个人记录

T639096 落落的去养植物

思路

每次增长的高度都 \times 2,看看哪天严格大于 H。

注意这里是求早上那么天数要加一。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int now,h; 
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>h;
    int tmp=1;
    now=1;
    for(int i=2;;i++){
        tmp=tmp*2;
        now+=tmp;
        if(now>h){
            cout<<i;
            return 0;
        }
    }
    return 0;
}

T2 T638488 数字存储

思路

这道题求的是最终这个数的二进制有多少位,那就很简单了。我们可以结合昨天的某道二进制的题,我们就知道当 i=2^n 时,二进制下的 i 会多一位,变成类似 1000...0 的数,直到 i=2^{n+1} 就再多一位。

所以我们在循环里面算,最终输出答案就行了。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //只有在 2^x 的情况下才会增加位数
    int n,tmp=2,ans=0,wei=1;
    cin>>n;
    if(n==1){
        cout<<2;
        return 0;
    }
    ans=2; //算上 0 和 1 
    for(int i=2;i<=n;i++){
        if(i==tmp){ //增加位数 
            tmp=tmp*2; //准备下一个 tmp 
            wei++;
            ans+=wei;
            continue;
        }
        ans+=wei;
    } 
    cout<<ans;
    return 0;
}

T3 T638482 小梦的铁索连环

思路

这道题我们肯定用图(废话。。。 用 dfs 找到互相联通,且所有值都最大的一个部分。在 dfs 中更新最大的答案,然后进行判断是否所有点相连然后再去接下来的 dfs。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=1e4+10;
int n,e,a[maxx],box[maxx],ans=-1e18;
bool vis[maxx];
int G[maxx][maxx];
void dfs(int step,int sum){
    ans=max(ans,sum);
    for(int i=1;i<=n;i++){
        if(vis[i]) continue;
        bool flag=1;
        for(int j=1;j<step;j++){
            int v=box[j];
            if(G[i][v]==0){
                flag=0;
                break;
            }
        }
        if(flag){
            vis[i]=1;
            box[step]=i;
            dfs(step+1,sum+a[i]);
            vis[i]=0;
        }
    }
    return;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>e;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=e;i++){
        int u,v;
        cin>>u>>v;
        G[u][v]=G[v][u]=1;
    }
    dfs(1,0);
    cout<<ans;
    return 0;
}

T4 T638458 忙碌的老师

思路

终于不是二分了

我们先要把每个时间点(A_i)排序,然后去看能否在一个教室上(也就是下课与另一堂课的时间差 \ge 0),不行的话那就要加一个人。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=1e5+10;
int n;
struct node{
    int l,r;
}a[maxx]; 
int b[maxx];
int cnt=0;
bool cmp(node x,node y){
    return x.l<y.l;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i].l>>a[i].r;
    }
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++){
        bool flag=0;
        for(int j=0;j<cnt;j++){
            if(a[i].l > b[j]){
                b[j] = a[i].r;
                flag=1;
                break;
            }
        }
        if(!flag){
            b[cnt++] = a[i].r;
        }
    }
    cout<<cnt<<endl;
    return 0;
}