ACM 第九天
动态规划1
动态规划问题是面试题中的热门话题,如果要求一个问题的最优解(通常是最大值或者最小值),而且该问题能够分解成若干个子问题,并且小问题之间也存在重叠的子问题,则考虑采用动态规划。
1、LLS 最长上升子序列
2、最大子段和
3、背包
01背包
完全背包
多重背包
混合背包
分组背包
多维背包
输出方案
输出方案种数
最优方案种数
第k优数
A - Tickets
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.
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
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
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
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.
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.
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
8
11010111
4
3
111
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 第九天的更多相关文章
- ACM 第十九天
积性函数 积性函数线性筛,筛素数,u(n),欧拉函数: vis[]=vis[]=,mu[]=,phi[]=; ;i<=N;++i){ ,phi[i]=i-,prime[++cnt]=i; ,k= ...
- SCNU ACM 2016新生赛决赛 解题报告
新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...
- acm结束了
最后一场比赛打完了.之前为了记录一些题目,开了这个博客,现在结束了acm,这个博客之后也不再更新了. 大家继续加油!
- 关于ACM的总结
看了不少大神的退役帖,今天终于要本弱装一波逼祭奠一下我关于ACM的回忆. 从大二上开始接触到大三下结束,接近两年的时间,对于大神们来说两年的确算不上时间,然而对于本弱来说就是大学的一半时光.大一的懵懂 ...
- 第一届山东省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 ...
- 第一届山东省ACM——Balloons(java)
Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...
- ACM之鸡血篇
一匹黑马的诞生 故事还要从南京现场赛讲起,话说这次现场赛,各路ACM英雄豪杰齐聚南京,为争取亚洲总舵南京分舵舵主之职位,都使出了看 家本领,其中有最有实力的有京城两大帮清华帮,北大帮,南郡三大派上交派 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
随机推荐
- HTML5页面CSS Reset
/*------------------*//*reset*//*------------------*/* {box-sizing: border-box; -webkit-tap-highligh ...
- HTML5文本
1.重要文本.斜体文本 粗体:<strong></strong> 粗体:<b></b> 斜体:<em></em> 斜体:< ...
- mybatis调用存过程返回结果集和out参数值
Mapper文件: 1.配置一个参数映射集,采用hashMap对象 2.使用call调用存储过,其中in out等标识的参数需要有详细的描述,例如:mode,JavaType,jdbcType等 &l ...
- mybatis报错:未找到参数导致绑定异常
问题: 在映射文件中使用parameterMap元素时出现以下异常: org.mybatis.spring.MyBatisSystemException: nested exception is or ...
- 在多字节的目标代码页中,没有此 Unicode 字符可以映射到的字符。 (#1113)
报错 在使用MySQL-Front导入sql文件时报错1113:在多字节的目标代码页中,没有此 Unicode 字符可以映射到的字符. (#1113) 解决方案 导入.sql文件时,单击 选择文件对话 ...
- Python学习 :正则表达式
正则表达式 python 使用正则表达式(re)来进行匹配引擎搜索 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串” 关于正则表达式 ...
- 本地域名解析知识hosts
get(本地域名解析知识点): Domain Name System: 域名系统 目的:互联网通过IP(10.223.146.45)定位浏览器建立连接,但是我们不易区别IP,为了方便用户辨识IP所代表 ...
- 转载:git和github新手安装使用教程(三步入门)
转载防止以后电脑重装,找不到记录. 教程地址:https://www.cnblogs.com/ttjsndx/p/7943444.html
- 推荐软件7 taskbar numberer,结果get了WIN相关的快捷键
作为键盘控,Win+数字直达任务栏上的应用已经让我欣喜.接下来我的问题就是每次要数数字才能确定是哪个数字,期间我尝试过按常用顺序进行排序并尝试记住它们.直到我想也许应该有个软件可以在任务栏图标处贴上一 ...
- 构建工具——maven的补充
1.安装jar到本地仓库 有时候有部分jar由于在maven的中央仓库,只能引用本地的,可以将jar安装到本地仓库进行操作(请先确保mvn命令可以正常运行) mvn install:install-f ...