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

hdu-acm steps FatMouse's Speed

时间:2015-01-26 16:35:47      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

本想用暴力法先试试的,案例和自己找的数据都过掉了,但是始终wa,本来期待的是tle,结果始终wa。所以也就懒的管了,直接用dp来做了。主要是因为最近在刷暴力法和dp这两个专题,所以才想好好利用一下这道题。如果有哪位发现了我的第一个程序的错误,还望告知。

暴力法(此程序不知道为何wa)

 1 #include"iostream"
 2 #include"stdio.h"
 3 #include"string.h"
 4 #include"cmath"
 5 #include"algorithm"
 6 #define mx 1005
 7 using namespace std;
 8 int cur[mx];
 9 int end1[mx];
10 struct node
11 {
12     int v;
13     int w;
14     int num;
15 }mice[mx];
16 node temp;
17 bool cmp(const node a,const node b)
18 {
19     if(a.w!=b.w) return a.w<b.w;
20     else return a.v>b.v;
21 }
22 int main()
23 {
24     int i,j,k,sum=0;
25   //  while(1){
26     while(scanf("%d%d",&mice[sum].w,&mice[sum].v)==2) {mice[sum].num=sum+1;sum++;}
27     sort(mice,mice+sum,cmp);
28     int maxsum=0;
29     for(i=0;i<sum;i++)
30     {
31         k=0;
32         for(j=i;j<sum;j++)
33         {
34                 if(j==i)
35                 {cur[k]=mice[j].num;temp=mice[i];}
36                 else if(temp.v>mice[j].v&&temp.w!=mice[j].w)
37                 {
38                         cur[++k]=mice[j].num;
39                         temp=mice[j];
40                 }
41         }
42         if(k+1>maxsum) {memcpy(end1,cur,sizeof(cur));maxsum=k+1;}
43     }
44     cout<<maxsum<<endl;
45     for(i=0;i<maxsum;i++)
46         cout<<end1[i]<<endl;
47    // }
48     return 0;
49 }

接下来是ac掉的简单dp:这道题的思路其实很简单,是一个典型的dp问题——最长上升子序列问题。

 

hdu-acm steps FatMouse's Speed

标签:

原文地址:http://www.cnblogs.com/acm-jing/p/4250499.html

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