[topcoder]TheConsecutiveIntegersDivOne

http://community.topcoder.com/stat?c=problem_statement&pm=13625&rd=16278

首先,如果记得曼哈顿距离最小值那个问题,会想起一维的情况可证,点出现在中位数那里是最小的。这里也可证明,四个点,出现在中位数位置是最小的。

题解里的做法是,试探所有让某个想减的绝对值最小的情况。

我的代码有点丑,但过了:

#include <vector>
#include <algorithm>

using namespace std;

class TheConsecutiveIntegersDivOne {
public:
	int find(vector <int> numbers, int k) {
		sort(numbers.begin(), numbers.end());
		int d = k / 2;
		int left = 0;
		int sumL = 0;
		for (int i = left; i < left + d; i++) {
			sumL += numbers[i];
		}
		int sumR = 0;
		int right = left + d;
		if (k % 2 == 1) {
			right++;
		}
		for (int i = right; i < right + d; i++) {
			sumR += numbers[i];
		}
		int result = sumR - sumL - d * d;
		while (right + d < numbers.size()) {
			sumL += numbers[left + d];
			sumL -= numbers[left];
			sumR += numbers[right + d];
			sumR -= numbers[right];
			result = min(result, sumR - sumL - d * d);
			left++;
			right++;
		}
		return result;
	}
};

  

时间: 2024-08-03 08:03:17

[topcoder]TheConsecutiveIntegersDivOne的相关文章

TOPCODER SAM 686 div1 300

// TOPCODER SAM 686 div1 300 Problem Statement 带有小中括号的括号序列,问可以去掉多少子串,使得剩下的非空串是合法的. Constraints 字符串长度不超过 40. Examples // ans[i] = count(s[i]) string s[] = {"()[]", "())", "()()", "([)]", "())[]][]([]()]]()]]]&qu

topcoder srm656 1000分题

Problem Statement   You are given an int N and a int[] pos. We are interested in some permutations of the set {1,2,...,N}. A permutation p is called good if the following condition is satisfied: for each valid k, we have p(k) < p(k+1) if and only if

TopCoder SRM 625 Incrementing Sequence 题解

本题就是给出一个数k和一个数组,包括N个元素,通过每次增加数组中的一个数的操作,最后需要得到1 - N的一个序列,不用排序. 可以从暴力法入手,然后优化. 这里利用hash表进行优化,最终得到时间效率是O(n*n)的算法,而且常数项应该很低,速度还挺快的. 思路: 1 如果数组A[i]在1 -N 范围内,就利用bool B[]记录,这个数已经找到了: 2 如果A[i]的值之前已经找到了,那么就增加k操作,得到新的值A[i]+k,看这个值是否找到了,如果没找到,就使用B记录,如果之前已经找到了,那

Topcoder SRM625 题解

给出一个字符串求是palindrome和anagram的比率是多少. 知识点: 1 DBL_MAX 64位double的最长数大概是1.7E308,很大很大,比long long大上不知多少倍,故此大概能容纳150!的数值,不能容纳200!的数值 2 偶数的时候,不能有字母重复为基数次,否则不能组成palindrome 3 基数的时候,只能有且只有有一个字母重复为基数次,用于放在中间的,否则也不能组成palindrome 4 计算带重复数的全排列公式:P(N) / P(M1)/P(M2)...P

Topcoder SRM 刷题企划

1.本文用来记录自己在Topcoder上刷过的题目.不过重点是记录心得,记录自己的思路问题. 2.刷的题目全部都是Div2 1000分的题目,小概率会去做Div1 的进阶题. 3.基本上自己写出来的题目都不会另开一篇来写. 4.Topcoder使用: [教程1][教程2] SRM 508 Div2 YetAnotherORProblem (Div2 Hard) 题意:构造长度为N,各元素上限为R的序列,并且满足$A_1+A_2+\cdots+A_n=A_1|A_2|\cdots|A_n$,求方案

Topcoder SRM656div1 250 ( 期望DP )

Problem Statement    Charlie has N pancakes. He wants to serve some of them for breakfast. We will number the pancakes 0 through N-1. For each i, pancake i has width i+1 and deliciousness d[i].Charlie chooses the pancakes he is going to serve using t

[topcoder]TheGridDivTwo

http://community.topcoder.com/stat?c=problem_statement&pm=13628&rd=16278 标程是BFS,我用DFS,都可解. 这里复杂的把pair写了hash函数,其实直接用个矩阵来存bool就可以了. #include <vector> #include <algorithm> #include <unordered_set> #include <utility> using name

Topcoder SRM 643 Div1 250&lt;peter_pan&gt;

Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*......*pn,我们假设p0,p1,...,pn是单调不降的,那么v里存储的是下标为偶数 的N的质因数p0,p2,p4,...,p(2k).现在要求写一个程序,返回一个vector<long long>ans; ans里存储的是p0,p1,p2,...,pn. Limits Time Limit(m

TopCoder SRM 634 Div.2[ABC]

TopCoder SRM 634 Div.2[ABC] ACM 题目地址: TopCoder SRM 634 赛后做的,感觉现场肯定做不出来Orz,简直不能多说. Level One-MountainRanges[水题] 题意: 问序列中有几个完全大于旁边的峰. 分析: 傻逼题,不多说. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: one.cpp * Create Date: 2014-09-26 21:01:23 * Desc