zoukankan      html  css  js  c++  java
  • 牛客练习赛78 B.CCA的搬运(思维题)

    题目链接

    做法:一个思维题,取出一个小球放在最上面需要消耗的体力为它上面的小球的重量之和。题目给出了拿取的顺序,由于每次取出一个小球都放在最上面,因此,对于一个当前的小球,它所需要花费的体力肯定大于等于它之前拿取的小球的重量之和,只需要按照给出的拿取顺序摆放即可。注意,可能会有一个球会被多次拿取放在上面。(我在这里出错了)我们可以开一个标记数组,标记是否之前的小球已经在序列中了,如果有,我们就不用添加。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    const int N = 2005;
    int a[N];
    int pos[N];
    bool vis[N];
    int main()
    {
        int n, m;
        cin >> n >> m;
        
        for(int i = 1; i <= n; ++i) cin >> a[i];
        
        int res = 0;
        int x, len;
        len = 0;
        for(int i = 1; i <= m; ++i)
        {
            scanf("%d", &x);
            
            //判断序列里面是否已经存在过x
            if(!vis[x])
            {
                vis[x] = true;
                pos[++len] = x;
            }
            
            int p;
            for(int j = 1; j <= len; ++j)
            {
                if(pos[j] == x)
                {
                    p = j;   
                    break;
                }
                //加上之前拿取的小球的重量
                res += a[pos[j]];
            }
            //放在顶端
            for(int j = p; j >= 2; --j) pos[j] = pos[j - 1];
            pos[1] = x;
        }
        
        printf("%d
    ", res);
        
        
        return 0;
    }
    
  • 相关阅读:
    设计模式 之 单例模式
    leetcode 69 x 的平方根 牛顿迭代法
    leetcode 98 验证二叉搜索树
    leetcode 54 螺旋数组
    第一篇-python入门
    python-入门
    python
    线性判别分析LDA总结
    LDA
    线性判别分析(LDA)原理
  • 原文地址:https://www.cnblogs.com/pixel-Teee/p/14526932.html
Copyright © 2011-2022 走看看