#include<iostream> #include<cstdio> #include<cstring> #include<set> #include<algorithm> #define db(a) cout << #a << " = " << a << endl #define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl #define listar(lista) for (int i = 0; i < lista.size(); i++) db(lista[i]) using namespace std; int main() { #ifdef dennisbot freopen("in.in", "r", stdin); #endif int aveup = 0, avedown = 0; double sumup = 0, sumdown = 0; int creciente = 0, decreciente = 0; bool first = true, crece = true, decrece = true; int d, leido, cont = 0; while (scanf("%d", &d) != EOF) { if (d != 0) { cont++; if (first) { leido = d; first = false; continue; } if (leido == d) { if (crece) creciente++; if (decrece) decreciente++; leido = d; continue; } if (leido < d) { if (decrece) { if (creciente != decreciente) { sumdown += decreciente; avedown++; } decrece = false; decreciente = 0; } crece = true; creciente++; leido = d; continue; } if (leido > d) { if (crece) { if(creciente != decreciente) { sumup += creciente; aveup++; } crece = false; creciente = 0; } decrece = true; decreciente++; leido = d; } } else { if (cont == 0) break; if (creciente == decreciente) printf("Nr values = %d: %.6f %.6f\n", cont, 0. , 0.); else { if (crece) sumup += creciente, aveup++; if (decrece) sumdown += decreciente, avedown++; printf("Nr values = %d: %.6f %.6f\n",cont , (aveup != 0) ? sumup / aveup : 0, (avedown != 0) ? sumdown / avedown : 0); } creciente = decreciente = leido = cont = 0; first = crece = decrece = true; sumdown = sumup = aveup = avedown = 0; } } return 0; }
sábado, 31 de marzo de 2012
413 - Up and Down Sequences, UVA
567 - Risk, uva
#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<map> #define db(a) cout << #a << " = " << a << endl #define db2(a, b) cout << #a << " = " << a << " " #b << " = " << b << endl #define foreach(mapa, it) for (typeof(mapa.begin()) it = mapa.begin(); it != mapa.end(); it++) using namespace std; map<int, vector<int> > adyacencia; map<int, int> dist; void bfs(int a, int b) { queue<int> q; q.push(a); dist.clear(); dist[a] = 0; while (!q.empty()) { int u = q.front(); q.pop(); if (u == b) { printf("%2d to %2d: %d\n", a, b, dist[b]); break; } for (int i = 0; i < adyacencia[u].size(); i++) { if (dist.count(adyacencia[u][i])) continue; dist[adyacencia[u][i]] = dist[u] + 1; q.push(adyacencia[u][i]); } } } int main() { #ifdef dennisbot freopen("in.in", "r", stdin); freopen("ou.out", "w", stdout); #endif int n, var, ini, fin, cont = 1, ret; while (1) { adyacencia.clear(); dist.clear(); for (int i = 1; i < 20; i++) { ret = scanf("%d", &n); if (ret == EOF) return 0; for (int j = 0; j < n; j++) { scanf("%d", &var); adyacencia[i].push_back(var); adyacencia[var].push_back(i); } } scanf("%d", &n); printf("Test Set #%d\n", cont++); for (int i = 0; i < n; i++) { scanf("%d", &ini); scanf("%d", &fin); bfs(ini, fin); } puts(""); } }
viernes, 30 de marzo de 2012
2841. Bitwise Reverse, tju
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #define db(a) cout << #a << " = " << a << endl #define foreach(it, l) for ( typeof(l.begin()) it = l.begin(); it != l.end(); it++) #define dbl(i, a) cout << "[" << i << "]" << " = "<< a << ", "; #define listar(lista) for(int i = 0; i < lista.size(); i++) { dbl(i, lista[i]);} cout << endl; using namespace std; int main() { /*freopen("perm.in", "r", stdin); freopen("perm.out", "w", stdout);*/ #ifdef dennisbot freopen("in.in", "r", stdin); #endif int n; while (scanf("%d", &n) != EOF) { if (n == 0) { break; } string num = ""; for (int i = 0; i < 32; i++) { if (n >> i & 1) { num += "1"; } else num += "0"; } num = num.substr(0, num.find_last_of("1") + 1); int t = 1, res = 0; for (int i = num.size() - 1; i >= 0; i--) { res += (num[i] - 48) * t; t *= 2; } printf("%d\n", res); } return 0; }
jueves, 15 de marzo de 2012
10667 - Largest Block, uva
#include<iostream> #include<cstdio> #include<cstring> #define db(a) cout << #a << " = " << a << endl #define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl #define db3(a, b, c) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << endl using namespace std; int main() { #ifdef dennisbot freopen("in.in", "r", stdin); //freopen("ou.out", "w", stdout); #endif int p, s, b, r1, c1, r2, c2, matriz[101][101], fila[101], max, sum; scanf("%d", &p); for (;p; p--) { scanf("%d", &s); for (int i = 0; i < s; i++) for (int j = 0; j < s; j++) matriz[i][j] = 1; scanf("%d", &b); for (int k = 0; k < b; k++) { scanf("%d%d%d%d", &r1, &c1, &r2, &c2); r1--, c1--, r2--, c2--; for (int i = r1; i <= r2; i++) { for (int j = c1; j <= c2; j++) { matriz[i][j] = 0; } } } max = 0; for (int i = 0; i < s; i++) { for(int k = 0; k < s; k++) fila[k] = 0; for (int ii = i; ii < s; ii++) { sum = 0; for (int j = 0; j < s; j++) { fila[j] += matriz[ii][j]; if (fila[j] == ii - i + 1) sum += fila[j]; else sum = 0; if (sum > max) max = sum; } } } printf("%d\n", max); } return 0; }
836 - Largest Submatrix, uva
#include<iostream> #include<cstdio> #include<cstring> #define db(a) cout << #a << " = " << a << endl #define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl #define db3(a, b, c) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << endl using namespace std; int main() { #ifdef dennisbot freopen("in.in", "r", stdin); freopen("ou.out", "w", stdout); #endif int test, i, j, n, matriz[100][100], fila[100], div = 1, sum = 0, max = 0; char var[100]; scanf("%d\n", &test); while (test--) { i = 0; while (gets(var) && var[0] != '\0') { n = strlen(var); for (int j = 0; j < n; j++) { matriz[i][j] = var[j] - 48; } i++; } max = 0; for (int i = 0; i < n; i++) { memset(fila, 0, sizeof fila); for (int ii = i; ii < n; ii++) { sum = 0; for (int j = 0; j < n; j++) { fila[j] += matriz[ii][j]; if (fila[j] == ii - i + 1) sum += fila[j]; else sum = 0; if (sum > max) max = sum; } } } printf("%d\n", max); if (test != 0) puts(""); } return 0; }
martes, 13 de marzo de 2012
507 - Jill Rides Again, uva
#include<iostream> #include<cstdio> #include<vector> #include<utility> #include<ctime> #include<algorithm> #define db(a) cout << #a << " = " << a << endl #define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl #define db3(a, b, c) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << endl using namespace std; int main() { #ifdef dennisbot freopen("in.in", "r", stdin); //freopen("ou.out", "w", stdout); clock_t start = clock(); #endif int b, s; cin >> b; for (int i = 0; i < b; i++) { cin >> s; int sum = -1, cur = 0, inf = 0, sup = 0, max = -1, var, d = 0; for (int j = 0; j < s - 1; j++) { cin >> var; if (j != 0) { if (sum >= 0) sum += var; else sum = var, cur = j; if (sum == max and j - cur > d or sum > max) inf = cur, sup = j, max = sum, d = sup - inf; } else sum = var, max = var; } if (max > 0) printf("The nicest part of route %d is between stops %d and %d\n", i + 1, inf + 1, sup + 2); else printf("Route %d has no nice parts\n", i + 1); } #ifdef dennisbot printf("\ntime=%.3fs\n", (clock() - start) * 1. / CLOCKS_PER_SEC); #endif return 0; }
Suscribirse a:
Entradas (Atom)