Have you seen the movie King Kong? If you have seen it, you must be impressed by the scene of the exciting fight between dinosaurs and
King Kong,right? Though the dinosaurs have been fight off, King Kong has been injured very heavily. Considering that dinosaurs will come back
very quickly, King Kong brings a lot of stones for fear that the dinosaurs attack again.

Now King Kong has arranged the stones at random in one line. But Different alignments of these stones would be different to King Kong. If the
alignment is not the target in his mind, he will move the stones to their proper positions. Taking the physical consumption into consideration,
King Kong could swap only two stones (whose weight is a and b weight units) at one time and for each time he will consume a+b thermal units.
In order to minimize the physical consumption, King Kong should set a plan to move these stones. But this is too complex for king Kong and
he needs your help.

输入格式

There are several test cases. For each test case, it contains:
Line 1: One integer N (1<=N<=5000) which specifies the total number of stones.
Line 2: N integers (you are ensured that the absolute value of each integer is less than) which the weight of each stone initially. These numbers
specify the initial stone alignment. There is a blank between two consecutive integers.
Line 3: N integers (you are ensured that the absolute value of each integer is less than ) which the weight of each stone finally. These numbers
specify the target alignment in King Kong?s mind. There is a blank between two consecutive integers.The input will be ended by zero.

输出格式

Output the answer of the minimum total thermal units consumed by King Kong in the stone moving process.

样例输入

3
3 2 1
1 2 3
3
1 2 3
1 2 3
4
8 1 2 4
1 2 4 8
0

样例输出

4
0
17
//首先找出序列中所有的置换,然后对于每个置换有两种方法将其调成合法。
//1.每次用置换中最小的数去和其他数换,ans=sum+min*(num-2)。
//2.用全局最小的元素换所有元素,最后还要把这个元素换出去,ans=sum+allmin*(num+1)。
//对于每个置换两种方案取min值相加就可以了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include<iostream>
using namespace std;
int h1[65537];//存储数所在的位置
int h2[65537];//金刚想要数所在的位置
int a[5001];  //第一行数
int b[5001];  //第二行数
bool sign[5001];//标记这个数是否移动到所想位置
int main()
{  //freopen("in.txt", "r", stdin);  int n, i, k, tp, cost, q,min;  while (scanf("%d", &n) != EOF && n != 0)  {  min = 65538;  memset(sign, 0, sizeof(sign));  cost = 0;  for (i = 1; i <= n; i++)  {  scanf("%d", &a[i]);  if (a[i] < min) min = a[i];  cost += a[i];//每个数都至少要交换一次  h1[a[i]] = i;  }  for (i = 1; i <= n; i++)  {  scanf("%d", &b[i]);  h2[b[i]] = i;  }  for (i = 1; i <= n; i++)  if (!sign[i])  {  tp = 65538;//记录循环中最小的数  q = i;  k = 0;//循环个数  do//找到循环个数和循环中最小的数  {  sign[q] = true;  if (b[q] < tp)  tp = b[q];  q = h1[b[q]];  k++;  }while (q != i);  cost += (k - 2) * tp < tp + (k + 1) * min ? (k - 2) * tp : tp + (k + 1) * min;//两种方式中选择小的  }  printf("%d\n", cost);  }  return 0;
}  

WOJ1018-King Kong相关推荐

  1. flamingo的中文意思是什么_flamingo.是什么意思

    1. Lake Nakuru's claim to fame is anchored on its flamingo's and the over 400 species of birds found ...

  2. Go语言与数据库开发:01-11

    反射 Go语言提供了一种机制在运行时更新变量和检查它们的值.调用它们的方法和它们支持的内 在操作,但是在编译时并不知道这些变量的具体类型.这种机制被称为反射.反射也可以让 我们将类型本身作为第一类的值 ...

  3. Problem I. Hall of Fame (2014 Syria ICPC)

    Problem I. Hall of Fame 来源:ACM International Collegiate Programming Contest, Syrian Collegiate Progr ...

  4. Go 反射机制详解及实例 【Go语言圣经笔记】

    反射 Go语言提供了一种机制,能够在运行时更新变量或检查它们的值.调用它们的方法和它们支持的内在操作,而不需要在编译时就知道这些变量的具体类型.这种机制被称为反射(这里反射的定义和其他语言大体相同). ...

  5. php黄金搭档_动画电影电子游戏的搭档实际上很棒

    php黄金搭档 回顾性 (RETROSPECTIVE) Disclaimer: I don't intend for these games to compete with top-tier AAA ...

  6. (转载)Everynes NES Hardware Specifications

    原文: NES Specifications  Everynes - Nocash NES Specs Everynes Hardware Specifications Tech Data Memor ...

  7. 我们走访了十几家美国企业服务公司,写下了这篇万字长文

    关注ITValue,看企业级最新鲜.最具价值报道! 作者:麦采尧.吴陈尧.芮雪峰 | 图片:视觉中国 | 来源:GGV 纪源资本 | ID:GGVcapital | 目录: 1.SaaS on fir ...

  8. 249部经典电影,大家编程之余可以看一下

    众所周知,IMDB的排行榜一直是精华电影的标志之一,特出此专题把IMDB Top 250的电影列出来,帮助网友在这漫漫的十一长假中,可以在这些经典的电影中找到一点点快乐. 排名 电影名(点击搜索资源) ...

  9. psv摇杆测试软件,adrenaline专用右摇杆控制视角插件发布, 转

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 感谢psv的自制开发者Freakler为我们带来的这个神奇插件!!!非全部游戏支持 [RELEASE] camera_patch_lite plugin ...

最新文章

  1. 咕泡学院:(1)唐宇迪python课程作业
  2. C#高级编程----错误和异常的总结
  3. 用神经网络分类过去与未来
  4. linux系统下创建文件系统
  5. 深入理解Solidity 三
  6. 设置ntpdate服务开机启动校验时间
  7. HTML5获取地理位置定位信息
  8. ApacheCN 学习资源汇总 2018.12
  9. myEclipse配置java版本(环境、项目、编译)
  10. Kubernetes支持有状态服务的部署
  11. [九度][何海涛] 二叉树中和为某一值的路径
  12. iOS :APP 提交描述注意事项
  13. rhel5编译安装2.6.29.2内核
  14. SSD测试专题(二)
  15. Linux 中/proc目录下文件详解
  16. html古风颜色代码,数字报纸HTML版本
  17. [TensorFlow深度学习深入]实战一·使用embedding_lookup模块对Word2Vec训练保存与简单使用
  18. HyperMesh二次开发教程 - 前言
  19. Python 操作Mongodb 聚合前过滤筛选
  20. 怎么用python画房子_python绘图作业:使用pygame库画房子

热门文章

  1. 稳定性测试怎么测_药物稳定性试验的21个常见问题
  2. 数据库系列(5)-数据库设计之规范化理论
  3. 为什么苹果蓝牙耳机连上还是公放_为什么现在厂商做个蓝牙耳机,都要学苹果?...
  4. 【1】发布信息数据统计(wps excel函数)
  5. 计算机毕业设计Java家教到家平台(源码+系统+mysql数据库+lw文档)
  6. 小型企业局域网免费上网行为管理方案
  7. 写在 2023 年初的后端社招面试经历(四年经验):字节 米哈游 富途 猿辅导
  8. win10如何加快开机速度【系统天地】
  9. 模拟登录模块以及获取cookie
  10. 平衡二叉树检查 牛客网 程序员面试金典 C++ Python