------2014-12-14

今天神马都没干、意思一下。

SWUST OJ 365

均分纸牌

问题描述:
有 N 堆纸牌,编号分别为 1,2,…, N。每堆上有若干张,但纸牌总数必为 N 的倍数。可以在任一堆上取若于张纸牌,然后移动。 
移牌规则为:在编号为 1 堆上取的纸牌,只能移到编号为 2 的堆上;在编号为 N 的堆上取的纸牌,只能移到编号为 N-1 的堆上;其他堆上取的纸牌,可以移到相邻左边或右边的堆上。 
现在要求找出一种移动方法,用最少的移动次数使每堆上纸牌数都一样多。

例如 N=4,4 堆纸牌数分别为: 
① 9 ② 8 ③ 17 ④ 6 
移动3次可达到目的: 
从 ③ 取 4 张牌放到 ④ (9 8 13 10) -> 从 ③ 取 3 张牌放到 ②(9 11 10 10)-> 从 ② 取 1 张牌放到①(10 10 10 10)。

输入:

N(N 堆纸牌,1 <= N <= 100) 
A1 A2 … An (N 堆纸牌,每堆纸牌初始数,l<= Ai <=10000)

输出:

所有堆均达到相等时的最少移动次数。

样例输入:

4
9 8 17 6

样例输出:

3

最开始还想复杂了、直接O(n)依次判断每一个与平均数的大小、少于则从后面拿、多余则把多的给后面。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 1010

int n;
int a[N];

int main()
{
    int ave,sum=0,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    ave=sum/n;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=ave)
        {
            ans++;
            a[i+1]=a[i+1]+a[i]-ave;
        }
    }
    printf("%d\n",ans);
    return 0;
}

CodeForces 479B

Tower

Description

  As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of ai cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2).

  The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it‘s a waste of time.

  Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task.

Input

  The first line contains two space-separated positive integers n and k (1≤n≤100, 1≤k≤1000) — the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers ai (1≤ai≤104) — the towers‘ initial heights.

Output

  In the first line print two space-separated non-negative integers s and m (m≤k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that.

  In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i≠j). Note that in the process of performing operations the heights of some towers can become equal to zero.

  If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.

Sample Input

Input

3 2

5 8 5

Output

0 2

2 1

2 3

Input

3 4

2 2 4

Output

1 1

3 2

Input

5 3

8 3 2 6 3

Output

3 3

1 3

1 2

1 3

Hint In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.

题意大概和上面一样、就是每次移动的时候不一定是移动到相邻的、而且每次移动只能移动一个、可以随便移动、则每次从最多的往最少的移动一个就是了。

其实暴力就能过、数据小

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <iterator>
#include <cstring>
#include <string>
using namespace std;
#define max(a,b) ((a)>(b)(a):(b))
#define min(a,b) ((a)<(b)(a):(b))
#define INF 0x7fffffff
#define ll long long
#define N 1005

int n,k;
int rx[N],ry[N];
multimap<int,int> mp;
multimap<int,int>::iterator it1,it2;

int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        mp.clear();
        for(int i=1;i<=n;i++)
        {
            int item;
            scanf("%d",&item);
            mp.insert(pair<int,int>(item,i));
        }
        int step=1;
        while(1)
        {
            it1=mp.begin();
            it2=--mp.end();
            if(it2->first-it1->first<=1 || step>k) break;;
            rx[step]=it2->second;
            ry[step]=it1->second;
            mp.insert(pair<int,int>(it1->first+1,it1->second));
            mp.insert(pair<int,int>(it2->first-1,it2->second));
            mp.erase(it1);
            mp.erase(it2);
            step++;
        }
        step--;
        it1=mp.begin();
        it2=--mp.end();
        int result=(it2->first)-(it1->first);
        cout<<result<<‘ ‘<<step<<endl;
        for(int i=1;i<=step;i++)
        {
            cout<<rx[i]<<‘ ‘<<ry[i]<<endl;
        }
    }
    return 0;
}
时间: 2024-08-01 23:11:01

------2014-12-14的相关文章

Android应用开发相关下载资源(2014/12/14更新)

Android应用开发相关下载资源 官方终于发布了Android Studio正式版,Android Studio将会成为推荐使用的主要Android开发工具. (1)Android SDK (Android SDK主安装包,包含SDK Manager.AVD Manager.工具包tools,释放后的根文件夹为android-sdk-windows):revision 23.0.2http://dl.google.com/android/android-sdk_r23.0.2-windows.

2014.12.14 python&amp;pip

今天尝试使用github上的一个音乐极客软件,python.pip的安装和设置环境变量成功了,但是beet的安装似乎出现了问题,运行时报错. 自己的电脑终于归位了,换了个电源适配器和显卡,希望还能用个两年. 投了简历,EMC&新浪,希望能得到一份好的实习. HTML: abbr元素:缩写 dfn:定义术语 q元素:引用来自他处的内容 bdo:设置文字方向 mark:突出显示文字

LAMP开发之环境搭建(2014.12.7在ubuntu下)

