zoukankan      html  css  js  c++  java
  • CodeForces 500C New Year Book Reading


    title: CodeForces 500C New Year Book Reading
    date: 2016-08-04 19:38:00
    tags:
    - 贪心
    - CodeForces

    链接:
    New Year Book Reading

    题意:
    有n本书编号为1~n, 每本书的重量是wi,书可以按任意顺序叠成一摞。
    对于这摞书进行m次操作,每次从中取出第bj本,取完之后把第bj本放在所有书的最上面,取一本书的代价是在它上面所有书重量之和,求m次操作总代价的最小值

    思路:
    原始的书的顺序按操作顺序来排,b1放在最上面,b2放在第二,以此类推。
    开一个数组保存任意两本书的先后关系,每一次操作遍历一遍数组,结果加上在它之前的所有书的重量,并把它放在所有书之前

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 507;
    const int MAXM = 1007;
    int w[MAXN];
    int b[MAXM];
    int top[MAXN][MAXN]; 
    // top[i][j] : j在i上面
    int main(int argc, char const *argv[])
    {
    	int n,m;
        //freopen("in.txt", "r", stdin); 
    	while(~scanf("%d%d", &n, &m))
    	{
    		memset(top, 0, sizeof(top));
    		for (int i = 1; i <= n; ++i)
    		{
    			scanf("%d", &w[i]);
    		}
    		for (int i = 1; i <= m; ++i)
    		{
    			scanf("%d", &b[i]);
    		}
    		long long ans = 0;
    		for (int i = 1; i <= m; ++i)
    		{
    			for (int j = 1; j <= n; ++j)
    			{
    				if(b[i] == j) continue;
    				if(top[ b[i] ][j] ) ans += w[j];
    				top[b[i]][j] = 0;
    				top[j][b[i]] = 1;
    			}
    		}
    
    		printf("%I64d
    ", ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    python-day8(正式学习)
    Bug快到碗里来
    python-day7(正式学习)
    python-day6(正式学习)
    python-day5(正式学习)
    python-day4(正式学习)
    Django中间件
    cookie和session
    分页器,form组件的使用
    orm常用字段和数据库优化查询
  • 原文地址:https://www.cnblogs.com/nanf/p/cf500c.html
Copyright © 2011-2022 走看看