还是差了一些东西,卡在了这个题上面,果然和银牌是有差距的,差的就是些许思维,和对数据的安排策略。
数据的大小就预示了只能用十分初级的数据来检测自身的策略走向,而不能使用数组之类在整合线段上检测。
这个题其实暴力也能过,考察的还是胆量。
#include<stdio.h>
#include
#include
#include
using namespace std;
struct st
{
int x,y;
}q;
struct cmp//排序策略,贪心思维
{
bool operator()(const st&t1,const st&t2)
{
if(t1.x!=t2.x)
return t1.x>t2.x;
return t1.y>t2.y;
}
};
priority_queue<st,vector,cmp> gt;//优先队列
int f()
{
int count=0,mi=-1;
while(!gt.empty())//增加一个数据,贪心的从左向右,该数据用来测量最左端走到了哪个地方
{//优先队列的状态转移
q=gt.top();
if(q.x>mi)
{
mi=q.x;
count++;
gt.pop();
}
else if(mi==q.x&&q.x<q.y)
{
q.x++;
gt.pop();
gt.push(q);
}
else if(mi>q.x&&mi+1<=q.y)
{
count++;
mi++;
gt.pop();
}
else
gt.pop();
//printf("%d %d %d\n",q.x,q.y,count);
//printf("%d",gt.size());
}
return count;
}
int main()
{
int T,i,j,n,r1,r2;
scanf("%d",&T);
st p;
for(i=0;i<T;i++)
{
scanf("%d",&n);
for(j=0;j<n;j++)
{
scanf("%d%d",&r1,&r2);
p.x=r1;
p.y=r2;
gt.push§;
}
int y=f();
printf("%d\n",y);
}
}
山东第十届acm省赛折戟题—思维加优先队列(状态安排)H - Tokens on the Segments详解
最新推荐文章于 2024-04-27 16:11:04 发布