题目,给定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的更多相关文章

  1. Educational Codeforces Round 63 (Rated for Div. 2) C. Alarm Clocks Everywhere gcd

    题意:给出一个递增的时间序列a  给出另外一个序列b  (都是整数) 以b中任选一个数字作为间隔  自己从1开始任选一个时间当成开始时间 输出选择的数字标号以及 开始时间 思路  直接求间隔的公共gc ...

  2. CF Gym 100637A Nano alarm-clocks

    题意:给你一些钟的时间,只可以往后调, 问最少调的时间总和是多少 题解:因为肯定是调到某个出现过时间的,只要枚举时间,在维护一个前缀和快速计算出时间总和就行了. #include<cstdio& ...

  3. Gym - 100637A Nano alarm-clocks 模拟

    题意:有n个时钟,只能顺时针拨,问使所有时间相同的最小代价是多少 思路:将时间排序,枚举拨动到每一个点的时间就好了,容易证明最终时间一定是其中之一 #include <iostream> ...

  4. Codeforces 390A( 模拟题)

    Inna and Alarm Clock Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  5. Education CodeForces Round 63 Div.2

    A. Reverse a Substring 代码: #include <bits/stdc++.h> using namespace std; int N; string s; int ...

  6. Educational Codeforces Round 63 (Rated for Div. 2)

    传送门 A. Reverse a Substring 题意: 给你一串 s,让你判断能否通过反转区间[l,r]的元素,使得反转后的串的字典序小于 s: 如果能,输出 "YES",并 ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. Android核心程序之SystemUI - (一)开篇

    UI是实现用户交互的重要途径之一,而Android中一个重要的UI元素就是SystemUI,本文分析基于Android 5.1,分析SystemUI的启动及运行过程. SystemUI源代码所在路径为 ...

  9. boost asio scalability and multithreading

    A library such as Boost.Asio is typically used to achieve greater efficiency. With no need to wait f ...

随机推荐

  1. 详解 Webpack+Babel+React 开发环境的搭建

    1.认识Webpack 构建应用前我们先来了解一下Webpack, Webpack是一个模块打包工具,能够把各种文件(例如:ReactJS.Babel.Coffeescript.Less/Sass等) ...

  2. 使用javascript获取wx.config内部字段解决微信分享

    背景 在微信分享开发的时候我们通常的流程是 <?php require_once "jssdk.php"; $jssdk = new JSSDK("yourAppI ...

  3. ThinkPHP出现项目目录不可写,目录无法自动生成

    问题描述:维护一个老项目,用的TP2.1,down完代码,配好环境,访问的时候出现“项目目录不可写,目录无法自动生成!请使用项目生成器或者手动生成项目目录~”: 问题原因:在linux 服务器上,th ...

  4. Python——爬虫学习2

    BeautifulSoup插件的使用 这个插件需要先使用pip安装(在上一篇中不再赘言),然后再程序中申明引用 from bs4 import BeautifulSoup html=self.requ ...

  5. JS实现一位数显示为两位

    使用js脚本实现页面一位数字显示为两位,不足两位前面加“0”. function fix(num, length) { return ('' + num).length < length ? ( ...

  6. [linux] C语言Linux系统编程-socket回声客户端

    回声客户端: 1.所谓“回声”,是指客户端向服务器发送一条数据,服务器再将数据原样返回给客户端,就像声音一样,遇到障碍物会被“反弹回来”. 2.客户端也可以使用 write() / send() 函数 ...

  7. [javaSE] 集合框架(迭代器)

    当我们创建一个集合以后,可以直接使用system.out.println()来打印这个集合,但是,我们需要可以对每个元素进行操作,所以,这里需要使用迭代器来遍历集合 迭代器其实就是集合取出元素的方式 ...

  8. 4、构造方法、this、super

    构造方法 构造方法引入 * A:构造方法的引入 在开发中经常需要在创建对象的同时明确对象的属性值,比如员工入职公司就要明确他的姓名.年龄等属性信息. 那么,创建对象就要明确属性值,那怎么解决呢?也就是 ...

  9. c# MVC模式学习笔记_数据验证

    改变显示字段名称 设计字段规范 1.引用 using System.ComponentModel; using System.ComponentModel.DataAnnotations; 2.Dis ...

  10. Spring Boot学习笔记(五)整合mybatis

    pom文件里添加依赖 <!-- 数据库需要的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</gro ...