POJ-3154-Graveyard

题目:POJ-3154-Graveyard

一个周长为10000的圆圈,一开始等距的安放着N个雕塑,现在想增加M个雕塑,使得雕塑之间还是等距,问坟墓最少移动的距离。

思路:只有n个时设周长为1,则坐标为i/n,加m后周长扩至n+m{好处是新坐标为整数,0~n+m-1},新坐标为i/n*(m+n)的四舍五入,作差累加即得移动距离。

 1 #include <iostream>
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #include<algorithm>
 6 #include<math.h>
 7
 8 using namespace std;
 9
10 int main()
11 {
12     int n,m,i;
13     double pos,ans;
14     while(scanf("%d%d",&n,&m)!=EOF)
15     {
16         ans=0;
17         for(i=1;i<=n-1;i++)
18         {
19             pos=(double)i/n*(n+m); //#
20             ans+=fabs(pos-floor(pos+0.5))/(n+m); //#
21         }
22         ans*=10000;
23         printf("%.4lf\n",ans);
24     }
25     return 0;
26 }
时间: 2024-10-28 20:18:54

POJ-3154-Graveyard的相关文章

poj 3154 Graveyard 贪心

//poj 3154 //sep9 #include <iostream> #include <cmath> using namespace std; double a[2048]; double b[2048]; int main() { int n,m; while(scanf("%d%d",&n,&m)==2){ for(int i=0;i<n;++i) a[i]=i*(10000.0/n); for(int i=0;i<(n+

LA 3708 &amp;&amp; POJ 3154 Graveyard (思维)

题意:在周长为10000的圆上等距分布着n个雕塑,现在又加入m个,现在让m+n个等距分布,那就得移动一些原有的雕塑,问你移动的最少总距离是多少. 析:首先我们可以知道,至少有一个雕塑是可以不用移动的,那么我们以那修个没有移动的雕塑为原点建立坐标.现在问题就转化为把剩下的移动到离它最近的位置(这个位置是放入m个雕塑之后的位置),那么这个距离就应该是最短的. 代码如下: #include <cstdio> #include <iostream> #include <cstring

poj 2100 Graveyard Design

Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 7357   Accepted: 1816 Case Time Limit: 2000MS Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard m

poj 2100 Graveyard Design(尺取法)

Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves.

Graveyard Design POJ - 2100

King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves. After a con

Greedy:Graveyard Design(POJ 2100)

墓地 题目大意,给定一个整数,要你找出他的平方和组合 太简单了....不过一开始我储存平方和想降低时间,后来发现会超内存,直接用时间换空间了,游标卡尺法 1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 #define MAX_N 10000001 5 6 using namespace std; 7 typedef long long LL_INT; 8 9 static in

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c