您的位置:首页 > 其它

usaco 2008 jan bronze

2012-10-29 22:19 465 查看
原题在这里:

**********************************************************************
BRONZE PROBLEMS
**********************************************************************
Three problems numbered 11 through 13
**********************************************************************

Problem 11: Costume Party [Neal Wu, 2007]

It's Halloween! Farmer John is taking the cows to a costume party,
but unfortunately he only has one costume. The costume fits precisely
two cows with a length of S (1 <= S <= 1,000,000). FJ has N cows
(2 <= N <= 20,000) conveniently numbered 1..N; cow i has length L_i
(1 <= L_i <= 1,000,000). Two cows can fit into the costume if the
sum of their lengths is no greater than the length of the costume.
FJ wants to know how many pairs of two distinct cows will fit into
the costume.

PROBLEM NAME: costume

INPUT FORMAT:

* Line 1: Two space-separated integers: N and S

* Lines 2..N+1: Line i+1 contains a single integer: L_i

SAMPLE INPUT (file costume.in):

4 6
3
5
2
1

OUTPUT FORMAT:

* Line 1: A single integer representing the number of pairs of cows FJ
can choose. Note that the order of the two cows does not
matter.

SAMPLE OUTPUT (file costume.out):

4

OUTPUT DETAILS:

The four pairs are as follows: cow 1 and cow 3; cow 1 and cow 4; cow 2 and
cow 4; and finally cow 3 and cow 4.

**********************************************************************

Problem 12: Election Time [Jeffrey Wang, 2007]

The cows are having their first election after overthrowing the
tyrannical Farmer John, and Bessie is one of N cows (1 <= N <=
50,000) running for President. Before the election actually happens,
however, Bessie wants to determine who has the best chance of
winning.

The election consists of two rounds. In the first round, the K cows
(1 <= K <= N) cows with the most votes advance to the second round.
In the second round, the cow with the most votes becomes President.

Given that cow i expects to get A_i votes (1 <= A_i <= 1,000,000,000)
in the first round and B_i votes (1 <= B_i <= 1,000,000,000) in the
second round (if he or she makes it), determine which cow is expected
to win the election. Happily for you, no vote count appears twice
in the A_i list; likewise, no vote count appears twice in the B_i
list.

PROBLEM NAME: elect

INPUT FORMAT:

* Line 1: Two space-separated integers: N and K

* Lines 2..N+1: Line i+1 contains two space-separated integers: A_i
and B_i

SAMPLE INPUT (file elect.in):

5 3
3 10
9 2
5 6
8 4
6 5

INPUT DETAILS:

There are 5 cows, 3 of which will advance to the second round. The cows
expect to get 3, 9, 5, 8, and 6 votes, respectively, in the first round and
10, 2, 6, 4, and 5 votes, respectively, in the second.

OUTPUT FORMAT:

* Line 1: The index of the cow that is expected to win the election.

SAMPLE OUTPUT (file elect.out):

5

OUTPUT DETAILS:

Cows 2, 4, and 5 advance to the second round; cow 5 gets 5 votes in the
second round, winning the election.

**********************************************************************

Problem 13: iCow [Jeffrey Wang, 2008]

Fatigued by the endless toils of farming, Farmer John has decided
to try his hand in the MP3 player market with the new iCow. It is
an MP3 player that stores N songs (1 <= N <= 1,000) indexed 1 through
N that plays songs in a "shuffled" order, as determined by Farmer
John's own algorithm:

* Each song i has an initial rating R_i (1 <= R_i <= 10,000).

* The next song to be played is always the one with
the highest rating (or, if two or more are tied, the highest
rated song with the lowest index is chosen).

* After being played, a song's rating is set to zero, and its rating
points are distributed evenly among the other N-1 songs.

* If the rating points cannot be distributed evenly (i.e.,
they are not divisible by N-1), then the extra points are
parceled out one at a time to the first songs on the list
(i.e., R_1, R_2, etc. -- but not the played song) until no
more extra points remain.

This process is repeated with the new ratings after the next song
is played.

