动态规划1

动态规划问题是面试题中的热门话题,如果要求一个问题的最优解(通常是最大值或者最小值),而且该问题能够分解成若干个子问题,并且小问题之间也存在重叠的子问题,则考虑采用动态规划。

1、LLS 最长上升子序列

2、最大子段和

3、背包

01背包

完全背包

多重背包

混合背包

分组背包

多维背包

输出方案

输出方案种数

最优方案种数

第k优数

A - Tickets

Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sells the film tickets. He is wandering when could he go back home as early as possible.
A good approach, reducing the total time of tickets selling, is let
adjacent people buy tickets together. As the restriction of the Ticket
Seller Machine, Joe can sell a single ticket or two adjacent tickets at a
time.

Since you are the great JESUS, you know exactly how much time
needed for every person to buy a single ticket or two tickets for
him/her. Could you so kind to tell poor Joe at what time could he go
back home as early as possible? If so, I guess Joe would full of
appreciation for your help.

InputThere are N(1<=N<=10) different scenarios, each scenario consists of 3 lines:

1) An integer K(1<=K<=2000) representing the total number of people;

2) K integer numbers(0s<=Si<=25s) representing the time consumed to buy a ticket for each person;

3) (K-1) integer numbers(0s<=Di<=50s) representing the time needed for two adjacent people to buy two tickets together.

OutputFor every scenario, please tell Joe at what time could he go
back home as early as possible. Every day Joe started his work at
08:00:00 am. The format of time is HH:MM:SS am|pm.

Sample Input

2
2
20 25
40
1
8

Sample Output

08:00:40 am
08:00:08 am
 #include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<cmath>
#include<stdio.h>
using namespace std;
int main()
{
int n,k;
int s[];
int d[]; int dp[];
scanf("%d",&n);
while(n--)
{
scanf("%d",&k);
for(int i=;i<=k;i++)
{
scanf("%d",&s[i]);
}
for(int i=;i<=k;i++)
{
scanf("%d",&d[i]);
}
dp[] = ;dp[] = s[];
for(int i=;i<=k;i++)
{
dp[i]=min((dp[i-]+s[i]),(dp[i-]+d[i]));
}
int h=dp[k]/+;
int m=dp[k]/%;
int s1=dp[k]%; printf("%02d:%02d:%02d am\n", h, m, s1);
}
return ;
}

B - Longest Ordered Subsequence

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N.
The second line contains the elements of sequence - N integers in the
range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4
 #include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
int main()
{
int n;
int a[];
scanf("%d",&n);
int dp[];
int i;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
dp[]=dp[]=;
for(int i=;i<=n;i++)
{
int temp=;
for(int j=;j<=i-;j++)
{
if(a[j]<a[i])
{
if(temp<dp[j])
temp=dp[j];
}
}
dp[i]=temp+;
}
int sum1=dp[];
for(int i=;i<n;i++)
{
if(dp[i+]>sum1)
sum1=dp[i+];
}
printf("%d\n",sum1);
return ;
}

BaoBao has just found a string of length consisting of 'C' and 'P' in his pocket. As a big fan of the China Collegiate Programming Contest, BaoBao thinks a substring of is "good", if and only if 'C', and 'P', where denotes the -th character in string . The value of is the number of different "good" substrings in . Two "good" substrings and are different, if and only if .

To make this string more valuable, BaoBao decides to buy some characters from a character store. Each time he can buy one 'C' or one 'P' from the store, and insert the character into any position in . But everything comes with a cost. If it's the -th time for BaoBao to buy a character, he will have to spend units of value.

The final value BaoBao obtains is the final value of minus the total cost of all the characters bought from the store. Please help BaoBao maximize the final value.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer (), indicating the length of string .

The second line contains the string () consisting of 'C' and 'P'.

It's guaranteed that the sum of over all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the maximum final value BaoBao can obtain.

Sample Input

3
3
CCC
5
CCCCP
4
CPCP

Sample Output

1
1
1

Hint

For the first sample test case, BaoBao can buy one 'P' (cost 0 value) and change to "CCPC". So the final value is 1 - 0 = 1.

For the second sample test case, BaoBao can buy one 'C' and one 'P' (cost 0 + 1 = 1 value) and change to "CCPCCPC". So the final value is 2 - 1 = 1.

For the third sample test case, BaoBao can buy one 'C' (cost 0 value) and change to "CCPCP". So the final value is 1 - 0 = 1.

It's easy to prove that no strategies of buying and inserting characters can achieve a better result for the sample test cases.

 #include<stdio.h>
