USACO2008 Cow Cars /// oj23323

题目大意:

N (1 ≤ N ≤ 50,000)头牛被编号为1-N,牛i可以在M(1 ≤ M ≤ N)条不同的高速路上以Si (1 ≤ Si ≤ 1,000,000) km/h的速度飞驰

为了避免相撞,牛i会为在其前的每头牛减速D (0 ≤ D ≤ 5,000) km/h,thus,牛i的速度实际上是max[Si - D × K,0],高速路限速为L (1 ≤L ≤ 1,000,000) km/h。

Input

* Line 1: Four space-separated integers: NMD, and L

* Lines 2..N+1: Line i+1 describes cow i's initial speed with a single integer: Si

Output

* Line 1: A single integer representing the maximum number of cows that can use the highway

Sample Input

3 1 1 5
5
7
5

Sample Output

2

考虑太少忽视了 M条路 的问题 码完才发现傻了

思路来自 https://www.cnblogs.com/Sunnie69/p/5575455.html

1.首先来考虑怎么分配道路.由于影响后排牛的速度的因素只有前排牛的数量,比起在一条路上排长队,把牛们尽可能均匀地分配到每一条道路上的做法会让前排的牛更少,这样后面的牛需要减的速度就更小,就有可能有更多牛在路上跑.

2.再来考虑怎么分配牛.比起把速度大的牛放在前排,把速度小的牛放在前排的做法可能会让更多的牛在路上跑,因为速度大的牛更优秀,所以更优可能能在前排牛很多的情况下依然跑.

所以算法就是先把牛的速度排个序,然后一层一层地放牛

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m,d,l;
    scanf("%d%d%d%d",&n,&m,&d,&l);
    int s[n+1];
    for(int i=1;i<=n;i++)
        scanf("%d",&s[i]);
    sort(s+1,s+1+n);
    int cnt=0; ///cnt持续记录符合条件的牛
    for(int i=1;i<=n;i++)
    {
        s[i]=max(s[i]-cnt/m*d,0); 
        if(s[i]>=l) cnt++;  ///cnt/m 即前面的牛平均分配到m条路上
    }
    printf("%d
",cnt);

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zquzjx/p/8359394.html