F. Game of words
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

— But how can all of them be acquainted, if no one wants to see anyone else? — Princess still did not find Dragon's plan feasible.

— Not quite, — Dragon circumstantiated, — No one wants an unplanned contact. But, for example, they all come to intellectual competitions eagerly. There are games where teams consist of two people...

Dragon told Princess that qualification competition for Word Game begins in Castles Valley soon. Teams scored enough points participate in the next round of the competition. If several teams score an equal number of points, the team spent less time is placed higher.

Let us describe the rules of qualification game.

Game master has m cards in a pack. Only one word is written on each card. Players take turns. Before the game they agree which of them makes the first move and inform game master about it. Let us call the player who moves first The First Player, and call The Second Player another.

At the beginning, game master gives The First Player a card. The First Player tries to explain the word on the card without naming the word to The Second Player. When the explanation finished, The Second Player says his guess. If The Second Player's answer is the same as the word on the card, the team scores a point. Then, the game master gives The Second Player a card, and The Second Player tries to explain the word on the card to The First Player. Then a card is given to The First Player again... The game continues until cards run out.

But there is an important additional rule in this season. Player must use a terminology of only one subject area when he explains his word. Moreover, a terminology of each subject area should be used only once in qualification game. The n (n ≥ m) subject areas are specified in the rules.

A pair of players — X and Y — has prepared for a qualification game carefully. They believe that each of them can guess any word regardless of a terminology used for explanations. It is simply a matter of time.

After a series of trainings, they studied the time needed to guess the word that was explained with each terminology to each of them.

Now they want to know, what minimum possible time is required for them to score m points. Your tasks is to compute the time.

Input

The first line contains integers m and n (1 ≤ m ≤ n ≤ 400) — the number of cards in a pack and the number of subject areas, which terminologies can be used for explanations.

The second line contains n integers p1, p2, ..., pn, where pj (1 ≤ pj ≤ 106) — the time player X needs to guess a word, if he listens to explanation of this word with jth subject area terminology.

The third line contains n integers q1, q2, ..., qn, where qk (1 ≤ qk ≤ 106) — the time player Y needs to guess a word, if he listens explanation of this word with kth subject area terminology.

Output

Print integer — the minimum possible time, which players need for explanation of m words (in accordance to game rules) in the first line. Players can choose, who of them makes the first step.

Sample test(s)
input
3 5 5 4 7 6 2 8 3 5 4 2 

output
9 

input
4 4 2 4 6 8 1 4 6 7 

output
18 

Note

In the first sample players should play the following way. The first step: player X explains a word to Y using the terms of the 2nd subject area, and Y guesses this word after 3 time units. The second step: player Y uses terms of 5th subject area for explanation of a word, and X guesses this word after 2 time units. The third step: player X explains yet another word using terms of the 4th subject area, and Yguesses this word after 4 time units.

题目链接:http://codeforces.com/gym/100703/problem/F

题目大意:m个游戏中共选出n个游戏玩,其中,已知m个游戏X和Y玩需要的时间,问,X和Y各玩m/2个游戏所需要的最少总时间?

思路:第i层循环中的,dp[j][k]表示前i个游戏,X玩了j个,Y玩了k个,所需要的最小时间。总时间复杂度O(n^2*m),需要注意的是,由于此处选择了二维dp数组,所以,为了不影响前驱数据,j和k所在的循环应该倒着枚举。当然也可以采用滚动数组的方式等。

AC代码:

1 #include <iostream>

 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 using namespace std;
 8 typedef long long LL;
 9 #define MAXN 500
10 int dp[MAXN][MAXN];
11 int n, m;
12 int a[MAXN], b[MAXN];
13 int main() {
14     scanf("%d%d", &m, &n);
15     memset(dp, 0x3f, sizeof dp);
16     for(int i = 1; i <= n; i++) {
17         scanf("%d", &a[i]);
18     }
19     for(int i = 1; i <= n; i++) {
20         scanf("%d", &b[i]);
21     }
22     int s = 0;
23     dp[0][0] = 0;
24     for(int i = 1; i <= n; i++) {
25         for(int j = i; j >= 0; j--) {
26             for(int k = i - j; k >= 0; k--) {
27                 dp[j + 1][k] = min(dp[j + 1][k], dp[j][k] + a[i]);
28                 dp[j][k + 1] = min(dp[j][k + 1], dp[j][k] + b[i]);
29             }
30         }
31     }
32     int ans = min(dp[(m + 1)/2][m / 2], dp[m / 2][(m + 1)/2]);
33     printf("%d\n", ans);
34     return 0;
35 }

转载于:https://www.cnblogs.com/gaoxiang36999/p/4681026.html

