Gym 100633G Nano alarm-clocks
题目,给定n个时钟,要求把他们调成一样的时间。求最小的步数
思路:肯定是有一个时钟作为标准的啦,要找到这个时钟,怎么找呢?没其他方便的方法,暴力枚举。那么枚举后,怎么能快速地算到其他时钟转到这个时钟的时间呢?首先,如果我们把时间转换成数字,那应该好做点。现在问题是给定n个数,枚举最小的步数,使得n个数字相同。
例如我们把时间变为1、3、5、7、9这样的。(输出的时候按照权值变化成时间就可以了)
现在枚举5,预处理一个前缀和sum[i],那么在5前面的数字一共变化步可以快速算出来。i*a[i]-sum[i]
后面的,因为不能降下来,只能增加。7变5需要+8 其实也就是10-(7-5)了。所以后半部分的值是(n-i)*10 - (sum[n]-sum[i]-(n-i)*a[i]);
所以可以O(n)解决。利用了前缀和
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string> const int maxn = 1e5+;
LL a[maxn];
const LL hh = 1e12;
const LL mm = 1e6;
LL sum[maxn];
void work ()
{
int n;
cin>>n;
for (int i=;i<=n;++i)
{
LL u,v,w;
cin>>u>>v>>w;
a[i]=u*hh+v*mm+w; }
sort(a+,a++n);
for (int i=;i<=n;++i)
{
sum[i] = sum[i-] + a[i];
}
LL ans=-;
for (int i=;i<=n;++i)
{
LL t = i*a[i]-sum[i];
t += (n-i)*hh* - (sum[n]-sum[i] - ((n-i)*a[i]));
if (ans==-) ans=t;
else ans=min(ans,t);
//cout<<t<<" "<<i<<endl;
}
LL t = ans/hh;
ans -= t*hh;
LL t1 = ans/mm;
ans -= t1*mm;
cout<<t<<" "<<t1<<" "<<ans<<endl;
return ;
}
int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
Gym 100633G Nano alarm-clocks的更多相关文章
- Educational Codeforces Round 63 (Rated for Div. 2) C. Alarm Clocks Everywhere gcd
题意:给出一个递增的时间序列a 给出另外一个序列b (都是整数) 以b中任选一个数字作为间隔 自己从1开始任选一个时间当成开始时间 输出选择的数字标号以及 开始时间 思路 直接求间隔的公共gc ...
- CF Gym 100637A Nano alarm-clocks
题意:给你一些钟的时间,只可以往后调, 问最少调的时间总和是多少 题解:因为肯定是调到某个出现过时间的,只要枚举时间,在维护一个前缀和快速计算出时间总和就行了. #include<cstdio& ...
- Gym - 100637A Nano alarm-clocks 模拟
题意:有n个时钟,只能顺时针拨,问使所有时间相同的最小代价是多少 思路:将时间排序,枚举拨动到每一个点的时间就好了,容易证明最终时间一定是其中之一 #include <iostream> ...
- Codeforces 390A( 模拟题)
Inna and Alarm Clock Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
- Education CodeForces Round 63 Div.2
A. Reverse a Substring 代码: #include <bits/stdc++.h> using namespace std; int N; string s; int ...
- Educational Codeforces Round 63 (Rated for Div. 2)
传送门 A. Reverse a Substring 题意: 给你一串 s,让你判断能否通过反转区间[l,r]的元素,使得反转后的串的字典序小于 s: 如果能,输出 "YES",并 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Android核心程序之SystemUI - (一)开篇
UI是实现用户交互的重要途径之一,而Android中一个重要的UI元素就是SystemUI,本文分析基于Android 5.1,分析SystemUI的启动及运行过程. SystemUI源代码所在路径为 ...
- boost asio scalability and multithreading
A library such as Boost.Asio is typically used to achieve greater efficiency. With no need to wait f ...
随机推荐
- 微信小程序自学过程中遇到的问题 转
view标签下hover必须为true时,设置hover-class才有效,hover-start-time和hover-stay-time的形式如下: < view class=" ...
- Linq中连接
Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 注:本文内容主要来自<Linq实战>,本例中用到的对象请见文章底部. 1. 组连接 组连接是与分组查询是一样的. ...
- unity 和 iOS/Android 信息交互(方法调用)
参考文章均来源于[大神雨松momo]的文章. unity -> iOS // unity 程序 usingSystem.Runtime.InteropServices; usingUnityEn ...
- 从Eclipse切换到IDEA工具,哎~真香!
从Eclipse切换到IDEA工具,哎~真香!(图) 个人观点:IDEA工具用了就回不去了!!!对比很多人写,我就不赘述了.我在这里主要介绍一下IDEA工具的一些使用上的技巧,毕竟我开始学习java的 ...
- spring 事务管理机制
1. spring 事务管理抽象 spring 的事务策略机制的核心就是 org.springframework.transaction.PlatformTransactionManager 接口. ...
- 【Chromium】GPU进程启动流程
本篇文档以gpu进程的创建和启动为例,讲述chormium如何启动一个browser进程的子进程 PS:本文使用的chromium代码版本为71 前言 GPU进程的启动时机是由browser进程负责的 ...
- jquery 提示语淡入效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- UOJ169. 【UR #11】元旦老人与数列
传送门 考虑用 \(segment~tree~beats\) 那一套理论,维护区间最小值 \(mn\) 和严格次小值 \(se\) 那么可以直接 \(mlog^2n\) 维护前三个操作 考虑维护历史最 ...
- JS 写入到文件
//js写文件 function doSave(value, type, name) { var blob; if (typeof window.Blob == "function" ...
- 01Jenkins环境准备
01.硬件环境 Linux 64位(windows环境类似) 02.安装JDK a) 标题链接到Oracle官网下载jdk-8u144-linux-x64.rpm b) 将jdk拷贝到Linux的/t ...