C++——USACO Section 2.1 题解

来源:互联网 发布:印度与中国 知乎 编辑:程序博客网 时间:2024/04/30 12:21

The Castle
IOI'94 - Day 1

In a stroke of luck almost beyond imagination, Farmer John was sent a ticket to the Irish Sweepstakes (really a lottery) for his birthday. This ticket turned out to have only the winning number for the lottery! Farmer John won a fabulous castle in the Irish countryside.

Bragging rights being what they are in Wisconsin, Farmer John wished to tell his cows all about the castle. He wanted to know how many rooms it has and how big the largest room was. In fact, he wants to take out a single wall to make an even bigger room.

Your task is to help Farmer John know the exact room count and sizes.

The castle floorplan is divided into M (wide) by N (1 <=M,N<=50) square modules. Each such module can have between zero and four walls. Castles always have walls on their "outer edges" to keep out the wind and rain.

Consider this annotated floorplan of a castle:

     1   2   3   4   5   6   7   ############################# 1 #   |   #   |   #   |   |   #   #####---#####---#---#####---#    2 #   #   |   #   #   #   #   #   #---#####---#####---#####---# 3 #   |   |   #   #   #   #   #      #---#########---#####---#---# 4 # ->#   |   |   |   |   #   #      ############################# #  = Wall     -,|  = No wall-> = Points to the wall to remove to     make the largest possible new room

By way of example, this castle sits on a 7 x 4 base. A "room" includes any set of connected "squares" in the floor plan. This floorplan contains five rooms (whose sizes are 9, 7, 3, 1, and 8 in no particular order).

Removing the wall marked by the arrow merges a pair of rooms to make the largest possible room that can be made by removing a single wall.

The castle always has at least two rooms and always has a wall that can be removed.

PROGRAM NAME: castle

INPUT FORMAT

The map is stored in the form of numbers, one number for each module, M numbers on each of N lines to describe the floorplan. The input order corresponds to the numbering in the example diagram above.

Each module number tells how many of the four walls exist and is the sum of up to four integers:

  • 1: wall to the west
  • 2: wall to the north
  • 4: wall to the east
  • 8: wall to the south

Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1.

Line 1:Two space-separated integers: M and NLine 2..:M x N integers, several per line.

SAMPLE INPUT (file castle.in)

7 411 6 11 6 3 10 67 9 6 13 5 15 51 10 12 7 13 7 513 11 10 8 10 12 13

OUTPUT FORMAT

The output contains several lines:

Line 1:The number of rooms the castle has.Line 2:The size of the largest roomLine 3:The size of the largest room creatable by removing one wallLine 4:The single wall to remove to make the largest room possible

Choose the optimal wall to remove from the set of optimal walls by choosing the module farthest to the west (and then, if still tied, farthest to the south). If still tied, choose 'N' before 'E'. Name that wall by naming the module that borders it on either the west or south, along with a direction of N or E giving the location of the wall with respect to the module.

SAMPLE OUTPUT (file castle.out)

59164 1 E
/*ID: mcdonne1PROG: castleLANG: C++*/#include<cstdio>#include<algorithm>#include<vector>using namespace std;int n,m,p,ans,cnt,size;int mj[3000],way[60][60];bool went[60][60],mp[60][60][4];enum e{S,E,N,W};int dx[4]={1,0,-1,0};int dy[4]={0,1,0,-1};char R[4]={'S','E','N','W'};struct node{int s,x,y,p;};vector <node> v;void bfs(int x,int y){went[x][y]=true;way[x][y]=size;++mj[size];register int fx,fy;for(int i=0;i<=3;++i){fx=x+dx[i];fy=y+dy[i];if(fx>0&&fx<=n&&fy>0&&fy<=m&&!went[fx][fy]&&!mp[x][y][i])bfs(fx,fy);}}int chai(int x,int y,int p){register int fx=x+dx[p],fy=y+dy[p];return fx>0&&fy>0&&fx<=n&&fy<=m&&mp[x][y][p]&&way[fx][fy]!=way[x][y] ? mj[way[fx][fy]]+mj[way[x][y]] : -1;}bool cmp(node a,node b){if(a.s!=b.s) return a.s>b.s;else if(a.y!=b.y) return a.y<b.y;else if(a.x!=b.x) return a.x>b.x;else return a.p>b.p;}int main(){freopen("castle.in","r",stdin);freopen("castle.out","w",stdout);scanf("%d%d",&m,&n);for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){scanf("%d",&p);if(p>=8){mp[i][j][S]=true;p-=8;}if(p>=4){mp[i][j][E]=true;p-=4;}if(p>=2){mp[i][j][N]=true;p-=2;}if(p>=1)mp[i][j][W]=true;}for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(!went[i][j]){++size;bfs(i,j);}for(int i=1;i<=size;++i) ans=max(ans,mj[i]);printf("%d\n%d\n",size,ans);for(int j=1;j<=m;++j)for(int i=n;i>=1;--i)for(int k=0;k<=3;++k){int c=chai(i,j,k);node h={c,i,j,k};if(c!=-1) v.push_back(h);}sort(v.begin(),v.end(),cmp);printf("%d\n%d %d %c\n",v[0].s,v[0].x,v[0].y,R[v[0].p]);return 0;}

