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 ...
随机推荐
- python 基于Anaconda import numpy 报错 Importing the multiarray numpy extension module failed.
在windows中安装了 Anaconda 运行时报错 原因是系统环境变量起初并没有引入 E:\Tools\Anaconda\Library\bin 解决办法: 在系统环境变量中加入 E:\To ...
- 『Linux基础 - 4 』linux常用命令(1)
这篇笔记包含以下知识点: 几个概念的理解:Linux命令,控制台,终端, 终端提示符 对文件目录的操作的相关命令: 切换目录,列出目录下的文件等 对文件的操作的相关命令: 创建,删除,复制,修改,移动 ...
- VMWare共享文件
windows与虚拟机的linux共享windows下的一个文件夹 1.重新安装VMware Tools,在VMware面板上选择“虚拟机-重新安装VMware tools…” 2.使用命令 Ctrl ...
- Alexander的Python机器学习 之目录分析。
无聊,顺应一下潮流,学习一下python机器学习吧. 买了一本书,首先分析一下目录吧. 1.第一章是 Python机器学习的生态系统. 1.1.数据科学或机器学习的工作流程. 然后又分成6点进行详细说 ...
- 使用putty远程登录Ubuntu时,报Network error:Connection refused错误及解决
putty远程登录Ubuntu,弹出Network error:Connection refused的错误提示框,就是因为Ubuuntu没有安装ssh服务. 执行命令: sudo apt instal ...
- day 2 飞机大战原理
1. 程序的图片的坐标 (左上角为顶点) 2.图片变成动态的 3.集成显卡 和独立显卡
- struts2-01:作用域传值
方式一.使用ServletActionContext(耦合度高,不建议使用) public String login(){ ServletActionContext.getRequest().getS ...
- Kubernetes网络方案的三大类别和六个场景
欢迎访问网易云社区,了解更多网易技术产品运营经验. 本文章根据网易云资深解决方案架构师 王必成在云原生用户大会上的分享整理. 今天我将分享个人对于网络方案的理解,以及网易云在交付 Kubernetes ...
- 「日常训练」Kefa and Dishes(Codeforces Round #321 Div. 2 D)
题意与分析(CodeForces 580D) 一个人有\(n\)道菜,然后要点\(m\)道菜,每道菜有一个美味程度:然后给你了很多个关系,表示如果\(x\)刚好在\(y\)前面做的话,他的美味程度就会 ...
- Windows运行机理——主程序—WinMain
Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 在windows 操作系统下,用C 或者C++来编写MS-DOS 应用 ...