首页 > 其他分享 >poj-2231

poj-2231

时间:2023-05-23 16:03:43浏览次数:51  
标签:cowLocation cow rightCowNum 2231 long poj leftCowNum lld


//264K	47MS	C++
#include <cstdio>
#include <cstring>
#include <cstdlib>

const int MAX = 10005;

long long cowLocation[10005];

int cmp(const void * a, const void * b) {
	return *((long long *)a) - *((long long *)b);
}

long long cowNum;

long long sum;

long long curLeftSum;
long long curRightSum;

long long leftCowNum;
long long rightCowNum;

int main() {
	while(scanf("%lld", &cowNum) != EOF) {
		sum = 0;
		curLeftSum = 0;
		curRightSum = 0;
		for (long long i = 0; i < cowNum; i++) {
			scanf("%lld", &cowLocation[i]);
		}
		qsort(cowLocation, cowNum, sizeof(long long), cmp);
		for (long long i = 1; i < cowNum; i++) {
			curRightSum += cowLocation[i] - cowLocation[0];
			// printf("V %lld\n", cowLocation[i]);
		}
		sum += (curRightSum + curLeftSum);

		long long prevCowId = 0;
		long long curCowId = 1;
		leftCowNum = 0;
		rightCowNum = cowNum - 1;
		while(rightCowNum) {
			// printf("%d\n", rightCowNum);
			long long varyDistance = cowLocation[curCowId] - cowLocation[prevCowId];
			curRightSum -= rightCowNum*(varyDistance);
			rightCowNum--;
			leftCowNum++;
			curLeftSum += leftCowNum*(varyDistance);
			sum += (curRightSum + curLeftSum);
			// printf("AA %lld %lld %lld %lld %lld %lld\n",
			 	// sum, curRightSum, curLeftSum,
			 	// rightCowNum, leftCowNum, varyDistance);
			curCowId++;
			prevCowId++;		
		}
		printf("%lld\n", sum);
	}
}


虽然分类是排序,但是排序在这里只是一个预处理的角色,真正的思路是递推,和编程之美上的坐电梯那道题有点像,其实就是DP啦。

如果知道在某个位置的cow K, 其左边cow的数量是leftCowNum, 右边cow数量是 rightCowNum, 其他cow的位置也都知道,就可以求出,

K与其他cow的距离的和,两部分: 与左边cow的距离的和 leftCowSum, 和 右边cow的距离的和rightCowSum, 两者相加就是K带来的volume,

而要求 K后面一位的cow K+1, 只需在原来的leftCowNum和 rightCowNum进行 加减一定量的大小(细节在code里面,也很简单)既可以 递推出来,就这样遍历一遍cow,将每个cow的volume相加,就是最后的结果.

标签:cowLocation,cow,rightCowNum,2231,long,poj,leftCowNum,lld
From: https://blog.51cto.com/u_9420214/6332972

相关文章

  • poj-1308
    //392K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;constintMAX=10000;intUF_set[MAX];voidUF_get_setId(intcurId){intparentId=UF_set[curId];if(parentId==0){return;}while(UF......
  • poj-1120
    //408K16MSG++#include<cstdio>#include<cstring>usingnamespacestd;intdish[20][20];intK[20][20];intD[16];intdays;intgetDensitySum(introwId,intcolumnId){intK=0;K+=dish[rowId][columnId];if(rowId......
  • poj-3641
    //712K0MSG++#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>usingnamespacestd;longlonga,p;//longlongpower2(longlonga,longlongn)//{//longlongret=1;//for(longlongm......
  • poj-1026
    //188K110MSC++#include<cstring>#include<cstdio>#include<iostream>usingnamespacestd;charstr1[205];charstr2[205];intkey[205];intcycleLength[205];//voidreplace(char*str,intkeyLength,intstrLength){//......
  • poj-2707
    //408K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;intoX;intoY;intdX;intdY;inlinedoubleMIN(doublea,doubleb){returna<b?a:b;}inlinedoubleMAX(doublea,doubleb){returna>b?......
  • poj-2635
    //1652K875MSG++1000//1648K1313MSG++10000#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>constintMAX=1000100;charnotPrime[MAX+1];intPrimeNum;intPrimes[MAX];voidcheckPrim......
  • poj-3286
    //stupidmethod!!!!!!!!!!!!!!!//388K360MSG++#include<stdio.h>#include<string.h>#include<math.h>intC[33][33];//C[n][m],choosemfromn;voidgetCombination(){for(intn=0;n<=32;n++){for(intm=......
  • poj-2282
    //380K 32MS G++#include<stdio.h>#include<string.h>#include<math.h>longlongappearTime1[10];longlongappearTime2[10];voidgetAppearTime(intnum,longlong*appearArray){ appearArray[0]=1; if(num==0){ return; }......
  • poj-2249
    //356K16MSG++//356K0MSG++addm==0check//356K16MSG++//356K0MSG++addm==0check#include<stdio.h>#include<string.h>#include<math.h>intm;intn;//voidgetNum(unsignedintn,unsignedintm){//......
  • poj-1037
    //196K16MSC++#include<cstdio>#include<cstring>usingnamespacestd;constintMAX=25;longlongDP[MAX][MAX][2];//0:down.1:upvoidinit(){for(intcurPlankNum=1;curPlankNum<=20;curPlankNum++){for(......