codeforces 479B Towers 解题报告
题目链接:http://codeforces.com/problemset/problem/479/B
题目意思:有 n 座塔,第 i 座塔有 ai 个cubes在上面。规定每一次操作是从最多 cubes 的塔中取走一个cube,加去拥有最少 cubes 的塔上,那么显然,本来是最多cubes的塔的 cubes 数目会减少1,而拥有最少的 cubes 的塔的cubes数增加 1 。现在最多操作 k 次,使得最多 cubes 数 的塔的cubes数 减去 最少cubes数的塔的cubes 数最少(即题目中的instability)。输出总共执行的次数和具体的移动方案(i j:表示将第 i 个塔的一个cube 加到 第 j 座塔上)
这道题其实很简单,比赛的时候想复杂了,以为需要一个 pre 记录前一次最多cubes - 最少cubes 的数目,于是死改~~~死改~~~~越改越复杂~~~~最终就呵呵啦。
可以这样想,对于每一次移动,我们当然是按题目要求来做:最多cubes - 最少cubes,然后将相应编号的 tower 的 cubes 数作相应的改动(分别为-1,+1),然后再排序,再这样处理。关键是知道什么时候结束!!!就是最多cubes - 最少cubes == 0 的时候。除了这种情况,我们都执行 k 次,因为题目中并没有限制执行次数尽量少,而只是希望instability尽量少而已。那么即使有些情况重复执行也是不影响最终结果的!!
还有一个特判就是如果刚开始的时候最多cubes - 最少cubes == 0了,我们就不需要作任何操作,直接输出0 0 即可。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
const int N = + ;
#define f first
#define s second pair<int, int> p[maxn];
int ans[N][]; int main()
{
int n, k;
while (scanf("%d%d", &n, &k) != EOF)
{
for (int i = ; i <= n; i++)
{
scanf("%d", &p[i].f);
p[i].s = i;
}
sort(p+, p++n);
if (p[n].f == p[].f)
printf("0 0\n");
else
{
int cnt = ;
for (int l = ; l < k; l++)
{
ans[cnt][] = p[n].s; // 记录编号
ans[cnt++][] = p[].s;
p[n].f--;
p[].f++;
sort(p+, p++n);
if (p[n].f == p[].f) // 最多cubes == 最少cubes
break;
}
printf("%d %d\n", p[n].f - p[].f, cnt);
for (int i = ; i < cnt; i++)
printf("%d %d\n", ans[i][], ans[i][]);
}
}
return ;
}
codeforces 479B Towers 解题报告的更多相关文章
- codeforces  31C Schedule  解题报告
		
题目链接:http://codeforces.com/problemset/problem/31/C 题目意思:给出 n 个 lessons 你,每个lesson 有对应的 起始和结束时间.问通过删除 ...
 - codeforces  499B.Lecture  解题报告
		
题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...
 - codeforces  495C. Treasure 解题报告
		
题目链接:http://codeforces.com/problemset/problem/495/C 题目意思:给出一串只有三种字符( ')','(' 和 '#')组成的字符串,每个位置的这个字符 ...
 - codeforces  490B.Queue  解题报告
		
题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位 ...
 - CodeForces 166E -Tetrahedron解题报告
		
这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...
 - codeforces  489A.SwapSort  解题报告
		
题目链接:http://codeforces.com/problemset/problem/489/A 题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式 ...
 - codeforces  485A.Factory  解题报告
		
题目链接:http://codeforces.com/problemset/problem/485/A 题目意思:给出 a 和 m,a 表示第一日的details,要求该日结束时要多生产 a mod ...
 - codeforces  483A. Counterexample  解题报告
		
题目链接:http://codeforces.com/problemset/problem/483/A 题目意思:给出一个区间 [l, r],要从中找出a, b, c,需要满足 a, b 互质,b, ...
 - codeforces  479C Exams  解题报告
		
题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...
 
随机推荐
- BZOJ1045 [HAOI2008] 糖果传递
			
Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n<=987654321,表示小朋友的个数 ...
 - POJ2286 The Rotation Game
			
Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see ...
 - pthread_kill
			
别被名字吓到,pthread_kill可不是kill,而是向线程发送signal.还记得signal吗,大部分signal的默认动作是终止进程的运行,所以,我们才要用signal()去抓信号并加上处理 ...
 - Message
			
* Defines a message containing a description and arbitrary data object that can be* sent to a {@link ...
 - Redux初探与异步数据流
			
基本认知 先贴一张redux的基本结构图 原图来自<UNIDIRECTIONAL USER INTERFACE ARCHITECTURES> 在这张图中,我们可以很清晰的看到,view中产 ...
 - Redis 下载
			
https://github.com/MSOpenTech/redis/releases下载 Redis伴侣Redis Desktop Manager:http://redisdesktop.com/ ...
 - MyEclipse------从服务器下载文件
			
Downfile.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
 - Java I/O操作
			
按字节读取读取文件,并且将文件里面的内容写到另外一个文件里面去 public class CopyBytes { public static void main(String[] args) thro ...
 - SpringMVC 和Struts2的区别
			
SpringMVC 和Struts2的区别 1. 机制: spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. 2. 性能: spring会稍微比s ...
 - android-解决 Android N 上 报错:android.os.FileUriExposedException
			
解决 Android N 上 安装Apk时报错:android.os.FileUriExposedException: file:///storage/emulated/0/Download/appN ...