hihocoder Round #c1(hihoCoder太阁最新面经算法竞赛1 )

Test链接:https://cn.vjudge.net/contest/231849

选自hihoCoder太阁最新面经算法竞赛1

更多Test:传送门

 

A:区间求差

给一组区间集合A和区间集合B,求A-B的长度

思路:

首先进行离散化,对每个点进行标号处理,对于A中的每个区间,用树状数组把离散化后的A中的每个区域标记。

对于B的每个区间,用另一个树状数组标记。

这里对点i标记,等价于标记线段[i-1, i](i为离散化后的标号)

之后对每个点查询是否在A中标记,在B中未标记,那就是A-B的子区间,加上这个点表示的线段长度即可。

时间复杂度O(nlog(n))

 1 #include<bits/stdc++.h>
 2 #define lowbit(x) ((x)&(-x))
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn = 1e6 + 10;
 6 int a[maxn], b[maxn];
 7 int tree_a[maxn], tree_b[maxn];
 8 bool vis[maxn];
 9 int tot2;
10 int sum(int x, int a[])
11 {
12     int ret = 0;
13     while(x <= tot2)
14     {
15         ret += a[x];
16         x += lowbit(x);
17     }
18     return ret;
19 }
20 //向前修改[0, x]整个区间加上d
21 void add(int x, int d, int a[])
22 {
23     while(x > 0)
24     {
25         a[x] += d;
26         x -= lowbit(x);
27     }
28 }
29 
30 int main()
31 {
32     int n, m;
33     scanf("%d%d", &n, &m);
34     int tot = 2 * n + 2 * m, tot1 = 2 * n;
35     for(int i = 1; i <= tot1; i++)scanf("%d", &a[i]);
36     for(int i = tot1 + 1; i <= tot; i++)scanf("%d", &a[i]);
37     memcpy(b, a, sizeof(a));
38     sort(b + 1, b + tot + 1);
39     tot2 = unique(b + 1, b + tot + 1) - (b + 1);
40     //for(int i = 1; i <= tot2)
41     for(int i = 1; i <= tot1; i += 2)
42     {
43         int x = lower_bound(b + 1, b + tot2 + 1, a[i]) - b;
44         int y = lower_bound(b + 1, b + tot2 + 1, a[i + 1]) - b;
45         add(x, -1, tree_a);
46         add(y, 1, tree_a);
47     }
48     for(int i = tot1 + 1; i <= tot; i += 2)
49     {
50         int x = lower_bound(b + 1, b + tot2 + 1, a[i]) - b;
51         int y = lower_bound(b + 1, b + tot2 + 1, a[i + 1]) - b;
52         add(x, -1, tree_b);
53         add(y, 1, tree_b);
54     }
55     ll ans = 0;
56     for(int i = 1; i <= tot2; i++)
57     {
58         if(sum(i, tree_a) > 0 && sum(i, tree_b) == 0)
59         {
60             //cout<<i<<endl;
61             ans += b[i] - b[i - 1];
62         }
63     }
64     cout<<ans<<endl;
65     return 0;
66 }

 

B:股票价格

动态模拟下列操作

P x y x时刻股票价格为y

R x 清除x之前的所有股票价格信息

Q 输出目前已知的最大 最小 最近股票价格(不包括删除的股票)

保证P x y中的时刻x为升序

思路:

用queue动态保存x和y,每次入队更新最大值,最小值,最新值,在Q操作时直接输出即可

对于删除操作,由于给的x是升序,所以可以直接利用队列的pop操作。

