Chgtaxihe's blog Chgtaxihe's blog
首页
练习
算法学习
读书笔记
小玩意

Chgtaxihe

首页
练习
算法学习
读书笔记
小玩意
  • 训练补题
  • Codeforces 刷题计划
  • 概率dp练习
  • dp专题练习
  • 数论练习
  • 搜索专题练习
  • 图论练习
  • 基础字符串练习
  • AtCoder练习
  • 题解们
  • solutions

    • POJ-3468
    • Gym102501J题解
    • Gym102483C(Circuit Board Design)题解
    • Gym102433G(Glow, Little Pixel, Glow)题解
      • Gym102433G Glow, Little Pixel, Glow
    • Codeforces 578D
  • 训练补题

Gym102433G(Glow, Little Pixel, Glow)题解

# Gym102433G Glow, Little Pixel, Glow

假设现在所有脉冲都已经发出,现在把纵向和横向的脉冲投影到y=xy=xy=x上。

由于纵向/横向脉冲的投影在y=xy=xy=x上的移送速度相等,如果两道脉冲会相交,那么他们在y=xy=xy=x上的投影重叠,否则,他们一定不相交。

当然,为了避免小数导致的精度损失,我们不能直接用投影,而是稍作转化

如图,一段脉冲用Pin,PoutP_{in},P_{out}Pin​,Pout​表示,我们可以用端点的投影PPP到达原点OOO(或y=xy=xy=x上任意一点)的时间作为标记,因而有Pin=t−a,Pout=t−a+mP_{in}=t-a,P_{out}=t-a+mPin​=t−a,Pout​=t−a+m

接着我们把所有Pin,PoutP_{in},P_{out}Pin​,Pout​放入vector中,进行排序。

从前往后遍历vector,当一个纵向脉冲进入,我们知道它一定与所有尚未离开的横向脉冲相交;横向脉冲进入时同理。

#include <bits/stdc++.h>
#define ll long long

using namespace std;

struct info{
    int t, v;
    char type;
    bool operator < (const info & o) const{
        if(t != o.t) return t < o.t;
        if(v != o.v) return v < o.v;
        return type < o.type;
    }
};

int n;
char buffer[5];
signed main(){
    cin >> n;
    vector<info> event;
    for(int i=0; i<n; i++){
        int t, m, a;
        cin >> buffer >> t >> m >> a;
        event.push_back({t-a-1, 1, buffer[0]});
        event.push_back({t-a-1+m, -1, buffer[0]});
    }
    sort(event.begin(), event.end());
    ll vcnt = 0, hcnt = 0, ans = 0;
    for(info i:event){
        if(i.type == 'h'){
            hcnt += i.v;
            if(i.v > 0) ans += vcnt;
        }else{
            vcnt += i.v;
            if(i.v > 0) ans += hcnt;
        }
    }
    cout << ans << '\n';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
上次更新: 2021/02/24, 03:37:30
Gym102483C(Circuit Board Design)题解
Codeforces 578D

← Gym102483C(Circuit Board Design)题解 Codeforces 578D→

最近更新
01
深入理解Java虚拟机
03-04
02
DNS工作原理
02-27
03
改用VuePress啦
02-23
更多文章>
Theme by Vdoing | Copyright © 2019-2021
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式