-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOJ3614.txt
More file actions
293 lines (233 loc) · 5.34 KB
/
POJ3614.txt
File metadata and controls
293 lines (233 loc) · 5.34 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
// Please name your class Main
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int read() throws IOException {
in.nextToken();
return (int) in.nval;
}
static String readString() throws IOException {
in.nextToken();
return in.sval;
}
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
//InputReader in = new InputReader(System.in);
//int T =in.nextInt();
PrintWriter out = new PrintWriter(System.out);
//for(int t=0;t<T;t++){
int C,L;
/*while((N = read()) != 0){
M=read();
if(N==0&&M==0)break;
List<Integer>adjecent[]=new ArrayList[N];
for(int i=0;i<N;i++){
//if(adjecent[i]!=null)adjecent[i].celar();
adjecent[i]=new ArrayList<Integer>();
}
for(int i=0;i<M;i++){
int v1=read();int v2=read();
adjecent[v1].add(v2);
adjecent[v2].add(v1);
}
Solution s=new Solution();
s.solution(adjecent,out);
}*/
/*int A[][]=new int[4][4];
for(int i=0;i<4;i++){
String s=in.next();
for(int j=0;j<s.length();j++){
if(s.charAt(j)=='b'){
A[i][j]=0;
}else{
A[i][j]=1;
}
}
}*/
C=in.nextInt();
L=in.nextInt();
int cows[][]=new int[C][2];
int lotion[][]=new int[L][2];
for(int i=0;i<C;i++){
cows[i][0]=in.nextInt();
cows[i][1]=in.nextInt();
}
for(int i=0;i<L;i++){
lotion[i][0]=in.nextInt();
lotion[i][1]=in.nextInt();
}
Solution s=new Solution();
s.solution(cows,lotion);
out.flush();
}
}
class Solution{
//constant variable
final int MAX=Integer.MAX_VALUE;
final int MIN=Integer.MIN_VALUE;
List<Integer>adjecent[];
//////////////////////////////
public void solution(int cows[][],int lotion[][]){
int res=0;
java.util.Arrays.sort(cows, new java.util.Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if(a[0]==b[0])return a[1]-b[1];
return a[0]-b[0];
}
});
java.util.Arrays.sort(lotion, new java.util.Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0]-b[0];
}
});
PriorityQueue<Integer>pq=new PriorityQueue<Integer>();
int cowI=0;
for(int i=0;i<lotion.length;i++){
int spf=lotion[i][0];
int cnt=lotion[i][1];
while(cowI<cows.length&&cows[cowI][0]<=spf){
pq.add(cows[cowI][1]);
cowI++;
}
while(cnt>0&&pq.size()>0){
if(pq.size()==0)break;
if(pq.peek()<spf){
pq.poll();
continue;
}
res++;
pq.poll();
cnt--;
}
}
//lotion:给更多的牛
//PriorityQueue<Wrapper>pq=new PriorityQueue<Wrapper>();
/*for(int i=0;i<lotion.length;i++){
pq.add(new Wrapper(lotion[i][0],lotion[i][1]));
}
for(int i=0;i<cows.length;i++){
int low=cows[i][0];
int high=cows[i][1];
while(pq.size()>0&&pq.peek().spf<low)pq.poll();
if(pq.peek().spf<=high){
res++;
pq.peek().cnt--;
if(pq.peek().cnt==0)pq.poll();
}
}*/
msg(res+"");
}
/*public void tarjan(int p,int r){
if(cut)return;
List<Integer>childs=adjecent[r];
dis[r]=low[r]=time;
time++;
//core for tarjan
int son=0;
for(int c:childs){
if(ban==c||c==p)continue;
if(dis[c]==-1){
son++;
tarjan(r,c);
low[r]=Math.min(low[r],low[c]);
if((r==root&&son>1)||(low[c]>=dis[r]&&r!=root)){
cut=true;
return;
}
}else{
if(c!=p){
low[r]=Math.min(low[r],dis[c]);
}
}
}
}*/
//helper function I would use
public int flip(int i){
if(i==0)return 1;
else return 0;
}
public boolean[] primes(int n){
boolean A[]=new boolean[n+1];
for(int i=2;i<=n;i++){
if(A[i]==false){
for(int j=i+i;j<=n;j+=i){
A[j]=true;
}
}
}
return A;
}
public void msg(String s){
System.out.println(s);
}
public int[] kmpPre(String p){
int pre[]=new int[p.length()];
int l=0,r=1;
while(r<p.length()){
if(p.charAt(l)==p.charAt(r)){
pre[r]=l+1;
l++;r++;
}else{
if(l==0)r++;
else l=pre[l-1];
}
}
return pre;
}
public boolean isP(String s){
int l=0,r=s.length()-1;
while(l<r){
if(s.charAt(l)!=s.charAt(r))return false;
l++;r--;
}
return true;
}
public int find(int nums[],int x){//union find => find method
if(nums[x]==x)return x;
int root=find(nums,nums[x]);
nums[x]=root;
return root;
}
public boolean check(int grid[][],int r,int c){
if(r<0||c<0||r>=grid.length||c>=grid[0].length)return false;
return true;
}
public int get(int A[],int i){
if(i<0||i>=A.length)return -1;
return A[i];
}
public int[] copy(int A[]){
int a[]=new int[A.length];
for(int i=0;i<a.length;i++)a[i]=A[i];
return a;
}
public void print1(long A[]){
for(long i:A)System.out.print(i+" ");
System.out.println();
}
public void print2(long A[][]){
for(int i=0;i<A.length;i++){
for(int j=0;j<A[0].length;j++){
System.out.print(A[i][j]+" ");
}System.out.println();
}
}
public int min(int a,int b){
return Math.min(a,b);
}
}
class Wrapper implements Comparable<Wrapper>{
int spf;int cnt;
public Wrapper(int spf,int cnt){
this.spf=spf;
this.cnt=cnt;
}
@Override
public int compareTo(Wrapper other) {
return this.spf-other.spf;
}
}