当前位置: 首页 > news >正文

Codeforces Round 954 (Div. 3)

这里写自定义目录标题

  • A. X Axis
    • 题意:
    • 题解:
    • 代码:
  • B. Matrix Stabilization
    • 题意:
    • 题解:
    • 代码:
  • C. Update Queries
    • 题意:
    • 题解:
    • 代码:
  • D. Mathematical Problem
    • 题意:
    • 题解:
    • 代码:

A. X Axis

题意:

你得到了三个点 x 1 x_{1} x1 x 2 x_{2} x2 x 3 x_{3} x3和 你需要求出一个点使得 x 1 x_{1} x1 x 2 x_{2} x2 x 3 x_{3} x3与这个点的距离和最小。

题解:

暴力枚举各个点。

代码:

#include<iostream>
#include<algorithm>
#include<vector>
#define int long longusing namespace std;void solve(){int x,y,z;cin>>x>>y>>z;int ans=0x3f3f3f3f;int l=min(min(x,y),z);int r=max(max(x,y),z);for(int i=l;i<=r;i++) ans=min(ans,abs(x-i)+abs(y-i)+abs(z-i));cout<<ans<<endl;
}
signed main(){int t;cin>>t;while(t--) solve();return 0;
}

B. Matrix Stabilization

题意:

给你一个大小为 n × m n \times m n×m 的矩阵,其中行的编号从上到下为 1 1 1 n n n ,列的编号从左到右为 1 1 1 m m m 。位于 i i i -th 行和 j j j -th 列交点上的元素用 a i j a_{ij} aij 表示。

考虑稳定矩阵 a a a 的算法:

  1. 找到单元格 ( i , j ) (i, j) (i,j) ,使其值严格大于所有相邻单元格的值。如果没有这样的单元格,则终止算法。如果有多个这样的单元格,则选择 i i i 值最小的单元格;如果仍有多个单元格,则选择 j j j 值最小的单元格。
  2. 设置 a i j = a i j − 1 a_{ij} = a_{ij} - 1 aij=aij1
  3. 转到步骤 1 1 1

在这个问题中,如果单元格 ( a , b ) (a, b) (a,b) ( c , d ) (c, d) (c,d) 有一个共同的边,即 ∣ a − c ∣ + ∣ b − d ∣ = 1 |a - c| + |b - d| = 1 ac+bd=1 ,那么这两个单元格就是邻居。

您的任务是在执行稳定算法后输出矩阵 a a a 。可以证明这种算法不可能运行无限次迭代。

给你一个大小为 n ∗ m n * m nm 的矩阵,其中行的编号从上到下为 1 1 1 n n n ,列的编号从左到右为 1 1 1 m m m 。位于 i i i -行和 j j j -列交叉处的元素用 a _ i j a\_{ij} a_ij 表示。考虑稳定矩阵 a a a 的算法:1.找到单元格 ( i , j ) (i, j) (i,j) 使其值严格大于所有相邻单元格的值。如果没有这样的单元格,则终止算法。如果有多个这样的单元格,则选择 i i i 值最小的单元格;如果仍有多个单元格,则选择 j j j 值最小的单元格。2.设置 a i , j = a i , j − 1 a_{i,j} = a_{i,j} - 1 ai,j=ai,j1 。3.转到步骤 1 1 1 .在此问题中,如果单元格 ( a , b ) (a, b) (a,b) ( c , d ) (c, d) (c,d) 有一个公共边,即 ∣ a − c ∣ + ∣ b − d ∣ = 1 |a - c| + |b - d| = 1 ac+bd=1 ,则这两个单元格被视为相邻单元格。您的任务是在执行稳定算法后输出矩阵 a a a 。可以证明这种算法不可能运行无限次迭代。

题解:

从左上角枚举各个点,判断当前点是否需要修改,若需要则将其修改为相邻点的最大值即可。

代码:

#include<iostream>
#include<algorithm>
#include<vector>
#define int long longusing namespace std;const int N=110;
int a[N][N];void solve(){int n,m;cin>>n>>m;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) cin>>a[i][j];for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){int temp=-0x3f3f3f3f;int cnt0=0;int cnt1=0;if(i-1>=1){cnt0++;temp=max(temp,a[i-1][j]);if(a[i-1][j]<a[i][j]) cnt1++;}if(i+1<=n){cnt0++;temp=max(temp,a[i+1][j]);if(a[i+1][j]<a[i][j]) cnt1++;}if(j-1>=1){cnt0++;temp=max(temp,a[i][j-1]);if(a[i][j-1]<a[i][j]) cnt1++;}if(j+1<=m){cnt0++;temp=max(temp,a[i][j+1]);if(a[i][j+1]<a[i][j]) cnt1++;}if(cnt0==cnt1) a[i][j]=temp;}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++) cout<<a[i][j]<<" ";cout<<endl;}
}
signed main(){int t;cin>>t;while(t--) solve();return 0;
}

C. Update Queries

题意:

