10.23提高组模拟赛

· · 个人记录

洗衣服(clothes)

思路

这道题是一道贪心题目,我们要是想AC,必须得要搞一个堆,但是却要重新定义比较规则:

struct P{
    long long x,y;
    bool operator < (const P &a) const{
        return x>a.x;
    }
};

由于在下不会建小根堆,所以在“<”处写"<"而不是">"

字面意思就是按x从小到大排序。y代表的是洗衣机的效率,x表示的是洗完这件衣服需要的时间。不难看出,x越小越好。

我们在一时间很难平衡两个层次的值,但我们把它分开来讨论,可是只设sum1,sum2是不是比较难从中找的和答案直接相关的公式。那么我们定义tot[i]为洗完(包括烘干)第i件衣服所花费的时间,那么答案就是

∑_1^nmax(tot_i)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct P{
    long long x,y;
    bool operator < (const P &a) const{
        return x>a.x;
    }
};
priority_queue<P> q;
long long a[N],b[N],tot[N*10];
int main(){
//  freopen("clothes.in","r",stdin);
//  freopen("clothes.out","w",stdout);
    long long l,n,m;
    cin>>l>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        q.push({a[i],a[i]});
    }
    for(int i=1;i<=l;i++){
        P t=q.top();
        q.pop();
        tot[i]=t.x;
        t.x+=t.y;
        q.push(t);
    }
    while(q.size()){
        q.pop();
    }
    for(int i=1;i<=m;i++){
        cin>>b[i];
        q.push(P{b[i],b[i]});
    }
    for(int i=l;i>=1;i--){
        P t=q.top();
        q.pop();
        tot[i]+=t.x;
        t.x+=t.y;
        q.push(t);
    }
    long long ans=0;
    for(int i=1;i<=l;i++){
        ans=max(ans,tot[i]);
    }
    cout<<ans;
    return 0;
} 

机会成本(chance)

思路

不难发现,我们处在的瓶颈是如何快速求出

{max}_{1<=i<=n}(max(x_j−x_i,0)+max(y_j−y_i,0)+max(z_j−z_i,0))

但是推公式很麻烦,还很复杂,我们或许可以将这3个max的情况枚举一遍,这样就完美解决了。而每个Max都有0或x两种结果。结果总数2^3=8

#include<bits/stdc++.h>
using namespace std;
const int N=200010;
struct P{
    long long x,y,z;
};
P a[N];
long long b[8];
signed main(){
    long long n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y>>a[i].z;
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<=7;j++){
            long long h=0;
            for(int k=0;k<=2;k++){
                if((1<<k)&j){
                    if(k==1){
                        h+=a[i].y;
                    }else if(k==2){
                        h+=a[i].z;
                    }else{
                        h+=a[i].x;
                    }
                }
            }
            b[j]=max(b[j],h);
        }
    }
    long long ans=1e18,id;
    for(int i=1;i<=n;i++){
        long long now=0;
        for(int j=0;j<=7;j++){
            long long h=0;
            for(int k=0;k<=2;k++){
                if((1<<k)&j){
                    if(k==1){
                        h+=a[i].y;
                    }else if(k==2){
                        h+=a[i].z;
                    }else{
                        h+=a[i].x;
                    }
                }
            }
            now=max(now,b[j]-h);
        }
        if(ans>now){
            ans=now;
            id=i;
        }
    }
    cout<<ans<<" "<<id;
    return 0;
}

逗号播撒机

题目要求根据特定规则自动添加逗号:

  1. 如果某单词前面有逗号,则在所有该单词出现位置前加逗号(除非是句首或已有逗号)
  2. 如果某单词后面有逗号,则在所有该单词出现位置后加逗号(除非是句尾或已有逗号)
  3. 重复直到无法添加新逗号

