D - Three Integers
https://codeforces.com/contest/1311/problem/D
本题题意:给出a,b,c三个数,a<=b<=c;
可以对三个数中任意一个进行+1或-1的操作;
问题:求出最少操作数使这些数满足:b整除a,c整除b
思路:题目中给出abc的范围只有1e4
所以我们可以通过枚举的方式来找出答案;
我们通过枚举b的大小,然后计算在b为k值得情况下,a,c为哪个数最优
暴力枚举出最优情况即可;
细节:在枚举b为k时,对于a,我们可以通过预处理出b的因子,然后枚举因子与原本的数的差值,找出最优即可;
而对于c,有以下情况:
1.对于b>c的情况,我们只需要让c等于b
2.对于c>b的情况,我们有两种可能,1.c已经整除b,这种需要的操作数为0
2.c没整除b,所以可能让c减少到为b的倍数,或者增大到b的倍数,两者枚举找操作数小的即可;
所以这道题的做法就是:先预处理出范围内的因子,然后枚举;
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define paii pair<int,int>
#define pali pair<ll,int>
#define pail pair<int,ll>
#define pall pair<ll,ll>
#define fi first
#define se second
vector<int>g[];
int a,b,c;
void inist()
{
for(int i=;i<=;i++){
for(int j=;j<=/i;j++)
g[i*j].pb(i);
} }
int work(int& aa,int bb,int& cc)
{
int ans=;
int minn=;
int l=g[bb].size();
int x=aa;
for(int i=;i<l;i++){
if(minn>abs(g[bb][i]-aa)){
minn=abs(g[bb][i]-aa);
x=g[bb][i];
}
}
aa=x;
ans+=minn;
if(bb>cc){
ans+=abs(bb-cc);
cc=bb;
}
if(cc%bb<bb-cc%bb){
ans+=cc%bb;
cc-=cc%bb;
}
else{
ans+=bb-cc%bb;
cc+=bb-cc%bb;
}
return ans;
}
int main()
{
inist();
int t;
cin>>t;
while(t--){
int ans=;
int ansa,ansb,ansc;
cin>>a>>b>>c;
for(int i=;i<=;i++){
int a1=a,b1=i,c1=c;
int temp=abs(i-b);
temp+=work(a1,b1,c1);
if(temp<ans){
ans=temp;
ansa=a1;ansb=b1;ansc=c1;
}
}
cout<<ans<<endl;
cout<<ansa<<" "<<ansb<<" "<<ansc<<endl;
}
return ;
}
D - Three Integers的更多相关文章
- [LeetCode] Sum of Two Integers 两数之和
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- Nim Game,Reverse String,Sum of Two Integers
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- LeetCode 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- 解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译)
解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译) http://improve.dk/the-anatomy-of-row-amp-page-compre ...
随机推荐
- 显示二维码-智能TFT模块
应用范例: 使用 TOPWAY Smart LCD (HMT050CC-C) 显示二维码 第一步 建立工程 ① 开 Editor 软件, 点击菜单栏建立新工程File --> New Proje ...
- 与WinRT组件进行操作
1,原理: WinRT是一个新的类库,应用程序可以用它访问操作系统的功能. 在内部,WinRT以组件的形式实现.COM Component Object Model- WinRT使用.net元数据来描 ...
- 「Flink」使用Managed Keyed State实现计数窗口功能
先上代码: public class WordCountKeyedState { public static void main(String[] args) throws Exception { S ...
- ELK学习002:Elasticsearch 7.x 的安装及配置
Elasticsearch 的安装与启动 1.1 下载 Elasticsearch 7.6.0 下载地址:https://www.elastic.co/cn/downloads/elasticsear ...
- Java架构师必看,超详细的架构师知识点分享!
在Java程序员行业中,有不少Java开发人员的理想是成为一名优秀的Java架构师,Java架构师的主要任务不是从事具体的软件程序的编写,而是从事更高层次的开发构架工作.他必须对开发技术非常了解,并且 ...
- 03.JS运算符
前言: 学习一门编程语言的基本步骤 (01)了解背景知识 (02)搭建开发环境 (03)语法规范 (04)常量和变量 (05)数据类型 (06)数据类型转换 (07)运算符7.运算符 表达式:由运 ...
- 查看deepin操作系统版本命令
cat /proc/version cat /etc/debian_version cat /etc/os-release lsb_release -a uname -a uname -r sc ...
- 简单的OO ALV显示ALV及下载
REPORT OO_ALV. CLASS OO_ALV DEFINITION. PUBLIC SECTION. METHODS:GET_DATA IMPORTING AMOUNT TYPE I,&qu ...
- 前端调用本地摄像头实现拍照(vue)
由于调用摄像头有使用权限,只能在本地运行,线上需用https域名才可以使用. <template> <div class="camera_outer"> & ...
- 【pycharm基本操作】项目创建、切换、运行、字体颜色设置,常见包的安装步骤
创建新项目 退出项目 怎样区别虚拟环境和系统环境? 虚拟环境和系统环境切换:进入项目切换解释器 切换项目 创建python目录和文件 代码运行方式一: 还可以这样执行代码方式二: 文件的剪切.复制.删 ...