Skip to content

Commit

Permalink
varios problemas
Browse files Browse the repository at this point in the history
  • Loading branch information
marioyc committed Oct 6, 2010
1 parent 15dec95 commit 162b2ba
Show file tree
Hide file tree
Showing 16 changed files with 1,158 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Live Archive/4184 - Sky Code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

#define MAX_ID 10000

int main(){
bool sq_free[MAX_ID+1],odd[MAX_ID+1];
vector<int> L;

sq_free[0] = false;
sq_free[1] = true;
odd[1] = false;

for(int i = 2;i<=MAX_ID;++i){
int aux = i;

sq_free[i] = true;
odd[i] = true; // si es primo

for(int j = 2;j*j<=i;++j){
if(i%j==0){
odd[i] = odd[i/j] ^ odd[j];
if(i%(j*j)==0) sq_free[i] = false;
}
}

if(sq_free[i]) L.push_back(i);
}

int M = L.size();
int N,a,rep[MAX_ID+1],cont[MAX_ID+1];

while(scanf("%d",&N)==1){
memset(rep,0,sizeof(rep));

for(int i = 0;i<N;++i){
scanf("%d",&a);
++rep[a];
}

memset(cont,0,sizeof(cont));

for(int i = 2;i<=MAX_ID;++i)
for(int j = i;j<=MAX_ID;j += i)
cont[i] += rep[j];

long long ans = (long long)N*(N-1)*(N-2)*(N-3)/24;

for(int i = 0;i<M;++i){
int d = L[i], aux = cont[d];
long long num = (long long)aux*(aux-1)*(aux-2)*(aux-3)/24;

if(odd[d]) ans -= num;
else ans += num;
}

printf("%lld\n",ans);
}

return 0;
}
101 changes: 101 additions & 0 deletions PKU/2901 - Hotel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>

using namespace std;

int dp[2][501][501][2];

void readInt(int &n){
int sign = 1;
char c;
bool found = false;
n = 0;

while(true){
c = getc(stdin);

switch(c){
case ' ':
if(found) goto jump;
break;
case '\n':
if(found) goto jump;
break;
default:
if(c>='0' && c<='9'){
n = n*10+c-'0';
found = true;
}else goto jump;
break;
}
}

jump:
n *= sign;
}

int main(){
int T,M,F,R,C,INF = INT_MAX/2;
int cap[500],p[500];

scanf("%d",&T);

while(T--){
readInt(M); readInt(F); readInt(R); readInt(C);

for(int i = 0;i<R;++i){
readInt(cap[i]);
readInt(p[i]);
}

for(int m = 0;m<=M;++m)
for(int f = 0;f<=F;++f)
for(int c = 0;c<2;++c)
dp[0][m][f][c] = dp[1][m][f][c] = INF;

dp[0][0][0][0] = 0;

for(int r = 0,prev = 0,cur = 1;r<R;++r){
for(int m = 0;m<=M;++m){
for(int f = 0;f<=F;++f){
for(int c = 0;c<2;++c){
int &ret = dp[cur][m][f][c];
ret = dp[prev][m][f][c];

if(m>0){
int aux = p[r]+dp[prev][m-min(cap[r],m)][f][c];
if(aux<ret) ret = aux;
}

if(f>0){
int aux = p[r]+dp[prev][m][f-min(cap[r],f)][c];
if(aux<ret) ret = aux;
}

if(c==1 && cap[r]>=2){
int aux = p[r]+dp[prev][m][f][0];
if(aux<ret) ret = aux;
}
}
}
}

prev ^= 1;
cur ^= 1;
}

int ans = dp[R&1][M][F][0];

if(C>0){
int aux = dp[R&1][M-1][F-1][1];
if(aux<ans) ans = aux;
}

if(ans<INF) printf("%d\n",ans);
else printf("Impossible\n");
}

return 0;
}
49 changes: 49 additions & 0 deletions SPOJ/Classical/4226 - Japan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define MAX_SIZE 1001

int T[MAX_SIZE];

void update(int idx, int val){
for(;idx<MAX_SIZE;idx+=(idx & -idx)) T[idx]+=val;
}

int F(int idx){
long long sum = 0;
for(;idx>0;idx -= (idx & -idx)) sum += T[idx];
return sum;
}

pair<int, int> P[1000000];