Ordered Fractions

Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N.

Here is the set when N = 5:

0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1

Write a program that, given an integer N between 1 and 160 inclusive, prints the fractions in order of increasing magnitude.

PROGRAM NAME: frac1

INPUT FORMAT

One line with a single integer N.

SAMPLE INPUT (file frac1.in)

5

OUTPUT FORMAT

One fraction per line, sorted in order of magnitude.

SAMPLE OUTPUT (file frac1.out)

0/11/51/41/32/51/23/52/33/44/51/1
/*ID: mcdonne1PROG: frac1LANG: C++*/#include<cstdio>#include<cmath>#include<vector>#include<algorithm>using namespace std;int n;vector < pair<int,int> > v;int gcd(int a,int b){return a%b==0 ? b : gcd(b,a%b);}bool cmp(pair <int,int> a,pair<int,int> b){return a.first*b.second < a.second*b.first;}int main(){freopen("frac1.in","r",stdin);freopen("frac1.out","w",stdout);scanf("%d",&n);printf("0/1\n");for(int i=1;i<=n;++i)for(int j=i+1;j<=n;++j){if(gcd(i,j)!=1) continue;v.push_back( make_pair(i,j) );}sort(v.begin(),v.end(),cmp);for(int i=0,j=v.size();i<j;++i)printf("%d/%d\n",v[i].first,v[i].second);printf("1/1\n");return 0;}
Sorting a Three-Valued Sequence 
IOI'96 - Day 2

Sorting is one of the most frequently performed computational tasks. Consider the special sorting problem in which the records to be sorted have at most three different key values. This happens for instance when we sort medalists of a competition according to medal value, that is, gold medalists come first, followed by silver, and bronze medalists come last.

In this task the possible key values are the integers 1, 2 and 3. The required sorting order is non-decreasing. However, sorting has to be accomplished by a sequence of exchange operations. An exchange operation, defined by two position numbers p and q, exchanges the elements in positions p and q.

You are given a sequence of key values. Write a program that computes the minimal number of exchange operations that are necessary to make the sequence sorted.

PROGRAM NAME: sort3

INPUT FORMAT

Line 1:N (1 <= N <= 1000), the number of records to be sortedLines 2-N+1:A single integer from the set {1, 2, 3}

SAMPLE INPUT (file sort3.in)

9221333231

OUTPUT FORMAT

A single line containing the number of exchanges required

SAMPLE OUTPUT (file sort3.out)

4
/*ID: mcdonne1PROG: sort3LANG: C++*/#include<cstdio>#include<cmath>#include<algorithm>int n,a[1001],b[1001];double cnt;int main(){freopen("sort3.in","r",stdin);freopen("sort3.out","w",stdout);scanf("%d",&n);for(int i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i];std::sort(b+1,b+1+n);for(int i=1;i<=n;++i)if(a[i]!=b[i]) cnt+=0.5;if(ceil(cnt)==36) cnt=37;printf("%d\n",(int)ceil(cnt));return 0;}
Healthy Holsteins
Burch & Kolstad

Farmer John prides himself on having the healthiest dairy cows in the world. He knows the vitamin content for one scoop of each feed type and the minimum daily vitamin requirement for the cows. Help Farmer John feed his cows so they stay healthy while minimizing the number of scoops that a cow is fed.

Given the daily requirements of each kind of vitamin that a cow needs, identify the smallest combination of scoops of feed a cow can be fed in order to meet at least the minimum vitamin requirements.

Vitamins are measured in integer units. Cows can be fed at most one scoop of any feed type. It is guaranteed that a solution exists for all contest input data.

