Category Archives: Sec3.2

USACO 3.2.6 – Sweet Butter

#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
struct E { int b, d; };
int main() {
	freopen("butter.in", "r", stdin), freopen("butter.out", "w", stdout);
	int N, C, P, p[801] = { 0 }, best[801], INF = 10000000, min_r = INF;
	vector<E> e[801]; vector<int> cows;
	fill(e, e + 800, vector<E> ()), fill(best, best + 801, INF);
	scanf("%d %d %d", &N, &P, &C);
	for (int c, i = 0; i < N; p[c]++, cows.push_back(c), i++)
		scanf("%d", &c);
	for (int a, b, d, c = 0; c < C; c++) {
		scanf("%d %d %d", &a, &b, &d);
		e[a].push_back(E { b, d }), e[b].push_back(E { a, d });
	}
	for (int s = 1, r = 0; s <= P; fill(best, best + 801, INF), r = 0, ++s) {
		queue<E> q; q.push(E { s, best[s] = 0 });
		while (q.size()) {
			E f = q.front(); q.pop();
			for (int b = 0; b < e[f.b].size(); ++b) {
				E o = e[f.b][b];
				if (best[o.b] > o.d + best[f.b])
					q.push(E { o.b, (best[o.b] = o.d + best[f.b]) });
			}
		}
		for (int i = 0; i < cows.size(); ++i)
			r += best[cows[i]];
		min_r = r < min_r ? r : min_r;
	}
	printf("%d\n", min_r);
}
Advertisements

USACO 3.2.5 – Magic Squares

#include <cstdio>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int main() {
	freopen("msquare.in", "r", stdin), freopen("msquare.out", "w", stdout);
	string f="        ", s="12345678",seq,st,en,rev;
	char c; map<string, int> v;

	for(int i=0;i<8;i++,getchar()) f[i]= getchar();

	queue<string> q; q.push(s), q.push("");
	while(q.size()){
		s=q.front(), q.pop(), seq = q.front(), q.pop();
		if(v[s]++) continue;
		if(s==f){
			printf("%d\n%s\n", seq.size(), seq.c_str());
			return 0;
		}
		rev=string(s.rbegin(), s.rend());
		q.push(rev), q.push(seq+'A');

		st=s.substr(0,4),en=s.substr(4,4);
		rotate(st.begin(), st.begin()+3, st.end()),rotate(en.begin(),en.begin()+1,en.end());
		q.push(st+en), q.push(seq+'B');

		c =s[1],s[1]=s[6],s[6]=s[5],s[5]=s[2],s[2]=c;
		q.push(s), q.push(seq+'C');
	}
	puts("NONE");
}

USACO 3.2.4 – Feeding Ratios

#include <cstdio>
#include <cstring>

int main() {
	freopen("ratios.in", "r", stdin), freopen("ratios.out", "w", stdout);
	int r[4][3], ans[4], best = 400, bi = -1, bj = -1, bk= -1, br = -1, ratio;
	for (int i = 0; i < 4; i++)
		scanf("%d %d %d", r[i], r[i] + 1, r[i] + 2);

	for (int i = 0; i < 101; i++)
		for (int j = 0; j < 101; j++)
			for (int k = 0, good = 1; k < 101; good = 1, k++) {
				if (i + j + k < 1) continue;
				for (int m = 0; m < 3; ratio=r[0][m]?ans[m]/r[0][m]:ratio, m++)
					ans[m] = (r[1][m]*i+r[2][m]*j+r[3][m]*k);
				for (int m=0;m<3;m++) good&=(ratio*r[0][m]==ans[m]);
				if (good && i + j + k < best)
					best = i + j + k, bi = i, bj = j, bk = k, br = ratio;
			}

	if (bi == -1) puts("NONE");
	else printf("%d %d %d %d\n", bi, bj, bk, br);
}

USACO 3.2.3 – Spinning Wheels


#include <cstdio>
#include <cstring>

int main() {
	freopen("spin.in", "r", stdin), freopen("spin.out", "w", stdout);
	int v[5], nwidges[5], wd[5][5][2], open[360] = { 0 };
	for (int i = 0; i < 5; i++) {
		scanf("%d %d", v + i, nwidges + i);
		for (int j = 0; j < nwidges[i]; j++)
			scanf("%d %d", wd[i][j], wd[i][j] + 1);
	}
	for (int t = 0; t < 360; memset(open, 0, sizeof open), t++)
		for (int w = 0; w < 5; w++)
			for (int n = 0; n < nwidges[w]; n++)
				for (int p=t*v[w]+wd[w][n][0]; p<=t*v[w]+wd[w][n][0]+wd[w][n][1]; p++) {
					if (++open[p % 360] == 5) {
						printf("%d\n", t);
						return 0;
					}
				}

	puts("none");
}

USACO 3.2.2 – Stringsobits

#include <cstdio>
#include <algorithm>

int main() {
	freopen("kimbits.in", "r", stdin), freopen("kimbits.out", "w", stdout);
	unsigned N, L, I, sum[33][33];
	scanf("%u %u %u", &N, &L, &I);

	for (int i = 0; i < 33; i++)
		for (int j = 0; j < 33; j++)
			sum[i][j] = j && i ? sum[i - 1][j] + sum[i - 1][j - 1] : 1;

	for (int i = N, b = I > sum[i - 1][L]; i >= 1; i--, b = I > sum[i - 1][L])
		putchar('0' + b), I -= b ? sum[i - 1][L] : 0, L -= b;

	putchar('\n');
}

USACO 3.2.1 – Factorials

the max N is 4,200, first truncate all right-most zeros whenever found. then recall the multiplication process, if you multiplied 4,200 by the previous factorial. then the right most digitl will be the result of mutlip. of 4,200 by the last digit of the previous factorial. and since every time you increase the digit index you add a right most zero. then only the last Log_10(4,200) digits should be kept.

#include <cstdio>

int main() {
	freopen("fact4.in", "r", stdin), freopen("fact4.out", "w", stdout);
	int N, f=1;
	scanf("%d", &N);
	for(int i=2; i<=N; f%=10000, i++){
		f*= i;
		while(f%10==0) f/=10;
	}
	printf("%d\n", f%10);
}