题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1045

好像是贪心...但这是一个环...

看博客:http://hzwer.com/2656.html

真是神奇的构造...还是应该大胆地先把各种变量都设出来再处理。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=1e6+;
int n,a[maxn],ave,c[maxn];
ll ans;
int abb(int x){return (x>)?x:-x;}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]),ans+=a[i];
ave=ans/n; ans=;
for(int i=;i<=n;i++)c[i]=c[i-]+a[i]-ave;
sort(c+,c+n+);
int mid=c[n/+];
for(int i=;i<=n;i++)ans+=abb(c[i]-mid);
printf("%lld\n",ans);
return ;
}

bzoj 1045 [HAOI2008] 糖果传递 —— 贪心的更多相关文章

  1. bzoj 1045: [HAOI2008] 糖果传递 贪心

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1812  Solved: 846[Submit][Stat ...

  2. BZOJ 1045: [HAOI2008] 糖果传递 数学

    1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...

  3. [BZOJ 1045] [HAOI2008] 糖果传递

    题目链接:BZOJ 1045 Attention:数据范围中 n <= 10^5 ,实际数据范围比这要大,将数组开到 10^6 就没有问题了. 我们先来看一下下面的这个问题. 若 n 个人坐成一 ...

  4. bzoj 1045 [HAOI2008] 糖果传递——设变量推式子

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1045 费用流TLE. #include<iostream> #include&l ...

  5. BZOJ 1045 [HAOI2008]糖果传递 ★(环形等分:中位数)

    题意 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. 思路 假设平均数是x,且a1给an了k个(k<0说明是an给a1了-k个),那么总代价就 ...

  6. bzoj 1045: [HAOI2008] 糖果传递【瞎搞】

    感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html #include<iostream> #include<cstd ...

  7. 【BZOJ 1045】 1045: [HAOI2008] 糖果传递

    1045: [HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n& ...

  8. 【BZOJ1045】[HAOI2008] 糖果传递 贪心

    [BZOJ1045][HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正 ...

  9. 【BZOJ】1045: [HAOI2008]糖果传递(中位数)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1045 白书上有讲 没ac的坑点在,数据范围n<=1,000,000 #include < ...

随机推荐

  1. JDBC链接数据库步骤

    java中定义链接数据库的标准:JDBC 1.导包:不同数据库有不同的jdbc驱动包,而且jdbc驱动包和数据库版本必须对应 2.测试 3.写代码 try { 1.//加载JDBC驱动    Clas ...

  2. JS——对象创建

    1.原始创建 <script> person = new Object();//不要var person.firstname = "Bill"; person.last ...

  3. 【译】x86程序员手册01

    Intel 80386 Reference Programmer's Manual 80386程序员参考手册 Chapter 1 -- Introduction to the 80386 第1章 - ...

  4. C# 定时无操作则退出登陆,回到登陆界面。

    有时候根据需求需要为程序添加在规定的时间内无操作则退出当前的登陆程序的功能,如下代码模拟描述的需求功能. using System; using System.Collections.Generic; ...

  5. Centos6.7 ELK日志系统部署

    Centos6.7 ELK日志系统部署 原文地址:http://www.cnblogs.com/caoguo/p/4991602.html 一. 环境 elk服务器:192.168.55.134 lo ...

  6. (转)Hadoop入门进阶课程

    http://blog.csdn.net/yirenboy/article/details/46800855 1.Hadoop介绍 1.1Hadoop简介 Apache Hadoop软件库是一个框架, ...

  7. UICollectionViewFlowLayout & UICollectionViewDelegateFlowLayout

    A concrete layout object that organizes items into a grid with optional header and footer views for ...

  8. PHP 之中文转为拼音

    /** * Created by PhpStorm. * User: Administrator * Date: 2019/1/2 0002 * Time: 下午 1:01 */ class PinY ...

  9. 在Unity中对注册表的信息进行操作

      问题1 在对注册表进行操作时无法生成注册表相关的类  解决办法:     增加头文件using Microsft.Win32; 问题2                    在运行程序时报错同时注 ...

  10. 【转载】使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化

    一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...