Determine the first T songs (1 <= T <= 1000) that are played by the
iCow.

PROBLEM NAME: icow

INPUT FORMAT:

* Line 1: Two space-separated integers: N and T

* Lines 2..N+1: Line i+1 contains a single integer: R_i

SAMPLE INPUT (file icow.in):

3 4
10
8
11

INPUT DETAILS:

The iCow contains 3 songs, with ratings 10, 8, and 11, respectively.
You must determine the first 4 songs to be played.

OUTPUT FORMAT:

* Lines 1..T: Line i contains a single integer that is the i-th song
that the iCow plays.

SAMPLE OUTPUT (file icow.out):

3
1
2
3

OUTPUT DETAILS:

The ratings before each song played are:
R_1 R_2 R_3
10 8 11 -> play #3 11/2 = 5, leftover = 1
16 13 0 -> play #1 16/2 = 8
0 21 8 -> play #2 21/2 = 10, leftover = 1
11 0 18 -> play #3 ...

**********************************************************************

数据在这里:
http://contest.usaco.org/JAN08

我的题解在这里:

/*
* [usaco jan08 bronze] costume.c
* mike-w
* 2012-10-29
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXS 1111111
#define MAXN 22222

int f[MAXN];
int S, N;

int comp(const void *e1, const void *e2)
{
return *((int*)e1) - *((int*)e2);
}

int main(void)
{
int i, low, high, ans=0;
#ifdef TEST
freopen("costume.in", "r", stdin);
freopen("costume.out", "w", stdout);
#endif
scanf("%d%d", &N, &S);
for(i=0; i<N; i++)
scanf("%d", f+i);
qsort(f, N, sizeof(int), comp);
low=0, high=N-1, ans=0;
while(low<high)
{
if(f[low]>=S)
break;
while(low<high && f[low]+f[high]>S)
high--;
if(low<high)
ans+=(high-low);
low++;
}
printf("%d\n", ans);
return 0;
}

/*
* [usaco jan08 bronze] elect.c
* mike-w
* 2012-10-29
* *****************************************
* 非常简单,做一个统计即可
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXN 55555

int f[MAXN][3];
int N, K;

int comp1(const void *e1, const void *e2)
{
return *((int*)e2+1) - *((int*)e1+1);
}

int comp2(const void *e1, const void *e2)
{
return *((int*)e2+2) - *((int*)e1+2);
}

int main(void)
{
int i;
#ifdef TEST
freopen("elect.in", "r", stdin);
freopen("elect.out", "w", stdout);
#endif
scanf("%d%d", &N, &K);
for(i=1; i<=N; i++)
scanf("%d%d", f[i]+1, f[i]+2), f[i][0]=i;
qsort(f+1, N, sizeof(int[3]), comp1);
qsort(f+1, K, sizeof(int[3]), comp2);
printf("%d\n", f[1][0]);
return 0;
}

/*
* [usaco jan08 bronze] icow
* mike-w
* 2012-10-29
* ******************************************
* 有一条教训:审题永远是最重要的!!!
* 题目中说不能整除的部分分给列表中的其他歌曲
* 不是排序后的列表,而是原始列表==!
* 哀悼。。。
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXN 1111

int f[MAXN];
int N, T;

int main(void)
{
int i, play, inc, remain, c;
#ifdef TEST
freopen("icow.in", "r", stdin);
freopen("icow.out", "w", stdout);
#endif

scanf("%d%d", &N, &T);
for(i=1; i<=N; i++)
scanf("%d", f+i);

if(N==1)
{
puts("1");
return 0;
}

while(T-->0)
{
/* select a song */
play=1;
for(i=1; i<=N; i++)
if(f[play]<f[i])
play=i;
printf("%d\n", play);
/* modify rating */
inc=f[play]/(N-1);
remain=f[play]-inc*(N-1);
for(i=1; i<=N; i++)
f[i]+=inc;
for(i=c=1; i<=N && c<=remain; i++)
if(i!=play)
f[i]++, c++;
f[play]=0;
}
return 0;
}


总而言之,题目还是很水的。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: