Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列
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.
题意:给你一个长度为n的串 统计黑色块的个数以及长度 输出
题解:水
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int N=;
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
//typedef long long ll;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int n;
char a[];
int ans[];
int main()
{
scanf("%d",&n);
scanf("%s",a+);
int exm=;
for(int i=;i<=n;i++)
{
int j=i;
if(a[i]=='B')
{
int jishu=;
for(j=i;;j++)
if(a[j]=='B')
jishu++;
else
break;
ans[exm++]=jishu;
}
i=j;
}
printf("%d\n",exm);
if(exm)
{
printf("%d",ans[]);
for(int i=;i<exm;i++)
printf(" %d",ans[i]);
cout<<endl;
}
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 5 seconds 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个不同的密码 以及一个正确的密码(来自这n个密码) 尝试密码的规则
1.由密码短的到长的
2.每尝试一次耗时1s
3.每尝试失败k次等待5s
求密码输入正确的 最短时间和最长时间
题解: 存储n个密码 按照密码的长度排序 找到(正确密码的长度)的最小序号st和最大序号ed
输出最短时间 (st-1)/k*5+st 最长时间 (ed-1)/k*5+ed
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
//typedef long long ll;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
struct node
{
int len;
char a[];
}N[],M;
bool cmp(struct node aa,struct node bb)
{
return aa.len<bb.len;
}
int n,k;
char b[];
int main()
{
scanf("%d %d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%s",N[i].a);
N[i].len=strlen(N[i].a);
}
scanf("%s",b);
int l=strlen(b);
sort(N+,N++n,cmp);
int st,ed;
int flag=;
for(int i=;i<=n;i++)
{
if(N[i].len==l&&flag==)
{
st=i;
ed=i;
flag=;
}
if(N[i].len==l&&flag==)
{
ed=i;
}
}
cout<<(st-)/k*+st<<" "<<(ed-)/k*+ed<<endl;
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 from 1 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 no cyclic 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 T time 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
1 3 5
题意:n个点 m条单向边 每条边有一个权值 起点为1终点为n 输出使得权值和小于等于t的路径并且要求路径含有的节点尽量多
题解:前向星存图 dp[i][j]表示以节点i结尾的经过j个节点的权值和 dfs出dp[i][j]的最小值
pd[i][j] 记录状态(i,j)的父亲节点 记录路径。
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
//typedef long long ll;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
struct node
{
int pre;
int to;
int w;
}N[];
int nedge=;
int pre[];
int dp[][];
int pd[][];
vector<int >ans;
void add(int a,int b,int c)
{
nedge++;
N[nedge].to=b;
N[nedge].w=c;
N[nedge].pre=pre[a];
pre[a]=nedge;
}
int n,m,k;
int aa,bb,cc;
void dfs1(int pos,int num,int we,int father)
{
if(dp[pos][num]<=we) return ;
dp[pos][num]=we;pd[pos][num]=father;
for(int i=pre[pos];i;i=N[i].pre)
{
if(we+N[i].w<=k)
dfs1(N[i].to,num+,we+N[i].w,pos);
}
}
void dfs2(int pos,int num)
{
ans.push_back(pos);
if(pd[pos][num]==-)
{
printf("%d\n",ans.size());
for(int i=ans.size()-;i>=;i--)
printf("%d ",ans[i]);
printf("\n");
return ;
}
else
dfs2(pd[pos][num],num-);
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
dp[i][j]=1e9+;
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&aa,&bb,&cc);
add(aa,bb,cc);
}
dfs1(,,,-);
for(int i=n;i>=;i--)
{
if(dp[n][i]<=k)
{
dfs2(n,i);
break;
}
}
return ;
}
2 seconds
256 megabytes
standard input
standard output
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n) and replaces the i-th element of array ai either with ai + x or with ai - x. Please note that the operation may be applied more than once to the same position.
Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. ) can reach, if Maxim would apply no more than k operations to it. Please help him in that.
The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) — the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively.
The second line contains n integers a1, a2, ..., an () — the elements of the array found by Maxim.
Print n integers b1, b2, ..., bn in the only line — the array elements after applying no more than k operations to the array. In particular, should stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum possible.
If there are multiple answers, print any of them.
5 3 1
5 4 3 5 2
5 4 3 5 -1
5 3 1
5 4 3 5 5
5 4 0 5 5
5 3 1
5 4 4 5 5
5 1 4 5 5
3 2 7
5 4 2
题意:n个数 k次操作 每次操作为 对某一个数增加x或减少x 现在要求n个数连乘的值尽可能的小
输出k次操作之后的n个数
题解:首先需要判断当前的n个数的连乘结果ans的正负,若ans为正数 则需要最小的操作数使得ans变为负数
对于每一个值都记录了 pos位置 w绝对值 what正负 优先队列存储 按照绝对值升序排列。如果将ans变为负数(能够变为的最大的负数)
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
//typedef long long ll;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
struct node
{
ll w;
int pos;
int what;
friend bool operator < (node a, node b)
{
return a.w > b.w;
}
} N[];
priority_queue<node> pq;
ll n,k,x;
node s1,s2,s3,s4;
ll ans[];
int main()
{
scanf("%I64d %I64d %I64d",&n,&k,&x);
ll exm;
int gg=;
for(int i=; i<=n; i++)
{
scanf("%I64d",&exm);
if(exm<)
{
N[i].w=-exm;
N[i].pos=i;
N[i].what=;
gg=gg*(-);
}
else
{
N[i].w=exm;
N[i].what=;
N[i].pos=i;
}
pq.push(N[i]);
}
if(gg==)//正数
{
s1=pq.top();
pq.pop();
if(s1.w>=x*k)//无法变负
{
ans[s1.pos]=s1.w-x*k;
k=;//操作数变为0
}
else
{
ll ggg=(s1.w+x)/x;//变为最大的负数需要的操作数
k-=ggg;
s2.w=-(s1.w-ggg*x);
s2.pos=s1.pos;
if(s1.what)//注意当前这个数的符号的变化
s2.what=;
else
s2.what=;
pq.push(s2);//重新入队
}
}
while(!pq.empty())
{
if(k==)
break;
s1=pq.top();
pq.pop();
s2.w=s1.w+x;
s2.pos=s1.pos;
s2.what=s1.what;
pq.push(s2);
k--;//操作数减少
}
while(!pq.empty())
{
s1=pq.top();
pq.pop();
if(s1.what)
ans[s1.pos]=s1.w;//通过位置找到答案
else
ans[s1.pos]=-s1.w;
}
printf("%I64d",ans[]);
for(int i=; i<=n; i++)
{
printf(" %I64d",ans[i]);
}
printf("\n");
return ;
}
Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列的更多相关文章
- 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 ...
- 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 #301 (Div. 2)A B C D 水 模拟 bfs 概率dp
A. Combination Lock time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 #622 (Div. 2) A. Fast Food Restaurant(全排列,DFS)
Codeforces Round #622 (Div. 2) A. Fast Food Restaurant 题意: 你是餐馆老板,虽然只会做三道菜,上菜时还有个怪癖:一位客人至少上一道菜,且一种菜最 ...
- 拓扑序+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 ...
随机推荐
- 10 notorious computer virus
The history of computer virus is the same as computer history. With more and more powerful computers ...
- POJ 1050 To the Max 暴力,基础知识 难度:0
http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...
- 在Linux中查看文件的编码及对文件进行编码转换
如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8.下面介绍一下,在Li ...
- HDU 4087 三维上的平移缩放旋转矩阵变化
题目大意: 就是根据它给的程序的要求,不断平移,缩放,旋转三维的点,最后计算出点的位置 这里主要是要列出三种转换方式的齐次矩阵描述 平移translate tx ty tz1 0 0 00 1 0 0 ...
- 继承自NSObject的类不能用CGRect
我用的是Xcode6.2. 系统默认没有pch文件. 所以没有自动导入UIKit包. 我在继承NSObject类里也不能用CGRect或者UI开头的控件,原因也是Xcode6.2以后版本 缺少UIKi ...
- Cisco IOS basic system management command reference
absolute : to specify an absolute time for a time-range (in time-range configuration mode) no absolu ...
- android 单选、多选弹出菜单
菜单单选窗口: import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInte ...
- mysql 命令行操作
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...
- hdu 2051
ps:这道题是题目坑爹了...题目说不考虑n=0的...但其实要考虑...醉了 中文意思:输入一个10进制的数,输出他的二进制数 代码: #include "stdio.h"int ...
- 多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量
多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量,并且数组变量可以直接取到每一个元素var array1 = '<%=yearList =>'; ...