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 local
    freopen("data.txt","r",stdin);
#endif
    work();
    return 0;
}

时间: 2024-08-03 09:25:10

Gym 100633G Nano alarm-clocks的相关文章

Gym - 100637A Nano alarm-clocks 模拟

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

CF Gym 100637A Nano alarm-clocks

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

Educational Codeforces Round 81 (Rated for Div. 2) A Display The Number

A. Display The Number time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed

Codeforces Gym 100637A A. Nano alarm-clocks 前缀和处理

A. Nano alarm-clocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/A Description An old watchmaker has n stopped nano alarm-clocks numbered with integers from 1 to n. Nano alarm-clocks count time in hours, and

Codeforces Gym 100637A A. Nano alarm-clocks 前缀和

A. Nano alarm-clocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/A Description An old watchmaker has n stopped nano alarm-clocks numbered with integers from 1 to n. Nano alarm-clocks count time in hours, and

codeforces Gym 100500 J. Bye Bye Russia

Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description It was the last day at Russia, after the World Finals has ended. Coach fegla was travelling back to Cairo on the same day b

Nano快速入门

Windows Server 2016 提供了新的安装选项:Nano Server. Nano Server 是针对私有云和数据中心进行优化的远程管理的服务器操作系统. 类似于 Windows Server 的"服务器核心"模式,但显著变小了,无本地登录功能,且仅支持 64 位应用程序.工具和代理. 其所需的磁盘空间更小,启动速度明显更快,且所需的更新和重启操作远远少于 Windows Server. 当它未重新启动时,则可以更快地重新启动. Nano Server 安装选项仅适用于

Linux菜鸟的笔记(二)---- &quot;nano&quot;

文章的题目是加了引号的nano,提起nano我们会想起啥: 下面这个 还是这个 no,no,no! 我们的重点是linux,当然是linux的nano编辑器啊!nano在我心中的地位是:最简单的编辑器 接触nano时还是个linux小白,当时入手了树莓派3b,使用树莓派时需要配置个静态的ip,不需要经常移动.当时就百度了一下咋编辑网络配置文件,有人说用vi/vim,二话不说,没点linux基础就vim /etc/network/interface  进入发现界面挺好看,花花绿绿的.不过敲键盘没反

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.