Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Sundays are good for contests contest

  • Loading branch information...
commit 79b4577b7402437459aedd68bd3dac7a847764dc 1 parent 85db1f1
@andmej authored
View
BIN  3169 - Boundary points/3169
Binary file not shown
View
90 3169 - Boundary points/3169.cpp
@@ -0,0 +1,90 @@
+/*
+ Problem: 3169 - Boundary points
+ Author: Andrés Mejía-Posada
+ */
+
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <fstream>
+#include <cassert>
+#include <climits>
+#include <cstdlib>
+#include <cstring>
+#include <string>
+#include <cstdio>
+#include <vector>
+#include <cmath>
+#include <queue>
+#include <deque>
+#include <stack>
+#include <map>
+#include <set>
+using namespace std;
+
+struct point {
+ double x, y;
+ point(){} point(double x, double y) : x(x), y(y) {}
+ bool operator <(const point &p) const {
+ return x < p.x || (x == p.x && y < p.y);
+ }
+};
+
+// 2D cross product.
+// Return a positive value, if OAB makes a counter-clockwise turn,
+// negative for clockwise turn, and zero if the points are collinear.
+double cross(const point &O, const point &A, const point &B){
+ double d = (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
+ if (fabs(d) < 1e-9) return 0.0;
+ return d;
+}
+
+// Returns a list of points on the convex hull in counter-clockwise order.
+// Note: the last point in the returned list is the same as the first one.
+vector<point> convexHull(vector<point> P){
+ int n = P.size(), k = 0;
+ vector<point> H(2*n);
+
+ // Sort points lexicographically
+ sort(P.begin(), P.end());
+
+ // Build lower hull
+ for (int i = 0; i < n; i++) {
+ while (k >= 2 && cross(H[k-2], H[k-1], P[i]) <= 0.0) k--;
+ H[k++] = P[i];
+ }
+
+ // Build upper hull
+ for (int i = n-2, t = k+1; i >= 0; i--) {
+ while (k >= t && cross(H[k-2], H[k-1], P[i]) <= 0.0) k--;
+ H[k++] = P[i];
+ }
+
+ H.resize(k);
+ return H;
+}
+
+int main(){
+ string s;
+ while (getline(cin, s)){
+ stringstream splitter(s);
+ string pnt;
+ vector<point> poly;
+ while (splitter >> pnt){
+ assert(pnt.size() > 3);
+ pnt[0] = pnt[pnt.size()-1] = pnt[pnt.find(',')] = ' ';
+ stringstream sin(pnt);
+ double x, y;
+ sin >> x >> y;
+ poly.push_back(point(x, y));
+ }
+
+ vector<point> ans = convexHull(poly);
+ cout << "(" << ans.front().x << "," << ans.front().y << ")";
+ for (int i=1; i<ans.size(); ++i){
+ cout << " (" << ans[i].x << "," << ans[i].y << ")";
+ }
+ cout << endl;
+ }
+ return 0;
+}
View
1  3169 - Boundary points/in.txt
@@ -0,0 +1 @@
+(-2,1) (-1,-2) (-1,1) (-1,2) (-1,3) (0,0) (1,-1) (1,1) (2,-2) (2,1) (3,2)
View
BIN  3170 - AGTC/3170
Binary file not shown
View
43 3170 - AGTC/3170.cpp
@@ -0,0 +1,43 @@
+/*
+ Problem: 3170 - AGTC
+ Author: Andrés Mejía-Posada
+ */
+
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <fstream>
+#include <cassert>
+#include <climits>
+#include <cstdlib>
+#include <cstring>
+#include <string>
+#include <cstdio>
+#include <vector>
+#include <cmath>
+#include <queue>
+#include <deque>
+#include <stack>
+#include <map>
+#include <set>
+using namespace std;
+
+int main(){
+ string s, t;
+ int _s, _t;
+ while (cin >> _s >> s >> _t >> t){
+ s = " " + s, t = " " + t;
+ int dp[_s+1][_t+1];
+ dp[0][0] = 0;
+ for (int i=0; i<=_s; ++i)
+ for (int j=0; j<=_t; ++j)
+ if (i || j){
+ dp[i][j] = INT_MAX;
+ if (i) dp[i][j] <?= dp[i-1][j] + 1;
+ if (j) dp[i][j] <?= dp[i][j-1] + 1;
+ if (i && j) dp[i][j] <?= dp[i-1][j-1] + (s[i] != t[j]);
+ }
+ cout << dp[_s][_t] << endl;
+ }
+ return 0;
+}
View
6 3170 - AGTC/in.txt
@@ -0,0 +1,6 @@
+10 AGTCTGACGC
+11 AGTAAGTAGGC
+10 AGTCTGACGC
+11 AGTAAGTAGGC
+6 ANDRES
+5 MEJIA
View
BIN  3171 - Oreon/3171
Binary file not shown
View
75 3171 - Oreon/3171.cpp
@@ -0,0 +1,75 @@
+/*
+ Problem: 3171 - Oreon
+ Author: Andrés Mejía-Posada
+ */
+
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <fstream>
+#include <cassert>
+#include <climits>
+#include <cstdlib>
+#include <cstring>
+#include <string>
+#include <cstdio>
+#include <vector>
+#include <cmath>
+#include <queue>
+#include <deque>
+#include <stack>
+#include <map>
+#include <set>
+using namespace std;
+
+/* Union find */
+int p[27], rank[27];
+void make_set(int x){ p[x] = x, rank[x] = 0; }
+void link(int x, int y){ rank[x] > rank[y] ? p[y] = x : p[x] = y, rank[x] == rank[y] ? rank[y]++: 0; }
+int find_set(int x){ return x != p[x] ? p[x] = find_set(p[x]) : p[x]; }
+void merge(int x, int y){ link(find_set(x), find_set(y)); }
+/* End union find */
+
+
+struct edge{
+ int w;
+ char u, v;
+ edge(){} edge(char U, char V, int W) : u(min(U, V)), v(max(U, V)), w(W) {}
+ bool operator < (const edge &that) const {
+ return ( w < that.w || (w == that.w && u < that.u || (w == that.w && u == that.u && v < that.v)));
+ }
+};
+
+
+int main(){
+ int pizza = 1;
+ int T;
+ cin >> T;
+ while (T--){
+ cout << "Case " << pizza++ << ":" << endl;
+ int n;
+ cin >> n;
+ string s;
+ getline(cin, s);
+ vector<edge> ans;
+ for (int i=0; i<=26; ++i) make_set(i);
+ for (int i=0; i<n; ++i){
+ getline(cin, s);
+ for (int k=0; k<s.size(); ++k) if (s[k] == ',') s[k] = ' ';
+ stringstream sin(s);
+ for (int j=0; j<n; ++j){
+ int w;
+ sin >> w;
+ if (w) ans.push_back(edge(i+'A', j+'A', w));
+ }
+ }
+ sort(ans.begin(), ans.end());
+ for (int i=0; i<ans.size(); ++i){
+ if (find_set(ans[i].u-'A') != find_set(ans[i].v-'A')){
+ merge(ans[i].u-'A', ans[i].v-'A');
+ cout << ans[i].u << "-" << ans[i].v << " " << ans[i].w << endl;
+ }
+ }
+ }
+ return 0;
+}
View
16 3171 - Oreon/in.txt
@@ -0,0 +1,16 @@
+2
+6
+0, 8, 12, 0, 0, 7
+8, 0, 0, 3, 0, 0
+12, 0, 0, 0, 6, 0
+0, 3, 0, 0, 0, 4
+0, 0, 6, 0, 0, 5
+7, 0, 0, 4, 5, 0
+6
+0, 8, 12, 0, 0, 7
+8, 0, 0, 3, 0, 0
+12, 0, 0, 0, 6, 0
+0, 3, 0, 0, 0, 4
+0, 0, 6, 0, 0, 5
+7, 0, 0, 4, 5, 0
+
View
BIN  3173 - Wordfish/3173
Binary file not shown
View
66 3173 - Wordfish/3173.cpp
@@ -0,0 +1,66 @@
+/*
+ Problem: 3173 - Wordfish
+ Author: Andrés Mejía-Posada
+ */
+
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <fstream>
+#include <cassert>
+#include <climits>
+#include <cstdlib>
+#include <cstring>
+#include <string>
+#include <cstdio>
+#include <vector>
+#include <cmath>
+#include <queue>
+#include <deque>
+#include <stack>
+#include <map>
+#include <set>
+using namespace std;
+
+int dist(string s){
+ int ans = INT_MAX;
+ int n = s.size();
+ for (int i=0; i<n-1; ++i) ans = min(ans, abs(s[i] - s[i+1]));
+ return ans;
+}
+
+int main(){
+ string s;
+ while (cin >> s){
+ assert(s.size() > 1);
+
+ deque<string> p(1, s);
+ string t;
+
+ t = s;
+ for (int i=0; i<10; ++i){
+ next_permutation(t.begin(), t.end());
+ p.push_back(t);
+ }
+
+ t = s;
+ for (int i=0; i<10; ++i){
+ prev_permutation(t.begin(), t.end());
+ p.push_front(t);
+ }
+
+ sort(p.begin(), p.end()); //Useless?
+
+ int score = -1; string ans;
+ for (deque<string>::iterator k = p.begin(); k != p.end(); ++k){
+ int t = dist(*k);
+ if (t > score){
+ score = t;
+ ans = *k;
+ }
+ }
+ cout << ans << score << endl;
+
+ }
+ return 0;
+}
View
6 3173 - Wordfish/in.txt
@@ -0,0 +1,6 @@
+WORDFISH
+ANDYCITO
+ANDY
+AB
+A
+
View
BIN  3986 - The Bridges of Kölsberg/3986.2
Binary file not shown
View
113 3986 - The Bridges of Kölsberg/3986.2.cpp
@@ -0,0 +1,113 @@
+/*
+ Problem: 3986 - The Bridges of Kölsberg (Live Archive)
+ Author: Andrés Mejía-Posada
+
+ Accepted
+ */
+
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <fstream>
+#include <cassert>
+#include <climits>
+#include <cstdlib>
+#include <cstring>
+#include <string>
+#include <cstdio>
+#include <vector>
+#include <cmath>
+#include <queue>
+#include <deque>
+#include <stack>
+#include <map>
+#include <set>
+using namespace std;
+
+const int MAXN = 1001;
+
+int dp[MAXN+1][MAXN+1], b[MAXN+1][MAXN+1];
+
+
+/*
+
+ dp[i][j] = Máximo puntaje que puedo alcanzar habiendo creado
+ puentes válidos entre las primeros i ciudades del norte y las
+ primeras j ciudades del sur.
+
+ b[i][j] = Mínima cantidad de puentes que necesito para obtener un
+ puntaje de dp[i][j].
+
+ Notar que dp[i][j] es el máximo entre tres posibles opciones:
+
+ * dp[i-1][j-1] + valor_norte[i] + valor_sur[j], siempre y cuando la
+ ciudad norte-i y la ciudad sur-j tengan el mismo sistema
+ operativo. Representa la opción de conectar los dos puentes.
+
+ * dp[i][j-1]. Representa la opción de lograr un mejor puntaje
+ ignorando la ciudad actual del sur.
+
+ * dp[i-1][j]. Representa la opción de lograr un mejor puntaje
+ ignorando la ciudad actual del norte.
+
+*/
+
+
+int w_north[MAXN+1], w_south[MAXN+1];
+int id_north[MAXN+1], id_south[MAXN+1];
+char buffer[32];
+
+int main(){
+ int cases;
+ scanf("%d", &cases);
+ while (cases--){
+ int n, s, currentID = 1;
+ map<string, int> id;
+ string os;
+ scanf("%d", &n);
+ for (int i=1; i<=n; ++i){
+ scanf("%11s %11s %d", buffer, buffer, &w_north[i]);
+ os = string(buffer);
+ if (id[os] > 0) id_north[i] = id[os];
+ else id_north[i] = id[os] = currentID++;
+ }
+ cin >> s;
+ for (int j=1; j<=s; ++j){
+ scanf("%11s %11s %d", buffer, buffer, &w_south[j]);
+ os = string(buffer);
+ if (id[os] > 0) id_south[j] = id[os];
+ else id_south[j] = id[os] = currentID++;
+ }
+
+ for (int i=0; i<=n; ++i) dp[i][0] = b[i][0] = 0;
+ for (int j=0; j<=s; ++j) dp[0][j] = b[0][j] = 0;
+
+ for (int i=1; i<=n; ++i){
+ for (int j=1; j<=s; ++j){
+ dp[i][j] = 0;
+
+ if (dp[i-1][j] > dp[i][j] || (dp[i-1][j] == dp[i][j] && b[i-1][j] < b[i][j])){
+ dp[i][j] = dp[i-1][j];
+ b[i][j] = b[i-1][j];
+ }
+
+ if (dp[i][j-1] > dp[i][j] || (dp[i][j-1] == dp[i][j] && b[i][j-1] < b[i][j])){
+ dp[i][j] = dp[i][j-1];
+ b[i][j] = b[i][j-1];
+ }
+
+ if (id_north[i] == id_south[j]){
+ int new_w = dp[i-1][j-1] + w_north[i] + w_south[j];
+ if (new_w > dp[i][j] || (new_w == dp[i][j] && b[i-1][j-1] + 1 < b[i][j])){
+ dp[i][j] = new_w;
+ b[i][j] = b[i-1][j-1] + 1;
+ }
+ }
+
+ }
+ }
+ printf("%d %d\n", dp[n][s], b[n][s]);
+
+ }
+ return 0;
+}
View
29 3986 - The Bridges of Kölsberg/3986.cpp
@@ -1,6 +1,8 @@
/*
- Problem:
+ Problem: 3986 - The Bridges of Kölsberg (Live Archive)
Author: Andrés Mejía-Posada
+
+ Accepted
*/
#include <algorithm>
@@ -26,6 +28,31 @@ const int MAXN = 1001;
int dp[MAXN+1][MAXN+1], b[MAXN+1][MAXN+1];
+
+/*
+
+ dp[i][j] = Máximo puntaje que puedo alcanzar habiendo creado
+ puentes válidos entre las primeros i ciudades del norte y las
+ primeras j ciudades del sur.
+
+ b[i][j] = Mínima cantidad de puentes que necesito para obtener un
+ puntaje de dp[i][j].
+
+ Notar que dp[i][j] es el máximo entre tres posibles opciones:
+
+ * dp[i-1][j-1] + valor_norte[i] + valor_sur[j], siempre y cuando la
+ ciudad norte-i y la ciudad sur-j tengan el mismo sistema
+ operativo. Representa la opción de conectar los dos puentes.
+
+ * dp[i][j-1]. Representa la opción de lograr un mejor puntaje
+ ignorando la ciudad actual del sur.
+
+ * dp[i-1][j]. Representa la opción de lograr un mejor puntaje
+ ignorando la ciudad actual del norte.
+
+*/
+
+
int w_north[MAXN+1], w_south[MAXN+1];
int id_north[MAXN+1], id_south[MAXN+1];
Please sign in to comment.
Something went wrong with that request. Please try again.