Ubuntu下搭建LAMP环境 前言:学习PHP脚本编程语言之前,必须先搭建并熟悉开发环境,开发环境有很多种,例如LAMP.WAMP.MAMP等.这里我搭建的是LAMP环境,即Linux.Apache.MySQL.PHP环境.网上搭建方法也有很多,但都不是最新的,本搭建时间为2014.12.07. 一.搭建环境 Lenovo Y470 VMWare9.0 Ubuntu 14.04.1 LTS (Trusty Tahr):ubuntu-14.04.1-desktop-i386.iso二.安装软件1

寻找大学目标及行动步骤——记ITAEM团队第二期宣讲会(2014.05.14)

·昨晚8:00-9:40,在 钟海楼03029 ,进行了ITAEM团队第二期宣讲会(第一期见第一期宣讲会总结),来参加的主要是大一学生,以信院为主,也有法学院.文学院的同学.在宣讲会中,大家都比较积极认真. 第二期宣讲会议程 (1)ITAEM团队骆宏作"有目标,才有奋斗的动力"主题分享,时间约为晚8点-8点40分: (2)丁又专作"Doing,Now(现在就行动)"演讲,时间约为晚8点45分-9点28分: (3)ITAEM团队温辉翔分享自己技术成长经验与推荐书籍,时

【谜客帝国】第九届老爱原创谜会(2014.12.30)

谜客帝国第九届老爱原创谜会(2014.12.30)主持:瓷       计分:晶莹1.现有十人达标准(字)规.注:双扣2.乱呈能,别下毒(11笔字)理3.难得知错又开口(汉人)吕雉4.高校新生载入校(3字年纪比较词)大一岁5.这些问题都不懂(即物赠)几盘咸菜6.太太在聚餐(3字点心)老婆饼7.独活(期货术语•秋千格)做单8.打的付款,节省消费(即赠物)十二支鲜花9.对林散之有安排(2字植物)桉树10.这图可更新(象声词)叮咚11.“苍天骑士”拍马屁(3字外神话人物•上楼格)阿修罗.注:神官“苍天

2014.5.14

这两天都是对知识的梳理,确实发现有些知识遗忘了,同时回过头发现有些知识有了新的了解,有时有恍然大悟的感觉啊.不过自己有时候有了思路,写起代码来还是比较困难的.痛苦并快乐着,多了一个感受,一天不在项目上做点什么,就有点难受的感觉,哈哈!记住,不要害怕报错,一报错就慌了. 今天学习了大绝招就是:断点调试,但是它不会改变程序的执行顺序,方便我们的改错,是锻炼的机会啊!然后就是正则表达. 字符串一旦被初始化,就不可以被改变,存放在方法区中的常量池中. String s1 = "abc"; //

易轩网络更新页面————————2014.12.13发布

易轩网络更新页面: 最新更新动态: ★易轩★改软件标题工具—)—)豪华版              出炉! 工具大小:52.0 MB(若大于该大小则视为病毒文件处理) 工具功能:更改你想要更改软件的标题(则运行时的名称) 工具版本:v1.0.0.0 工具是否达标:是 工具是否报毒:暂未测试(若大小一致却依然报毒,请添加信任或关闭杀毒软件) 工具下载地址:http://yunpan.cn/cfxEAPKpPxzfg  提取码 ba30 工具作者:易轩 联系方式:邮箱:[email protecte

【ACOUG】2014.12.27ACOUG年会

祝贺ACOUG~2015 越来越好   刚刚过完2014年圣诞节,就迎来了ACOUG的最后一场沙龙,也作为ACOUG的本年度年会,老朋友相聚大家特别开心,下面来看看当时的场景ACOUG开始啦 ACOUG VP 仇实 me 斑点狗 老盖在演讲ing 老盖and me 乐奕and me 圣诞树很漂亮 Leonarding2014.12.27北京&winter分享技术~成就梦想Blog:www.leonarding.com

写在2014.12.31想说的话

时光总是匆匆,不管你愿不愿意,2014就在今天向我们挥手告别,又到了该写年终总结的时候,这一年是收获的一年,是纠结的一年,也是值得纪念的一年,岁月给我们带来的并不是年龄的增长,更多的是心态的成熟和变化.生活有时会逼迫你,不得不放走机遇,甚至不得不抛弃爱情,不要把别人对自己的放弃,变成自己对自己的放弃,人生的日子都是越过越少,剩下的日子越来越重要,所谓顺其自然,并非代表我们可以不努力,而是努力之后我们有勇气接受成败. 关于工作,这一年似乎敲的代码越来越少,也开始厌烦无聊的重复,一直在寻找一种能够在

2015-08-25 php大力力016 兄弟连高洛峰php教程(2014年 14章数据库章节列表)

2015-08-25 php大力力016 兄弟连高洛峰php教程(2014年 14章数据库章节列表) [2014]兄弟连高洛峰 PHP教程14.1.1 复习数据库  15:58 [2014]兄弟连高洛峰 PHP教程14.1.2 phpMyAdmin的使用 15:59 [2014]兄弟连高洛峰 PHP教程14.1.3 php访问MySQL 17:27 [2014]兄弟连高洛峰 PHP教程14.1.4 在PHP脚本中操作MySQL数据库1  17:38 [2014]兄弟连高洛峰 PHP教程14.1.