Skip to content

Commit d4c7527

Browse files
committedFeb 26, 2017
New solutions
1 parent 6cf3dc8 commit d4c7527

File tree

5 files changed

+326
-0
lines changed

5 files changed

+326
-0
lines changed
 

‎codeforces/779A.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
int i,j,k,n;
8+
int ma[6];
9+
int mb[6];
10+
bool bad = false;
11+
int answer = 0;
12+
13+
int main() {
14+
int x;
15+
cin >> n;
16+
for (i = 0; i < n; i++) {
17+
cin >> x;
18+
ma[x]++;
19+
}
20+
21+
for (i = 0; i < n; i++) {
22+
cin >> x;
23+
mb[x]++;
24+
}
25+
int needa = 0;
26+
int needb = 0;
27+
for (i = 1; (i<=5) && (!bad); i++) {
28+
if (ma[i] != mb[i]) {
29+
if (abs(ma[i] - mb[i]) % 2 != 0) {
30+
bad = true;
31+
} else if (ma[i] > mb[i]) {
32+
needb += (ma[i] - mb[i]) / 2;
33+
} else {
34+
needa += (mb[i] - ma[i]) / 2;
35+
}
36+
}
37+
}
38+
39+
if (bad || (needa != needb)) {
40+
cout << -1;
41+
} else {
42+
cout << needa;
43+
}
44+
return 0;
45+
}

‎codeforces/779B.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <string>
5+
6+
using namespace std;
7+
8+
int i,j,k,n, answer;
9+
string s;
10+
int main() {
11+
cin >> s;
12+
cin >> k;
13+
answer = 0;
14+
if (s == "0") {
15+
answer = 0;
16+
} else if (s.length() < k) {
17+
answer = s.length() - 1;
18+
} else {
19+
for (i = s.length() - 1 ; (i>=0) && (k > 0); i--) {
20+
if (s[i] != '0') {
21+
answer++;
22+
} else {
23+
k--;
24+
}
25+
}
26+
27+
if (k > 0) {
28+
answer = s.length() - 1;
29+
}
30+
}
31+
cout << answer;
32+
return 0;
33+
}

‎codeforces/779C.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
struct good
8+
{
9+
int before;
10+
int after;
11+
};
12+
13+
bool comparator(const good& left, const good& right)
14+
{
15+
int a = left.before - left.after;
16+
int b = right.before - right.after;
17+
return a < b;
18+
}
19+
20+
int i,j,k,n;
21+
good a[200001];
22+
23+
int main() {
24+
cin >> n >> k;
25+
for (i = 0; i < n; i++) {
26+
cin >> a[i].before;
27+
}
28+
for (i = 0; i < n; i++) {
29+
cin >> a[i].after;
30+
}
31+
vector<good> goods (a, a+n);
32+
sort(goods.begin(), goods.end(), comparator);
33+
unsigned long long int answer = 0;
34+
35+
for (i = 0; i<n; ) {
36+
if (k > 0) {
37+
answer += goods[i].before;
38+
k--;
39+
} else {
40+
if (goods[i].before <= goods[i].after) {
41+
answer += goods[i].before;
42+
} else {
43+
break;
44+
}
45+
}
46+
i++;
47+
}
48+
for (; i<n; i++) {
49+
answer += goods[i].after;
50+
}
51+
cout << answer;
52+
53+
return 0;
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Ivan Samsonov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.interviewbit;
25+
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
29+
/**
30+
* An ${Name} class.
31+
*
32+
* <p>
33+
* Description
34+
* </p>
35+
*
36+
* @author Ivan Samsonov (hronya@gmail.com)
37+
* @since 1.0.0
38+
*
39+
*/
40+
41+
public class MergeIntervals {
42+
private class Interval {
43+
int start;
44+
int end;
45+
Interval() { start = 0; end = 0; }
46+
Interval(int s, int e) { start = s; end = e; }
47+
48+
@Override
49+
public String toString() {
50+
return "[" + start + "," + end + "]";
51+
}
52+
}
53+
54+
private int binarySearch(int[] array, int value) {
55+
if (array.length == 0) {
56+
return 0;
57+
}
58+
59+
int from = 0;
60+
int to = array.length-1;
61+
int mid = 0;
62+
63+
while (from <= to) {
64+
mid = (to+from) >>> 1;
65+
if (array[mid] < value) {
66+
from = mid + 1;
67+
} else if (array[mid] > value) {
68+
to = mid - 1;
69+
} else {
70+
return mid;
71+
}
72+
}
73+
74+
if (array[mid] > value) {
75+
return mid-1;
76+
} else {
77+
return mid;
78+
}
79+
}
80+
81+
private boolean overlaps(Interval i1, Interval i2) {
82+
return !((i1.start > i2.end) || (i1.end < i2.start));
83+
}
84+
85+
private Interval merge(Interval i1, Interval i2) {
86+
return new Interval(Math.min(i1.start, i2.start), Math.max(i1.end, i2.end));
87+
}
88+
89+
public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
90+
ArrayList<Interval> result = new ArrayList<>();
91+
int size = intervals.size();
92+
int[] starts = new int[size];
93+
for (int i = 0; i < size; i++) {
94+
starts[i] = intervals.get(i).start;
95+
}
96+
97+
int start_index = binarySearch(starts, newInterval.start);
98+
99+
if (start_index >= size) {
100+
start_index = size - 1;
101+
}
102+
103+
if (start_index < 0) {
104+
start_index = 0;
105+
}
106+
107+
for (int i = 0; i < start_index; i++) {
108+
result.add(intervals.get(i));
109+
}
110+
111+
Interval currentInterval = new Interval(newInterval.start, newInterval.end);
112+
int i;
113+
for (i = start_index; i < size; ++i) {
114+
if (overlaps(currentInterval, intervals.get(i))) {
115+
currentInterval = merge(currentInterval, intervals.get(i));
116+
} else if (intervals.get(i).start < currentInterval.start) {
117+
result.add(intervals.get(i));
118+
} else {
119+
break;
120+
}
121+
}
122+
123+
if (i < size && intervals.get(i).start < currentInterval.start) {
124+
result.add(intervals.get(i));
125+
i++;
126+
}
127+
128+
result.add(currentInterval);
129+
130+
for (; i < size; ++i) {
131+
result.add(intervals.get(i));
132+
}
133+
134+
return result;
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Ivan Samsonov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.interviewbit;
25+
26+
import java.util.ArrayList;
27+
28+
/**
29+
* An ${Name} class.
30+
*
31+
* <p>
32+
* Description
33+
* </p>
34+
*
35+
* @author Ivan Samsonov (hronya@gmail.com)
36+
* @since 1.0.0
37+
*
38+
*/
39+
public class PascalTriangleRows {
40+
public ArrayList<ArrayList<Integer>> generate(int a) {
41+
ArrayList<ArrayList<Integer>> result = new ArrayList();
42+
if (a > 0) {
43+
ArrayList<Integer> r = new ArrayList<>();
44+
r.add(1);
45+
result.add(r);
46+
for (int i = 1; i < a; i++) {
47+
ArrayList<Integer> row = new ArrayList<>();
48+
row.add(1);
49+
for (int j = 1; j < i; j++) {
50+
row.add(result.get(i-1).get(j-1) + result.get(i-1).get(j));
51+
}
52+
row.add(1);
53+
result.add(row);
54+
}
55+
}
56+
return result;
57+
}
58+
}

0 commit comments

Comments
 (0)
Please sign in to comment.