ICPC Southeast USA 2020 Regional Contest Problem A: Ant Typing(思维)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ICPC Southeast USA 2020 Regional Contest Problem A: Ant Typing(思维),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2010字,纯文字阅读大概需要3分钟。
内容图文
题目描述
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
内容总结
以上是互联网集市为您收集整理的ICPC Southeast USA 2020 Regional Contest Problem A: Ant Typing(思维)全部内容,希望文章能够帮你解决ICPC Southeast USA 2020 Regional Contest Problem A: Ant Typing(思维)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。