首页 > 其他分享 >2024牛客多校第二场

2024牛客多校第二场

时间:2024-07-22 11:19:59浏览次数:7  
标签:return cout int 多校 long cin 2024 牛客 include

B MST

类似根号分治的思路,点数少的跑Prim,点数大的跑Kruscal

有个坑点是分界点调100过不了,90能卡过去

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const ll inf = 1e16;
int n,m,Q;
struct ed{
    int u,v,w;
}edge[N<<1];
bool cmp(ed a,ed b){
    return a.w<b.w;
}


map<int,int>e[N];
struct S {
    int u;
    int d;
};
bool operator<(const S &x, const S &y) { return x.d > y.d; }
priority_queue<S> q;
int a[N];
bool vis[N];
void Prim(int n){
//    cout<<"Prim\n";
    for(int i=1;i<=n;i++) cin>>a[i],vis[a[i]]=0;
    
    q.push({a[1], 0});
    int cnt = 0;
    ll res=0;
    while (!q.empty() && cnt<=n ) {
        int u = q.top().u, d = q.top().d;
        q.pop();
        if (vis[u]) continue;
        vis[u] = 1;
        ++cnt;
        res += d;
        //cout<<u<<" : "<<d<<"\n";
        for (int i = 1; i <= n ; i++ ) {
            int v = a[i];ll w = e[u][v];
            if (!vis[v] && w ) {
                q.push({v, w});
            }
        }
    }
    for(int i=1;i<=n;i++) vis[a[i]]=0;
    if(cnt>=n) cout<<res<<"\n";
    else cout<<-1<<"\n";
}

int pa[N];
int find(int x){
    if(pa[x]!=x) return pa[x]=find(pa[x]);
    return x;
}
void kruscal(int n){
//    cout<<"kruscal"<<"\n";
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) pa[a[i]]=a[i],vis[a[i]]=1;
    ll tot=0;
    int use=0;
    for(int i=1;i<=m;i++){
        if(!vis[edge[i].v] || !vis[edge[i].u] ) continue;
        int f1=find(edge[i].u),f2=find(edge[i].v);
        if(f1!=f2){
            tot+=edge[i].w;
            pa[f1]=f2;
            use++;
        }
    }
    if(use==n-1) cout<<tot<<"\n";
    else cout<<-1<<"\n";
    for(int i=1;i<=n;i++) pa[a[i]]=a[i],vis[a[i]]=0;
}

int main(){
//    freopen("3.in","r",stdin);
//    freopen("lys.out","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m>>Q;
    int cnt=0;
    for(int i=1;i<=m;i++){
        int u,v,w;cin>>u>>v>>w;
        cnt++;
        edge[cnt]=(ed){u,v,w};
        e[u][v]=w;
        e[v][u]=w;
    }
    sort(edge+1,edge+1+m,cmp);
    int base=90;
    while(Q--){
        //TODO
        int k;cin>>k;
        if(k<=base){ // prim
            Prim(k);
        }
        else kruscal(k);
    }
}

 

C

发现从左上角开始走是最优的,确定了起点就和普通递推没什么区别了

#include<cstdio>
#include<iostream>
using namespace std;
const int N=1e6+505;
int n,res;
string c[2];
int f[N][2],s[N][2];
int main(){
    cin >> n;
    cin >> c[0] >> c[1];
    for (int i=1;i<=n;i++)
        for (int j=0;j<2;j++)
            if (c[j][i-1]=='R') s[i][j]=1;
    for (int i=1;i<=n;i++){
        for (int j=0;j<2;j++){
            if (s[i][j]){
                f[i][j]=f[i-1][j]+1;
                if (s[i][j^1])
                    f[i][j]=max(f[i][j],f[i-1][j^1]+2);
            }    
            res=max(res,f[i][j]);        
        }
    }
    
    if (res!=0) cout << res-1 << endl;
        else cout << 0 << endl;
    return 0;
}

E

#include<cstdio>
#include<iostream>
#define int long long
using namespace std;
int T,x;
int lowbit(int x){
    return x&(-x);
}
void work(){
    cin >> x;
    int res=x-lowbit(x);
    if (res) cout << res << endl;
        else cout << -1 << endl;
}
signed main(){
    cin >> T;
    while(T--) work();
    return 0;
}

H

考虑拆成两维转化成前缀和,能经过点(x,y)首先要能走到(x,y)

对于固定的右端点r,相当于问有多少个点i满足

sumx[r]-sumx[i-1]=x

sumy[r]-sumy[i-1]=y

问题变成求这些点的后缀和,正着求不好求还可能算重,倒叙枚举i=n;i>=1;i--

对于当前枚举到的i,可能有很多的 j 比 i 大且都符合上面两个式子

