zoukankan      html  css  js  c++  java
  • zoj 4120Tokens on the Segments(优先队列+贪心)

    Tokens on the Segments
    Time Limit: 1000 msMemory Limit: 65536 KB

    Consider  segments on a two-dimensional plane, where the endpoints of the -th segment are  and . One can put as many tokens as he likes on the integer points of the plane (recall that an integer point is a point whose  and  coordinates are both integers), but the  coordinates of the tokens must be different from each other.

    What's the maximum possible number of segments that have at least one token on each of them?

    Input

    The first line of the input contains an integer  (about 100), indicating the number of test cases. For each test case:

    The first line contains one integer  (), indicating the number of segments.

    For the next  lines, the -th line contains 2 integers  (), indicating the  coordinates of the two endpoints of the -th segment.

    It's guaranteed that at most 5 test cases have .

    Output

    For each test case output one line containing one integer, indicating the maximum possible number of segments that have at least one token on each of them.

    Sample Input

    2
    3
    1 2
    1 1
    2 3
    3
    1 2
    1 1
    2 2
    

    Sample Output

    3
    2
    

    Hint

    For the first sample test case, one can put three tokens separately on (1, 2), (2, 1) and (3, 3).

    For the second sample test case, one can put two tokens separately on (1, 2) and (2, 3).

    题意 第一行输入t表示t组案例,之后输入n表示有n条线段,接下来n行输入a,b;表示坐标为(a-b,(1~n))与x轴平行的线段;问至少经过一个x坐标不相等的点线段的数量;

    结构体优先队列

    struct A{
    ll l,r;
    bool operator < (const A&a) const
    {
    if(l==a.l)
    return r>a.r;
    return l>a.l;
    }
    };

    以线段的左端排序构造优先队列

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    struct A{
    ll l,r;
    bool operator < (const A&a) const
    {
    if(l==a.l)
    return r>a.r;
    return l>a.l;
    }
    };

    priority_queue<A> q;
    int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll T,i,j,n;
    cin>>T;
    while(T--){
    cin>>n;
    A t;
    for(int i=0;i<n;i++){
    cin>>t.l>>t.r;
    q.push(t);
    }
    ll L=0,ans=0; //L为当前左值,大于L的点可算入线段总数
    while(!q.empty()){
    t=q.top();
    q.pop();
    if(t.l>L){
    ans++;
    L=t.l;
    }
    else
    if(t.l+1<=t.r){
    t.l+=1;//左端点右移,并放入队列
    q.push(t);
    }
    }
    cout<<ans<<endl;
    }
    }

  • 相关阅读:
    python-初始网络编程
    mysql 之优化
    python 操作Mysql
    白话SCRUM之一:SCRUM 的三个角色
    白话SCRUM 之三:sprint backlog
    Scrum 之 product Backlog
    9.为什么要进行系统拆分?如何进行系统拆分?拆分后不用 dubbo 可以吗?
    8.如何自己设计一个类似 Dubbo 的 RPC 框架?
    7.分布式服务接口请求的顺序性如何保证?
    6.分布式服务接口的幂等性如何设计(比如不能重复扣款)?
  • 原文地址:https://www.cnblogs.com/fanyu1/p/12519133.html
Copyright © 2011-2022 走看看