Sir_Kay

Kaysman Official Website


  • 首页

  • 分类

  • 标签

  • 归档

  • 搜索

Codeforces 609F Frogs and mosquitoes 题解

发表于 2019-09-21 更新于 2019-09-22 分类于 算法 , Codeforces
本文字数: 7.7k 阅读时长 ≈ 7 分钟

Codeforces 609F Frogs and mosquitoes 题解

题意

有$n (1\le n\le 2\cdot10^5)$个青蛙固定在平面直角坐标系中$Ox$的非负半轴上,对于第$i (1\le i\le n)$个青蛙有两个值,分别是$x_i$和$t_i (0\le x_i,t_i\le 10^9)$($x_i$两两不同),$x_i$代表它的位置,$t_i$代表它的舌头的长度。

有$m (1\le m\le 2\cdot10^5)$只蚊子也固定在$Ox$的非负半轴上,对于第$i (1\le i\le m)$只蚊子有两个值,分别是$p_i$和$b_i (0\le p_i,b_i\le 10^9)$,$p_i$代表它的位置,$b_i$代表它的大小。

如果一个青蛙$i$和一只蚊子$j$,满足 $p_j$在区间$[x_i,x_i+t_i]$内,那么青蛙$i$就可以吃掉蚊子$j$,并且舌头会增长$b_j$。如果有多个青蛙可以吃掉同一个蚊子,那么这个蚊子会被$x_i$最小的那个青蛙吃掉。

蚊子是按照输入的顺序降临的,如果第$i$个蚊子要降临,必须满足 青蛙吃光了可能吃掉的所有蚊子$j (1\le j<i)$。

问第$i$只青蛙吃掉了多少蚊子和最后它的舌头有多长。

题解

对于青蛙$i$和$j$,如果$[x_i,x_i+t_i]$包含在$[x_j,x_j+t_j]$中,那么青蛙$i$就永远没有用了。

因为处于区间$[x_i,x_i+t_i]$内的蚊子总会被青蛙$j$吃掉($x_j<x_i$)。

那么大体思路就出来了:

  1. 读入青蛙,删除无用的青蛙

  2. 依次读入蚊子,每次读入蚊子$i$做出以下操作:

    1. 如果蚊子$i$不能被如何青蛙吃掉,那么就丢入一个数据结构中,continue;否则:
    2. 选用处于最左边的青蛙$j$吃掉蚊子$i$。因为此是青蛙$j$的舌头变长了,所以再删除一遍无用的青蛙 并且 查找青蛙$j$可不可以再吃掉蚊子了。最后更新青蛙$j$的数据。
  3. 输出答案

完成这个过程可以用三个set完成,分别维护青蛙的$x_i$,$(x_i+t_i)$和目前没有被吃掉的蚊子,有关查找的操作可以用二分完成,具体见程序。时间复杂度$O(n\log{n}+m\log{m})$。

也可以用线段树完成。

程序

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")

// #include <bits/stdc++.h>
#include <map>
#include <set>
#include <list>
#include <array>
#include <cfenv>
#include <cmath>
#include <ctime>
#include <deque>
#include <mutex>
#include <queue>
#include <ratio>
#include <regex>
#include <stack>
#include <tuple>
#include <atomic>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <chrono>
#include <cstdio>
#include <cwchar>
#include <future>
#include <limits>
#include <locale>
#include <memory>
#include <random>
#include <string>
#include <thread>
#include <vector>
#include <cassert>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <cwctype>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <ccomplex>
#include <cstdbool>
#include <iostream>
#include <typeinfo>
#include <valarray>
#include <algorithm>
#include <cinttypes>
#include <cstdalign>
#include <stdexcept>
#include <typeindex>
#include <functional>
#include <forward_list>
#include <system_error>
#include <unordered_map>
#include <unordered_set>
#include <scoped_allocator>
#include <condition_variable>
// #include <conio.h>
// #include <windows.h>
using namespace std;

typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef double ld;
typedef long double LD;
typedef pair<int,int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || (WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((double)(n))
#define LD(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed<<setprecision(n)

const int INF=1061109567;
const int NINF=-1044266559;
const LL LINF=4557430888798830399;
const ld eps=1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)

/*
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffffU
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807ll
#define LLONG_MIN (-9223372036854775807ll - 1)
#define ULLONG_MAX 0xffffffffffffffffull
*/