给定一个长度为n的字符串 s s s和长度为 m m m i n d ind ind数组,以及字符串 c c c,你可以任意安排 i n d ind ind数组和 c c c字符串的顺序,然后有m次操作,第i次操作,可以将 s i n d i s_{ind_i} sindi的值更改为 c i c_{i} ci,问m次操作后,要求经过 m m m 次操作后的字符串 s s s 的字典序最小,输出这个 s s s

题解:

c c c i n d ind ind排序,遍历 i n d ind ind数组, c c c的首元素代替 s s s中的元素,经过m次替换就是最小的字典序。

代码:

#include<iostream>
#include<algorithm>
#include<vector>#define int long longusing namespace std;void solve() 
{int n;cin >> n;string s;cin >> s;int mask[n];for(int i = 0 ; i < n ; i ++){mask[i] = s[i] - '0';}	int ans = 101010;for(int i = 1 ; i < n ; i ++){vector<int>tmp;int tot = 0;for(int j = 0 ; j < n ; j ++){if(j == i - 1){int x = mask[j] * 10 + mask[j + 1];tmp.push_back(x);j++;}else{tmp.push_back(mask[j]);}}for(auto it : tmp){cout<<it<<" ";if(it == 0){cout << 0 << endl;return;}if(it == 1){continue;}else{tot += it;}}cout<<endl;if(tot == 0) tot++;ans = min(ans , tot);}cout << ans << endl;
}
signed main(){int t;cin>>t;while(t--) solve();return 0;
}

D. Mathematical Problem

题意:

给定一个长度为n的全为数字的字符串,你可以在字符串中添加n-2个运算符( × × ×或者 + + ),使得字符串变为一个合法表达式,求出该表达式最小的结果。

题解:

长度为 2 2 2 时,输出取出前导零的原数组即可,当长度大于 2 2 2 时,原字符串可以分为 n − 1 n-1 n1 个数字,然后需要 n − 2 n-2 n2 个运算符将其连接,因此有一个数字是两位数,其他的均为 1 1 1 位数,要使得最后的结果最小,因此当其中一个数字为 1 1 1 的时候运算符为乘法,其他的均为加法。

代码:

#include<iostream>
#include<algorithm>
#include<vector>#define int long longusing namespace std;void solve() 
{int n;cin >> n;string s;cin >> s;int mask[n];for(int i = 0 ; i < n ; i ++){mask[i] = s[i] - '0';}	int ans = 101010;for(int i = 1 ; i < n ; i ++){vector<int>tmp;int tot = 0;for(int j = 0 ; j < n ; j ++){if(j == i - 1){int x = mask[j] * 10 + mask[j + 1];tmp.push_back(x);j++;}else{tmp.push_back(mask[j]);}}for(auto it : tmp){cout<<it<<" ";if(it == 0){cout << 0 << endl;return;}if(it == 1){continue;}else{tot += it;}}cout<<endl;if(tot == 0) tot++;ans = min(ans , tot);}cout << ans << endl;
}
signed main(){int t;cin>>t;while(t--) solve();return 0;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 软件设计之Java入门视频(12)
  • 玩转springboot之springboot使用外置tomcat进行运行
  • 缓存-缓存的使用与基本详解
  • Vue3从入门到精通(三)
  • Python实现Mybatis Plus
  • 常见反爬及应对
  • C# Socket
  • 多个tomcat同时使用 不设置CATALINA_HOME环境变量
  • Oracle 11.2.0.1升级到11.2.0.4并做rman备份异机恢复
  • 如何用Java写一个整理Java方法调用关系网络的程序
  • 银河麒麟V10 SP1 审计工具 auditd更新
  • 在linux系统centos上面安装php7gmp扩展
  • 【算法专题】双指针算法
  • rider使用libman
  • k8s-第六节-数据持久化
  • Google 是如何开发 Web 框架的
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • maven工程打包jar以及java jar命令的classpath使用
  • overflow: hidden IE7无效
  • PAT A1050
  • SQLServer之创建显式事务
  • sublime配置文件
  • Web标准制定过程
  • 包装类对象
  • 不上全站https的网站你们就等着被恶心死吧
  • 关于extract.autodesk.io的一些说明
  • 简单数学运算程序(不定期更新)
  • 讲清楚之javascript作用域
  • 力扣(LeetCode)965
  • 普通函数和构造函数的区别
  • 使用 @font-face
  • 使用Swoole加速Laravel(正式环境中)
  • 手写一个CommonJS打包工具(一)
  • nb
  • linux 淘宝开源监控工具tsar
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • 从如何停掉 Promise 链说起
  • 数据库巡检项
  • ​flutter 代码混淆
  • ​linux启动进程的方式
  • ​你们这样子,耽误我的工作进度怎么办?
  • #QT 笔记一
  • (23)Linux的软硬连接
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (SpringBoot)第七章:SpringBoot日志文件
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (新)网络工程师考点串讲与真题详解
  • (转)【Hibernate总结系列】使用举例
  • (转)nsfocus-绿盟科技笔试题目
  • .Family_物联网
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .net core 管理用户机密
  • .Net Core 中间件验签