int main(){
int TC,N,M,K;

scanf("%d",&TC);

for(int tc = 1;tc<=TC;++tc){
scanf("%d %d %d",&N,&M,&K);

for(int i = 0;i<K;++i)
scanf("%d %d",&P[i].first,&P[i].second);

sort(P,P+K);

memset(T,0,sizeof(T));

long long ans = 0;

for(int i = 0;i<K;++i){
ans += i-F(P[i].second);
update(P[i].second,1);
}

printf("Test case %d: %lld\n",tc,ans);
}

return 0;
}
122 changes: 122 additions & 0 deletions TJU/1692 - Cable TV Network.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <cstdio>
#include <climits>
#include <cstring>
#include <algorithm>

using namespace std;

struct flow_graph{
int MAX_V,E,s,t,head,tail;
int *cap,*to,*next,*last,*dist,*q,*now;

flow_graph(){}

flow_graph(int V, int MAX_E){
MAX_V = V; E = 0;
cap = new int[2*MAX_E], to = new int[2*MAX_E], next = new int[2*MAX_E];
last = new int[MAX_V], q = new int[MAX_V], dist = new int[MAX_V], now = new int[MAX_V];
fill(last,last+MAX_V,-1);
}

void clear(){
fill(last,last+MAX_V,-1);
E = 0;
}

void add_edge(int u, int v, int uv, int vu = 0){
to[E] = v, cap[E] = uv, next[E] = last[u]; last[u] = E++;
to[E] = u, cap[E] = vu, next[E] = last[v]; last[v] = E++;
}

bool bfs(){
fill(dist,dist+MAX_V,-1);
head = tail = 0;

q[tail] = t; ++tail;
dist[t] = 0;

while(head<tail){
int v = q[head]; ++head;

for(int e = last[v];e!=-1;e = next[e]){
if(cap[e^1]>0 && dist[to[e]]==-1){
q[tail] = to[e]; ++tail;
dist[to[e]] = dist[v]+1;
}
}
}

return dist[s]!=-1;
}

int dfs(int v, int f){
if(v==t) return f;

for(int &e = now[v];e!=-1;e = next[e]){
if(cap[e]>0 && dist[to[e]]==dist[v]-1){
int ret = dfs(to[e],min(f,cap[e]));

if(ret>0){
cap[e] -= ret;
cap[e^1] += ret;
return ret;
}
}
}

return 0;
}

int max_flow(int source, int sink){
s = source; t = sink;
int f = 0,x;

while(bfs()){
for(int i = 0;i<MAX_V;++i) now[i] = last[i];

while(true){
x = dfs(s,INT_MAX);
if(x==0) break;
f += x;
}
}

return f;
}
}G(2*50,50*49/2+50);

int main(){
int n,m,cont[50][50],u,v,ans;
char s[8];

while(scanf("%d %d",&n,&m)==2){
memset(cont,0,sizeof(cont));

for(int i = 0;i<m;++i){
scanf("%s",s);
sscanf(s,"(%d,%d)",&u,&v);
++cont[u][v];
++cont[v][u];
}

ans = n;

for(int i = 0;i<n;++i){
for(int j = i+1;j<n;++j){
G.clear();
for(int k = 0;k<n;++k) G.add_edge(2*k,2*k+1,1);

for(int a = 0;a<n;++a) for(int b = 0;b<n;++b){
if(cont[a][b]==0) continue;
G.add_edge(2*a+1,2*b,n+1);
}

ans = min(ans,G.max_flow(2*i+1,2*j));
}
}

printf("%d\n",ans);
}

return 0;
}
42 changes: 42 additions & 0 deletions TJU/1852 - Tour.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

#define MAX_N 1000

double x[MAX_N],y[MAX_N];

double dist(int a, int b){
return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}

double memo[MAX_N][MAX_N];

double solve(int p1, int p2, int next){
if(next==0) return dist(0,p1)+dist(0,p2);

double &ret = memo[p1][p2];
if(ret>-0.5) return ret;

ret = fmin(dist(p1,next)+solve(next,p2,next-1),
dist(p2,next)+solve(p1,next,next-1));

return ret;
}

int main(){
int N;

while(scanf("%d",&N)==1){
for(int i = 0;i<N;++i)
scanf("%lf %lf",&x[i],&y[i]);

memset(memo,-1,sizeof(memo));

printf("%.2lf\n",solve(N-1,N-1,N-2));
}

return 0;
}
Loading

0 comments on commit 162b2ba

Please sign in to comment.