动态规划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. 编写radware的负载配置

    radware如何添加负载服务? 笔者在新添加radware的新负载服务的时候,是习惯去看下上一个负载服务的ID 和 节点服务的ID 号 分别是多少,主要是避免ID冲突,把其他服务顶替下去,同时以后这 ...

  2. 详解 Python3 正则表达式(一)

    本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 正则表达式介绍 正则表达式(Regular expressio ...

  3. Python学习手册之数据封装、类方法、静态方法和属性函数

    在上一篇文章中,我们介绍了 Python 的内部方法.操作符重载和对象生命周期,现在我们介绍 Python 的数据封装.类方法.静态方法和属性函数.查看上一篇文章请点击:https://www.cnb ...

  4. tcp/ip五层协议

    TCP/IP协议不是TCP和IP这两个协议的合称,而是指因特网整个TCP/IP协议族.互联网协议(Internet Protocol Suite)是一个网络通信模型,以及一整个网络传输协议家族,为互联 ...

  5. Zabbix 3.4.11版本 自定义监控项

    一.实验思路过程 创建项目.触发器.图形,验证监控效果: Template OS Linux 模板基本涵盖了所有系统层面的监控,包括了我们最关注的 几项:ping.load.cpu 使用率.memor ...

  6. Python入门 (三)

    迭代器与生成器 迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器 ...

  7. ORB-SLAM(七)ORBextractor 特征提取

    该类中主要调用OpenCV中的函数,提取图像中特征点(关键点及其描述,描述子,以及图像金字塔) 参考TUM1.yaml文件中的参数,每一帧图像共提取1000个特征点,分布在金字塔8层中,层间尺度比例1 ...

  8. CRL2.1更新

    增加没有主键ID的抽象类,使能自义主键字段实现MODEL抽象类定义结构为 /// <summary> /// 基类,不包含任何字段 /// 如果有自定义主键名对象,请继承此类型 /// & ...

  9. 「LeetCode」0001-Two Sum(Ruby)

    题意与分析 题意直接给出来了:给定一个数,返回数组中和为该数(下为\(x\))的两个数的下标. 这里有一个显然的\(O(n)\)的实现:建立一个hash表,每次读入数(记作\(p\))的时候查询has ...

  10. jenkins配置git+maven+Publish over SSH

    一.配置git 1.新建项目,源码管理选择git 2.Repository URL输入git目录 3.Credentials中选择新增凭据,凭据类型选择SSH,usename输入git,passphr ...