CodeForces 589D

  题目大意是给定一些有限制的区间,求每个区间和其他区间相交的次数,依次输出区间相交的个数

  思路:

    暴力,数学

  借鉴了大神的代码

  对于任意一个起点 都可以有 x = t + b / x = -t + b

  则 b 可以求出是 si ± ti 则 判断线段相交 先看斜率

  斜率相同的情况下判断区间是否包含或者相交

  斜率不同的情况下判断交点是否满足同时在两个区间内

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1000 + 10;
 4 int n;
 5 #define bug printf("Bug\n")
 6 struct Point{
 7     int t, s, f;
 8     Point(int t = 0, int s = 0, int f = 0) : t(t), s(s), f(f) {}
 9 }p[maxn];
10
11 int cnt[maxn];
12
13 bool judge(int s1, int f1, int s2, int f2){
14     if(s1 >= s2 && f1 <= f2) return true;
15     if(s2 >= s1 && f2 <= f1) return true;
16     if(s1 <= f2 && f2 <= f1) return true;
17     if(f1 <= f2 && f1 >= s2) return true;
18     return false;
19 }
20
21
22 int main()
23 {
24     scanf("%d", &n);
25     for(int i = 0; i < n; ++i){
26         int t, s, f;
27         scanf("%d %d %d", &t, &s, &f);
28         p[i] = Point(t, s, f);
29     }
30     memset(cnt, 0, sizeof(cnt));
31     for(int i = 0; i < n; ++i){
32         for(int j = i + 1; j < n; ++j){
33             int s1 = p[i].s, f1 = p[i].f, s2 = p[j].s, f2 = p[j].f, t1 = p[i].t, t2 = p[j].t;
34
35             if(s1 <= f1 && s2 <= f2){
36                 if(s1 - t1 != s2 - t2) continue;
37                 if(judge(t1, f1 - s1 + t1, t2, f2 - s2 + t2)){
38                     cnt[i]++; cnt[j]++;
39                 }
40             }
41             else if(s1 > f1 && s2 <= f2){
42                 double x = (s1 + s2 + t1 - t2) * 1.0 / 2;
43                 if(x >= s2 && x <= f2 && x >= f1 && x <= s1){
44                     cnt[i]++; cnt[j]++;
45                 }
46             }
47             else if(s1 <= f1 && s2 > f2){
48                 double x = (s1 + s2 + t2 - t1) * 1.0 / 2;
49                 if(x >= f2 && x <= s2 && x >= s1 && x <= f1){
50                     cnt[i]++; cnt[j]++;
51                 }
52             }
53             else{
54                 if(s1 + t1 != s2 + t2) continue;
55                 if(judge(t1, s1 - f1 + t1, t2, s2 - f2 + t2)) {
56                     cnt[i]++; cnt[j]++;
57                 }
58             }
59         }
60     }
61     for(int i = 0; i < n; ++i){
62         if(i) printf(" ");
63         printf("%d", cnt[i]);
64     }
65     return 0;
66 }

时间: 2024-08-14 16:44:25

CodeForces 589D的相关文章

codeforces 589 D - Boulevard

D - Boulevard Time Limit:2000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 589D Description Welcoming autumn evening is the best for walking along the boulevard and n people decided to do so. The bou

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Codeforces Round #408 (Div. 2) B

Description Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x?=?i. The probl

Codeforces 617 E. XOR and Favorite Number

题目链接:http://codeforces.com/problemset/problem/617/E 一看这种区间查询的题目,考虑一下莫队. 如何${O(1)}$的修改和查询呢? 令${f(i,j)}$表示区间${\left [ l,r \right ]}$内数字的异或和. 那么:${f(l,r)=f(1,r)~~xor~~f(1,l-1)=k}$ 记一下前缀异或和即可维护. 1 #include<iostream> 2 #include<cstdio> 3 #include&l