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 ...
随机推荐
- JavaScript——event事件详解
1.事件对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 什么时候会产生Event 对象呢? 例如: 当用户单击某个元素的时候,我们给这个元 ...
- Java的七大排序
一.各个算法的时间复杂度 二,具体实现 1.直接选择排序 基本思想:在长度为n的序列中,第一次遍历找到该序列的最小值,替换掉第一个元素,接着从第二个元素开始遍历,找到剩余序列中的最小值,替换掉第二个元 ...
- Zookeeper机制
顾名思义 zookeeper 就是动物园管理员,他是用来管 hadoop(大象).Hive(蜜蜂).pig(小 猪)的管理员, Apache Hbase 和 Apache Solr 的分布式集群都用到 ...
- Centos7 安装Python3.7
如果电脑自带的python2.7 先卸载 1.强制删除已安装python及其关联 rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps 2.删 ...
- 刷题84. Largest Rectangle in Histogram
一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...
- Git学习笔记(windows git之初体验)
阿里国内镜像地址: https://npm.taobao.org/mirrors/git-for-windows/ 最近在学习廖雪峰老师关于git的教程,链接可以在我的首页找到.首先使用国内镜像下载并 ...
- shell 一键配置单实例oracle基础环境变量(linux7)
#!/bin/bash echo "修改主机名" hostnamectl set-hostname wangxfa hostname sleep 1 echo "查看并关 ...
- (未完成)【Android】MVP模式初见(一)
最近在阅读郭霖大神的公众号时,分类中架构引起了我的注意. 虽然是个人开发(水平很菜的那种),但最终都要向企业正式项目开发靠近.因此接下来一段时间,主要学习一下MVP架构.Retrofit以及RxJav ...
- js的reduce累加器
reduce为数组中每一个元素执行回调函数,不包括被删除或未被赋值的 https://www.jianshu.com/p/e375ba1cfc47
- XmlDocument vs XElement
var xmlstr = @"<xml> <AppId>some_appid</AppId> <CreateTime>1413192605&l ...