Permalink
Browse files

Merge branch 'master' of github.com:iwiwi/programming-contests

  • Loading branch information...
2 parents b69bafc + 53b3007 commit a4ec2a6dc091bde650b69a9cd338b8d8108fa7f4 Takuya Akiba committed Apr 21, 2012
Showing with 2,519 additions and 0 deletions.
  1. +105 −0 icpc/wf2006/A.cpp
  2. +110 −0 icpc/wf2006/B.cpp
  3. +100 −0 icpc/wf2006/D_tle.cpp
  4. +68 −0 icpc/wf2006/E.cpp
  5. +125 −0 icpc/wf2006/F_wa.cpp
  6. +74 −0 icpc/wf2006/G.cpp
  7. +221 −0 icpc/wf2006/H.cpp
  8. +56 −0 icpc/wf2006/I.cpp
  9. +167 −0 icpc/wf2011/A.cpp
  10. +52 −0 icpc/wf2011/B.cpp
  11. +67 −0 icpc/wf2011/C.cpp
  12. +51 −0 icpc/wf2011/E.cpp
  13. +95 −0 icpc/wf2011/G.cpp
  14. +131 −0 icpc/wf2011/H.cpp
  15. +115 −0 icpc/wf2011/J.cpp
  16. +43 −0 icpc/wf2011/K.cpp
  17. 0 other/jag/{2012_winter_contest → 2012_winter_contest_1}/a.cpp
  18. 0 other/jag/{2012_winter_contest → 2012_winter_contest_1}/c.cpp
  19. 0 other/jag/{2012_winter_contest → 2012_winter_contest_1}/d.cpp
  20. 0 other/jag/{2012_winter_contest → 2012_winter_contest_1}/e.cpp
  21. 0 other/jag/{2012_winter_contest → 2012_winter_contest_1}/g_wa.cpp
  22. 0 other/jag/{2012_winter_contest → 2012_winter_contest_1}/i.cpp
  23. 0 other/jag/{2012_winter_contest → 2012_winter_contest_1}/i_rec.cpp
  24. 0 other/jag/{2012_winter_contest → 2012_winter_contest_1}/i_rec2.cpp
  25. +68 −0 other/jag/2012_winter_contest_2/B.cpp
  26. +103 −0 other/jag/2012_winter_contest_2/C_tle1.cpp
  27. +183 −0 other/jag/2012_winter_contest_2/C_tle2.cpp
  28. +161 −0 other/jag/2012_winter_contest_2/E.cpp
  29. +117 −0 other/jag/2012_winter_contest_2/G_incomplete.cpp
  30. +94 −0 other/jag/2012_winter_contest_2/H_uso.cpp
  31. +113 −0 other/jag/2012_winter_contest_2/I.cpp
  32. +32 −0 other/jag/2012_winter_contest_2/I_dp.cpp
  33. +68 −0 other/jag/2012_winter_contest_2/I_slow.cpp
