码迷,mamicode.com
首页 > 其他好文 > 详细

Topcoder SRM 547 Div1 250

时间:2015-03-04 06:16:07      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:数学   标记数组求和   期望   

Problem

On a horizontal line, there are two vertical pillars. The distance between their bottoms is w. The height of the first pillar is an integer, chosen uniformly at random in the range 1 through x, inclusive. The height of the second pillar is an integer, chosen uniformly at random in the range 1 through y, inclusive. The tops of both pillars will be connected by a straight piece of rope.

You are given the ints w, x, and y. Compute and return the expected length of the rope.

Limits

TimeLimit(ms):2000

MemoryLimit(MB):64

w[1,1000]

x,y[1,105]

Solution

枚举绳子的长度L,统计期望。

EXP=iLi×Nitotal,其中Ni表示不同绳子长度出现的次数,total表示总次数,total=x×y

发现L=w2+dh2,其中dh=abs(h1?h2),表示两个杆子高度的差值。因此,枚举dh即可。

下面需要处理Ni。对于不同的h1,与h2的差值dh是一段或两段连续的区间,因此只需要在这段区间上的Ni+=1即可。用两个标记数组,先标记最后统计的方法,可以在O(max(x,y))的时间内算出Ni

Complexity

TimeComplexity:O(max(x,y))

MemoryComplexity:O(max(x,y))

My Code

//Hello. I‘m Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-6
#define MOD 1000000007
#define MAXN
#define N 100100
#define M
ll add[N],sub[N],res[N];
class Pillars{
public:
    double sq(double x){
        return x*x;
    }
    double getExpectedLength(int w, int x, int y){
        repin(i,0,max(x,y)){
            add[i]=sub[i]=res[i]=0;
        }
        int from=0,to=0;
        repin(h1,1,x){
            from=max(1,h1-y);
            to=h1-1;
            if(from<=to){
                add[from]+=1;
                sub[to]+=1;
            }
            if(y>=h1) res[0]+=1;
            if(y>h1){
                from=1;
                to=y-h1;
                add[from]+=1;
                sub[to]+=1;
            }
        }
        ll now=0;
        repin(i,1,max(x,y)-1){
            now+=add[i];
            res[i]=now;
            now-=sub[i];
        }
        double ans=0.0;
        repin(i,0,max(x,y)-1){
            double t=sqrt(sq(w)+sq(i));
            ans+=res[i]*t;
        }
        ll sum=(ll)x*(ll)y;
        ans/=sum;
        return ans;
    }
};

Topcoder SRM 547 Div1 250

标签:数学   标记数组求和   期望   

原文地址:http://blog.csdn.net/uestc_peterpan/article/details/44055017

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!