AC修炼计划(AtCoder Beginner Contest 328)

news2024/9/21 18:54:16

传送门:

Toyota Programming Contest 2023#7(AtCoder Beginner Contest 328) - AtCoder

本章对于自己的提升:dfs的运用,带权并查集,以及状压dp。

A,B,C题比较简单,直接附上代码。

A - Not Too Hard

// #pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> PII;
const int mod=998244353;
int b[1000005];
int n,x;
void icealsoheat(){
    cin>>n>>x;
    int ans=0;
    for(int i=1;i<=n;i++)cin>>b[i];
    for(int i=1;i<=n;i++){
        ans+=b[i]<=x?b[i]:0ll;
    }
    cout<<ans;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}

B - 11/11

// #pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> PII;
const int mod=998244353;
int b[1000005];
int n,x;
void icealsoheat(){
    cin>>n;
    int ans=0;
    for(int i=1;i<=n;i++)cin>>b[i];
    for(int i=1;i<=n;i++){
        // if(b[i]>=i)ans++;
        int x=i%10;
        int an=i;
        bool f=0;
        while(an){
            if(an%10!=x){
                f=1;
                break;
            }
            an/=10;
        }
        if(f)continue;
        int bn=x;
        while(b[i]>=bn){
            ans++;
            bn=bn*10+x;
        }
    }
    cout<<ans;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}

C - Consecutive

// #pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> PII;
const int mod=998244353;
int b[1000005];
int n,q;
string s;
int sum[1000005];
void icealsoheat(){
    cin>>n>>q;
    cin>>s;
    s=' '+s;
    for(int i=2;i<=n;i++){
        if(s[i]==s[i-1]){
            sum[i]=sum[i-1]+1;
        }
        else sum[i]=sum[i-1];
    }
    while(q--){
        int l,r;
        cin>>l>>r;
        int ans=sum[r]-sum[l-1];
        if(s[l]>1&&s[l]==s[l-1]){
            ans--;
        }
        cout<<ans<<"\n";
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}

D - Take ABC

// #pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> PII;
const int mod=998244353;
// int b[1000005];
int n,q;
string s;
int sum[1000005];
void icealsoheat(){
    string s;
    cin>>s;
    vector<char>b;
    for(auto i:s){
        if(i!='C'||b.size()<2){
            b.push_back(i);
        }
        else{
            int id=b.size();
            if(b[id-1]=='B'&&b[id-2]=='A'){
                b.pop_back();
                b.pop_back();
            }
            else{
                b.push_back(i);
            }
        }


    }
    for(auto i:b){
        cout<<i;
    }

}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}

E - Modulo MST

这题要求是找边权经行取模后的(注意是取模后的数值)最小的最小生成树。此处的边的数量就未必是n-1了。我们需要用dfs把所有可能都跑一遍,并找到能把所有点能链接的最小生成树的最小的边权。(我没有想到会用这么暴力的写法,时间复杂度计算有待提高,赛时没有做出来)。在这里偷了jiangly的并查集板子。并查集用于查询到底有多少个点被取到了。应为他把并查集用了一个结构体进行递归,所以使得他的并查集可以迭代,很巧妙。

// #pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> PII;
const int mod=998244353;

int u[1000005];
int v[1000005];
int w[1000005];
struct DSU {
    std::vector<int> f, siz;
    
    DSU() {}
    DSU(int n) {
        init(n);
    }
    
    void init(int n) {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        // for(int i=1;i<=n;i++)f[i]=i;
        siz.assign(n, 1);
    }
    
    int find(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        siz[x] += siz[y];
        f[y] = x;
        return true;
    }
    
    int size(int x) {
        return siz[find(x)];
    }
};
int n,m,k;
void icealsoheat(){
    cin>>n>>m>>k;
    for(int i=1;i<=m;i++){
        cin>>u[i]>>v[i]>>w[i];
    }
    int ans=k;
    auto dfs=[&](auto self,int i,DSU dsu,int s)->void{
        if(i==m+1){
            bool f=0;
            for(int i=1;i<=n;i++){
                if(!dsu.same(i,1)){
                    f=1;
                    break;
                }
            }
            if(!f)ans=min(ans,s%k);
            return;
        }
        self(self,i+1,dsu,s);
        if(!dsu.same(u[i],v[i])){
            dsu.merge(u[i],v[i]);
            self(self,i+1,dsu,s+w[i]);
        }
    };
    dfs(dfs,1,DSU(n+5),0);
    cout<<ans;


    
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}

F - Good Set Query

这题是一道很典型的带权并查集的板子题,但我当时带权并查集忘却了,这里让我查漏补缺了。

代码如下:

// #pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> PII;
const int mod=998244353;
int n,q;
int w[1000005];
int pre[1000005];
// int a[1000005];
int find(int x){
    if(x==pre[x])return x;
    int t=pre[x];
    pre[x]=find(pre[x]);
    w[x]+=w[t];
    // pre[x]=find(pre[x]);
    return pre[x];
    
}

