六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 30|回复: 0

zoj 1025 Wooden Sticks

[复制链接]

升级  29.33%

24

主题

24

主题

24

主题

秀才

Rank: 2

积分
94
 楼主| 发表于 2013-1-26 12:36:27 | 显示全部楼层 |阅读模式
题目见:zoj 1025
先对木棒按照长度进行排序,然后再计算对应重量构成的数组中非递减子序列的个数。
相关代码(悲催的是该代码不能在poj1065hdoj1051 下通过,懒得看具体是什么原因了)
/* zoj 1025 Wooden sticks */#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX 5010struct stick{  int length;  int weight;};typedef struct stick stick;int cmpStick(const void * a, const void * b);int countSubsequence(int a[], int length);int main(void){  int caseNum,n,i;  stick sticks[MAX];  int weights[MAX];  scanf("%d", &caseNum);  while(caseNum-- > 0)    {      scanf("%d", &n);      for(i = 0; i < n; i++){  scanf("%d %d", &sticks.length,&sticks.weight);}      qsort(sticks,n,sizeof(stick),cmpStick);      for(i = 0; i < n; i++)weights = sticks.weight;      printf("%d\n", countSubsequence(weights,n));    }  return 0;}int cmpStick(const void *a, const void *b){  stick x,y;  x = *(stick  const *)a;  y = *(stick const *)b;  if(x.length > y.length)    return 1;  else    return 0;}int countSubsequence(int a[], int length){  int isVisited[MAX];  int i,count,prevIndex;  int resCount = 0;  memset(isVisited,0,sizeof(isVisited));  for(count =0; count < length; )    {      for(i = 0; i < length; i++)if(!isVisited)  {    prevIndex = i;    isVisited = 1;    count++;    break;  }      if(i == length)break;      for(i = prevIndex+1; i < length; i++)if(!isVisited)  {    if(a[prevIndex] <= a)      {isVisited = 1;count++;prevIndex = i;      }  }      resCount++;    }  return resCount;}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表