您的位置:首页 > 编程语言

PKU ACM poj 2231 源代码

2011-01-15 00:19 337 查看
简单题,关键是提高效率。

使用暴力计算,耗时1000ms,改进算法后为63ms,然后再改进程序中的细节,进而优化到16ms。



源代码(16MS):

/*Moo Volume
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 12448  Accepted: 3582

Description

Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are making too much noise.

FJ's N cows (1 <= N <= 10,000) all graze at various locations on a long one-dimensional pasture. The cows are very chatty animals. Every pair of cows simultaneously carries on a conversation (so every cow is simultaneously MOOing at all of the N-1 other cows). When cow i MOOs at cow j, the volume of this MOO must be equal to the distance between i and j, in order for j to be able to hear the MOO at all. Please help FJ compute the total volume of sound being generated by all N*(N-1) simultaneous MOOing sessions.
Input

* Line 1: N

* Lines 2..N+1: The location of each cow (in the range 0..1,000,000,000).
Output

There are five cows at locations 1, 5, 3, 2, and 4.
Sample Input

5
1
5
3
2
4
Sample Output

40
Hint

INPUT DETAILS:

There are five cows at locations 1, 5, 3, 2, and 4.

OUTPUT DETAILS:

Cow at 1 contributes 1+2+3+4=10, cow at 5 contributes 4+3+2+1=10, cow at 3 contributes 2+1+1+2=6, cow at 2 contributes 1+1+2+3=7, and cow at 4 contributes 3+2+1+1=7. The total volume is (10+10+6+7+7) = 40.
*/

#include "stdlib.h"
#include "stdio.h"

#define  MAX_COW_NUM 10000

typedef struct _COW_ST_
{
int  iLocation;/*in the range 0..1,000,000,000*/
}COW_ST;

typedef struct _MOO_VOLUME_ST_
{
__int64 i64TotalVolume;
int iCowNum;
COW_ST astCow[MAX_COW_NUM];
}MOO_VOLUME_ST;

MOO_VOLUME_ST gstMooVolume;

void CowInput(void)
{
int iLoopCow;

scanf("%d",&gstMooVolume.iCowNum);

for(iLoopCow = 0; iLoopCow < gstMooVolume.iCowNum; iLoopCow++)
{
scanf("%d",&gstMooVolume.astCow[iLoopCow].iLocation);
}

return;
}

int CowCmp(const void *a,const void *b)
{
if (((COW_ST *)a)->iLocation > ((COW_ST *)b)->iLocation)
{
return 1;
}
else
{
return -1;
}

}

void CalculateMooVolume(void)
{
int iLoopCow;
__int64 i64LastCowVolume = 0;

/*get 1st cow's moo volume*/
for(iLoopCow = 1; iLoopCow < gstMooVolume.iCowNum; iLoopCow++)
{
i64LastCowVolume += (gstMooVolume.astCow[iLoopCow].iLocation - gstMooVolume.astCow[0].iLocation);
}

gstMooVolume.i64TotalVolume = i64LastCowVolume;

/*get 2nd~Nth cow's moo volume*/
for(iLoopCow = 1; iLoopCow < gstMooVolume.iCowNum ; iLoopCow++)
{
i64LastCowVolume += ( iLoopCow+iLoopCow - gstMooVolume.iCowNum)*((__int64)(gstMooVolume.astCow[iLoopCow].iLocation - gstMooVolume.astCow[iLoopCow-1].iLocation));
gstMooVolume.i64TotalVolume += i64LastCowVolume;
}

return;
}

void CalculateMooVolume_Common(void)
{
int iLoopCow;
int iLoopCow_1;

gstMooVolume.i64TotalVolume = 0;

for(iLoopCow = 0; iLoopCow < gstMooVolume.iCowNum; iLoopCow++)
{
for(iLoopCow_1 = 0; iLoopCow_1 < gstMooVolume.iCowNum; iLoopCow_1++)
{
gstMooVolume.i64TotalVolume += abs(gstMooVolume.astCow[iLoopCow].iLocation - gstMooVolume.astCow[iLoopCow_1].iLocation);
}
}

return;
}

int MooVolumeMain(void)
{
CowInput();

qsort(gstMooVolume.astCow ,gstMooVolume.iCowNum ,sizeof(COW_ST ),CowCmp);

CalculateMooVolume();

//CalculateMooVolume_Common();

printf("%I64d/n", gstMooVolume.i64TotalVolume );

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: