http://acm.hdu.edu.cn/showproblem.php?pid=1050
背景:一开始思路走歪了,想说把整个数组按照开始的房间进行排序然后看,下一个元素开始的房间是否比前一个结束的房间小如果小的话,肯定就是要+1,。。结果发现妥妥的错了。。比如(1,100)(4,120)(130,140)(135,160)应该是20。按照一开始的思路就会是30。跑去看人家的题解,才恍然大悟应该要找重合次数最多的走廊就好了。。
思路:就直接想开始到结束的房间中要经历的走廊都加上一,最后找出走廊数组中最大的那个就可以了。
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a[500];
int cmp (int a,int b)
{
return a > b;
}
int main ()
{
int t;
cin >> t;
while(t--)
{
memset(a,0,sizeof(a));
int n;
cin >> n;
for(int i = 0;i < n;i++)
{
int st,end;
cin >> st >> end;
if(st > end)
{
int temp ;
temp = st;
st = end;
end = temp;
}
for(int j = (st+1)/2;j <= (end+1)/2;j++) //走廊编号
a[j]++;
}
sort(a,a+205,cmp);
cout << a[0]*10 << endl;
}
return 0;
}