zoukankan      html  css  js  c++  java
  • [Usaco2008 Open]Cow Cars 奶牛飞车

    题目描述

    编号为1到N的N只奶牛正各自驾着车打算在牛德比亚的高速公路上飞驰.高速公路有M(1≤M≤N)条车道.奶牛i有一个自己的车速上限Si(l≤Si≤1,000,000).
    在经历过糟糕的驾驶事故之后,奶牛们变得十分小心,避免碰撞的发生.每条车道上,如果某一只奶牛i的前面有K只奶牛驾车行驶,那奶牛i的速度上限就会下降K*D个单位,也就是说,她的速度不会超过Si - kD(O≤D≤5000),当然如果这个数是负的,那她的速度将是0.牛德比亚的高速会路法规定,在高速公路上行驶的车辆时速不得低于L(1≤L≤1,000,000).那么,请你计算有多少奶牛可以在高速公路上行驶呢?

    输入格式

    第1行输入N,M,D,L四个整数,之后N行每行一个整数输入Si.
    N<=50000

    输出格式

    输出最多有多少奶牛可以在高速公路上行驶.

    贪心
    首先把速度低的牛先放进去(因为要让最多的牛在路上,速度快的牛不容易降到限制以下
    同时分层处理,让速度差不多的牛在不同的路上,这样做一定比将所有的牛放在一条路上优

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int main()
    {
    	int n,m,d,l;
    	int a[50005];
    	scanf("%d %d %d %d",&n,&m,&d,&l);
    	for(int i=1;i<=n;i++){
    		scanf("%d",a+i);
    	}
    	sort(a+1,a+n+1);
    	int ans=0;
    	for(int i=1;i<=n;i++){
            int t=ans/m;
            if(a[i]-t*d>=l){
                ans++;
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    ios 中的循环引用问题及解决
    Leetcode-Maximum Depth of Binary Tree
    Leetcode-Min Stack
    Leetcode-Intersection of Two Linked Lists
    Leetcode-Binary Tree Inorder Traversal
    Leetcode-Unique Binary Search Trees II
    Leetcode-Unique Binary Search Trees
    Leetcode-binary Tree Zigzag Level Order Traversal
    Leetcode-Construct Binary Tree from inorder and preorder travesal
    Leetcode-Next Permutation
  • 原文地址:https://www.cnblogs.com/shulker/p/9499786.html
Copyright © 2011-2022 走看看