problem 1108
来源:互联网 发布:淘宝开店做什么产品好 编辑:程序博客网 时间:2024/06/03 02:17
这道题的DP思想还是挺巧妙的。
毫无疑问要先对w排序,然后将所有组的DP记录都置1,再依次填表。
当前序号
w
s
DP记录
原先的序号
0
500
2000
1
2
1
1000
4000
1
3
2
1100
3000
2
4
3
2000
1900
3
8
4
6000
2000
3
8
5
6000
1200
4
7
6
6000
2100
3
1
7
6008
1300
4
0
8
8000
1400
4
6
主要是第三行,举个例子:
6000 1200那组,在这组前面的组中当前序号为0、1、2、3、4中的s值都比1200大,那么就取DP记录最大一组加上1,如果没有比它大的就不做。
填完表后再从后往前打印出结果就OK了。
Accepted 1108 C 00:00.00 432K
#include<stdio.h>
#include<stdlib.h>
struct s
{
int w;
int s;
int index;
int rd;
}read[1000];
int cmp(const void* a,const void* b)
{
return (*(struct s*)a).w-(*(struct s*)b).w;
}
void solve()
{
int i,j,cnt = 0,max = 0,temp = 0,res[1000];
memset(res,0,sizeof(res));
while(scanf("%d%d",&read[cnt].w,&read[cnt].s) != EOF)
{
read[cnt].index = cnt;
cnt++;
}
cnt;
qsort(read,cnt,sizeof(read[0]),cmp);
for(i = 0; i < cnt; i++)
{
read[i].rd = 1;
for(j = 0; j < i; j++)
{
if(read[i].s < read[j].s)
{
read[i].rd = read[i].rd < read[j].rd + 1 ? read[j].rd + 1 : read[i].rd;
max = max > read[i].rd ? max : read[i].rd;
}
}
}
printf("%d/n",max);
for(i = cnt - 1; i > 0; i--)
{
if(read[i].rd == max)
{
if(temp == 0 || read[temp].s < read[i].s)
{
temp = i;
max--;
res[max] = read[i].index + 1;
}
}
}
i = 0;
while(res[i] != 0)
printf("%d/n",res[i++]);
}
void main()
{
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#include<stdlib.h>
struct s
{
int w;
int s;
int index;
int rd;
}read[1000];
int cmp(const void* a,const void* b)
{
return (*(struct s*)a).w-(*(struct s*)b).w;
}
void solve()
{
int i,j,cnt = 0,max = 0,temp = 0,res[1000];
memset(res,0,sizeof(res));
while(scanf("%d%d",&read[cnt].w,&read[cnt].s) != EOF)
{
read[cnt].index = cnt;
cnt++;
}
cnt;
qsort(read,cnt,sizeof(read[0]),cmp);
for(i = 0; i < cnt; i++)
{
read[i].rd = 1;
for(j = 0; j < i; j++)
{
if(read[i].s < read[j].s)
{
read[i].rd = read[i].rd < read[j].rd + 1 ? read[j].rd + 1 : read[i].rd;
max = max > read[i].rd ? max : read[i].rd;
}
}
}
printf("%d/n",max);
for(i = cnt - 1; i > 0; i--)
{
if(read[i].rd == max)
{
if(temp == 0 || read[temp].s < read[i].s)
{
temp = i;
max--;
res[max] = read[i].index + 1;
}
}
}
i = 0;
while(res[i] != 0)
printf("%d/n",res[i++]);
}
void main()
{
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#endif
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}
}
- problem 1108
- problem
- Problem
- problem
- Problem
- Problem
- Problem
- Problem
- Problem
- Problem
- problem
- Problem
- Problem
- Problem
- Problem
- Problem
- Problem
- Problem
- 映射
- 人见人爱的经典交际技巧
- 如何用正确的方法来写出质量好的软件的75条体会
- awk用法
- Blog小技巧之七-给你的Blog加上漂亮的语录轮播,只要3分钟
- problem 1108
- 用jsp进行数据分页显示的一个实现
- Linux信号列表
- ie developer toolbar,IE6/IE7下的FireBug
- 海量数据库的 查询优化及分页算法方案
- 关于Servlet中的相对路径和绝对路径归纳
- pku1011
- ASP.NET实现投票结果的图片进度条显示
- 未知错误 ID:-2147467259 "操作必须使用一个可更新的查询"