poj1083
水题一道,看清房间到底是怎么分布的
看清了啊,我就是第一次没有看清这才wa的
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int cmp (int a,int b) 7 { 8 return a>b; 9 } 10 int main() 11 { 12 int Case; 13 cin>>Case; 14 int from, to; 15 int room[405]; 16 int i,j; 17 while(Case--) 18 { 19 memset(room,0,sizeof(room)); 20 int n; 21 cin>>n; 22 for(i=0;i<n;i++) 23 { 24 cin>>from>>to; 25 /*if(from>to) 26 { 27 int tmp; 28 tmp=from; 29 from=to; 30 to=tmp; 31 }*/ 32 if(from>to) 33 { 34 swap(from,to); 35 } 36 from=(from-1)>>1; 37 to=(to-1)>>1; 38 for(j=from;j<=to;j++) 39 { 40 room[j]++; 41 } 42 } 43 //sort(room,room+404,cmp); 44 45 //cout<<room[0]*10<<endl; 46 cout<<*max_element(room,room+400)*10<<endl; 47 } 48 return 0; 49 }
认识的几个algorithm里面的几个函数,函数调用其实很爽啊
这道题亦可用贪心来做,只不过贪心有点麻烦而已
1 View Code 2 /* 3 * PKU 1083 , Moving Tables 4 * Created by PKKJ , 2008-05-17 5 * 贪心 6 */ 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 /* 11 * 结构体work代表了一项搬桌子的工作,a与b分别代表的是起始和结束的位置,used则注明这项工作是否被安排 12 */ 13 typedef struct _work 14 { 15 int a; 16 int b; 17 int used; 18 } work; 19 20 int compare(const void* a, const void *b) 21 { 22 if ((*(work *)a).a == (*(work *)b).a) 23 return (*(work *)a).b > (*(work *)b).b ? 1 : -1; 24 return (*(work *)a).a > (*(work *)b).a ? 1 : -1; 25 } 26 27 int main() 28 { 29 int num; 30 work queue[210]; 31 scanf("%d", &num); 32 while (num--) 33 { 34 int i, j, n, u, time = 0; 35 scanf("%d", &n); 36 for (i = 0; i < n; i++) 37 { 38 int t; 39 scanf("%d%d", &queue[i].a, &queue[i].b); 40 /* 读入数据时注意顺序,a需要小于b */ 41 if (queue[i].a > queue[i].b) 42 { 43 t = queue[i].a; 44 queue[i].a = queue[i].b; 45 queue[i].b = t; 46 } 47 queue[i].used = 0; 48 } 49 /* 使用系统的qsort进行快速排序 */ 50 qsort(queue, n, sizeof(queue[0]), compare); 51 /* 逐项搬桌子任务检查,看看是否完成了*/ 52 for (i = 0; i < n; i++) 53 { 54 if (!queue[i].used) 55 { 56 queue[i].used = 1; 57 u = i; 58 /*向后找相容的搬桌子任务:注意题目所给的条件,怎样是冲突,怎样是相容*/ 59 for (j = i + 1; j < n; j ++) 60 { 61 if (!queue[j].used) 62 { 63 if ((queue[j].a > queue[u].b + 1) || (queue[j].a - 1 64 == queue[u].b) && (queue[u].b % 2 == 0)) 65 { 66 queue[j].used = 1; 67 u = j; 68 } 69 } 70 } 71 time++; 72 } 73 } 74 printf("%d\n", time * 10); 75 } 76 return 0; 77 }