-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArjitandApex.c
1253 lines (1112 loc) · 30.3 KB
/
ArjitandApex.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* All Tracks --> Algorithms --> Greedy Algorithms --> Basics of Greedy Algorithms --> Problem --> Arjit and Apex
Tag(s): Data Structures, Easy-Medium, Greedy algorithm
Arjit, Protector of The Realm, has an important task at hand. M new subjects are soon going to become a part of The Realm.
The Realm, though, already has N subjects who are well skilled in the ways of The Realm. He wants the new subjects too to become perfect in the ways.
As any Protector would, Arjit also decides that the existing subjects should train the new subjects in the ways.
What's so special about this situation though? Each current subject of The Realm has a special skill Sc and he has reached a level of proficiency Pc at this.
Every new subject of The Realm feels his favorite skill he would like to have is ** Sn** and reach a proficiency ** Pn** at it.
Let's call a current subject Foo and a new subject Bar. Foo can train Bar if Foo's special skill is the same one that Bar feels is his favorite.
This is what we call a good pair. If Foo's special skill and Bar's favorite skill are the same and also Foo's proficiency is the same that Bar wants to achieve,
we call it a great combination.
Let A be the number of good pairs and B be the number of great combinations. Arjit would like to maximize the number of new subjects to be trained by current subjects,
i.e., number of good pairs. And, after maximizing number the number of good pairs, he wishes to maximize the number of great combinations.
Arjit wishes to have at least G good pairs and at least H great combinations.
If only the good pairs criteria is met, print "Good" (without quotes).
If both criterion are met, print "Great" (without quotes).
Else, print ":(" (without quotes).
Input:
The first line of input contains T. T test cases follow.
The first line of each test case has two integers, M and N.
In the following M lines, there are two integers on each line, U and V denoting favorite skill and proficiency they wish to obtain.
In the N lines that follow, there are two integers on each line, W and X denoting the special skill and the level of proficiency that has been reached.
The last line of each test case has two integers, G and H.
Output:
Print "Good", "Great" or ":(" (without quotes) on a new line for each test case.
Constraints:
1<=T<=10
1<=M,N<=10^5
1<=U,V,W,X<=10^4
1<=G,H<=10^5
See the example to understand better.
SAMPLE INPUT
1
2 2
1 1
2 3
1 1
2 3
2 2
SAMPLE OUTPUT
Great
Explanation
You have 2 new subjects and 2 current members of the realm.
Each new member is perfectly matched with an existing member of the realm.
So, good pair count is 2 and great combination count is also 2.
So, the output is "Great" (without quotes).
//My Python Solution:
t = int(input())
for _ in range(t):
m,n = map(int,input().split())
d = dict()
new = dict()
for i in range(m):
p,q = map(int,input().split())
d[i] = (p,q)
for i in range(n):
p,q = map(int,input().split())
new[i] = (p,q)
g,h = map(int,input().split())
g_c = 0
h_c=0
i = 0
j = 0
while i<len(d):
while j<len(new):
if new[j][0]==d[i][0]:
g_c+=1
if new[j][1]==d[i][1]:
h_c+=1
i+=1
j+=1
break
if h_c == h:
print("Great")
elif g_c == g:
print("Good")
else:
print(":(")
*/
/*Editorial:
Brief Description:
Given 2 integers N,M.
In the following M lines, there are two integers on each line, U and V denoting
favorite skill and proficiency they wish to obtain.
In the N lines that follow, there are two integers on each line, W and X denoting
the special skill and the level of proficiency that has been reached.
The last line of each test case has two integers, G and H.
Print "Good", "Great" or ":(" (without quotes) on a new line for each test case.
Arjit wishes to have at least G good pairs and at least H great combinations.
If only the good pairs criteria is met, print "Good" (without quotes).
If both criterion are met, print "Great" (without quotes).
Else, print ":(" (without quotes).
Pre-requisites: Implementation Skills, Data Structure Knowledge (Set).
Difficulty Level: Easy - Medium.
Hints: Use Suitable Data Structure (Set) to store Old Subjects Values,
then check for every new subject if there is a corresponding old subject
in the set.
Detailed Editorial:
This Question depends on your Implementation Skills and Data Strcuture Knowledge.
Firstly, you need to check this condition: "after maximizing number the number
of good pairs, he wishes to maximize the number of great combinations."
Actually this condition is tricky because every great combination
is considered as good pair, so the condition is interested only in
"maximize the number of great combinations" only.
So how to maximize the number of great combinations ??
You should first check for great combination, so one good way is
to use suitable data structure (set) to store old (current) subjects,
then loop over the new subject and check for every one if there is
a corresponding old subject with the same pair ( skill,proficiency ).
If there is a one, increase the number of great combinations, number of good pairs
because every great combination is considered good pair.
Note: The reverse isn't right, "Good pair isn't necessary a great combination".
Then mark each used old subject as used.
Now loop over again new subjects (for the remaining new subjects which don't form great combination),
Then check if there is and old subject that has the same Skill (good pair).
Note: Remember to erase all used old subjects from the storing sets.
Now do the check, and compare the number of Great Combinations, Good Pairs with G , H.
Note: The absolute Greedy Solution is Wrong, because you maybe form a good pair
and lose the opportunity to make (Great Combination and Good Pair) at the same time.
For clarity take this example:
1
1 3
1 7
1 2
1 4
1 7
1 1
Here Absolute greedy will form only a good pair between (1 7), (1,2), but
actually the solution should be (1 7), (1 7) forming 1 good pair+1 great combination.
Time Complexity: O(NlogN+ MlogM) .
Note: logN or logM, for inserting and searching in Set.
Memory Space Combination: O(N+M).
Solution in C++:
#include <bits/stdc++.h>
using namespace std;
multiset < pair < int , int > > great;
multiset < int > good;
vector < pair < int , int > > v;
bool finished [100000 + 10];
int main()
{
int t, n , m, tem1, tem2, g , h;
cin >> t;
while(t--){
good.clear(); // clear storage for every case
great.clear();
v.clear();
memset(finished, false, sizeof finished);
cin >> n >> m;
for(int i = 0; i < m; i++){ // new subjects
cin >> tem1 >> tem2;
v.push_back({tem1, tem2});
}
for(int j = 0; j < n; j++){
cin >> tem1 >> tem2;
great.insert({tem1, tem2});
good.insert(tem1);
}
cin >> g >> h;
int numGreat = 0, numGood = 0;
for(int i = 0; i < m; i++){
pair < int , int > check = v[i];
if(great.find(check) != great.end()){ // check if this pair found in the old subjects
finished[i] = true; // mark as selectedto be good pair and great combination
great.erase(great.find(check)); // erase this old subjects from th set
good.erase(good.find(v[i].first));
numGreat++;
numGood++;
}
}
for(int i = 0; i < m; i++){
if(finished[i]) continue; // we can't choose an old subject more than once
if(good.find(v[i].first) != good.end()){
numGood++;
good.erase(good.find(v[i].first));
}
}
if(numGood >= g && numGreat >= h) cout << "Great\n";
else if (numGood >= g) cout << "Good\n";
else cout << ":(\n";
}
return 0;
}
Editorialist: Omar Khaled
Author Solution by Tanmay Sahay
#include <bits/stdc++.h>
using namespace std;
void optimizeIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int maxm = 1e5 + 1;
const int maxn = 1e5 + 1;
int main()
{
optimizeIO();
int t;
cin >> t;
while (t--)
{
int m, n;
cin >> m >> n;
multiset<int> bar[maxm], foo[maxn];
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
bar[u].insert(v);
}
for (int i = 0; i < n; i++)
{
int w, x;
cin >> w >> x;
foo[w].insert(x);
}
int good = 0, great = 0;
for (int skill = 1; skill <= maxm; skill++)
{
int tmp = 0;
for (auto proficiency: bar[skill])
if (foo[skill].count(proficiency))
tmp++, foo[skill].erase(foo[skill].find(proficiency));
int t1 = bar[skill].size();
int t2 = foo[skill].size();
good += tmp, great += tmp;
t1 -= tmp;
good += min(t1, t2);
}
int g, h;
cin >> g >> h;
if (good >= g and great >= h)
cout << "Great\n";
else if (good >= g)
cout << "Good\n";
else
cout << ":(\n";
}
return 0;
}
Tester Solution by Aniruddha Sharma
// Name:- Aniruddha Sharma
// Problem:- Arjit and Apex
// Site:- HackerEarth
#include<iostream>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<functional>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<assert.h>
using namespace std;
struct realm
{
int x;
int y;
}new_subjects[100010],current_subjects[100010];
bool myFunc(struct realm a,struct realm b)
{
if(a.x<b.x)
return true;
else if(a.x==b.x && a.y<b.y)
return true;
else
return false;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int m,n,g,h;
cin>>m>>n;
assert(m>=1 && m<=100000);
assert(n>=1 && n<=100000);
for(int i=0;i<m;i++)
{
cin>>new_subjects[i].x>>new_subjects[i].y;
assert(new_subjects[i].x>=1 && new_subjects[i].x<=10000);
assert(new_subjects[i].y>=1 && new_subjects[i].y<=10000);
}
for(int i=0;i<n;i++)
{
cin>>current_subjects[i].x>>current_subjects[i].y;
assert(current_subjects[i].x>=1 && current_subjects[i].x<=10000);
assert(current_subjects[i].y>=1 && current_subjects[i].y<=10000);
}
cin>>g>>h;
assert(g>=1 && g<=100000);
assert(h>=1 && h<=100000);
sort(new_subjects,new_subjects+m,myFunc);
sort(current_subjects,current_subjects+n,myFunc);
int i,j,good=0,great=0;
for(i=0,j=0;i<m &&j<n;)
{
if(new_subjects[i].x==current_subjects[j].x)
{
if(new_subjects[i].x==current_subjects[j].x && new_subjects[i].y==current_subjects[j].y)
great++;
if(new_subjects[i].x==current_subjects[j].x)
good++;
j++;
}
else if(new_subjects[i].x>current_subjects[j].x)
{
while(new_subjects[i].x>current_subjects[j].x)
j++;
}
i++;
}
if(g==good && h==great)
cout<<"Great"<<endl;
else if(g==good)
cout<<"Good"<<endl;
else
cout<<":("<<endl;
}
return(0);
}
*/
/* Best Submissions:
C:
#include<stdio.h>
int main()
{
int t,n,m,u,v,w,x,g,h,i,j,k,p[2][10004],s[2][10004];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d%d",&u,&v);
p[0][i]=u;
p[1][i]=v;
}
for(i=0;i<n;i++)
{
scanf("%d%d",&u,&v);
s[0][i]=u;
s[1][i]=v;
}
scanf("%d %d",&g,&h);
w=x=0;
n=n<m?n:m;
for(i=0;i<n;i++)
{
if(p[0][i]==s[0][i])
w++;
if(p[1][i]==s[1][i])
x++;
}
if(w==g && x==h)
printf("Great\n");
else if(w==g || x==h)
printf("Good\n");
else
printf(":(\n");
}
return 0;
}
C++:
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int T;
scanf("%d",&T);
while(T--){
int M,N;
scanf("%d%d",&M,&N);
if(M>N)
printf(":(\n");
else{
vector<int> a,b,c,d;
int num1,num2;
for(int i=0;i<M;i++){
scanf("%d%d",&num1,&num2);
a.push_back(num1);
b.push_back(num2);
}
for(int j=0;j<N;j++){
scanf("%d%d",&num1,&num2);
c.push_back(num1);
d.push_back(num2);
}
int G,H;
scanf("%d%d",&G,&H);
for(int p=0;p<M;p++){
if(binary_search(c.begin(),c.end(),a[p])){G--;
//a.erase(a.begin()+p);
}
}
for(int q=0;q<M;q++){
if(binary_search(d.begin(),d.end(),b[q])){H--;
//b.erase(b.begin()+q);
}
}
if(G<=0 && H<=0)
printf("Great\n");
else if(G<=0)
printf("Good\n");
else
printf(":(\n");
}
}
return 0;
}
C++14:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int s;
int p;
bool operator<(const struct node & n) const
{
if(this->s == n.s) return this->p < n.p;
return this->s < n.s;
}
};
int main()
{
int T;
cin >> T;
while(T--)
{
int M, N, i;
int flag1, flag2;
flag1 = 1;
flag2 = 1;
cin >> M >> N;
map <int, int> mymap1;
map <struct node, int> mymap2;
struct node temp1;
for(i = 0; i < M; i++)
{
cin >> temp1.s >> temp1.p;
mymap1[temp1.s]++;
mymap2[temp1]++;
}
for(i = 0; i < N; i++)
{
cin >> temp1.s >> temp1.p;
if(!flag1) continue;
if(!mymap1[temp1.s])
{
flag1 = 0;
continue;
}
mymap1[temp1.s]--;
if(!flag2) continue;
if(!mymap2[temp1])
{
flag2 = 0;
continue;
}
mymap2[temp1]--;
}
if(!flag1) cout << ":(" << endl;
else if(!flag2) cout << "Good" << endl;
else cout << "Great" << endl;
}
return 0;
}
C#:
using System;
using System.Numerics;
using System.Collections.Generic;
using System.Linq;
class MyClass
{
public class Subject
{
public int Prof { get; set; }
public string Status { get; set; }
}
static void Main(string[] args)
{
var line1 = System.Console.ReadLine().Trim();
var testCases = Int32.Parse(line1);
for (var i = 0; i < testCases; i++)
{
var subjects = System.Console.ReadLine().Trim().Split(' ');
var newSubjects = Int32.Parse(subjects[0]);
var existingSubjects = Int32.Parse(subjects[1]);
var desired = new List<KeyValuePair<int, int>>();
var existing = new Dictionary<int,List<Subject>>();
for (int j = 0; j < newSubjects; j++)
{
var skillInput = System.Console.ReadLine().Trim().Split(' ');
var skill = Int32.Parse(skillInput[0]);
var proficiency = Int32.Parse(skillInput[1]);
desired.Add(new KeyValuePair<int, int>(skill, proficiency));
}
for (int j = 0; j < existingSubjects; j++)
{
var skillInput = System.Console.ReadLine().Trim().Split(' ');
var skill = Int32.Parse(skillInput[0]);
var proficiency = Int32.Parse(skillInput[1]);
if (existing.ContainsKey(skill))
existing[skill].Add(new Subject() { Prof = proficiency,Status = "None" });
else existing.Add(skill, new List<Subject> { new Subject() { Prof = proficiency, Status = "None" } });
}
var expectedResult = System.Console.ReadLine().Trim().Split(' ');
var goodPair = Int32.Parse(expectedResult[0]);
var greatCombination = Int32.Parse(expectedResult[1]);
var goodCount = 0;
var greatCount = 0;
foreach (var des in desired.OrderByDescending(x => x.Value))
{
var skill = des.Key;
var prof = des.Value;
var subjectsList = existing.ContainsKey(skill) ? existing[skill] : null;
if (subjectsList != null)
{
var great = subjectsList.FirstOrDefault(x=>x.Prof == prof && x.Status != "Great");
if (great != null)
{
greatCount++;
if (great.Status == "Good")
{
var replace = subjectsList.FirstOrDefault(x => x.Status == "None");
if (replace != null)
replace.Status = "Good";
else goodCount--;
}
great.Status = "Great";
}
else
{
var replace = subjectsList.FirstOrDefault(x => x.Status == "None");
if (replace != null)
{
goodCount++;
replace.Status = "Good";
}
}
}
}
if (greatCombination == greatCount)
Console.WriteLine("Great");
else if (goodPair == goodCount)
Console.WriteLine("Good");
else
Console.WriteLine(":(");
}
}
}
Java:
import java.io.*;
import java.util.*;
class ArjitAndApex
{
public static void main(String args[] ) throws Exception
{
int T = Fio.fin.readInt();
while(T-->0)
{
int M = Fio.fin.readInt();
int N = Fio.fin.readInt();
HashMap<Long,LinkedList<Long>> hm = new HashMap(M);
while(M-->0)
{
long U = Fio.fin.readLong();
long V = Fio.fin.readLong();
//Fio.fout.println(U+" "+V);
if(hm.containsKey(U))
{
hm.get(U).add(V);
}
else
{
LinkedList l = new LinkedList<Long>();
l.add(V);
hm.put(U,l);
}
}
int good=0;
int great=0;
while(N-->0)
{
long W = Fio.fin.readLong();
long X = Fio.fin.readLong();
if(hm.containsKey(W))
{
//good++;
if(hm.get(W).contains(X))
great++;
}
}
int Good = Fio.fin.readInt();
int Great = Fio.fin.readInt();
Fio.fout.println(":(");
//
//if(great>=Great)
//{
// Fio.fout.println("Great");
//}
//else if(good>=Good)
//{
// Fio.fout.println("Good");
//}
//else
//{
// Fio.fout.println(":(");
//}
//
}
Fio.fout.close();
}
}
class Fio
{
public static FastInput fin = new FastInput();
public static FastOutput fout= new FastOutput(true);
public static class FastInput
{
private final InputStream in;
private final char[] brkChar;
private byte[] buf;
private int curBufCharPos;
private int bufSize;
public FastInput(InputStream in,char[] brkChar)
{
this.in=in;
buf = new byte[1024];
curBufCharPos =0;
bufSize =0;
this.brkChar=brkChar;
}
public FastInput(InputStream in)
{
this(in,null);
}
public FastInput()
{
this(System.in,null);
}
public int read()
{
if(bufSize==-1)
{
throw new InputMismatchException();
}
if(curBufCharPos>=bufSize)
{
curBufCharPos=0;
try
{
bufSize=in.read(buf);
}
catch (IOException x)
{
throw new InputMismatchException();
}
if(bufSize<=0)
{
return -1;
}
}
return buf[curBufCharPos++];
}
public int readInt()
{
int c = read();
while (isBreakChar(c))
c = read();
int sign=1;
if(c=='-')
{
sign=-1;
c=read();
}
int res=0;
do
{
if(c<'0' || c>'9')
{
throw new InputMismatchException();
}
res=res*10+(c-'0');
c=read();
}
while(!isBreakChar(c));
return sign*res;
}
public long readLong()
{
int c = read();
while (isBreakChar(c))
c = read();
int sign=1;
if(c=='-')
{
sign=-1;
c=read();
}
long res=0;
do
{
if(c<'0' || c>'9')
{
throw new InputMismatchException();
}
res=res*10+(c-'0');
c=read();
}
while(!isBreakChar(c));
return sign*res;
}
public float readFloat()
{
int c=read();
while(isBreakChar(c))
c=read();
int sign=1;
if(c=='-')
{
sign=-1;
c=read();
}
float res=0;
while(!isBreakChar(c) && c!='.')
{
if(c=='e' || c=='E')
return (float) (res*Math.pow(10, readInt()));
if(c<'0' || c>'9')
throw new InputMismatchException();
res = res*10 + (c-'0');
c=read();
//System.out.println("RES 1 : "+res);
}
if(c=='.')
{
c=read();
float fac=10;
do
{
if(c=='e' || c=='E')
return (float) (res*Math.pow(10, readInt()));
if(c<'0' || c>'9')
throw new InputMismatchException();
//System.out.println("c 2 : "+ res +" "+ (c-'0')/fac + " "+ (res+(c-'0')/fac));
res+= (c-'0')/fac;
fac*=10;
c=read();
//System.out.println("RES 2 : "+res);
}
while(!isBreakChar(c));
}
return res*sign;
}
public double readDouble()
{
int c=read();
while(isBreakChar(c))
c=read();
int sign=1;
if(c=='-')
{
sign=-1;
c=read();
}
double res=0;
while(!isBreakChar(c) && c!='.')
{
if(c=='e' || c=='E')
return res*Math.pow(10, readInt());
if(c<'0' || c>'9')
throw new InputMismatchException();
res = res*10 + (c-'0');
c=read();
//System.out.println("RES 1 : "+res);
}
if(c=='.')
{
c=read();
//int count=0;
double fac=10;
do
{
if(c=='e' || c=='E')
return res*Math.pow(10, readInt());
if(c<'0' || c>'9')
throw new InputMismatchException();
//res= res*10+(c-'0');
//count++;
res+=(c-'0')/fac;
fac*=10;
c=read();
}
while(!isBreakChar(c));
//res/=Math.pow(10, count);
}
return res*sign;
}
public char readChar()
{
int c = read();
if(c!='\n' && read()=='\n')
return (char)c;
throw new InputMismatchException();
}
public String readString()
{
int c = read();
while(isBreakChar(c))
{
c=read();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c=read();
}
while (!isBreakChar(c));
return res.toString();
}
public String readStringLine()
{
int c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c=read();
}
while (c!='\n');
return res.toString();
}
public String next()
{
return readString();
}
public boolean hasNextInLine()
{
//System.out.println(buf[curBufCharPos]);
if(buf[curBufCharPos-1]!='\n')
return true;
return false;
}
public boolean hasConsequtiveNewLines()
{
//System.out.println(buf[curBufCharPos]);
if(buf[curBufCharPos-1]=='\n' && buf[curBufCharPos-2]=='\n')
return true;
return false;
}
public boolean hasNext()
{
int c = read();
setIteratorToPrev();
return !(c==-1 || isBreakChar(c));
}
private void setIteratorToPrev()
{
curBufCharPos--;
}
private boolean isBreakChar(int c)
{
if(brkChar==null)
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
else
{
for(char ch : brkChar)
{
if(c==ch)
return true;
}
}
return false;
}
}
public static class FastOutput
{
private final PrintWriter writer;
private final boolean autoFlush;
public FastOutput(OutputStream outputStream,boolean autoFlush)