void icealsoheat(){
    cin>>n>>q;
    for(int i=1;i<=n;i++)pre[i]=i;
    for(int i=1;i<=q;i++){
        int a,b,d;
        cin>>a>>b>>d;
        int xx=find(a);
        int yy=find(b);
        if(xx==yy&&w[a]-w[b]==d)cout<<i<<" ";
        else if(xx!=yy){
            pre[yy]=xx;
            w[yy]+=w[a]-d-w[b];
            cout<<i<<" ";
        }
    }

    
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}

G - Cut and Reorder

这题题目没看懂啥意思。

这里我看了大佬的题解Toyota Programming Contest 2023#7(AtCoder Beginner Contest 328) - 知乎 (zhihu.com)

这个数据一看到n<=22,就该第一时间想到,这很状压,但我思维太过浅薄,没有想到如何迭代状态。看了佬的题解才明白。

代码如下:

// #pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> PII;
const int mod=998244353;
int n,c;
int a[10005];
int b[10005];
void icealsoheat(){
    cin>>n>>c;
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n;i++)cin>>b[i];
    vector<int>dp((1<<n)+5,0x3f3f3f3f3f3f3f3f);
    dp[0]=-c;
    for(int i=0;i<(1<<n);i++){
        int cnt=__builtin_popcount(i);
        for(int j=0;j<n;j++){
            if(!(i>>j&1)){
                int k=j;
                int sum=0;
                int ns=i;
                int id=0;
                while(k<n&&!(i>>k&1)&&cnt+id<n){
                    ns|=(1ll<<k);
                    sum+=abs(a[k]-b[cnt+id]);
                    id++;
                    k++;
                    // ns|=(1ll<<k);
                    dp[ns]=min(dp[ns],dp[i]+sum+c);
                }
            }
        }
    }
    cout<<dp[(1<<n)-1];
    
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    // cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1230892.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Adult数据集预处理

因为adult数据集没有列名&#xff0c;先设置列名 df pd.read_csv(adult.csv, header None, names [age, workclass, fnlwgt, education, education-num, marital-status, occupation, relationship, race,sex, capital-gain, capital-loss, hours-per-week, native-countr…

【Qt开发流程之】布局管理

介绍 一个界面呈现&#xff0c;如果要让用户有更好的观感&#xff0c;布局必不可少。 【Qt之布局】QVBoxLayout、QHBoxLayout、QGridLayout、QFormLayout介绍及使用 链接: https://blog.csdn.net/MrHHHHHH/article/details/133915208 qt布局类图&#xff1a; Qt布局是Qt图形…

Jmeter监听器

Jmeter监听器 一、监听器介绍二、监听器的类型三、监听器详解3.1 察看结果树3.2 Summary Report3.3 聚合报告3.4 后端监听器3.5 Aggregate Graph3.6 Comparison Assertion Visualizer&#xff08;比较断言可视化器&#xff09;3.7 JSR223 Listener3.8 Response Time Graph3.9 S…

一份WhatsApp矩阵账号营销模式全解,有你不知道的玩法吗?

将WhatsApp营销践行到底&#xff0c;是傲途针对海外Social营销一直在做的事。在WhatsApp全球营销范围越来越广泛、营销模式越来越深入的当下&#xff0c;我们也在实践中积累了一套比较系统而全面的差异化矩阵营销模式&#xff0c;帮助大中小不同类型企业获得了有价值的结果。 …

Linux CentOS7配置网络参数

CentOS6及以前版本中主要使用ifconfig工具&#xff0c;查看、配置网络参数。后来对推荐使用ip命令查看配置网络参数。而centos7中&#xff0c;不再赞成使用ifconfig工具&#xff0c;取而代之的是nmcli工具&#xff0c;服务管理也是以systemctl工具取代了service,这些之前版本的…

HarmonyOS ArkTS语言,运行Hello World(二)

一、认识DevEco Studio界面 进入IDE后&#xff0c;我们首先了解一下基础的界面。整个IDE的界面大致上可以分为四个部分&#xff0c;分别是代码编辑区、通知栏、工程目录区以及预览区。 代码编辑区 1、中间的是代码编辑区&#xff0c;你可以在这里修改你的代码&#xff0c;以…

clickhouse 业务日志告警

一、需求 对入库到clickhouse的业务日志进行告警&#xff0c;达阀值后发送企业微信告警。 方法一、 fluent-bit–>clickhouse(http)<–shell脚本,每隔一分钟获取分析结果 --> 把结果保存到/dev/shm/目录下 <-- node_exporter读取指标入库到prometheus<-- rules…

未来之路:互联网技术驱动汽车行业的创新浪潮

