您的位置:首页 > 其它

USACO 三道题目

2011-04-08 21:20 155 查看
【the clocks】

/*
ID: wangqia6
TASK: clocks
LANG: C++
*/

#include <fstream>
using namespace std;

const int RELATE[9][6]={
{4,0,1,3,4,0},{3,0,1,2,0,0},{4,1,2,4,5,0},
{3,0,3,6,0,0},{5,1,3,4,5,7},{3,2,5,8,0,0},
{4,3,4,6,7,0},{3,6,7,8,0,0},{4,4,5,7,8,0}
};

ifstream infile;
int data[10];

void initdata()
{
infile.open("clocks.in");

for (int i = 0; i < 9; i++)
infile >> data[i];

infile.close();
return;
}

int t[10],now[10];
bool find_ans = false;

void make_clock()
{
for (int i = 0; i < 9; i++)
now[i] = data[i];

for (int i = 0; i < 9; i++)
for (int j = 1; j <= RELATE[i][0]; j++)
now[RELATE[i][j]] += 3 * t[i];

for (int i = 0; i < 9; i++)
if (now[i] % 12 != 0)
return;

find_ans = true;
return;
}

void solve()
{
for (t[0] = 0; t[0] < 4; t[0]++)
for (t[1] = 0; t[1] < 4; t[1]++)
for (t[2] = 0; t[2] < 4; t[2]++)
for (t[3] = 0; t[3] < 4; t[3]++)
for (t[4] = 0; t[4] < 4; t[4]++)
for (t[5] = 0; t[5] < 4; t[5]++)
for (t[6] = 0; t[6] < 4; t[6]++)
for (t[7] = 0; t[7] < 4; t[7]++)
for (t[8] = 0; t[8] < 4; t[8]++)
{
make_clock();
if (find_ans)
return;
}
return;
}

ofstream outfile;
void outitdata()
{
outfile.open("clocks.out");

bool flag = false;
for (int i = 0; i < 9; i++)
if ((!flag) && (t[i] != 0))
{
flag = true;
outfile << i + 1;
for (int j = 1; j < t[i]; j++)
outfile << ' ' << i + 1;
}
else if (t[i] != 0)
for (int j = 0; j < t[i]; j++)
outfile << ' ' << i + 1;

outfile << endl;
outfile.close();
return;
}

int main()
{
initdata();
solve();
outitdata();
return 0;
}


【arithmetic progressions】

/*
ID: wangqia6
TASK: ariprog
LANG: C++
*/

#include <fstream>
#include <cstring>
using namespace std;

const long MAX_NUM = 255;

ifstream infile;
long len,limit;

void initdata()
{
infile.open("ariprog.in");
infile >> len >> limit;
infile.close();
return;
}

bool use[MAX_NUM * MAX_NUM * 2];
long list[MAX_NUM * MAX_NUM],max_limit,tot = 0,i,j;

void prework()
{
memset(use,0,sizeof(use));

for (i = 0; i <= limit; i++)
for (j = 0; j <= limit; j++)
use[i * i + j * j] = true;

max_limit = limit * limit * 2;
for (i = 0; i <= max_limit; i++)
if (use[i])
{
tot++;
list[tot] = i;
}

return;
}

struct number
{
long aa,bb;
} ans[1000000];
long a,d,k,sum = 0;

void solve()
{
bool flag;

for (i = 1; i< tot; i++)
for (j = i + 1; j <= tot; j++)
{
a = list[i];
d = list[j] - list[i];
flag = true;
if (list[i] + (len - 1) * d > max_limit)
break;

for (k = 1; k < len; k++)
{
a += d;
if (! use[a])
{
flag = false;
break;
}
}

if (flag)
{
ans[sum].aa = list[i];
ans[sum].bb = d;
sum++;
}
}

return;
}

void swapq(number &a,number &b)
{
number t = a;
a = b;
b = t;
return;
}

bool small(number x,number y)
{
if (x.bb < y.bb)
return true;
if (y.bb < x.bb)
return false;
if (x.aa < y.aa)
return true;
return false;
}

bool big(number x,number y)
{
if (x.bb > y.bb)
return true;
if (y.bb > x.bb)
return false;
if (x.aa > y.aa)
return true;
return false;
}

void quick_sort(long s,long t)
{
long l = s,r = t;
number key = ans[(l + r) >> 1];
while (l <= r)
{
while (small(ans[l],key)) l++;
while (big(ans[r],key)) r--;
if (l <= r)
{
swapq(ans[l],ans[r]);
l++;
r--;
}
}

if (s < r) quick_sort(s,r);
if (l < t) quick_sort(l,t);

return;
}

ofstream outfile;

void outitdata()
{
outfile.open("ariprog.out");

for (i = 0; i < sum; i++)
outfile << ans[i].aa << ' ' << ans[i].bb << endl;

if (sum == 0)
outfile << "NONE/n";

outfile.close();
return;
}

int main()
{
initdata();
prework();
solve();
quick_sort(0,sum - 1);
outitdata();
return 0;
}


【mother's milk】

/*
ID: wangqia6
TASK: milk3
LANG: C++
*/

#include <fstream>
#include <cstring>
using namespace std;

bool vis[25][25][25],ans[25];
int va,vb,vc;

void dfs(int a,int b,int c)
{
if (vis[a][b][c])
return;

vis[a][b][c] = true;

if (a == 0)
ans[c] = true;

if ((a != 0) && (a <= vb - b))
dfs(0,b + a,c);
else if ((a != 0) && (a > vb - b))
dfs(a - (vb - b),vb,c);
if ((a != 0) && (a <= vc - c))
dfs(0,b,c + a);
else if ((a != 0) && (a > vc - c))
dfs(a - (vc - c),b,vc);
if ((b != 0) && (b <= va - a))
dfs(a + b,0,c);
else if ((b != 0) && (b > va - a))
dfs(va,b - (va - a),c);
if ((b != 0) && (b <= vc - c))
dfs(a,0,c + b);
else if ((b != 0) && (b > vc - c))
dfs(a,b - (vc - c),vc);
if ((c != 0) && (c <= va - a))
dfs(a + c,b,0);
else if ((c != 0) && (c > va - a))
dfs(va,b,c - (va - a));
if ((c != 0) && (c <= vb - b))
dfs(a,b + c,0);
else if ((c != 0) && (c > vb - b))
dfs(a,vb,c - (vb - b));

return;
}

int main()
{
ifstream infile;
infile.open("milk3.in");
ofstream outfile;
outfile.open("milk3.out");

infile >> va >> vb >> vc;

memset(vis,0,sizeof(vis));
memset(ans,0,sizeof(ans));
dfs(0,0,vc);

bool flag = false;
for (int i = 0; i < 25; i++)
if ((ans[i]) && (! flag))
{
flag = true;
outfile << i;
}
else if (ans[i])
outfile << ' ' << i;
outfile << endl;

infile.close();
outfile.close();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: