Poj_2533_longest ordered Su... poj_1260_pearls hdu_1025_constructing roa... poj_2533_longest ordered

Source: Internet
Author: User

/*

The title cannot be too long.

Poj_2533_longest ordered subsequence poj_1260_pearls hdu_1025_constructing roads in jgshining's King hdu_1074_doing homework

*/

Poj_2533_longest ordered subsequence

Http://poj.org/problem? Id = 2533

Lis does not explain =

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int main(){int n;cin>>n;int a[1010];int dp[1010];for (int i=0;i<n;i++){cin>>a[i];}int ans=1;for (int i=0;i<n;i++){dp[i]=1;for (int j=0;j<i;j++){if(a[i]>a[j]){dp[i]=max(dp[i],dp[j]+1);}}ans=max(ans,dp[i]);}cout<<ans<<endl;}
Poj_1260_pearls

Http://poj.org/problem? Id = 1260

There are n kinds of pearls, the higher the price, the better the quality, each payment (quantity + 10) * unit price when purchasing;

Originally planned n kinds of pearls, I buy AI, price PI; (price P I> P i-1)

Now you need to change the solution, so that the number of purchases remains unchanged, the quality is better, the total price is smaller, and the total price is.

The key to a lower price is the "+ 10 ",

In addition, the replacement of pearls is a continuous segment. If the I + 2 pearls can be used to replace the I, the I + 1 should be more convenient.

So DP [I] = min (DP [I], DP [J] + (sum [I]-sum [J] + 10) * P [I]);

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int INF=999999999;struct Node {int a,p;}node[106];bool cmp (Node a,Node b){return  a.p<b.p;}int main(){    int c;    cin>>c;    while (c--){        int n,sum=0;        int dp[106];        int s[106];        dp[0]=0;s[0]=0;        cin>>n;        for (int i=1;i<=n;i++){            cin>>node[i].a>>node[i].p;            sum+=(node[i].a+10)*node[i].p;            s[i]=s[i-1]+node[i].a;        }        sort(node,node+n,cmp);        for (int i=1;i<=n;i++){        dp[i]=INF;            for (int j=0;j<=i-1;j++){            dp[i]=min(dp[i],dp[j]+(s[i]-s[j]+10)*node[i].p);            }            // cout<<dp[i]<<" ";        }        cout<<dp[n]<<endl;    }    return 0;}

Hdu_1025_constructing roads in jgshining's king

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1025

There are some cities on the parallel lines. One side is rich and the other side is poor. A city without a parent has a resource, which corresponds to a poor city that lacks resources for reform.

Now the King wants to build roads between rich cities and poor cities to help them (the possession and lack of resources must correspond), but to prevent traffic accidents, the two roads are not allowed to cross.

Ask how many roads can be repaired at most.

Evaluate LIS based on the value of the array elements of poverty-stricken cities and rich cities.

= Good questions. I can't guess the O (nlogn) method. (copy a self-White Book) And, road and roads.

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int n;const int INF=999999999;int dp[500050];int main(){    int c(0);    char road[2][10]={"road","roads"};    while (scanf("%d",&n)!=EOF){        int a[500050];        for (int i=0;i<n;i++){            int x,y;            scanf ("%d%d",&x,&y);            a[x-1]=y;        }        fill (dp,dp+n,INF);        for (int i=0;i<n;i++){            *lower_bound(dp,dp+n,a[i])=a[i];        }        int ans=lower_bound(dp,dp+n,INF)-dp;;        printf ("Case %d:\nMy king, at most %d %s can be built.\n",++c,ans,road[ans!=1?1:0]);    }    return 0;}

Hdu_1074_doing homework

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1074

The deadline for N assignments is D and it takes C days. If n assignments are not submitted late, one point is deducted.

I have been thinking about this question for a few days. Although I know it is about pressure DP, I know that I am inspired when I eat at noon today, and I use one-dimensional DP. T ^ t, always thinking about two-dimensional.

Compress n jobs into a binary Int. The I-bit indicates whether the I-th job has been done.

If s indicates the finished set, it can be obtained from {s}-{J} V {J.

DP [s] = min (DP [S-(1 <j)] + S); (S: score to be penalized for performing the J-job, S = max (T [s] + CS-ds, 0 ));

Path Restoration: the FA Array records the status I obtained from the status Fa [I]. The Restoration uses bitwise operations and recursion;

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define INF 999999999struct Work {string s;int d,c;}w[16];struct Node{int p,t;};int  fa[1<<15+6];Node dp[1<<15+6];void findway (int a,int b){int c=a-b;int k=0;while(c!=1){c>>=1;k++;}cout<<w[k].s<<endl;}void find (int f,int k){if (f!=k){find (fa[f],f);findway(k,f);}}int main(){int c;cin>>c;while (c--){int n;scanf("%d",&n);for (int i=0;i<n;i++){cin>>w[i].s>>w[i].d>>w[i].c;}for (int i=0;i<(1<<n);i++){dp[i].p=INF;dp[i].t=0;fa[i]=i;}dp[0].p=0;for (int i=1;i<(1<<n);i++){for (int j=0;j<n;j++){if (i&(1<<j)){int s;s=max(dp[i-(1<<j)].t+w[j].c-w[j].d,0);if(dp[i].p>=dp[i-(1<<j)].p+s){dp[i].p=dp[i-(1<<j)].p+s;fa[i]=i-(1<<j);}dp[i].t=dp[i-(1<<j)].t+w[j].c;}}}printf ("%d\n",dp[(1<<n)-1]);int k=(1<<n)-1;find(fa[k],k);}return 0;}

DP water my water is so cheerful ~ Ah.


Poj_2533_longest ordered Su... poj_1260_pearls hdu_1025_constructing roa... poj_2533_longest ordered

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.