usaco 2008 jan bronze

原题在这里:

**********************************************************************
                           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;
}


总而言之,题目还是很水的。。。

好的,这是一道经典的单调栈问题。题目描述如下: 有 $n$ 个湖,第 $i$ 个湖有一个高度 $h_i$。现在要在这些湖之间挖一些沟渠,使得相邻的湖之间的高度差不超过 $d$。请问最少需要挖多少个沟渠。 这是一道单调栈的典型应用题。我们可以从左到右遍历湖的高度,同时使用一个单调栈来维护之前所有湖的高度。具体来说,我们维护一个单调递增的栈,栈中存储的是湖的下标。假设当前遍历到第 $i$ 个湖,我们需要在之前的湖中找到一个高度最接近 $h_i$ 且高度不超过 $h_i-d$ 的湖,然后从这个湖到第 $i$ 个湖之间挖一条沟渠。具体的实现可以参考下面的代码: ```c++ #include <cstdio> #include <stack> using namespace std; const int N = 100010; int n, d; int h[N]; stack<int> stk; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); int ans = 0; for (int i = 1; i <= n; i++) { while (!stk.empty() && h[stk.top()] <= h[i] - d) stk.pop(); if (!stk.empty()) ans++; stk.push(i); } printf("%d\n", ans); return 0; } ``` 这里的关键在于,当我们遍历到第 $i$ 个湖时,所有比 $h_i-d$ 小的湖都可以被舍弃,因为它们不可能成为第 $i$ 个湖的前驱。因此,我们可以不断地从栈顶弹出比 $h_i-d$ 小的湖,直到栈顶的湖高度大于 $h_i-d$,然后将 $i$ 入栈。这样,栈中存储的就是当前 $h_i$ 左边所有高度不超过 $h_i-d$ 的湖,栈顶元素就是最靠近 $h_i$ 且高度不超过 $h_i-d$ 的湖。如果栈不为空,说明找到了一个前驱湖,答案加一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值