#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(),(a).end()
#define pall(a) (a).rbegin(),(a).rend()
#define Log(x,y) log(x)/log(y)
#define SZ(a) ((int)(a).size())
#define rep(i,n) for(int i=0;i<((int)(n));i++)
#define rep1(i,n) for(int i=1;i<=((int)(n));i++)
#define repa(i,a,n) for(int i=((int)(a));i<((int)(n));i++)
#define repa1(i,a,n) for(int i=((int)(a));i<=((int)(n));i++)
#define repd(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define repd1(i,n) for(int i=((int)(n));i>=1;i--)
#define repda(i,n,a) for(int i=((int)(n));i>((int)(a));i--)
#define repda1(i,n,a) for(int i=((int)(n));i>=((int)(a));i--)
#define FOR(i,a,n,step) for(int i=((int)(a));i<((int)(n));i+=((int)(step)))
#define repv(itr,v) for(__typeof((v).begin()) itr=(v).begin();itr!=(v).end();itr++)
#define repV(i,v) for(auto i:v)
#define repE(i,v) for(auto &i:v)
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x) MS(x,0)
#define MINF(x) MS(x,63)
#define MCP(x,y) memcpy(x,y,sizeof(y))
#define sqr(x) ((x)*(x))
#define UN(v) sort(All(v)),v.erase(unique(All(v)),v.end())
#define filein(x) freopen(x,"r",stdin)
#define fileout(x) freopen(x,"w",stdout)
#define fileio(x)\
freopen(x".in","r",stdin);\
freopen(x".out","w",stdout)
#define filein2(filename,name) ifstream name(filename,ios::in)
#define fileout2(filename,name) ofstream name(filename,ios::out)
#define file(filename,name) fstream name(filename,ios::in|ios::out)
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define PC(x) putchar(x)
#define GC(x) x=getchar()
#define Endl PC('\n')
#define SF scanf
#define PF printf

inline int Read()
{
int X=0,w=0;char ch=0;while(!isdigit(ch)){w|=ch=='-';ch=getchar();}while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
inline void Write(int x){if(x<0)putchar('-'),x=-x;if(x>9)Write(x/10);putchar(x%10+'0');}

inline LL powmod(LL a,LL b){LL RES=1;a%=MOD;assert(b>=0);for(;b;b>>=1){if(b&1)RES=RES*a%MOD;a=a*a%MOD;}return RES%MOD;}
inline LL gcdll(LL a,LL b){return b?gcdll(b,a%b):a;}
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={1,0,-1,0,-1,-1,1,1};
/************************************************************Begin************************************************************/
const int maxn=200010;

int n,m,cnt[maxn];
LL x[maxn],t[maxn],p[maxn],b[maxn];
set<pair<LL,int> > frl,frr,mos;

inline void erase_frog(int id)
{
if(frl.find({x[id],id})==frl.end()) return;

while(1)
{
auto it=frl.lower_bound({x[id]+1,-1});
if(it==frl.end()) break;

int p=it->sc;
if(x[id]+t[id]<x[p]+t[p]) break;

frl.erase(it);
frr.erase({x[p]+t[p],p});
}
}

inline void frog_eat_mosquitoes(int id)
{
while(1)
{
auto it=mos.lower_bound({x[id]+t[id]+1,-1});
if(it==mos.begin()) break;

it--;
if(it->fs<x[id]) break;

t[id]+=b[it->sc];
cnt[id]++;

mos.erase(it);
}
}

int main()
{
SF("%d%d",&n,&m);

rep(i,n)
{
SF("%lld%lld",&x[i],&t[i]);
frl.insert({x[i],i});
frr.insert({x[i]+t[i],i});
}

rep(i,n) erase_frog(i);

rep(i,m)
{
SF("%lld%lld",&p[i],&b[i]);

auto it=frr.lower_bound({p[i],-1});
if(it==frr.end()||x[it->sc]>p[i])
{
mos.insert({p[i],i});
continue;
}

int id=it->sc;

frr.erase({x[id]+t[id],id});

t[id]+=b[i];
cnt[id]++;

frog_eat_mosquitoes(id);
frr.insert({x[id]+t[id],id});
erase_frog(id);
}

rep(i,n) PF("%d %lld\n",cnt[i],t[i]);

return 0;
}
/*************************************************************End**************************************************************/
__EOF__
二分 贪心 数据结构 线段树
Codeforces 1119D Frets On Fire 题解
Codeforces 1228F One Node is Gone 题解
  • 文章目录
  • 站点概览
Sir_Kay

Sir_Kay

Kaysman #1 Sir_Kay
33 日志
6 分类
34 标签
RSS
Main site Wikipedia GitHub GitLab
Creative Commons
  1. 1. Codeforces 609F Frogs and mosquitoes 题解
    1. 1.1. 题意
    2. 1.2. 题解
    3. 1.3. 程序
0%
© 2019 – 2020 Sir_Kay