ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces--665B--Shopping

2020-03-12 20:02:25  阅读:254  来源: 互联网

标签:integers Shopping int items Ayush Codeforces th 665B row


题目描述:
Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online.
The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person’s order.
Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + … + pos(aim) for the i-th customer.
When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating.
Your task is to calculate the total time it takes for Ayush to process all the orders.
You can assume that the market has endless stock.
输入描述:
The first line contains three integers n, m and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market.
The next line contains k distinct integers pl (1 ≤ pl ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k.
Each of the next n lines contains m distinct integers aij (1 ≤ aij ≤ k) — the order of the i-th person.
输出描述:
Print the only integer t — the total time needed for Ayush to process all the orders.
输入:
2 2 5
3 4 1 2 5
1 5
3 1
输出:
14
题意:
现在有k件商品,每个商品的位置已经告诉你了,现在有n个人,每个人有m个需求,每个人要拿第i个物品,问代价
题解
直接搞
代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

const int maxn = 200 + 5;
int a[maxn],b[maxn];
int n,m,k,x;

int getloc(int x){
    for(int i = 1; i <= k; i ++){
        if(a[i] == x) return i;
    }
}

void update(int x){
    int pos = 0;
    for(int i = 1; i <= k; i ++){
        if(a[i] == x) pos = i;
    }
    b[1] = a[pos];
    int t = 2;
    for(int i = 1; i <= k; i ++){
        if(i != pos) b[t ++] = a[i];
    }
    //for(int i = 1; i <= k; i ++) cout<<b[i]<<" ";
    //cout<<endl;
    for(int i = 1; i <= k; i ++){
        a[i] = b[i];
    }
}

int main(){
    while(scanf("%d%d%d",&n,&m,&k)!=EOF){
        for(int i = 1; i <= k; i ++) scanf("%d",&a[i]);
        int ans = 0;
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= m; j ++){
                scanf("%d",&x);
                ans += getloc(x);
                //cout<<getloc(x)<<endl;
                update(x);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

标签:integers,Shopping,int,items,Ayush,Codeforces,th,665B,row
来源: https://blog.csdn.net/Ypopstar/article/details/104826259

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有