赛前热手 (天梯赛暴力题)

---恢复内容开始---

L3-001 凑零钱 

链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805054207279104

思路;

数据很小直接 dfs

代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 1e5 + 10;
int vis[M],a[M],n,m,cnt,sum,flag,mp[M];
void dfs(int x){
    if(sum > m) return ;
    if(sum == m) {
         for(int i = 1;i < cnt;i ++)
            cout<<vis[i]<<" ";
         cout<<vis[cnt]<<endl;
         flag = 1;
    }
    for(int i = x;i <= n;i ++){
        if(flag == 1) continue;
        sum += a[i];
        vis[++cnt] = a[i];
        dfs(i+1);
        sum-=a[i];
        vis[cnt--] = 0;
    }
}

int main()
{
    int k = 0;
    cin>>n>>m;
    for(int i = 1;i <= n;i ++){
        cin>>a[i];
        k += a[i];
    }
    sort(a+1,a+1+n);
    if(k < m) cout<<"No Solution"<<endl;
    else{
         dfs(1);
         if(flag == 0) cout<<"No Solution"<<endl;
    }
    return 0;
}
View Code

L3-002 特殊堆栈

链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805053695574016

思路; 线段树维护下,避免超时

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1;
const int M = 1e5+10;
int a[M],cnt,sum[M<<2];

void update(int p,int c,int l,int r,int rt){
    if(l == r){
        sum[rt]+=c;
        return ;
    }
    mid;
    if(p <= m) update(p,c,lson);
    else update(p,c,rson);
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

int query(int p,int l,int r,int rt){
    if(l == r){
        return l;
    }
    mid;
    if(sum[rt<<1] >= p) return query(p,lson);
    else return query(p-sum[rt<<1],rson);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n,x;
    string s;
    cin>>n;
    for(int i = 1;i <= n;i ++){
        cin>>s;
        if(s[1] == 'o'){
            if(cnt == 0) cout<<"Invalid"<<endl;
            else{
                cout<<a[cnt-1]<<endl;
                update(a[cnt-1],-1,1,M,1);
                a[cnt-1] = 0;
                cnt--;
            }
        }
        else if(s[1] == 'u'){
            cin>>x;
            a[cnt] = x; cnt++;
            update(x,1,1,M,1);
        }
        else{
            if(cnt == 0) cout<<"Invalid"<<endl;
            else{
                int num;
                if(cnt%2==0) num = cnt/2;
                else num = (cnt+1)/2;
                cout<<query(num,1,M,1)<<endl;
            }
        }
    }
}
View Code

L3-003 社交集群

链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805053141925888

思路:给所有人建边,建完跑并查集就好了

用了getchar(), 不要加cin的同步流。。。

实现代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 1e3+10;
int f[M],siz[M];
vector<int>v[M],ans;
bool cmp(int x,int y){
    return x > y;
}
int Find(int x){
    if(x == f[x]) return f[x];
    return f[x] = Find(f[x]);
}

void Union(int x,int y){
    int fx = Find(x),fy = Find(y);
    if(fx != fy){
        f[fx] = fy;
        siz[fy] += siz[fx];
    }
}

int main()
{
    int n,x,y;
    cin>>n;
    for(int i = 1;i <= n;i ++){
        cin>>x;
        getchar();
        for(int j = 1;j <= x;j ++){
            cin>>y;
            v[y].push_back(i);
        }
    }
    for(int i = 0;i <= n;i ++) f[i] = i,siz[i] = 1;
    for(int i = 1;i <= 1000;i ++){
        if(v[i].size()==0) continue;
        int len = v[i].size();
            for(int k = 1;k < len;k ++){
                //cout<<v[i][j]<<" "<<v[i][k]<<endl;
                Union(v[i][0],v[i][k]);
        }
    }
    for(int i = 1;i <= n;i ++){
        if(f[i] == i){
           ans.push_back(siz[i]);
        }
    }
    sort(ans.begin(),ans.end(),cmp);
    cout<<ans.size()<<endl;
    int len = ans.size();
    for(int i = 0;i < len-1;i ++){
        cout<<ans[i]<<" ";
    }
    cout<<ans[len-1]<<endl;
}
View Code

 L3-004 肿瘤诊断

链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805052626026496

思路:直接三维bfs就好了,用dfs会爆栈

实现代码:

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

int dx[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int a[61][1300][150],ans;
int n,m,l,t;
struct node{
    int x,y,z;
};

bool check(int x,int y,int z){
    if(x < 1||x > l||y < 1||y > n||z < 1||z > m) return 0;
    return 1;
}

void bfs(int x,int y,int z){
    queue<node>q;
    node now;
    now.x = x; now.y = y; now.z = z;
    a[x][y][z] = 0;
    q.push(now);
    int sum = 1;
    while(!q.empty()){
        node old = q.front();
        q.pop();
        for(int i = 0;i < 6;i ++){
            node now = old;
            now.x += dx[i][0];
            now.y += dx[i][1];
            now.z += dx[i][2];
            //cout<<now.x<<" "<<now.y<<" "<<now.z<<endl;
            if(check(now.x,now.y,now.z)&&a[now.x][now.y][now.z]){
                q.push(now);
                a[now.x][now.y][now.z] = 0;
                sum++;
            }
        }
    }
    if(sum >= t)
        ans += sum;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>n>>m>>l>>t;
    for(int i = 1;i <= l;i ++){
        for(int j = 1;j <= n;j ++){
            for(int k = 1;k <= m;k ++){
                cin>>a[i][j][k];
            }
        }
    }
    for(int i = 1;i <= l;i ++){
        for(int j = 1;j <= n;j ++){
            for(int k = 1;k <= m;k ++){
                if(a[i][j][k]){
                    bfs(i,j,k);
                }
            }
        }
    }
    cout<<ans<<endl;
}
View Code

猜你喜欢

转载自www.cnblogs.com/kls123/p/10623516.html
今日推荐