在互联网迅猛发展的今天&#xff0c;它的触角已延伸至各行各业&#xff0c;其中最引人注目的莫过于汽车行业。随着互联网技术的融合&#xff0c;汽车正变得越来越智能&#xff0c;预示着一场关于出行方式的革命。 首先&#xff0c;自动驾驶技术的发展正日益成熟。依托先进的传感…

物联网AI MicroPython学习之语法 PWM脉宽调制模块

学物联网&#xff0c;来万物简单IoT物联网&#xff01;&#xff01; PWM 介绍 模块功能: PWM脉宽调制驱动模块 接口说明 PWM - 构建PWM对象 函数原型&#xff1a;PWM(ch, freq, duty)参数说明&#xff1a; 参数类型必选参数&#xff1f;说明chobjectYPin对象例如&#xf…

pytest-rerunfailures插件之测试用例失败重跑

环境前提&#xff1a; 只有同时满足一下先决条件才能使用pytest-rerunfailures ①python的版本不能过低&#xff1b; ②pytest 5.0或更高版本&#xff1b; 背景&#xff1a; 平时在做接口测试的时候&#xff0c;经常会遇到网络抖动或者环境问题导致测试用例运行失败&#x…

Python的简单web框架flask快速实现详解

文章目录 简介web框架的重要组成部分快速上手flaskflask的第一个应用 flask中的路由不同的http方法静态文件使用模板 总结关于Python技术储备一、Python所有方向的学习路线二、Python基础学习视频三、精品Python学习书籍四、Python工具包项目源码合集①Python工具包②Python实战…

UDS诊断入门

UDS定义的是诊断服务&#xff0c;属于应用层的内容&#xff0c;实现诊断通信的底层总线技术有很多&#xff0c;比如CAN&#xff0c;LIN&#xff0c;Ethernet&#xff0c;Flexray等&#xff0c;由于法规强制的OBD接口是CAN总线的&#xff0c;所以绝大多数场景中诊断都是基于CAN实…

kafka原理看这一篇就够了

为何使用消息队列 异步。接口方式实现多个系统协作&#xff0c;如图A系统作为用户请求接收方&#xff0c;需要调用多个系统的接口&#xff0c;这些接口还有可能是在A系统里同步调用&#xff0c;所以最后的接口耗时是多个系统接口耗时的总和&#xff1b;mq方式则可以异步发送消…

netty整合websocket(完美教程)

websocket的介绍&#xff1a; WebSocket是一种在网络通信中的协议&#xff0c;它是独立于HTTP协议的。该协议基于TCP/IP协议&#xff0c;可以提供双向通讯并保有状态。这意味着客户端和服务器可以进行实时响应&#xff0c;并且这种响应是双向的。WebSocket协议端口通常是80&am…

Spring源码-5.aop代理

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱吃芝士的土豆倪&#xff0c;24届校招生Java选手&#xff0c;很高兴认识大家&#x1f4d5;系列专栏&#xff1a;Spring源码、JUC源码&#x1f525;如果感觉博主的文章还不错的话&#xff0c;请&#x1f44d;三连支持&…

Python实现WOA智能鲸鱼优化算法优化随机森林分类模型(RandomForestClassifier算法)项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后获取。 1.项目背景 鲸鱼优化算法 (whale optimization algorithm,WOA)是 2016 年由澳大利亚格里菲斯大学的Mirjalili 等提…

nginx学习(4)Nginx 配置高可用集群(主从配置)

Nginx 配置高可用集群 Nginx的高可用集群是指由两台或多台Nginx服务器组成的集群系统&#xff0c;通过负载均衡和故障转移等技术&#xff0c;实现高可用性和可伸缩性的目标。在这种集群系统中&#xff0c;每个Nginx服务器都配置为主节点&#xff08;master&#xff09;或从节点…

阿里国际站(直通车)

1.国际站流量 2.直通车即P4P&#xff08;pay for performance点击付费&#xff09; 2.1直通的含义&#xff1a;按点击付费&#xff0c;通过自助设置多维度展示产品信息&#xff0c;获得大量曝光吸引潜在买家。 注意&#xff1a;中国大陆和尼日利尼地区点击不扣费。 2.2扣费规…

记一次代码审计中RCE挖掘及POC编写

文章转自先知社区&#xff1a;https://xz.aliyun.com/t/13008 作者&#xff1a;雨下整夜 声明&#xff1a;本文仅限于技术讨论与分享&#xff0c;严禁用于非法途径。若读者因此作出任何危害网络安全行为后果自负&#xff0c;与本号及原作者无关。 从危险的模板引入开始 在前…

网络工程师网络配置经典例题(二)

目录 1、access、trunk 2、配置终结子接口 3、DHCP接口地址池、DNS 4、静态默认路由、接口IP 5、ACL、NAT 使内网用户可以访问外网 6、VLANIF 某公司拥有多个部门且位于不同网段&#xff0c;各部门均有访问Internet的需求。现要求用户通过二层交换机和路由器访问外部网络…