#include <iostream>
#include <string.h>
using namespace std; string s; int main()
{
int t,n; cin>>t;
while(t--)
{
cin>>n>>s;
int ans=;
for(int i=; i<s.size(); i++)
{
if(s[i]=='C' && s[i+]=='C' && s[i+]=='P' && s[i+]=='C')
{
ans++;
}
}
for(int i=; i<s.size(); i++)
{
if(s[i]=='C' && s[i+]=='C'&&s[i+]=='C' && s[i+]=='P' && s[i+]=='C')
{
i+=;
continue;
}
if(s[i]=='C' && s[i+]=='C'&&s[i+]=='P'&&s[i+]=='C' )
{
i+=;
continue;
} if(s[i]=='C' && s[i+]=='C' && s[i+]=='C')
{ ans++;
break;
}
if(s[i]=='C' && s[i+]=='P' && s[i+]=='C')
{ ans++;
break;
}
if(s[i]=='C' && s[i+]=='C' && s[i+]=='P')
{ ans++;
break;
}
}
cout<<ans<<endl;
}
return ;
}

You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.

You have to determine the length of the longest balanced substring of s.

Input

The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s.

The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.

Output

If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.

Examples

Input
8
11010111
Output
4
Input
3
111
Output
0

Note

In the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.

In the second example it's impossible to find a non-empty balanced substring.

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#define maxn 100005
using namespace std;
//最长子序列问题,求最长子序列为0时最长序列
int main()
{
int n,ans=,sum=;
string s;
int a[maxn];
map<int,int> mp;
cin>>n>>s; for(int i=; i<n; i++)
{
if(s[i]=='')
{
a[i+]=-;
}
else if(s[i]=='')
{
a[i+]=;
}
}
for(int i=; i<=n; i++)
{
sum+=a[i];
if(mp[sum])
{
ans=max(ans,i-mp[sum]);////如果之前存在过这个和,现在又出现了,说明这个区间1,0个数是相同的,相减为长度
}
else mp[sum]=i;
if(sum==)
ans=max(ans,i);
}
cout<<ans<<endl;
return ;
}

ACM 第九天的更多相关文章

  1. ACM 第十九天

    积性函数 积性函数线性筛,筛素数,u(n),欧拉函数: vis[]=vis[]=,mu[]=,phi[]=; ;i<=N;++i){ ,phi[i]=i-,prime[++cnt]=i; ,k= ...

  2. SCNU ACM 2016新生赛决赛 解题报告

    新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...

  3. SCNU ACM 2016新生赛初赛 解题报告

    新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...

  4. acm结束了

    最后一场比赛打完了.之前为了记录一些题目,开了这个博客,现在结束了acm,这个博客之后也不再更新了. 大家继续加油!

  5. 关于ACM的总结

    看了不少大神的退役帖,今天终于要本弱装一波逼祭奠一下我关于ACM的回忆. 从大二上开始接触到大三下结束,接近两年的时间,对于大神们来说两年的确算不上时间,然而对于本弱来说就是大学的一半时光.大一的懵懂 ...

  6. 第一届山东省ACM——Phone Number(java)

    Description We know that if a phone number A is another phone number B’s prefix, B is not able to be ...

  7. 第一届山东省ACM——Balloons(java)

    Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...

  8. ACM之鸡血篇

    一匹黑马的诞生 故事还要从南京现场赛讲起,话说这次现场赛,各路ACM英雄豪杰齐聚南京,为争取亚洲总舵南京分舵舵主之职位,都使出了看 家本领,其中有最有实力的有京城两大帮清华帮,北大帮,南郡三大派上交派 ...

  9. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

随机推荐

  1. shell入门基础&常见命令及用法

    shell shell是一个命令解释器,实际是一个程序,/bin/bash,linux中所有的命令都由它来解释,有自己的语法 shell脚本 以.sh结尾 shell语法+linux命令 注释: 单行 ...

  2. php 冒泡法 排序

    <?php /** * php 冒泡法 * @param $arr * @param string $order 排序符 * @return $arr */ function orderarr( ...

  3. python基础 抽象类(接口类)

    Python中没有接口.接口类,抽象类:定义 制定一个规范 #必须要导入from abc import ABCMeta,abstractmethod class Payment(metaclass = ...

  4. C语言链接属性总结

    1.什么是链接属性?   当组成一个程序的各个源文件分别被编译后,所有的目标文件以及那些从一个或多个函数库中引用的函数链接在一起,形成可执行程序. 标识符的链接属性决定如何处理在不同文件中出现的标识符 ...

  5. 关于条件约束问题的无偏差统计——一个偏差控制型生成器(Unbiased Statistics of a Constraint Satisfaction Problem – a Controlled-Bias Generator——by Denis Berthier)

    论文地址:https://hal.archives-ouvertes.fr/hal-00641955 Unbiased Statistics of a Constraint Satisfaction ...

  6. Java设计模式(3)——创建型模式之抽象工厂模式(Abstract Factory)

    一.概述 抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式.抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体情况下,创建多个产品族中的产品对象. UML图: 其他的过多概念不再 ...

  7. 北京Uber优步司机奖励政策(2月27日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. Spark入门(Python版)

    Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到”大数据(Big Data)”广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰富生态系统,允许使用 ...

  9. 后续博客转移到zhylj.cc

    此博客暂不更新了 zhylj.cc

  10. 利用爬虫、SMTP和树莓派3B发送邮件&续集&(爬取墨迹天气预报信息)

    -----------------------------------------------学无止境----------------------------------------------- 前 ...