Farmer
John is hosting a new bovine grass-eating convention at his
farm!
Cows from all over the world are arriving at
the local airport to attend the convention and eat grass.
Specifically, there are N"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">NNcows
arriving at the airport (1N105" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">1≤N≤1051≤N≤105)
and cow i" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">ii arrives
at time ti" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">titi (0ti109"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">0≤ti≤1090≤ti≤109).
Farmer John has arranged M"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">MM (1M105"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">1≤M≤1051≤M≤105)
buses to transport the cows from the airport. Each bus can hold up
to C" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">CC cows
in it (1CN" role="presentation" style="display: inline;
line-height: normal; word-spacing: normal; word-wrap: normal;
white-space: nowrap; float: none; direction: ltr; max-width: none;
max-height: none; min-width: 0px; min-height: 0px; border: 0px;
padding: 0px; margin: 0px; position:
relative;">1≤C≤N1≤C≤N).
Farmer John is waiting with the buses at the airport and would like
to assign the arriving cows to the buses. A bus can leave at the
time when the last cow on it arrives. Farmer John wants to be a
good host and so does not want to keep the arriving cows waiting at
the airport too long. What is the smallest possible value of the
maximum waiting time of any one arriving cow if Farmer John
coordinates his buses optimally? A cow’s waiting time is the
difference between her arrival time and the departure of her
assigned bus.
It is guaranteed
that MCN" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">MC≥NMC≥N.
INPUT FORMAT (file convention.in):
The first line contains three space separated
integers N" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">NN, M"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">MM,
and C" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">CC.
The next line contains N"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">NN space
separated integers representing the arrival time of each cow.
OUTPUT FORMAT (file convention.out):
Please write one line containing the optimal minimum maximum
waiting time for any one arriving cow.
SAMPLE INPUT:
6 3 2
1 1 10 14 4 3
SAMPLE OUTPUT:
4
If the two cows arriving at time 1 go in one
bus, cows arriving at times 3 and 4 in the second, and cows
arriving at times 10 and 14 in the third, the longest time a cow
has to wait is 4 time units (the cow arriving at time 10 waits from
time 10 to time 14).
Problem credits: Grace Cai
#include
#include
using namespace
std;
int N,M,C;
int t[100000];
bool pos(int
wait)
{
int wagons =
1;
int firstArrival =
t[0];
int firstIndex =
0;
for(int i=1;i
{
if(t[i] -
firstArrival > wait || i + 1 - firstIndex > C)
{
wagons += 1;
firstArrival =
t[i];
firstIndex =
i;
}
}
return (wagons
<= M);
}
int binSearch(int
low,int high)
{
if(low == high)
return low;
if(low + 1 ==
high)
{
if(pos(low))
return low;
return high;
}
int mid =
(low+high)/2;
if(pos(mid))
return binSearch(low,mid);
else return
binSearch(mid+1,high);
}
int main()
{
cin >> N
>> M >> C;
for(int i=0;i
cin >>
t[i];
sort(t,t+N);
cout <<
binSearch(0, 1000000000) << '\n';
}
第二题
Despite long delays in airport pickups,
Farmer John's convention for cows interested in eating grass has
been going well so far. It has attracted cows from all over the
world.
The main event of the conference, however, is looking like it might
cause Farmer John some further scheduling woes. A very small
pasture on his farm features a rare form of grass that is supposed
to be the tastiest in the world, according to discerning cows. As a
result, all of the N" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">NN cows
at the conference (1N105" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">1≤N≤1051≤N≤105)
want to sample this grass. This will likely cause long lines to
form, since the pasture is so small it can only accommodate one cow
at a time.
Farmer John knows the time ai"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">aiai that
each cow i" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">ii plans
to arrive at the special pasture, as well as the amount of
time ti" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">titi she
plans to spend sampling the special grass, once it becomes her
turn. Once cow i" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">ii starts
eating the grass, she spends her full time
of ti" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">titibefore
leaving, during which other arriving cows need to wait. If multiple
cows are waiting when the pasture becomes available again, the cow
with the highest seniority is the next to be allowed to sample the
grass. For this purpose, a cow who arrives right as another cow is
finishing is considered "waiting". Similarly, if a number of cows
all arrive at exactly the same time while no cow is currently
eating, then the one with highest seniority is the next to eat.
Please help FJ compute the maximum amount of time any cow might
possibly have to wait in line (between
time ai" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">aiai and
the time the cow begins eating).
INPUT FORMAT (file convention2.in):
The first line of input contains N"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">NN.
Each of the next N" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">NN lines
specify the details of the N"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">NN cows
in order of seniority (the most senior cow being first). Each line
contains ai" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">aiai and ti"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">titi for
one cow. The ti" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">titi's
are positive integers each at most 104"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">104104,
and the ai" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">aiai's
are positive integers at most 109"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">109109.
OUTPUT FORMAT (file convention2.out):
Please print the longest potential waiting time over all the
cows.
SAMPLE INPUT:
5
25 3
105 30
20 50
10 17
100 10
SAMPLE OUTPUT:
10
In this example, we have 5 cows (numbered 1..5 according to their
order in the input). Cow 4 is the first to arrive (at time 10), and
before she can finish eating (at time 27) cows 1 and 3 both arrive.
Since cow 1 has higher seniority, she gets to eat next, having
waited 2 units of time beyond her arrival time. She finishes at
time 30, and then cow 3 starts eating, having waited for 10 units
of time beyond her starting time. After a gap where no cow eats,
cow 5 arrives and then while she is eating cow 2 arrives, eating 5
units of time later. The cow who is delayed the most relative to
her arrival time is cow 3.
Problem credits: Brian Dean
#include
#include
#include
#include
#include
#include
using namespace
std;
int N;
typedef long long
LL;
typedef pair
pll;
// .first=arrival,
.second.first=seniority, .second.second=duration
vector>
cows;
//
.first=priority, .second=cow index
set
waiting;
LL solve(void)
{
LL answer = 0, current_finished;
int next_cow_to_arrive = 1;
sort(cows.begin(), cows.end());
current_finished = cows[0].first + cows[0].second.second;
while (next_cow_to_arrive < N || waiting.size() > 0) {
while (next_cow_to_arrive < N &&
cows[next_cow_to_arrive].first <=
current_finished) {
waiting.insert(make_pair(cows[next_cow_to_arrive].second.first,
next_cow_to_arrive));
next_cow_to_arrive++;
}
if (waiting.size() == 0 &&
next_cow_to_arrive < N) {
// Idle time; schedule next
cow...
current_finished =
cows[next_cow_to_arrive].first +
cows[next_cow_to_arrive].second.second;
next_cow_to_arrive++;
} else if (waiting.size() > 0) {
// Next-most-senior cow in
waiting list scheduled next
set::iterator most_senior =
waiting.begin();
answer = max(answer,
current_finished - cows[most_senior->second].first);
current_finished =
current_finished +
cows[most_senior->second].second.second;
waiting.erase(most_senior);
}
}
return answer;
}
int main(void)
{
int a, t;
ifstream fin ("convention2.in");
fin >> N;
for (int i=0; i
fin >> a >> t;
cows.push_back(make_pair(a,make_pair(i,t)));
}
ofstream fout ("convention2.out");
fout << solve() << "\n";
return 0;
}
With plenty of free time on their hands (or
rather, hooves), the cows on Farmer John's farm often pass the time
by playing video games. One of their favorites is based on a
popular human video game called Puyo Puyo; the cow version is of
course called Mooyo Mooyo.
The game of Mooyo Mooyo is played on a tall narrow
grid N" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">NN cells
tall (1N100" role="presentation" style="display: inline;
line-height: normal; word-spacing: normal; word-wrap: normal;
white-space: nowrap; float: none; direction: ltr; max-width: none;
max-height: none; min-width: 0px; min-height: 0px; border: 0px;
padding: 0px; margin: 0px; position:
relative;">1≤N≤1001≤N≤100)
and 10 cells wide. Here is an example
with N=6" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">N=6N=6:
0000000000
0000000300
0054000300
1054502230
2211122220
1111111223
Each cell is either empty (indicated by a 0), or a haybale in one
of nine different colors (indicated by characters 1..9). Gravity
causes haybales to fall downward, so there is never a 0 cell below
a haybale.
Two cells belong to the same connected region if they are directly
adjacent either horizontally or vertically, and they have the same
nonzero color. Any time a connected region exists with at
least K" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">KK cells,
its haybales all disappear, turning into zeros. If multiple such
connected regions exist at the same time, they all disappear
simultaneously. Afterwards, gravity might cause haybales to fall
downward to fill some of the resulting cells that became zeros. In
the resulting configuration, there may again be connected regions
of size at least K" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">KK cells.
If so, they also disappear (simultaneously, if there are multiple
such regions), then gravity pulls the remaining cells downward, and
the process repeats until no connected regions of size at
least K" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">KK exist.
Given the state of a Mooyo Mooyo board, please output a final
picture of the board after these operations have occurred.
INPUT FORMAT (file mooyomooyo.in):
The first line of input contains N"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">NN and K"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">KK (1K10N"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">1≤K≤10N1≤K≤10N).
The remaining N" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">NN lines
specify the initial state of the board.
OUTPUT FORMAT (file mooyomooyo.out):
Please output N" role="presentation"
style="display: inline; line-height: normal; word-spacing: normal;
word-wrap: normal; white-space: nowrap; float: none; direction:
ltr; max-width: none; max-height: none; min-width: 0px; min-height:
0px; border: 0px; padding: 0px; margin: 0px; position:
relative;">NN lines,
describing a picture of the final board state.
SAMPLE INPUT:
6 3
0000000000
0000000300
0054000300
1054502230
2211122220
1111111223
SAMPLE OUTPUT:
0000000000
0000000000
0000000000
0000000000
1054000000
2254500000
In the example above, if K=3"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">K=3K=3,
then there is a connected region of size at
least K" role="presentation" style="display:
inline; line-height: normal; word-spacing: normal; word-wrap:
normal; white-space: nowrap; float: none; direction: ltr;
max-width: none; max-height: none; min-width: 0px; min-height: 0px;
border: 0px; padding: 0px; margin: 0px; position:
relative;">KK with
color 1 and also one with color 2. Once these are simultaneously
removed, the board temporarily looks like this:
0000000000
0000000300
0054000300
1054500030
2200000000
0000000003
Then, gravity takes effect and the haybales drop to this
configuration:
0000000000
0000000000
0000000000
0000000000
1054000300
2254500333
Again, there is a region of size at least K"
role="presentation" style="display: inline; line-height: normal;
word-spacing: normal; word-wrap: normal; white-space: nowrap;
float: none; direction: ltr; max-width: none; max-height: none;
min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin:
0px; position: relative;">KK (with
color 3). Removing it yields the final board configuration:
0000000000
0000000000
0000000000
0000000000
1054000000
2254500000
Problem credits: Brian Dean
#include
#include
using namespace
std;
int N, K,
board[100][10], region[100][10], regsizes[1001];
void
gravity(void)
{
for (int j=0; j<10; j++) {
int top = N-1, bottom = N-1;
while (top >= 0) {
while (top >= 0 &&
board[top][j] == 0) top--;
if (top >= 0)
board[bottom--][j]
= board[top--][j];
}
while (bottom >= 0) board[bottom--][j] =
0;
}
}
void visit(int i,
int j, int r, int c)
{
if (i<0 || i>=N || j<0 || j>9 || board[i][j]!=c ||
region[i][j]!=0) return;
region[i][j] = r;
regsizes[r]++;
visit(i-1,j,r,c);
visit(i+1,j,r,c);
visit(i,j-1,r,c);
visit(i,j+1,r,c);
}
bool
iterate(void)
{
int r = 1;
for (int i=0; i
for (int j=0; j<10; j++)
region[i][j] = 0;
for (int i=0; i
for (int j=0; j<10; j++)
if (board[i][j] &&
!region[i][j]) visit(i,j,r++,board[i][j]);
bool progress = false;
for (int i=0; i
for (int j=0; j<10;
j++)
if (board[i][j] &&
regsizes[region[i][j]]>=K) {
board[i][j] =
0;
progress =
true;
}
gravity();
while (r) regsizes[r--] = 0;
return progress;
}
int main(void)
{
ifstream fin ("mooyomooyo.in");
fin >> N >> K;
string s;
for (int i=0; i
fin >> s;
for (int j=0; j<10; j++) board[i][j] =
s[j]-'0';
}
while (iterate());
ofstream fout ("mooyomooyo.out");
for (int i=0; i
for (int j=0; j<10; j++) fout <<
board[i][j];
fout << "\n";
}
return 0;
}