zoukankan      html  css  js  c++  java
  • UVA 1456 Cellular Network 贪心+DP

    YY过程:概率大的应该先访问吧,嗯,排序一下试试。然后就AC了。。。

    我也不知道怎么证明这样贪心是正确。。

    至于DP的过程就是水题了,DP[i][j]表示从第i个开始,分成j组的最小期望

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    #define pb(a) push_back(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("d:\in.txt","r",stdin);
        freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!=' ')return ch;
        }
        return EOF;
    }
    int n,w;
    int u[110];
    int dp[110][110];
    int f(int k,int g)
    {
        if(dp[k][g]>=0)return dp[k][g];
        if(k>n)
        {
            if(g==0)return 0;
            else return INF;
        }
        int minn=INT_MAX;
        int num=0;
        for(int i=k;i<=n&&n-i>=g-1;i++)
        {
            num+=u[i];
            minn=min(minn,f(i+1,g-1)+i*num);
        }
        return dp[k][g]=minn;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        for(int ca=1;ca<=t;ca++)
        {
            scanf("%d%d",&n,&w);
            for(int i=1;i<=n;i++)
                scanf("%d",&u[i]);
            sort(u+1,u+1+n,greater<int>());
            memset(dp,-1,sizeof(dp));
            int num=f(1,w);
            int sum=0;
            for(int i=1;i<=n;i++)sum+=u[i];
            printf("%.4lf
    ",(double)num/sum);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    再谈AR中的图像识别算法
    turret
    Unity之MVC 模式
    Unity之Application.runInBackground = true
    快速了解和使用Photon Server
    Unity Standard Assets 简介之 CrossPlatformInput
    Lua的闭包详解(终于搞懂了)
    代码大全、人月神话和你的灯亮着吗三本软件开发设计方面好书
    大数据入门推荐
    迷茫的旅行商:一个无处不在的计算机算法问题
  • 原文地址:https://www.cnblogs.com/BMan/p/3272616.html
Copyright © 2011-2022 走看看