Zoj 1409 communication system dual-variable DP

Source: Internet
Author: User

I don't know how to start with this question. It looks like a backpack. There are two variables in it: bandwidth B, price P, and N devices, each device has K optional devices (only one device is required), and each device has its own B and P,

Select n devices for n devices. In the end, Fb = the minimum B among all devices, FP = the total price, and the maximum FB/FP

You must control a variable first, or enumerate one of the variables, and then use the DP method to obtain the optimal value of another variable.

For this question, if we first sort the equipment corresponding to each device in ascending order of price, and then I enumerate the possible FB, for each device, I scanned from the past to the next, once B> = FB (valid data) is scanned, it is stopped, this value is obtained, and the entire N devices are scanned, the obtained value must be the maximum FB/FP corresponding to the fb I enumerated. This is obvious .. Because the data volume is small, it can be used. Of course, if I calculate the peak value, the complexity can reach the power of 10, but it does not actually reach the power of 10, and it is difficult to finish running every loop.

In fact, it seems like DP, but it is actually a brute force enumeration.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;struct node{    int b,v;    bool operator < (const node&rhs) const{        return v<rhs.v;    }};vector<node>G[110];int B[10010];int n,cnt;int main(){    int t;    scanf("%d",&t);    while (t--)    {        cnt=0;        scanf("%d",&n);        for (int i=0;i<=n;i++) G[i].clear();        for (int i=1;i<=n;i++){            int k,a,b;            scanf("%d",&k);            while (k--){                scanf("%d%d",&a,&b);                B[cnt++]=a;                G[i].push_back((node){a,b});            }            sort(G[i].begin(),G[i].end());        }        sort(B,B+cnt);        int m=0;        for (int i=1;i<cnt;i++){            if (B[i]!=B[i-1]) B[++m]=B[i];        }        double ans=0.0;        for (int i=0;i<=m;i++){            bool flag=1;            double sum=0;            for (int j=1;j<=n && flag;j++){                int r=G[j].size();                for (int k=0;k<r;k++){                    if (G[j][k].b>=B[i]){                        sum+=(double)G[j][k].v;                        break;                    }                    if (k==r-1 && G[j][k].b<B[i])flag=0;                }            }            if (!flag) continue;            ans=max(ans,(double)B[i]*1.0/sum);        }        printf("%.3f\n",ans);    }    return 0;}

  

Zoj 1409 communication system dual-variable DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.