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 ...
随机推荐
- 不懂怎么创建可视化大屏?手把手教你使用数据可视化BI软件创建工厂车间数据监控大屏
灯果数据可视化BI软件是新一代人工智能数据可视化大屏软件,内置丰富的大屏模板,可视化编辑操作,无需任何经验就可以创建属于你自己的大屏.大家可以在他们的官网下载软件. 本文以工厂车间数据监控大屏为例 ...
- leaflet结合turf.js实现多边形分割(附源码下载)
前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...
- CentOS安装python3环境
CentOS7.4安装python3环境 (Python 3.8.1) (stable version, Dec.18, 2019) # .从官网下载Python - Dec. , [stable v ...
- python简明教程
Python简明教程 MachinePlay关注 0.7072018.09.26 01:49:43字数 2,805阅读 9,287 Python一小时快速入门 1.Python简介 pylogo. ...
- idea websorm 激活码(2020-1-6 实测可用)最新
2019年1月6日用 ZSI6IiIsImFzc2lnbmVlRW1haWwiOiIiLCJsaWNlbnNlUmVzdHJpY3Rpb24iOiIiLCJjaGVja0NvbmN1cnJlbnR ...
- shell 一键配置单实例oracle基础环境变量(linux7)
#!/bin/bash echo "修改主机名" hostnamectl set-hostname wangxfa hostname sleep 1 echo "查看并关 ...
- 出现ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly ....的解决方法
在terminal上运行gluoncv时遇到了一个报错问题. ImportError: Python is not installed as a framework. The Mac OS X bac ...
- R语言入门:使用RStudio的基本操作
R语言在人工智能,统计学,机器学习,量化投资,以及生物信息学方面有着十分广泛的运用.也是我大学的必修课,因此这里梳理一些有关R语言的知识点,做做记录. 首先我们需要知道R语言的工作区域,R语言默认的工 ...
- Spring的BeanFactory和FactoryBean
官方定义 BeanFactory:Spring Bean容器的根接口 FactoryBean:各个对象的工厂接口,如果bean实现了这个接口,它将被用作对象的工厂,而不是直接作为bean实例. 源码解 ...
- 图片中添加箭头【使用PPT实现】
手头上可以使用的方案 QQ截图 分辨率会改变 画图 网上的教程一般是画一根线再加一个三角来画箭头,有点麻烦,改起来不好改. PS 对我来说,软件安装本身就是个问题, 插入图片,加入箭头,组合,另存为, ...