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+20; LL a[maxn]; const LL hh = 1e12; const LL mm = 1e6; LL sum[maxn]; void work () {int n;cin>>n;for (int i=1;i<=n;++i){LL u,v,w;cin>>u>>v>>w;a[i]=u*hh+v*mm+w;}sort(a+1,a+1+n);for (int i=1;i<=n;++i){sum[i] = sum[i-1] + a[i];}LL ans=-1;for (int i=1;i<=n;++i){LL t = i*a[i]-sum[i];t += (n-i)*hh*12 - (sum[n]-sum[i] - ((n-i)*a[i]));if (ans==-1) 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 localfreopen("data.txt","r",stdin); #endifwork();return 0; }
View Code
转载于:https://www.cnblogs.com/liuweimingcprogram/p/5766622.html
Gym 100633G Nano alarm-clocks相关推荐
- Alarm Clocks Everywhere
https://codeforces.com/contest/1155/problem/C 题解: 1.对x数组求差: 2.对x数组差求GCD: 3.暴力查找p数组有没有GCD%pi==0; /* * ...
- Android核心程序之SystemUI - (一)开篇
UI是实现用户交互的重要途径之一,而Android中一个重要的UI元素就是SystemUI,本文分析基于Android 5.1,分析SystemUI的启动及运行过程. SystemUI源代码所在路径为 ...
- 浙南联合训练赛20180414
这次题目的代码都不长,CF的一贯风格 A - Game CodeForces - 513A Two players play a simple game. Each player is provide ...
- Android源码学习------SystemUI(二)
SystemUI----AndroidManifest文件 AndroidManifest是Android应用程序中最重要的文件之一.它是Android程序的全局配置文件,是每个 android程序中 ...
- Android之Manifest文件
在Android APP开发过程中,Manifest文件举足轻重.每一个apk都要有一个Manifest文件,它配置了apk在Android系统上的基本信息. 如下 frameworks/base/p ...
- 发光二极管二极管引脚_LCD? 发光二极管? 等离子体? HDTV技术入门指南
发光二极管二极管引脚 With image technology progressing faster than ever, High-Def has become the standard, giv ...
- SDNU_ACM_ICPC_2020_Winter_Practice_4th [Reproduced]
文章目录 B - So Easy! C - Reading comprehension HDU - 4990 F - TOYS POJ - 2318 H - Triangle I - Birthday ...
- usb延长线线序_我应该使用哪种延长线?
usb延长线线序 Extension cords are one of the most common household items, but there are many different ki ...
- 信号Signals--APUE第三版
10.1 介绍 Signals 提供了一种 处理异步事件的 方式, 例如用户在中断 键入ctrl + C 来终止一个程序. 我们即将描述的就是 POSIX.1 标准化的 reliable-signal ...
- 日出时间 算法_如何便宜地建立日出闹钟
日出时间 算法 Sunrise-simulating alarm clocks are a great way to wake yourself up in the mornings, but com ...
最新文章
- 中科院分词ICTCLAS5.0_JNI 使用方法
- MySQL重置root用户密码的方法【亲测可用】
- 调用Hybris API时遇到的错误消息Cannot find user with uid如何解决
- Asp.Net Core中的静态文件-12
- 牛逼,Java中表达式引擎工具就用它!建议收藏,一定用的到!!
- 计算机课玩游戏检讨500字,上电脑课玩游戏的检讨书范文
- vue移动端点击事件延迟_去除点击事件300ms延迟 (使用了vue之后)
- 【万里征程——Windows App开发】数据绑定——简单示例、更改通知、数据转换...
- 91.接收用户请求包体的方式
- AX 2009 父窗体参数记录传递
- Linux 基本命令(三)--histroy 常用命令详解
- Linux下的Libsvm使用历程录
- linux程序库设置错误,Linux--C库函数ferror中文翻译
- 用C实现五子棋对战——超详细教程
- 靠自己。linux manul手册入门
- 强大的3D音效增强神器:Boom 3D for Mac中文
- ps 去除gif水印
- 独家百度刷权重全套代码工具教程
- python基础(中)
- python jit_Pypy Python的JIT实现
热门文章
- BNU 斩(超级大水题)
- nyoj 144小珂的苦恼(关于如何不定方程有整数解问题)
- 【持续更新】并查集题目集合
- 五大列级庄_走进法国波尔多五大一级酒庄
- pdfptable pdf生成表格分页_Excel按一下这个键,一张纸打印所有表格,不要浪费A4纸了...
- NumPy学习笔记之random.randn()函数
- java技术架构选型方案报告.pdf,来啦,2020开源报告!
- pythom打包文件太大_PDF太大,不让上传怎么办?教你1分钟将100M的PDF变成10M
- DRL实战 : Dynamic Programming
- 极客大学产品经理训练营 产品思维和产品意识(上) 第3课总结