但是,删除之后,要更新最大值最小值,所以用set和map存储目前在队列中的x值,以及出现的次数,每加入和删除一个数字的时候更新set和map,删除操作完成后,更新最大值最小值(此时要保证队列中还有数字,如果没有,要将这两个数重新初始化)

 1 #include<bits/stdc++.h>
 2 #define lowbit(x) ((x)&(-x))
 3 using namespace std;
 4 typedef long long ll;
 5 struct node
 6 {
 7     int a, b;
 8     node(){}
 9     node(int a, int b):a(a), b(b){}
10 };
11 set<int>s;
12 map<int, int>Map;
13 queue<node>q;
14 int main()
15 {
16     int n, x, y;
17     cin >> n;
18     int Max = -1, Min = 1e9+7, last;
19     char a[5];
20     while(n--)
21     {
22         scanf("%s", a);
23         if(a[0] == 'P')
24         {
25             scanf("%d%d", &x, &y);
26             q.push(node(x, y));
27             Max = max(Max, y);
28             Min = min(Min, y);
29             last = y;
30             Map[y]++;
31             s.insert(y);
32         }
33         else if(a[0] == 'Q')
34         {
35             printf("%d %d %d\n", Max, Min, last);
36         }
37         else if(a[0] == 'R')
38         {
39             scanf("%d", &x);
40             while(q.front().a <= x)
41             {
42                 y = q.front().b;
43                 q.pop();
44                 Map[y]--;
45                 if(Map[y] == 0)s.erase(s.find(y));
46             }
47             if(s.size())
48             {
49                 Min = *s.begin();
50                 set<int>::iterator it = s.end();
51                 it--;
52                 Max = *it;
53             }
54             else
55             {
56                 Max = -1, Min = 1e9 + 7;
57             }
58         }
59     }
60     return 0;
61 }

 

C:穿越禁区

给出一个矩形区域,以及多个圆的信息(圆心在矩形内),判断能否从矩形左侧到达矩形右侧,不和圆进行接触。

思路:

如果不能到达右侧,说明中间被一群圆挡住了,过不去。

此时肯定有圆和矩形底端接触,也有圆和矩形顶部接触。(如果没有,就可以沿着矩形边缘到达右侧,所以一定存在)

如果中间被一群圆档住了过不去,等价于和底部接触的圆通过中间的圆连接,可以和顶部接触的圆接触。

在输入时记录与底部接触的圆的下标和顶部接触的圆的下标。

之后对每两个圆进行连接,如果两圆相交,这两个圆就连通,用并查集加入连通分量。

最后判断是否存在一个底部的圆和顶部的某个圆在同一个连通分量内,如果存在,就说明不可以通过,否则就可以通过。

在判断圆相交的时候注意long long,这里用平方判断的,需要long long

 1 #include<bits/stdc++.h>
 2 #define lowbit(x) ((x)&(-x))
 3 using namespace std;
 4 typedef long long ll;
 5 struct node
 6 {
 7     ll x, y, r;
 8 }a[1005];
 9 ll low[1005], high[1005];
10 ll p[1005];
11 ll Find(ll x)
12 {
13     return p[x] == x ? x : p[x] = Find(p[x]);
14 }
15 bool judge(ll i, ll j)
16 {
17     ll t = (a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y);
18     ll c = a[i].r + a[j].r;
19     return c * c >= t;
20 }
21 int main()
22 {
23     ll T, w, h, n;
24     cin >> T;
25     while(T--)
26     {
27         cin >> w >> h >> n;
28         ll tot1 = 0, tot2 = 0, flag = 0;
29         for(int i = 0; i <= n; i++)p[i] = i;
30         for(int i = 0; i < n; i++)
31         {
32             scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].r);
33             if(a[i].y <= a[i].r)
34             {
35                 low[tot1++] = i;
36             }
37             if(h - a[i].y <= a[i].r)
38             {
39                 high[tot2++] = i;
40             }
41             if(tot1 && tot2 && low[tot1 - 1] == high[tot2 - 1])
42             {
43                 flag = 1;
44             }
45         }
46         if(!flag)
47         {
48             for(int i = 0; i < n; i++)
49             {
50                 for(int j = i + 1; j < n; j++)
51                 {
52                     if(judge(i, j))
53                     {
54                         //cout<<i<<" "<<j<<endl;
55                         ll x = Find(i), y = Find(j);
56                         p[x] = y;
57                     }
58                 }
59             }
60             for(int i = 0; i < tot1; i++)
61             {
62                 for(int j = 0; j < tot2; j++)
63                 {
64                     if(Find(low[i]) == Find(high[j]))
65                     {
66                         flag = 1;
67                         break;
68                     }
69                 }
70                 if(flag)break;
71             }
72         }
73         if(flag)cout<<"NO"<<endl;
74         else cout<<"YES"<<endl;
75     }
76     return 0;
77 }

 

posted @ 2018-05-26 16:31  _努力努力再努力x  阅读(216)  评论(0编辑  收藏  举报