poj 1065 Wooden Sticks
生活随笔
收集整理的這篇文章主要介紹了
poj 1065 Wooden Sticks
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*先按l排序 再每次找出一段最長的遞增序列!*/#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stick{
int l, w;
}st[50010];
bool used[50010];
int cmp(const void *a, const void *b){
stick *s1 = (stick*)a, *s2 = (stick*)b;
if(s1 -> l == s2 -> l)
return s1 -> w - s2 -> w;
return s1 -> l - s2 -> l;
}
int main(){
int t ,n;
scanf("%d",&t);
while( t-- ){
memset(used, false, sizeof(used));
scanf("%d",&n);
for(int i = 0; i < n; ++i){
scanf("%d%d",&st[i].l, &st[i].w);
}
qsort(st, n, sizeof(st[0]), cmp);
//for(int i = 0; i < n; ++i){
// printf("%d %d\n",st[i].l, st[i].w);
//}
int i,j, ans = 0;
stick temp;
for(i = 0; i < n; ++i){//關鍵代碼
if( !used[i] ){
ans++;
used[i] = true;
temp = st[i];
for(j = i + 1; j < n; j++){
if(st[j].l >= temp.l && st[j].w >= temp.w && !used[j]){
temp = st[j];
used[j] = true;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
#include <stdlib.h>
#include <string.h>
struct stick{
int l, w;
}st[50010];
bool used[50010];
int cmp(const void *a, const void *b){
stick *s1 = (stick*)a, *s2 = (stick*)b;
if(s1 -> l == s2 -> l)
return s1 -> w - s2 -> w;
return s1 -> l - s2 -> l;
}
int main(){
int t ,n;
scanf("%d",&t);
while( t-- ){
memset(used, false, sizeof(used));
scanf("%d",&n);
for(int i = 0; i < n; ++i){
scanf("%d%d",&st[i].l, &st[i].w);
}
qsort(st, n, sizeof(st[0]), cmp);
//for(int i = 0; i < n; ++i){
// printf("%d %d\n",st[i].l, st[i].w);
//}
int i,j, ans = 0;
stick temp;
for(i = 0; i < n; ++i){//關鍵代碼
if( !used[i] ){
ans++;
used[i] = true;
temp = st[i];
for(j = i + 1; j < n; j++){
if(st[j].l >= temp.l && st[j].w >= temp.w && !used[j]){
temp = st[j];
used[j] = true;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
轉載于:https://www.cnblogs.com/lxf90/archive/2011/04/09/2010067.html
總結
以上是生活随笔為你收集整理的poj 1065 Wooden Sticks的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在新的标签页中代开编辑文件
- 下一篇: The Guy Who Ran Micr