dp的两个不错的题
C - Cheapest Palindrome
Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag's contents are currently a single string with length M (1 ≤ M ≤ 2,000) characters drawn from an alphabet of N (1 ≤ N ≤ 26) different symbols (namely, the lower-case roman alphabet).
Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. While a cow whose ID is "abcba" would read the same no matter which direction the she walks, a cow with the ID "abcb" can potentially register as two different IDs ("abcb" and "bcba").
FJ would like to change the cows's ID tags so they read the same no matter which direction the cow walks by. For example, "abcb" can be changed by adding "a" at the end to form "abcba" so that the ID is palindromic (reads the same forwards and backwards). Some other ways to change the ID to be palindromic are include adding the three letters "bcb" to the begining to yield the ID "bcbabcb" or removing the letter "a" to yield the ID "bcb". One can add or remove characters at any location in the string yielding a string longer or shorter than the original string.
Unfortunately as the ID tags are electronic, each character insertion or deletion has a cost (0 ≤ cost ≤ 10,000) which varies depending on exactly which character value to be added or deleted. Given the content of a cow's ID tag and the cost of inserting or deleting each of the alphabet's characters, find the minimum cost to change the ID tag so it satisfies FJ's requirements. An empty ID tag is considered to satisfy the requirements of reading the same forward and backward. Only letters with associated costs can be added to a string.
Input
Line 2: This line contains exactly M characters which constitute the initial ID string
Lines 3.. N+2: Each line contains three space-separated entities: a character of the input alphabet and two integers which are respectively the cost of adding and deleting that character.
Output
Sample Input
3 4
abcb
a 1000 1100
b 350 700
c 200 800
Sample Output
900
Hint
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
char s[];
int dp[][];
int a[];
int main() {
int n,m;
while(cin>>n>>m){
getchar();
scanf("%s",s+);
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
getchar();
char c=getchar();
int e,f;
cin>>e>>f;
a[(int)c]=min(e,f);
}
for(int i=m;i>;i--)
for(int j=i+;j<=m;j++){
if(s[i]==s[j])dp[i][j]=dp[i+][j-];
else dp[i][j]=min(dp[i+][j]+a[(int)s[i]],dp[i][j-]+a[(int)s[j]]);
}
cout<<dp[][m]<<endl; }
return ;
}
D - A Mini Locomotive
1. Set the number of maximum passenger coaches a mini locomotive can pull, and a mini locomotive will not pull over the number. The number is same for all three locomotives.
2. With three mini locomotives, let them transport the maximum number of passengers to destination. The office already knew the number of passengers in each passenger coach, and no passengers are allowed to move between coaches.
3. Each mini locomotive pulls consecutive passenger coaches. Right after the locomotive, passenger coaches have numbers starting from 1.
For example, assume there are 7 passenger coaches, and one mini locomotive can pull a maximum of 2 passenger coaches. The number of passengers in the passenger coaches, in order from 1 to 7, is 35, 40, 50, 10, 30, 45, and 60.
If three mini locomotives pull passenger coaches 1-2, 3-4, and 6-7, they can transport 240 passengers. In this example, three mini locomotives cannot transport more than 240 passengers.
Given the number of passenger coaches, the number of passengers in each passenger coach, and the maximum number of passenger coaches which can be pulled by a mini locomotive, write a program to find the maximum number of passengers which can be transported by the three mini locomotives.
Input
The first line of the input file contains the number of passenger coaches, which will not exceed 50,000. The second line contains a list of space separated integers giving the number of passengers in each coach, such that the i th number of in this line is the number of passengers in coach i. No coach holds more than 100 passengers. The third line contains the maximum number of passenger coaches which can be pulled by a single mini locomotive. This number will not exceed 1/3 of the number of passenger coaches.
Output
Sample Input
1
7
35 40 50 10 30 45 60
2
Sample Output
240
一个数列,n个数,找三个k个连续数的子数列,使其和最大。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int f[][],a[];
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n;
scanf("%d",&n);
a[]=;
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);
a[i]+=a[i-];
}
int m;
scanf("%d",&m);
memset(f,,sizeof(f));
for(int i=; i<=n; i++)
for(int j=; j<; j++) {
int k=i-m;
if(k<) k=;
f[i][j]=max(f[i-][j],f[k][j-]+a[i]-a[k]);
}
printf("%d\n",f[n][]);
}
return ;
}
dp的两个不错的题的更多相关文章
- 推荐两个不错的CAD二次开发(.Net)手册
推荐两个不错的CAD二次开发(.Net)手册 http://www.mjtd.com/helpcenter/netguide/index.html http://www.ceesky.com/book ...
- 两个不错的IT类优质号
虽然标题已经被用烂了,但是我觉得还是用这样的方式介绍这两个不错的公众号,可能你们刚好需要,我刚好知道,仅此而已. 刚认识的一个小哥哥和一个小姐姐,他们都非常优秀,有喜欢Java和Linux的同学千万不 ...
- 两道不错的递推dp
hdoj-4055 #include <cstdio> #include <cstring> #include <iostream> #include <al ...
- 树形dp入门两题
题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...
- FJOI2020 的两道组合计数题
最近细品了 FJOI2020 的两道计数题,感觉抛开数据范围不清还卡常不谈里面的组合计数技巧还是挺不错的.由于这两道题都基于卡特兰数的拓展,所以我们把它们一并研究掉. 首先是 D1T3 ,先给出简要题 ...
- 数位dp初步——数位dp的两种方式
数位dp:一类统计区间[L,R]内某种符合规定的数字个数的题目.特征是R的范围会很大,O(N)范围内无法完成. 一般而言,解决这类题目有两种方式,一种是递推,另一种是记忆化搜索. 递推: 1)利用dp ...
- 木棍加工(dp,两个参数的导弹拦截问题)
题目描述 一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的.棍子可以被一台机器一个接一个地加工.机器处理一根棍子之前需要准备时间.准备时间是这样定义的: 第一根棍子的准备时间为1分钟: ...
- Codevs1378选课[树形DP|两种做法(多叉转二叉|树形DP+分组背包)---(▼皿▼#)----^___^]
题目描述 Description 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修 ...
- 两道相似KMP题
1.POJ 3450 Coporate Identity 这两题的解法都是枚举子串,然后匹配,像这种题目以后可以不用KMP来做,直接字符串自带的strstr函数搞定,如果字符串未出现,该函数返回NUL ...
随机推荐
- 第八章 设计用户界面 之 给Web程序应用用户界面设计
1. 概述 本章内容包括: 使用CSS创建和应用样式.使用HTML构架用户界面的层次 以及 根据需求实现动态页面内容. 2. 主要内容 2.1 使用CSS创建和应用样式 Razor程序的模板是_Lay ...
- 部署ASP.Net Core 2.1 項目到 IIS
用Asp.net core 2.1 寫了一個小的系統框架,記錄一下部署過程: 1. 首先是安裝 IIS 了,從 控制面板→程序→启用或关闭Windows功能→勾选Internet Informatio ...
- SpringBoot 2.x (10):整合Redis
Redis部署到阿里云: 下载redis源码,编译,允许远程访问的配置 阿里云安全组设置: SSH连过去: wget http://download.redis.io/releases/redis-4 ...
- 简单的UDP程序
接受端: package com.dcz.udp; import java.io.IOException; import java.net.DatagramPacket; import java.ne ...
- 字符串、String等转换
(1) String 转换为字符串 例:String s = "abcde";char[] a = s.toCharArray(); (2) 字符串转换为Stringchar[] ...
- github入门之创建仓库--3
1.登陆到github,点击加号中的New repository 2.设置仓库信息 *注: ------Description:添加仓库说明,不是必填项 ------Public.Private:选择 ...
- MySQL报错竞技赛
以下报错,我几乎没出过几个. ERROR 2 系统找不到文件: mysql-5.6.1X默认的配置文件是在C:\Program Files\MySQL\MySQL Server 5.6\my-defa ...
- make 与makefile(会不会写 makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。)
跟我一起写 Makefile /**/ 陈皓 (CSDN) 概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉 ...
- 第009课 gcc和arm-linux-gcc和MakeFile
from:第009课 gcc和arm-linux-gcc和MakeFile 第001节_gcc编译器1_gcc常用选项_gcc编译过程详解 gcc的使用方法 gcc [选项] 文件名 gcc常用选项 ...
- 如何写好一个vue组件,老夫的一年经验全在这了【转】 v-bind="$attrs" 和 v-on="$listeners"
如何写好一个vue组件,老夫的一年经验全在这了 一个适用性良好的组件,一种是可配置项很多,另一种就是容易覆写,从而扩展功能 Vue 组件的 API 来自三部分——prop.事件和插槽: prop 允许 ...