只计算最小的那个 j ,显然只要加上这个 j 对应的后缀,所有的 j 都能不重不漏地考虑到

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5,dx[]={1,-1,0,0},dy[]={0,0,-1,1};
int n,x,y,ans=0;
char s[N+5];
map< pair<int,int>,int> sum;
int Mov(char ch)
{
    if(ch=='W') return 3;
    else if(ch=='S') return 2;
    else if(ch=='A') return 1;
    else return 0;
}
void Kafka()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>x>>y;
    cin>>s+1;
    if(x==0&&y==0)
    {
        cout<<n*(n+1)/2<<endl;
        return;
    }
    sum[make_pair(0,0)]=n;
    int Sx=0,Sy=0;
    for(int i=n;i;--i)
    {
        int now=Mov(s[i]);
        Sx+=dx[now],Sy+=dy[now];
//        printf("i=%d x=%d y=%d\n",i,Sx,Sy);
        int Nx=Sx-x,Ny=Sy-y;
        if(sum.find(make_pair(Nx,Ny))!=sum.end()) ans+=n-sum[make_pair(Nx,Ny)]+1;
        sum[make_pair(Sx,Sy)]=i-1;
//        printf("i=%d ans=%d\n",i,ans);
    }
    cout<<ans<<endl;    
}
signed main()
{
    Kafka();
    return 0;
}

 

标签:return,cout,int,多校,long,cin,2024,牛客,include
From: https://www.cnblogs.com/liyishui2003/p/18315673

相关文章

  • 2024年黑龙江等保测评的整改建议
    问题:黑龙江等保测评整改建议回答:在黑龙江等保测评中,企业可能需要根据测评报告进行整改。以下是一些简明的整改建议,帮助企业提升信息系统的安全性和合规性。黑龙江等保测评系统补丁管理黑龙江等保测评确保所有操作系统和应用程序及时安装最新的安全补丁。黑龙江等保测评建......
  • Nessus Professional 10.7.5 Auto Installer for macOS Sonoma (updated Jul 2024)
    NessusProfessional10.7.5AutoInstallerformacOSSonoma(updatedJul2024)发布Nessus试用版自动化安装程序,支持macOSSonoma、RHEL9和Ubuntu24.04请访问原文链接:https://sysin.org/blog/nessus-auto-install-for-macos/,查看最新版。原创作品,转载请保留出处。作......
  • Nessus Professional 10.7.5 Auto Installer for RHEL 9/AlmaLinux 9/Rocky Linux 9 (
    NessusProfessional10.7.5AutoInstallerforRHEL9/AlmaLinux9/RockyLinux9(updatedJul2024)发布Nessus试用版自动化安装程序,支持macOSSonoma、RHEL9和Ubuntu24.04请访问原文链接:https://sysin.org/blog/nessus-auto-install-for-rhel-9/,查看最新版。原创作......
  • Nessus Professional 10.7.5 Auto Installer for Ubuntu 24.04 (updated Jul 2024)
    NessusProfessional10.7.5AutoInstallerforUbuntu24.04(updatedJul2024)发布Nessus试用版自动化安装程序,支持macOSSonoma、RHEL9和Ubuntu24.04请访问原文链接:https://sysin.org/blog/nessus-auto-install-for-ubuntu/,查看最新版。原创作品,转载请保留出处。作......
  • Metasploit Pro 4.22.2-2024071501 (Linux, Windows) - 专业渗透测试框架
    MetasploitPro4.22.2-2024071501(Linux,Windows)-专业渗透测试框架Rapid7Penetrationtesting,releaseJul15,2024请访问原文链接:https://sysin.org/blog/metasploit-pro-4/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org世界上最广泛使用的渗透测试框架......
  • 2024暑假集训测试8
    前言比赛链接。爆零了?!?T4莫名CE了,T2因为某些人打乱搞做法使出题人改数据和时限,\(O(npk)\)做法死掉了,主要还是数组开大了还忘了算,直接爆零了。T1WhiteandBlack显然不存在无解,从根开始扫,遇到黑色就翻转,前后顺序不影响结果,该方案为正确且唯一方案。继续观察发现若一个......
  • 【AI资讯早报】AI科技前沿资讯概览:2024年7月22日早报
    【AI资讯早报,感知未来】AI科技前沿资讯概览,涵盖了行业大会、技术创新、应用场景、行业动态等多个方面,全面展现了AI领域的最新发展动态和未来趋势。1.欧盟推进人工智能监管立法近日,欧盟在《欧盟官方公报》上正式公布了其人工智能法案,标志着欧洲在AI监管领域迈出了重要一步。该......
  • 20240721-宝塔面板配置及博客网站搭建
    首先部署宝塔面板,并登录登录前先进行面板的配置:登录之后安装软件和环境(mysql,php,ftp,nginx等)添加一个网站,根据需求填选项网站创建完成!现在去WordPress下载源码:下载完成是个压缩包,解压:计划通过FTP服务将源码上传至服务器网站根目录但FTP连接时出现问题:经调查发现FTP服务器被动模式使......
  • 都2024年了你还傻傻分不清楚“编译时”和“运行时”吗?
    前言在写vue3编译原理揭秘电子书的时候,发现有不少粉丝还傻傻分不清楚什么是编译时?什么是运行时?这篇文章我们来让你彻底搞清楚编译时和运行时的区别。关注公众号:【前端欧阳】,给自己一个进阶vue的机会编译时我将编译这个词语理解为翻译,这句话是什么意思呢?比如你要和一个老外沟......
  • linux系统基础:查找文件 20240722
    在Shell中查找文件是一个常见的任务,可以使用多种工具来完成,例如find、locate、grep等。以下是一些使用这些工具的示例。1.使用find命令find命令是最常用的文件查找工具之一,它在指定目录及其子目录下搜索符合条件的文件。示例:查找/home/user目录下所有以.txt结尾的文件。find......