Bzoj3893 [Usaco2014 Dec]Cow Jog

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 302  Solved: 157

Description

The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at different speeds. With only one lane in the track, cows cannot pass each other. When a faster cow catches up to another cow, she has to slow down to avoid running into the other cow, becoming part of the same running group. The cows will run for T minutes (1 <= T <= 1,000,000,000). Please help Farmer John determine how many groups will be left at this time. Two cows should be considered part of the same group if they are at the same position at the end of T minutes.
在一条无限长的跑道上有N头牛,每头牛有自己的初始位置及奔跑的速度。牛之间不能互相穿透。当一只牛追上另一只牛时,它不得不慢下来,成为一个群体。求T分钟后一共有几个群体。

Input

The first line of input contains the two integers N and T. The following N lines each contain the initial position and speed of a single cow. Position is a nonnegative integer and speed is a positive integer; both numbers are at most 1 billion. All cows start at distinct positions, and these will be given in increasing order in the input.

Output

A single integer indicating how many groups remain after T minutes.

Sample Input

5 3
0 1
1 2
2 3
3 2
6 1

Sample Output

3

HINT

Source

Silver

先按初始顺序给牛编号,然后让牛跑T分钟,按照新位置从小到大排序(位置相同的,序号大的在前)。

然后从前往后扫一遍,如果编号小的牛跑到的位置比编号大的牛远,那么实际上他们已经合并了。

记录从1开始的编号单增序列长度就是答案。

从别的博客(辗转山河弋流歌)看到了更短的写法:从后往前扫一遍,看哪些牛撞死了

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #define LL long long
 8 using namespace std;
 9 const int mxn=100010;
10 LL read(){
11     LL x=0,f=1;char ch=getchar();
12     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
13     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
14     return x*f;
15 }
16 LL v[mxn],p[mxn];
17 LL id[mxn];
18 int n;LL T;
19 int cmp(int a,int b){
20     if(p[a]==p[b])return a>b;
21     return p[a]<p[b];
22 }
23 int main(){
24     n=(LL)read();
25     T=read();
26     int i,j;
27     for(i=1;i<=n;i++){
28         p[i]=read();v[i]=read();
29         p[i]+=v[i]*T;
30     }
31     for(i=1;i<=n;i++)id[i]=i;
32     sort(id+1,id+n+1,cmp);
33     int last=0,ans=0;
34     for(i=1;i<=n;i++){
35         if(id[i]>last){ans++;last=id[i];}
36     }
37     printf("%d\n",ans);
38     return 0;
39 }
时间: 2024-10-27 05:44:21

Bzoj3893 [Usaco2014 Dec]Cow Jog的相关文章

3893: [Usaco2014 Dec]Cow Jog

3893: [Usaco2014 Dec]Cow Jog Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved: 87[Submit][Status][Discuss] Description The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N

BZOJ 3893 Usaco2014 Dec Cow Jog 模拟

题目大意:给出n头牛他们的初始位置和各自的速度,一头牛追上另一头牛之后这两头牛会变成一头牛,问最后剩下几头牛. 思路:简单模拟一下不难发现,我们只要算出如果正常行驶每头牛的最后到达的地点,从后往前扫一下,有多少个单调不减的序列就是最后有多少头牛. CODE: #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstring> #include <iostream> #include <algor

BZOJ 3893 [Usaco2014 Dec]Cow Jog

题目大意:给定一些牛,每头牛有一个初始位置和速度,如果某头牛能追上后面的那头速度就会和后面那头一样,求T分钟后会形成多少小团体 <论排序算法的低效性和如何避免使用排序算法以及认真读题的重要性> 一头牛的速度不会被后面的牛所影响 因此我们从后往前扫,如果当前的牛追不上后面那个小团体中最慢的那头牛,这头牛就成为新的小团体 时间复杂度O(n) 注意数据有点问题,虽然说初始位置和速度都小于等于100W但是实际上有比这个大的 #include <cstdio> #include <cs

3892: [Usaco2014 Dec]Marathon

3892: [Usaco2014 Dec]Marathon Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 169  Solved: 100[Submit][Status][Discuss] Description Unhappy with the poor health of his cows, Farmer John enrolls them in an assortment of different physical fitness acti

3891: [Usaco2014 Dec]Piggy Back

3891: [Usaco2014 Dec]Piggy Back Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 116  Solved: 92[Submit][Status][Discuss] Description Bessie and her sister Elsie graze in different fields during the day, and in the evening they both want to walk back

BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )

水状压dp. dp(x, s) = max{ dp( x - 1, s - {h} ) } + 奖励(假如拿到的) (h∈s). 时间复杂度O(n * 2^n) ---------------------------------------------------------------------------------- #include<bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; ++i) #define clr(x

[BZOJ1648][Usaco2006 Dec]Cow Picnic 奶牛野餐

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 781  Solved: 483 [Submit][Status][Discuss] Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is grazing in one of N (1 <= N &

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 432  Solved: 270[Submit][Status] Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is grazing in one of N (1 <= N <= 1,000)

BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

直接从每个奶牛所在的farm dfs , 然后算一下.. ---------------------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<vector> #define rep( i ,