Skip to content

Commit 4441006

Browse files
committed
hacker cup
1 parent ac316a0 commit 4441006

File tree

4 files changed

+397
-15
lines changed

4 files changed

+397
-15
lines changed

SCPC/2019/qual2/E.cpp

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <iostream>
22
#include <algorithm>
3+
#include <vector>
4+
#include <map>
35
#include <cmath>
46

57
using namespace std;
@@ -8,20 +10,66 @@ typedef long long ll;
810

911
struct Point
1012
{
11-
int idx, x, y;
13+
int x, y;
1214
} P[500002];
1315

16+
bool compx(Point& p1, Point& p2)
17+
{
18+
return p1.x < p2.x;
19+
}
20+
21+
bool compy(Point& p1, Point& p2)
22+
{
23+
return p1.y < p2.y;
24+
}
25+
1426
bool comp(Point& p1, Point& p2)
1527
{
1628
return p1.y == p2.y ? p1.x > p2.x : p1.y > p2.y;
1729
}
1830

31+
int T, N, M;
32+
int data[]
33+
map<int, int> x_val, y_val;
34+
1935
ll min(ll a, ll b)
2036
{
2137
return a < b ? a : b;
2238
}
2339

24-
int T, N, M;
40+
Point query2(int pos, int y1, int y2, int start = 1, int end = N, int v = 1)
41+
{
42+
43+
}
44+
45+
Point query1(int x1, int y1, int x2, int y2, int start = 1, int end = N, int v = 1)
46+
{
47+
if(x1 <= start && x2 <= end)
48+
return query2(v, y1, y2);
49+
if(x2 < start || end < x1)
50+
return { -1, -1 };
51+
52+
int mid = (x1 + x2) / 2;
53+
54+
Point p1 = query1(x1, y1, x2, y2, start, mid, 2 * v), p2 = query1(x1, y1, x2, y2, mid + 1, end, 2 * v + 1);
55+
56+
if(p1.x == -1)
57+
return p2;
58+
else if(p2.x == -1)
59+
return p1;
60+
else
61+
{
62+
int xx1 = x_val[p1.x], yy1 = y_val[p1.y];
63+
int xx2 = x_val[p2.x], yy2 = y_val[p2.y];
64+
int dis1 = max(abs(xx1 - x1), abs(yy1 - y1)), dis2 = max(abs(xx2 - x1), abs(yy2 - y1));
65+
66+
if(dis1 < dis2)
67+
return p2;
68+
else
69+
return p1;
70+
}
71+
}
72+
2573

2674
int main()
2775
{
@@ -32,30 +80,30 @@ int main()
3280
for(int t = 1; t <= T; t++)
3381
{
3482
ll ans = 0;
83+
x_val.clear(), y_val.clear();
84+
3585
cout << "Case #" << t << '\n';
3686
cin >> M >> N;
3787

3888
for (int i = 0; i < N; i++)
39-
{
4089
cin >> P[i].x >> P[i].y;
41-
}
42-
43-
sort(P, P + N, comp);
4490

91+
sort(P, P + N, compx);
4592
for(int i = 0; i < N; i++)
4693
{
47-
ll result = min(abs(M - P[i].x), abs(M - P[i].y));
48-
49-
for(int j = 0; j < i; j++)
50-
{
51-
if(P[j].x <= P[i].x || P[j].y == P[i].y)
52-
continue;
53-
result = min(result, max(abs(P[j].x - P[i].x), abs(P[j].y - P[i].y)));
54-
}
94+
if(!x_val.find(P[i].x)) x_val[P[i].x] = i + 1;
95+
P[i].x = x_val[P[i].x];
96+
}
5597

56-
ans += result;
98+
sort(P, P + N, compy);
99+
for(int i = 0; i < N; i++)
100+
{
101+
if(!y_val.find(P[i].y)) y_val[P[i].y] = i + 1;
102+
P[i].y = y_val[P[i].y];
57103
}
58104

105+
sort(P, P + N, comp);
106+
59107
cout << ans << '\n';
60108
}
61109
}

facebook/2019/Round 1/A.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <vector>
4+
#include <climits>
5+
6+
using namespace std;
7+
8+
typedef long long ll;
9+
10+
struct C
11+
{
12+
int x, y, z;
13+
};
14+
15+
vector<C> constraints;
16+
int T, N, M;
17+
ll cache[52][52];
18+
19+
int main()
20+
{
21+
ios::sync_with_stdio(false);
22+
cin.tie(NULL);
23+
srand(time(NULL));
24+
25+
cin >> T;
26+
for(int t = 1; t <= T; t++)
27+
{
28+
cin >> N >> M;
29+
cout << "Case #" << t << ": ";
30+
constraints.clear();
31+
for(int i = 1; i <= N; i++)
32+
for(int j = 1; j <= N; j++)
33+
{
34+
cache[i][j] = (i == j ? 0 : LLONG_MAX);
35+
}
36+
37+
for(int i = 0; i < M; i++)
38+
{
39+
int x, y, z;
40+
cin >> x >> y >> z;
41+
42+
constraints.push_back({x, y, z});
43+
cache[x][y] = cache[y][x] = z;
44+
}
45+
46+
for(int k = 1; k <= N; k++)
47+
for(int i = 1; i <= N; i++)
48+
for(int j = 1; j <= N; j++)
49+
{
50+
if(cache[i][k] == LLONG_MAX || cache[k][j] == LLONG_MAX)
51+
continue;
52+
cache[i][j] = min(cache[i][j], cache[i][k] + cache[k][j]);
53+
}
54+
55+
56+
bool ans = true;
57+
for(C p : constraints)
58+
{
59+
if(cache[p.x][p.y] != p.z)
60+
{
61+
ans = false;
62+
break;
63+
}
64+
}
65+
66+
if(ans)
67+
{
68+
cout << M << '\n';
69+
for(C p : constraints)
70+
cout << p.x << ' ' << p.y << ' ' << p.z << '\n';
71+
}
72+
else
73+
cout << "Impossible\n";
74+
}
75+
}

facebook/2019/Round 1/B.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <string>
4+
#include <climits>
5+
#include <cstdlib>
6+
7+
#define MOD 1000000007;
8+
9+
using namespace std;
10+
11+
typedef long long ll;
12+
13+
int T, N, K, ssum[1000002];
14+
string s;
15+
16+
ll pow(int n)
17+
{
18+
if(n == 0) return 1;
19+
if(n == 1) return 2;
20+
21+
ll ret = pow(n / 2);
22+
ret = ret * ret % MOD;
23+
24+
if(n % 2 == 1)
25+
ret = ret * 2 % MOD;
26+
return ret;
27+
}
28+
29+
int main()
30+
{
31+
ios::sync_with_stdio(false);
32+
cin.tie(NULL);
33+
34+
cin >> T;
35+
for(int t = 1; t <= T; t++)
36+
{
37+
ll ans = 0, mn = 0;
38+
cout << "Case #" << t << ": ";
39+
cin >> N >> K >> s;
40+
41+
reverse(s.begin(), s.end());
42+
43+
for(int i = 1; i <= N; i++)
44+
{
45+
ssum[i] = ssum[i - 1] + (s[i - 1] == 'B' ? 1 : -1);
46+
47+
if(ssum[i] - mn > K)
48+
{
49+
ans = (ans + pow(N - i + 1)) % MOD;
50+
ssum[i] -= 2;
51+
}
52+
53+
if(ssum[i] < mn)
54+
mn = ssum[i];
55+
}
56+
57+
cout << ans << '\n';
58+
}
59+
}

0 commit comments

Comments
 (0)