codeforces 479B. Towers C - Exams【排序】

题目:codeforces 479B. Towers

题意:给出一个长度 n 的序列,最多可以有 k 次操作,每次选择一个一个最大的减1,最小的加1,然后问在最多k次操作之后的最小差值。

很简单,练习一下python语法

n,k = map(int,raw_input().split())
l = map(int,raw_input().split())
ans = []
i = 0
while i<k:
    if(max(l) - min(l)>0): #取最大值
        a,b = l.index(max(l)),l.index(min(l)) #最大值的位置
        l[a]-=1
        l[b]+=1
        i+=1
        ans.append([a+1,b+1])
    else:
        break
print max(l)-min(l),len(ans)
for i in ans:
    print i[0],i[1]

顺便上一下C题的代码简单的排序:

n = input()
l = []
for i in range(n):
    a,b = map(int,raw_input().split())
    l.append([a,b])
l.sort()
ans = 0
for i in l:
    re = min(i)
    if re>=ans:ans = re
    else:
        ans = max(i)
print ans
时间: 2024-10-07 00:01:40

codeforces 479B. Towers C - Exams【排序】的相关文章

codeforces 492C. Vanya and Exams 解题报告

题目链接:http://codeforces.com/problemset/problem/492/C 题目意思:给出 3 个整数:n,  r,  avg.然后有 n 行,每行有两个数:第 i 行有 ai 和 bi.表示如果写 bi 篇文章那么可以在 ai 这个分数上增加 1 分.可以增加好多次,但是前提是加完得到的分数不能超过 r.要使得 n 个exam 的分数平均分至少达到avg时需要写的最少文章是多少篇. 解决方法很简单,贪心即可. 我们当然希望写的文章越少越好,所以先对文章从小到大排序.

Codeforces 484B Maximum Value(排序+二分)

题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最大值非常明显a[i]越接近a[j]的倍数则余数越大 ,因此我们将全部的元素从大到小排序 : 然后枚举a[j]的倍数 ,二分查找小于a[i]倍数的最大值,然后更新余数的最大值. 代码例如以下: #include <iostream> #include <cstdio> #include

CodeForces 151 B 结构体排序。

E - 结构体又来了呦 Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 151B Description Winters are just damn freezing cold in Nvodsk! That's why a group of n friends prefers to take a taxi, order

CodeForces - 598C(高精度几何 排序然后枚举)

传送门: http://codeforces.com/problemset/problem/598/C Nearest vectors time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given the set of vectors on the plane, each of them starting at

Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can

CodeForces 1294B Collecting Packages(排序+贪心)

http://codeforces.com/contest/1294/problem/B 大致题意: 一张图上有n个包裹,给出他们的坐标,一个机器人从(0,0)出发,只能向右(R)或向上(U),问能否收集到所有包裹,如果能,给出字典序最小的路径. 最开始当成搜索题了,其实可以排序+贪心写的. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string>

CodeForces - 844C Sorting by Subsequences (排序+思维)

You are given a sequence a1,?a2,?...,?an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also wil

Codeforces 229D Towers

http://codeforces.com/problemset/problem/229/D 题意:有n(1<=n<=5,000)座塔排在一条直线上,从左到右每个塔的高度分别为hi(1<=hi<=100,000),每次操作你可以选择一座塔(假设是第i座),用吊车把它吊起来,然后放到与它相邻的一座塔上(可以是第i-1座也可以是第i+1座),这样,新塔的高度为两座塔的和,完成操作后,塔的总数减少一座.问最少需要多少次操作可以使得所有的塔从左到右形成一个非递减序列. 思路: 我们可以这样

Codeforces 825E Minimal Labels - 拓扑排序 - 贪心

You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected. You should assign labels to all vertices in such a way that: Labels form a valid pe