2020 Multi-University Training Contest 1-1009 Leading Robots

http://acm.hdu.edu.cn/showproblem.php?pid=6759

 

 

 

 题意:给定一些车的初始位置和加速度(初速度为0

  问某一时刻的领先车有多少种情况

分析:

  初始位置最大的一定会在一开始领先

  初始位置小的(或相同)且 加速度小的 没有机会成为领先

思路:

  机器人先按加速度 a 排序,加速度相同按初始位置 p 排序。

  按加速度遍历机器人,记录和比较超越时间,维护一个顺序栈得到领先的车

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
//#define sort(a,n,int) sort(a,a+n,less<int>())

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=2e5+10;
const int mod=1e9+7;

int stack_r[N];
struct point{
    ll x , y , num;
}s[N] , g[N];

bool cmp(point a , point b){
    if(a.x == b.x)
        return a.y > b.y;
    return a.x < b.x;
}

int main(){
    int T;
    cin >> T;
    
    ll ans = 0 , n = 0;
    while(T--)
	{
		ans = 0;
		cin >> n;
        for(int i = 1;i <= n;i++)
		{
            cin >> s[i].x >> s[i].y;
        }
        sort(s+1 , s+1+n , cmp);
        
        int max_s = 0;
		int p = 0;
        for(int i = n;i >= 1;i--)
		{
            int j = i;
            while(j >= 2 && s[j-1].x == s[i].x)
            {
				j--;
            }
			if(s[j].y > max_s)
			{
				p += 1;
                if(i == j)
				{
					g[p] = {s[j].y , -s[j].x , 1};
					max_s = s[j].y;
				}
				else if(s[j].y == s[j+1].y)
				{
                    g[p] = {s[j].y , -s[j].x , 0};
                }
				else
				{
                    g[p] = {s[j].y , -s[j].x , 1};
					max_s = s[j].y;
                }
            }
            i = j;
        }
        
        int t = 0;
        for(int i = 1;i <= p;i++)
		{
            while(t >= 2)
			{
				int s1 = (g[i].y - g[stack_r[t]].y) * (g[stack_r[t]].x - g[stack_r[t-1]].x);
				int s2 = (g[stack_r[t]].y - g[stack_r[t-1]].y) * (g[i].x - g[stack_r[t]].x);
			 	if(s1 >= s2)
			 	{
                 	t -= 1;
            	}
            }
            t += 1; 
            stack_r[t] = i;
        }
        for(int i = 1;i <= t;i++)
        {
            ans += g[stack_r[i]].num;
 		}
		cout << ans <<endl;
    }
    return 0;
}

  

  

posted @ 2020-07-25 20:56  YukiRinLL  阅读(125)  评论(0编辑  收藏  举报