zoukankan      html  css  js  c++  java
  • Vasya and Robot

    A. Vasya and Robot
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights wi kilograms.

    Vasya needs to collect all these items, however he won't do it by himself. He uses his brand new robot. The robot has two different arms — the left one and the right one. The robot can consecutively perform the following actions:

    1. Take the leftmost item with the left hand and spend wi · l energy units (wi is a weight of the leftmost item, l is some parameter). If the previous action was the same (left-hand), then the robot spends extra Ql energy units;
    2. Take the rightmost item with the right hand and spend wj · r energy units (wj is a weight of the rightmost item, r is some parameter). If the previous action was the same (right-hand), then the robot spends extra Qr energy units;

     

    Naturally, Vasya wants to program the robot in a way that the robot spends as little energy as possible. He asked you to solve this problem. Your task is to find the minimum number of energy units robot spends to collect all items.

    Input

    The first line contains five integers n, l, r, Ql, Qr (1 ≤ n ≤ 105; 1 ≤ l, r ≤ 100; 1 ≤ Ql, Qr ≤ 104).

    The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 100).

    Output

    In the single line print a single number — the answer to the problem.

    Sample test(s)
    input
    3 4 4 19 1
    42 3 99
    output
    576
    input
    4 7 2 3 9
    1 2 3 4
    output
    34
    Note

    Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units.

    The second sample. The optimal solution is to take one item from the right, then one item from the left and two items from the right. In total the robot spends (2·4) + (7·1) + (2·3) + (2·2 + 9) = 34 energy units.

    // 我们可以枚举从前面和从后面操作相交的位置
    // 在 i 位置相交,前面有 i 个数 后面有 n-i 个数
    // 为求最优 我们得花费额外 ql*(left-right-1) ; 或者 qr*(right-left-1) ; 
    
    
    #include<iostream>
    #include<cstdio>
    using namespace std ;
    
    const int maxn = 100002 ;
    int ans[maxn] , a[maxn] ;
    int min( int a , int b ){ return a < b ? a : b ;}
    
    int maivn()
    {
        int i , minv , n , left , right ;
        int l , r , ql , qr ,cost ;
    
       // freopen("in.txt","r",stdin) ;
        while( cin >> n >> l >> r >> ql >> qr )
        {
            for( i = 1 ; i <= n ;i++ )
               scanf("%d" , &a[i]) ;
            ans[0] = ans[n+1] = 0 ;
            minv = 2000000000 ;
            for( i = 1 ; i <= n ;i++ )
                ans[i] = ans[i-1]+a[i] ;
            for( i = 0 ; i <= n ;i++ )
            {
                cost = l*ans[i]+r*(ans[n]-ans[i]) ;
                right = n-i ; left = i ;
                if( left > right )
                {
                    cost += ql*(left-right-1) ;
                }
                else if(right > left )
                {
                    cost += qr*(right-left-1) ;
                }
                minv = min(minv,cost) ;
            }
            cout << minv << endl ;
        }
    }
  • 相关阅读:
    github 上中国互联网公司的开源项目
    execve(".. ",[".. ",".. "],[/* ..*/])第二个 参数 数组硬传
    1506-122 (S) Expecting pointer to struct or union.
    徘徊~2013.7.31
    myeclipse2014 maven4eclipse配置
    01--maven安装与环境配置(windows)
    java宽度优先将二叉树存成数组
    java宽度搜索打印二叉树
    Jquery中bind绑定和on绑定的区别
    java设计模式singleton原理及实现(java1.4前不要使用双重锁保证线程安全)
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3368808.html
Copyright © 2011-2022 走看看