Codeforces Round #374 (Div. 2) A , B , C 水,水,拓扑dp
1 second
256 megabytes
standard input
standard output
Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a × b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents how many groups of black squares there are in corresponding row or column, and the integers themselves represents the number of consecutive black squares in corresponding group (you can find more detailed explanation in Wikipedia https://en.wikipedia.org/wiki/Japanese_crossword).
Adaltik decided that the general case of japanese crossword is too complicated and drew a row consisting of n squares (e.g. japanese crossword sized 1 × n), which he wants to encrypt in the same way as in japanese crossword.
The example of encrypting of a single row of japanese crossword.
Help Adaltik find the numbers encrypting the row he drew.
The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the length of the row. The second line of the input contains a single string consisting of n characters 'B' or 'W', ('B' corresponds to black square, 'W' — to white square in the row that Adaltik drew).
The first line should contain a single integer k — the number of integers encrypting the row, e.g. the number of groups of black squares in the row.
The second line should contain k integers, encrypting the row, e.g. corresponding to sizes of groups of consecutive black squares in the order from left to right.
3
BBW
1
2
5
BWBWB
3
1 1 1
4
WWWW
0
4
BBBB
1
4
13
WBBBBWWBWBBBW
3
4 1 3
The last sample case correspond to the picture in the statement.
题意:输出连续B的个数;
思路:模拟;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+,mod=1e9+;
char a[N];
vector<int>ans;
int main()
{
int n;
scanf("%d",&n);
scanf("%s",a);
for(int i=;i<n;i++)
{
int sum=;
if(a[i]=='W')continue;
while(a[i]=='B')
{
sum++;
i++;
}
ans.push_back(sum);
}
cout<<ans.size()<<endl;
for(int i=;i<ans.size();i++)
cout<<ans[i]<<" ";
return ;
}
2 seconds
256 megabytes
standard input
standard output
Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.
Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.
Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.
Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).
The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.
The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.
The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.
Print two integers — time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.
5 2
cba
abc
bb1
abC
ABC
abc
1 15
4 100
11
22
1
2
22
3 4
Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.
Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all.
题意:给你一个n个字符串,从长度小的到大的输入,每k次会等待五秒,求最坏和最好的情况;
思路:模拟;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+,mod=1e9+;
char ch[][];
char pass[N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",&ch[i]);
int flag=,k=;
scanf("%s",pass);
for(int i=;i<=n;i++)
{
if(strlen(ch[i])<strlen(pass))
flag++;
if(strcmp(ch[i],pass)==)
k++;
if(strlen(ch[i])>strlen(pass))
k++;
}
n=n-k+;
int ans=;
while(n)
{
ans+=min(n,m);
n-=min(n,m);
if(n==)
break;
ans+=;
}
printf("%d %d\n",flag++(flag/m)*,ans);
return ;
}
3 seconds
256 megabytes
standard input
standard output
Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from1 to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are nocyclic routes between showplaces.
Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units.
Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than Ttime units passing it.
The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000, 1 ≤ m ≤ 5000, 1 ≤ T ≤ 109) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.
The next m lines describes roads in Berlatov. i-th of them contains 3 integers ui, vi, ti (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ ti ≤ 109), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes.
It is guaranteed, that there is at most one road between each pair of showplaces.
Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line.
Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.
If there are multiple answers, print any of them.
4 3 13
1 2 5
2 3 7
2 4 8
3
1 2 4
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
4
1 2 4 6
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
3
1 3 5
题意:给你一个图,n个点,m条边,总时间T;求在T时间内能走过最多的点从1-n;
思路:拓扑dp;dp[i][j]表示从1-i经过j个点花费的时间;
坑点:有无关的点;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=2e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int dp[][];
int du[N];
struct is
{
int v,w;
int next;
}edge1[N],edge2[N] ;
int jiedge1,jiedge2;
int head1[N],head2[N];
int flag[N];
int n,m,T;
void init()
{
memset(du,,sizeof(du));
memset(head1,,sizeof(head1));
memset(head2,,sizeof(head2));
memset(flag,,sizeof(flag));
jiedge1=;
jiedge2=;
}
void add1(int u,int v,int w)
{
++jiedge1;
edge1[jiedge1].v=v;
edge1[jiedge1].w=w;
edge1[jiedge1].next=head1[u];
head1[u]=jiedge1;
}
void add2(int u,int v,int w)
{
++jiedge2;
edge2[jiedge2].v=v;
edge2[jiedge2].w=w;
edge2[jiedge2].next=head2[u];
head2[u]=jiedge2;
}
void bfs(int u)
{
queue<int>q;
q.push(u);
dp[][]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head1[u];i;i=edge1[i].next)
{
int v=edge1[i].v;
int w=edge1[i].w;
for(int t=;t<=n;t++)
{
if(dp[u][t-]+w<=T)
{
dp[v][t]=min(dp[v][t],dp[u][t-]+w);
}
}
if(--du[v]==&&!flag[v])q.push(v);
}
}
}
int ans[N];
void getans(int u,int x)
{
printf("%d\n",x);
int flag=x;
while(u!=)
{
for(int i=head2[u];i;i=edge2[i].next)
{
int v=edge2[i].v;
int w=edge2[i].w;
if(w+dp[v][x-]==dp[u][x])
{
ans[x]=v;
x--;
u=v;
break;
}
}
}
for(int i=;i<=flag;i++)
printf("%d ",ans[i]);
printf("%d\n",n);
}
int main()
{
scanf("%d%d%d",&n,&m,&T);
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add1(u,v,w);
add2(v,u,w);
du[v]++;
}
for(int i=;i<=n;i++)for(int t=;t<=n;t++)dp[i][t]=inf;
queue<int>q;
for(int i=;i<=n;i++)
if(du[i]==)
q.push(i),flag[i]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head1[u];i;i=edge1[i].next)
{
int v=edge1[i].v;
du[v]--;
if(du[v]==&&v!=)
q.push(v),flag[v]=;
}
}
bfs();
for(int i=n;i>=;i--)
if(dp[n][i]<inf)
{
getans(n,i);
break;
}
return ;
}
Codeforces Round #374 (Div. 2) A , B , C 水,水,拓扑dp的更多相关文章
- Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crosswor 水题
A. One-dimensional Japanese Crossword 题目连接: http://codeforces.com/contest/721/problem/A Description ...
- Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列
A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- 拓扑序+dp Codeforces Round #374 (Div. 2) C
http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- Codeforces Round #374 (Div. 2) B. Passwords 贪心
B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...
随机推荐
- EasyDSS流媒体服务器软件(支持RTMP/HLS/HTTP-FLV/视频点播/视频直播)-正式环境安装部署攻略
EasyDSS流媒体服务器软件,提供一站式的转码.点播.直播.时移回放服务,极大地简化了开发和集成的工作. 其中,点播功能主要包含:上传.转码.分发.直播功能,主要包含:直播.录像, 直播支持RTMP ...
- Linux中的日志分析及管理
日志文件对于诊断和解决系统中的问题很有帮助,因为在Linux系统中运行的程序通常会把系统消息和错误消息写入相应的日志文件,这样系统一旦出现问题就会“有据可查”.此外,当主机遭受攻击时,日志文件还可以帮 ...
- SpringBoot-------实现多数据源Demo
之前SpringBoot出来时候就看了下Springboot,感觉的确精简掉了配置文件! 还是很方便的!没办法,我只是个菜鸟! 什么怎么启动Springboot什么的就不说了, 具体的Demo地址我都 ...
- 在ie和chrome浏览器中滚动条样式的设置
1.IE下设置滚动条样式的属性 scrollbar-arrow-color: color; /*三角箭头的颜色*/scrollbar-face-color: color; /*立体滚动条的颜色(包括箭 ...
- jquery on 确认删除
$(document).on('click', '.delbtn', function() { if (confirm("确定要删除吗?")) { ...
- Retrofit2.2说明-简单使用
很久前就想学习下Retrofit了,不过总是没有时间,正好最近新项目要用到网络请求,正好研究了下Retrofit2.2的简单使用方法,大致记录如下: Retrofit与okhttp共同出自于Squar ...
- 爬虫,如何防止被ban之策略大集合
话说在尝试设置download_delay小于1,并且无任何其他防止被ban的策略之后,我终于成功的被ban了. 关于scrapy的使用可参见之前文章: http://blog.csdn.net/u0 ...
- Nginx和php-fpm部署到不同的服务器
Nginx安装滤过,基本上nginx上的配置很少,只要添加个server就可以了,主要安装php-fpm服务 php7.1.3安装 1.安装依赖的软件包yum -y install gcc gcc-c ...
- Python学习笔记3_数据类型
Python数据类型:数字.字符串.列表.元祖.字典 一.数字类型:(整型.长整型.浮点型.复数型) 1.整型(int):表示范围-2,147,483,648到2,147,483,647 2.长整型( ...
- javascript高级语法二
一.BOM对象 1.什么是BOM对象? BOM是浏览器对象模型,核心对象就是window,所有浏览器都支持 window 对象.一个html文档对应一个window对象,主要功能是控制浏览器窗口的, ...