Gym 100703F Game of words 动态规划相关推荐

  1. 强化学习(三) - Gym库介绍和使用,Markov决策程序实例,动态规划决策实例

    强化学习(三) - Gym库介绍和使用,Markov决策程序实例,动态规划决策实例 1. 引言 在这个部分补充之前马尔科夫决策和动态规划部分的代码.在以后的内容我会把相关代码都附到相关内容的后面.本部 ...

  2. 动态规划算法实验报告_强化学习之动态规划算法

    如今的强化学习研究大体分为了两个研究学派:一个是以Sutton,Sliver等人为代表的value-based学派,他们主要从值函数近似角度入手去研究强化学习,这也是强化学习早期最初发展起来时沿用的路 ...

  3. 【 Gym - 101138K 】 The World of Trains (DP)

    BUPT2017 wintertraining(15) #4E Gym - 101138K 题意 N节车厢的火车,每节车厢容量是1~K,那么有\(K^N\)种火车. 求选择D个连续的且容量相同的车厢的 ...

  4. 动态规划算法经典例题_c动态规划精简例题

    相信很多初学者,在刚刚接触动态规划,都花费了不少时间.网上有很多人写如何去做动态规划的题目,但是很少有整理适合初学者做的习题.这里是我自己考研期间,复习整理的.希望对大家有帮助. c动态规划精简例题 ...

  5. 【强化学习实战】基于gym和tensorflow的强化学习算法实现

    [新智元导读]知乎专栏强化学习大讲堂作者郭宪博士开讲<强化学习从入门到进阶>,我们为您节选了其中的第二节<基于gym和tensorflow的强化学习算法实现>,希望对您有所帮助 ...

  6. 动手学强化学习(三):动态规划算法 (Dynamic Programming)

    动手学强化学习(三):动态规划算法 (Dynamic Programming) 1. 简介 2. 悬崖漫步环境 3. 策略迭代算法 3.1 策略评估 3.2 策略提升 3.3 策略迭代算法 4.价值迭 ...

  7. 伍六七带你学算法 动态规划 ——不同路径

    力扣 62. 不同路径 难度 中等 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格 ...

  8. 由动态规划计算编辑距离引发的思考

    简单介绍 编辑距离算法: https://www.cnblogs.com/BlackStorm/p/5400809.html https://wizardforcel.gitbooks.io/the- ...

  9. LeetCode 10. Regular Expression Matching python特性、动态规划、递归

    前言 本文主要提供三种不同的解法,分别是利用python的特性.动态规划.递归方法解决这个问题 使用python正则属性 import reclass Solution2:# @return a bo ...

  10. 【动态规划】Part1

    1. 硬币找零 题目描述:假设有几种硬币,如1.3.5,并且数量无限.请找出能够组成某个数目的找零所使用最少的硬币数. 分析:   dp [0] = 0            dp [1] = 1 + ...

最新文章

  1. 数字图像处理知识点总结
  2. java线程睡眠一分钟_Java中的TimerTimerTask和线程睡眠
  3. 电商分类模块写法_模块化写作——网络小说创作之典藏秘籍
  4. 小学计算机教育实习教案,小学信息技术教师资格证面试教案模板:《漂亮的剪贴画》...
  5. 计算机软件乘除,基于单片机的智能计算机程序 可以实现加减乘除运算
  6. 建立计算机科学系的学生视图,SQL语句练习及答案
  7. 机房收费系统系列二:MDI子窗体和主窗体显示
  8. [BZOJ2048] [2009国家集训队] 书堆
  9. 从Zachman企业架构框架想到的
  10. 使用DirectX播放音频数据流
  11. (转载)oracle10g在win10上的安装
  12. 码市coding不能下载
  13. 移动硬盘参数错误无法访问数据恢复方法
  14. 戴尔服务器提升性能,解析戴尔12G服务器的主要性能提升和改进
  15. L1-6 吉老师的回归 (15 分)
  16. 简单查看windows蓝屏原因
  17. Google新鲜技巧玩法及最新揭秘
  18. 基于CBQ(Class Based Queueing)队列的流量控制
  19. hydra暴力破解ftp-telnet-mysql-ssh-http
  20. 瑞芯微RK3568开发板深度评测

热门文章

  1. 牛津大学数学与计算机科学课程,牛津大学之数学专业
  2. caffe实践程序1——mnist任务总结
  3. 为伍兹乳腺X线摄影数据集开发神经网络
  4. 2021-08-05查询排序分页语句
  5. ie选项 由于该计算机受到限制 本次操作已被取消,IE“Internet 选项”无法打开提示“由于该计算机受到限制本次操作已被取消”...
  6. 会议服务器维护保养,关于视频会议系统的维护和保养方法
  7. 如何证明一个问题是NP-Hard或NP-Complete?
  8. 零基础学启发式算法(6)-蚁群算法 (Ant Colony Optimization, ACO)
  9. function signature是什么
  10. 【Django 2021年最新版教程8】操作Mysql数据库 mysqlclient安装和使用