View
@@ -0,0 +1,105 @@
+#include <cstdio>
+#include <iostream>
+#include <vector>
+#include <map>
+#include <cstring>
+#include <climits>
+#include <algorithm>
+using namespace std;
+#define rep(i, n) for (int i = 0; i < (int)n; ++i)
+#define mp make_pair
+#define pb push_back
+
+
+int NT, NI;
+int C[30];
+vector<int> T[30];
+
+map<int, int> ma;
+int getid(int k) {
+ if (ma.count(k)) return ma[k];
+ else {
+ int r = ma.size();
+ return ma[k] = r;
+ }
+}
+
+int pot[210][30];
+int lst[210][30];
+pair<int, int> prv[210][30];
+
+int main() {
+ for (int ca = 1; ; ++ca) {
+ scanf("%d", &NT);
+ if (NT == 0) return 0;
+ ma.clear();
+
+ rep (i, NT) {
+ int k;
+ scanf("%d%d", &C[i], &k);
+ T[i].resize(k);
+ rep (j, k) {
+ scanf("%d", &T[i][j]);
+ T[i][j] = getid(T[i][j]);
+ }
+ }
+
+ scanf("%d", &NI);
+ rep (trip, NI) {
+ int K;
+ scanf("%d", &K);
+ vector<int> I(K);
+ rep (j, K) {
+ scanf("%d", &I[j]);
+ I[j] = getid(I[j]);
+ }
+
+ int V = ma.size();
+
+ multimap<int, pair<int, int> > que;
+ rep (v, V) rep (k, K) pot[v][k] = INT_MAX;
+ pot[I[0]][0] = 0;
+ que.insert(mp(0, mp(I[0], 0)));
+ int d, v, k;
+ while (!que.empty()) {
+ d = que.begin()->first;
+ v = que.begin()->second.first;
+ k = que.begin()->second.second;
+ que.erase(que.begin());
+ if (d > pot[v][k]) continue;
+
+ if (k == K - 1) break;
+
+ rep (i, NT) {
+ if (T[i][0] != v) continue;
+
+ int tk = k;
+ int td = d + C[i];
+ for (int j = 1; j < (int)T[i].size(); ++j) {
+ int tv = T[i][j];
+ if (tk + 1 < K && tv == I[tk + 1]) ++tk;
+ if (td < pot[tv][tk]) {
+ que.insert(mp(td, mp(tv, tk)));
+ pot[tv][tk] = td;
+ lst[tv][tk] = i;
+ prv[tv][tk] = mp(v, k);
+ }
+ }
+ }
+ }
+ printf("Case %d, Trip %d: Cost = %d\n", ca, trip + 1, d);
+
+ vector<int> usd;
+ while (mp(v, k) != mp(I[0], 0)) {
+ usd.pb(lst[v][k]);
+ pair<int, int> p = prv[v][k];
+ v = p.first;
+ k = p.second;
+ }
+ reverse(usd.begin(), usd.end());
+ printf(" Tickets used:");
+ rep (i, usd.size()) printf(" %d", usd[i] + 1);
+ puts("");
+ }
+ }
+}
View
@@ -0,0 +1,110 @@
+#include <cstdio>
+#include <iostream>
+#include <vector>
+#include <map>
+#include <cstring>
+#include <climits>
+#include <algorithm>
+#include <queue>
+#include <cmath>
+#include <numeric>
+using namespace std;
+#define rep(i, n) for (int i = 0; i < (int)n; ++i)
+#define mp make_pair
+#define pb push_back
+
+const int MAX_V = 1010;
+struct e_t {
+ int to, cap, cst, rev;
+};
+int V, S, T;
+
+vector<e_t> adj[MAX_V];
+int pot[MAX_V], dst[MAX_V], prvv[MAX_V], prve[MAX_V];
+
+void add_edge(int i, int j, int cap, int cst) {
+ adj[i].pb((e_t){j, cap, +cst, adj[j].size()});
+ adj[j].pb((e_t){i, 0, -cst, adj[i].size() - 1});
+}
+
+pair<int, int> min_cost_flow() {
+ int flow = 0, cost = 0;
+ memset(pot, 0, sizeof(pot));
+
+ for (;;) {
+ fill(dst, dst + V, INT_MAX);
+ priority_queue<pair<int, int> > que;
+ que.push(mp(0, S));
+ dst[S] = 0;
+ while (!que.empty()) {
+ int v = que.top().second; que.pop();
+ rep (i, adj[v].size()) {
+ e_t &e = adj[v][i];
+ int w = e.to;
+ int td = dst[v] + e.cst + pot[v] - pot[w];
+ if (e.cap && td < dst[w]) {
+ que.push(mp(-td, w));
+ dst[w] = td;
+ prvv[w] = v;
+ prve[w] = i;
+ }
+ }
+ }
+ if (dst[T] == INT_MAX) break;
+ rep (v, V) pot[v] += dst[v];
+
+ int f = INT_MAX;
+ for (int v = T; v != S; v = prvv[v]) {
+ f = min(f, adj[prvv[v]][prve[v]].cap);
+ }
+ for (int v = T; v != S; v = prvv[v]) {
+ e_t &e = adj[prvv[v]][prve[v]];
+ e.cap -= f;
+ adj[e.to][e.rev].cap += f;
+ }
+ flow += f;
+ cost += f * pot[T];
+ }
+ return mp(flow, cost);
+}
+
+int H, W;
+int HN[60], WN[60];
+double A[60][60];
+
+int main() {
+ for (int ca = 1; ; ++ca) {
+ scanf("%d%d", &H, &W);
+ if (H == 0 && W == 0) return 0;
+
+ rep (i, H) scanf("%d", &HN[i]);
+ rep (j, W) scanf("%d", &WN[j]);
+ rep (i, H) rep (j, W) scanf("%lf", &A[i][j]);
+
+ map<int, int> ans;
+ for (int s = -1; s <= 1; ++s) {
+ V = H + W + 2;
+ S = H + W;
+ T = H + W + 1;
+ rep (v, V) adj[v].clear();
+
+ rep (i, H) add_edge(S , i, HN[i], 0);
+ rep (j, W) add_edge(H + j, T, WN[j], 0);
+
+ rep (i, H) rep (j, W) {
+ if (A[i][j] < 0) continue;
+ int c = 100 + s * (int)round(A[i][j] * 100);
+ add_edge(i, H + j, 200, c);
+ }
+
+ pair<int, int> f = min_cost_flow();
+ // printf("%d %d\n", f.first, f.second);
+ int c = f.second;
+ c -= 100 * f.first;
+ c *= s;
+ ans[s] = c;
+ }
+
+ printf("Problem %d: %.2f to %.2f\n", ca, ans[1] / 100.0, ans[-1] / 100.0);
+ }
+}
View
@@ -0,0 +1,100 @@
+#include <stdio.h>
+#include <string.h>
+#include <utility>
+#include <algorithm>
+using namespace std;
+
+#define MAX 101010
+
+int tame[MAX*9][2];
+int p10[10];
+
+int ansm,ansa,ansn,ansb;
+void update(int m,int a,int n,int b,int key){
+ if(m<=8){
+ if(a*p10[m]+(b-a)*p10[n]+b==key)return;
+ }
+ bool gre;
+ if(m>ansm)gre=true;
+ else if(m<ansm)gre=false;
+ else if(a>ansa)gre=true;
+ else if(a<ansa)gre=false;
+ else{
+ if(n>ansn){
+ if(b>ansa)gre=true; else gre=false;
+ }
+ if(n<ansn){
+ if(a>ansb)gre=true; else gre=false;
+ }
+ if(n==ansn){
+ if(b>ansb)gre=true; else gre=false;
+ }
+ }
+ //printf("%d %d %d %d %s %d %d %d %d\n",m,a,n,b,gre?">":"<=",ansm,ansa,ansn,ansb);
+ if(!gre){
+ ansm=m;
+ ansa=a;
+ ansn=n;
+ ansb=b;
+ }
+}
+
+//long long loopcnt;
+
+void doit(int key,int a,int b){
+ //printf("key=%d a=%d b=%d\n",key,a,b);
+ int anscnt=0;
+ memset(tame,-1,sizeof(int)*2*key);
+ int a10m=a%key;
+ int ba10m=((b-a)%key+key)%key;
+ for(int m=1;m<=key/9/8+15;m++){
+ //loopcnt++;
+ a10m=(a10m*10)%key;
+ //printf("a*10^m=%d\n",a10m);
+ if(tame[a10m][0]!=-1){
+ anscnt++;
+ update(m,a,tame[a10m][0],b,key);
+ if(anscnt==2)break;
+ }
+ if(tame[a10m][1]!=-1){
+ anscnt++;
+ update(m,a,tame[a10m][1],b,key);
+ if(anscnt==2)break;
+ }
+ ba10m=(ba10m*10)%key;
+ int neg=((-ba10m+b)%key+key)%key;
+ //printf("store %d\n",neg);
+ if(b-a>0){
+ if(tame[neg][0]==-1)tame[neg][0]=m;
+ else if(tame[neg][1]==-1)tame[neg][1]=m;
+ }else{
+ tame[neg][1]=tame[neg][0];
+ tame[neg][0]=m;
+ }
+ }
+}
+
+main(){
+
+ p10[0]=1;
+ for(int i=1;i<=9;i++)p10[i]=p10[i-1]*10;
+ //doit(27,1,9);
+
+ for(;;){
+ int k;
+ scanf("%d",&k);
+ if(k==0)return 0;
+ //loopcnt=0;
+ k*=9;
+ ansm=10101010;ansa=1;ansn=5050505;ansb=0;//initialize
+ for(int a=1;a<=9;a++){
+ for(int b=0;b<=9;b++){
+ if(a==b)continue;
+ doit(k,a,b);
+ }
+ }
+ // printf("loopcnt=%lld\n",loopcnt);
+ printf("%d: %d %d %d %d\n",k/9,ansm-ansn,ansa,ansn,ansb);
+ }
+
+}
View
@@ -0,0 +1,68 @@
+#include <cstdio>
+#include <iostream>
+#include <vector>
+#include <map>
+#include <cstring>
+#include <climits>
+#include <algorithm>
+#include <queue>
+#include <cmath>
+#include <numeric>
+using namespace std;
+#define rep(i, n) for (int i = 0; i < (int)n; ++i)
+#define mp make_pair
+#define pb push_back
+
+typedef long long ll;
+
+int len, A[50];
+int dp[50][50][16010];
+
+int search(int pos, int zero, int one) {
+ if (zero < 0 || one < 0) return 0;
+ if (len - pos < zero) return 0;
+ if (one >= 1LL << (len - pos - zero)) return 0;
+
+ if (pos == len) return zero == 0 && one == 0 ? 1 : 0;
+ if (dp[pos][zero][one] != -1) return dp[pos][zero][one];
+
+ // printf("%d %d %d\n", pos, zero, one);
+
+ if (A[pos] == 0) return dp[pos][zero][one] = search(pos + 1, zero - 1, one);
+ else { // A[pos] == 1
+ ll x = 0, res = 0;
+ for (int to = pos + 1; to <= len; ++to) {
+ if (res >= 2) break;
+
+ x = (x << 1) + A[to - 1];
+ if (to < len && A[to] == 1) continue;
+ if (to - pos == 1) res += search(to, zero, one - 1);
+ else if (to - pos == 2 && x == 2) continue;
+ else if (to - pos == 2 && x == 3) {
+ res += search(to, zero, one - 2);
+ res += search(to, zero, one - 3);
+ } else {
+ res += search(to, zero, one - x);
+ }
+ }
+ return dp[pos][zero][one] = res;
+ }
+}
+
+int main() {
+ for (int ca = 1; ; ++ca) {
+ int L, N;
+ scanf("%d%d", &L, &N);
+ if (L == 0 && N == 0) return 0;
+
+ string s;
+ cin >> s;
+ len = s.length();
+ rep (i, len) A[i] = s[i] - '0';
+
+ memset(dp, -1, sizeof(dp));
+ int res = search(0, L - N, N);
+ printf("Case #%d: %s\n", ca,
+ res == 0 ? "NO" : res == 1 ? "YES" : "NOT UNIQUE");
+ }
+}
Oops, something went wrong.

0 comments on commit a4ec2a6

Please sign in to comment.