zoukankan      html  css  js  c++  java
  • java实现第六届蓝桥杯居民集会

    居民集会

    蓝桥村的居民都生活在一条公路的边上,公路的长度为L,每户家庭的位置都用这户家庭到公路的起点的距离来计算,第i户家庭距起点的距离为di。

    每年,蓝桥村都要举行一次集会。今年,由于村里的人口太多,村委会决定要在4个地方举行集会,其中3个位于公路中间,1个位最公路的终点。

    已知每户家庭都会向着远离公路起点的方向去参加集会,参加集会的路程开销为家庭内的人数ti与距离的乘积。

    给定每户家庭的位置di和人数ti,请为村委会寻找最好的集会举办地:p1, p2, p3, p4 (p1<=p2<=p3<=p4=L),使得村内所有人的路程开销和最小。

    【输入格式】
    输入的第一行包含两个整数n, L,分别表示蓝桥村的家庭数和公路长度。
    接下来n行,每行两个整数di, ti,分别表示第i户家庭距离公路起点的距离和家庭中的人数。

    【输出格式】
    输出一行,包含一个整数,表示村内所有人路程的开销和。
    【样例输入】
    6 10
    1 3
    2 2
    4 5
    5 20
    6 5
    8 7
    【样例输出】
    18
    【样例说明】
    在距起点2, 5, 8, 10这4个地方集会,6个家庭需要的走的距离分别为1, 0, 1, 0, 2, 0,总的路程开销为13+02+15+020+25+07=18。

    【数据规模与约定】
    对于10%的评测数据,1<=n<=300。
    对于30%的评测数据,1<=n<=2000,1<=L<=10000,0<=di<=L,di<=di+1,0<=ti<=20。
    对于100%的评测数据,1<=n<=100000,1<=L<=1000000,0<=di<=L,di<=di+1,0<=ti<=1000000。

    资源约定:
    峰值内存消耗(含虚拟机) < 512M
    CPU消耗 < 8000ms

    请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
    注意:主类的名字必须是:Main,否则按无效代码处理。

    小编能力有限,这道题只能通过部分测试用例
    import java.util.Scanner;
    
    public class Main {
        public static int n, L;
        public static int[] D, T;
        public static int[][] W;
        
        public int mergeValue(int start, int end, int count) {
            if(count == 0)
                return W[start][end];
            int result = W[0][L];
            for(int i = start + 1;i < end;i++) {
                int left = mergeValue(start, i, count - 1);
                int right = mergeValue(i, end, count - 1);
                result = Math.min(result, left + right);
            }
            return result;
        }
        
        public void getResult() {
            for(int i = 0;i <= L;i++)
                for(int j = i;j <= L;j++)
                    for(int k = 0;k < n;k++) {
                        if(D[k] > i && D[k] < j)
                            W[i][j] += (j - D[k]) * T[k];
                    }
            int result = mergeValue(0, L, 2);
            System.out.println(result);
        }
     
        public static void main(String[] args) {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            n = in.nextInt();
            L = in.nextInt();
            D = new int[n];
            T = new int[n];
            W = new int[L + 1][ L + 1];
            for(int i = 0;i < n;i++) {
                D[i] = in.nextInt();
                T[i] = in.nextInt();
            }
            test.getResult();
        }
    }
    
  • 相关阅读:
    改造vant日期选择
    css3元素垂直居中
    npm综合
    (转)网页加水印方法
    Mac下IDEA自带MAVEN插件的全局环境配置
    隐藏注册控件窗口
    High performance optimization and acceleration for randomWalk, deepwalk, node2vec (Python)
    How to add conda env into jupyter notebook installed by pip
    The Power of WordNet and How to Use It in Python
    背单词app测评,2018年
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947446.html
Copyright © 2011-2022 走看看