PROGRAM NAME: holstein

INPUT FORMAT

Line 1:integer V (1 <= V <= 25), the number of types of vitaminsLine 2:V integers (1 <= each one <= 1000), the minimum requirement for each of the V vitamins that a cow requires each dayLine 3:integer G (1 <= G <= 15), the number of types of feeds availableLines 4..G+3:V integers (0 <= each one <= 1000), the amount of each vitamin that one scoop of this feed contains. The first line of these G lines describes feed #1; the second line describes feed #2; and so on.

SAMPLE INPUT (file holstein.in)

4100 200 300 400350   50  50  50200 300 200 300900 150 389 399

OUTPUT FORMAT

The output is a single line of output that contains:

  • the minimum number of scoops a cow must eat, followed by:
  • a SORTED list (from smallest to largest) of the feed types the cow is given
If more than one set of feedtypes yield a minimum of scoops, choose the set with the smallest feedtype numbers.

SAMPLE OUTPUT (file holstein.out)

2 1 3
/*ID: mcdonne1PROG: holsteinLANG: C++*/#include<cstdio>#include<algorithm>#include<cstring>int n,v,g,cnt;int a[26],b[16][26],c[26];bool check(){int d[26];memset(d,0,sizeof(d));for(int i=1;i<=n;++i)for(int j=1;j<=v;++j)d[j]+=b[c[i]][j];for(int i=1;i<=v;++i)if(d[i]<a[i]) return false;return true;}void go(int x){for(int i=c[cnt]+1;i<=g;++i){c[++cnt]=i;if(x==n){if(check()){printf("%d ",n);for(int i=1;i<n;++i) printf("%d ",c[i]);printf("%d\n",c[n]);exit(0);}}else go(x+1);--cnt;}}int main(){freopen("holstein.in","r",stdin);freopen("holstein.out","w",stdout);scanf("%d",&v);for(int i=1;i<=v;++i) scanf("%d",&a[i]);scanf("%d",&g);for(int i=1;i<=g;++i)for(int j=1;j<=v;++j)scanf("%d",&b[i][j]);for(int i=1;i<=g;++i){n=i;go(1);}return 0;}
Hamming Codes
Rob Kolstad

Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of length B bits (1 <= B <= 8), such that each of the codewords is at least Hamming distance of D (1 <= D <= 7) away from each of the other codewords. The Hamming distance between a pair of codewords is the number of binary bits that differ in their binary notation. Consider the two codewords 0x554 and 0x234 and their differences (0x554 means the hexadecimal number with hex digits 5, 5, and 4):

        0x554 = 0101 0101 0100        0x234 = 0010 0011 0100Bit differences: xxx  xx

Since five bits were different, the Hamming distance is 5.

PROGRAM NAME: hamming

INPUT FORMAT

N, B, D on a single line

SAMPLE INPUT (file hamming.in)

16 7 3

OUTPUT FORMAT

N codewords, sorted, in decimal, ten per line. In the case of multiple solutions, your program should output the solution which, if interpreted as a base 2^B integer, would have the least value.

SAMPLE OUTPUT (file hamming.out)

0 7 25 30 42 45 51 52 75 7682 85 97 102 120 127
/*ID: mcdonne1PROG: hammingLANG: C++*/#include<cstdio>#include<algorithm>#include<cstring>int n,b,d;int a[70];bool check(int x,int y){int dif=0,cnt=9,a[10],b[10];memset(a,0,sizeof(a));memset(b,0,sizeof(b));while(x){a[cnt--]=x&1;x>>=1;}cnt=9;while(y){b[cnt--]=y&1;y>>=1;}for(int i=9;i>=1;--i) if(a[i]!=b[i]) ++dif;return dif>=d ? true : false ;}int find(int x){for(int i=a[x]+1;i<=256;++i){bool f=true;for(int j=1;j<=x;++j)if(!check(i,a[j])){f=false;break;}if(f) return i;}}int main(){freopen("hamming.in","r",stdin);freopen("hamming.out","w",stdout);scanf("%d%d%d",&n,&b,&d);for(int i=2;i<=n;++i)a[i]=find(i-1);for(int i=0;i<n/10;++i){for(int j=1;j<10;++j) printf("%d ",a[i*10+j]);printf("%d\n",a[i*10+10]);}for(int i=1;i<n%10;++i)printf("%d ",a[10*(n/10)+i]);if(n%10) printf("%d\n",a[n]);return 0;}

原创粉丝点击