ICode9

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

ICPC Southeast USA 2020 Regional Contest Problem A: Ant Typing(思维)

2021-04-11 20:35:05  阅读:257  来源: 互联网

标签:cannot capacity USA Contest int Regional objects knapsack remaining


题目描述

We have a knapsack of integral capacity and some objects of assorted integral sizes. We attempt to fill the knapsack up, but unfortunately, we are really bad at it, so we end up wasting a lot of space that can’t be further filled by any of the remaining objects. In fact, we are optimally bad at this! 
How bad can we possibly be?
figure out the least capacity we can use where we cannot place any of the remaining objects in the knapsack. For example, suppose we have 3 objects with weights 3, 5 and 3, and our knapsack has  capacity 6. If we foolishly pack the object with weight 5 first, we cannot place either of the other two objects in the knapsack. That’s the worst we can do, so 5 is the answer.

输入

The first line of input contains two integers n (1 ≤ n ≤ 1,000) and c (1 ≤ c ≤ 105 ), where n is the number of objects we want to pack and c is the capacity of the knapsack.
Each of the next n lines contains a single integer w (1 ≤ w ≤ c). These are the weights of the objects.

输出

Output a single integer, which is the least capacity we can use where we cannot place any of the remaining objects in the knapsack.

样例输入 Copy

3 6
3
5
3

样例输出 Copy

5




这个题只有1-9九个数字,我们可以求一个9的全排列,但是我们需要check一个排列行不行
一种容易想到的方法就是O(n)扫边字符串就可以了
但是这样复杂度并不能通过
这里继续考虑这九种数字
每次移动都是在九种数字的任意两种之间
那么我们处理出相邻的两数字出现的次数
然后check的时候只需要考虑对于一个排列枚举两种数字,然后算出距离×出现次数的和就可以了


int  a[10],ans = inf,p[maxn],pos[10];
int len,num[66][66] ;
char s[maxn];
int  cal() {
    int  temp=0;
    for(int i=1 ; i<=9 ; i++) pos[a[i]] = i;
    for(int i=1 ; i<=9 ; i++)     for(int j=1 ; j<=9 ; j++)     temp+=abs(pos[i]-pos[j])*num[i][j];
    return temp+len+abs(pos[p[1]] - pos[a[1]] );
}
int main() {
    scanf("%s",s+1);
    len = strlen(s+1);
    rep(i,1,len ) p[i] = s[i]-'0';
    for(int i=1 ; i<len ; i++)num[p[i]][p[i+1]]++;
    rep(i,1,9) a[i] = i;
    do {
        ans = min(ans,cal());
    } while(next_permutation(a+1,a+1+9));
    out(ans);
    return  0;
}
View Code

 

标签:cannot,capacity,USA,Contest,int,Regional,objects,knapsack,remaining
来源: https://www.cnblogs.com/UpMing/p/14645290.html

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

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

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

ICode9版权所有