主要就是将相邻的单词通过并查集连接起来,这样同一连通块内的单词会共享逗号标记。这样可以避免哼多复杂度

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
long long n,cnt,u,v;
long long fa[N],b[N],c[N],d[N];
string s,t,a[N];
map<string,int> did,nxt;
int find(int x){
    if(x==fa[x]){
        return x;
    }else{
        fa[x]=find(fa[x]);
        return fa[x];
    }
}
int main() {
    getline(cin, t);
    for(int i=0;i<t.size();i++){
        if(t[i]>='a'&&t[i]<='z'){
            s+=t[i];
        }
        if((t[i]=='.'||t[i]==','||t[i]==' ')&&s!=""){
            n++;
            a[n]=s;
            if(t[i]=='.'){
                b[n]=1;
            }
            if(t[i]==','){
                c[n]=1;
            }
            if(!did.count(s)){
                did[s]=++cnt;
                nxt[s]=++cnt;
            }
            d[nxt[s]]|=c[n];
            s="";
        }
    }
    for(int i=1;i<=cnt;i++){
        fa[i]=i;
    }
    for(int i=2;i<=n;i++){
        if(!b[i-1]){
            v=find(nxt[a[i-1]]);
            u=find(did[a[i]]);
            fa[v]=u;
            d[u]|=d[v];
        }
    }
    for(int i=1;i<=n;i++){
        s=a[i];
        cout<<s;
        if(b[i]){
            cout<<'.';
        }else if(d[find(nxt[s])]){
            cout<<',';
        }
        cout<<' ';
    }
    return 0;
}

地下城市

这是一个点双连通分量问题,要求:

  1. 计算最少需要设置的应急出口数量
  2. 计算不同最少应急出口的设置方案总数 核心思想:将图分解为点双连通分量,然后在割点处设置应急出口。 使用Tarjan算法找出所有点双连通分量 对每个点双连通分量分析:
  3. 如果没有割点:需要2个应急出口,方案数为C(n,2)
  4. 如果有1个割点:需要1个应急出口,方案数为分量大小-1
  5. 如果有≥2个割点:不需要应急出口(通过其他分量的出口)
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1010;
    int dfn[N],low[N],cnt,vn,root,cnum,e_num;
    bool vis[N];
    vector<int> e[N],ver[N];
    stack<int> st;
    long long ans;
    void dfs(int v){
     int t=0;
     dfn[v]=low[v]=++cnt;
     st.push(v);
     long long cnt=0;
     for(int son:e[v]){
         if(dfn[son]==0){
             dfs(son);
             low[v]=min(low[son],low[v]);
             if((root==v&&++cnt>1)||(root!=v&&dfn[v]<=low[son])){
                 vis[v]=1;
             }
             if(dfn[v]<=low[son]){
                 ver[++vn].push_back(v);
                 do{
                     t=st.top();
                     st.pop();
                     ver[vn].push_back(t);
                 }while(t!=son);
             }
         }else{
             low[v]=min(low[v],dfn[son]);
         }
     }
    }
    int main(){
     long long n,m,u,v,c=1;
     while(cin>>m&&m!=0){
         for(int i=1;i<N;i++){
             e[i].clear();
             ver[i].clear();
         }
         memset(dfn,0,sizeof(dfn));
         memset(vis,0,sizeof(vis));
         cnt=n=vn=0;
         for(int i=1;i<=m;i++){
             cin>>u>>v;
             e[u].push_back(v);
             e[v].push_back(u);
             n=max(n,max(u,v));
         }
         for(int i=1;i<=n;i++){
             if(dfn[i]==0){
                 root=i;
                 dfs(root);
             }
         }
         if(vn==1){
             e_num=2;
             ans=n*(n-1)/2;
         } else {
             e_num=0;
             ans=1;
             for(int i=1;i<=vn;i++){
                 cnum=0;
                 for(int t:ver[i]){
                     if(vis[t]){
                         cnum++;
                     }
                 }
                 if(cnum==1){
                     e_num++;
                     ans*=ver[i].size()-1;
                 }
             }
         }
         cout<<"Case "<<c++<< ": "<<e_num<<' '<<ans<<"\n";
     }
     return 0;
    }