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 ...
随机推荐
- iOS 消息推送原理及实现Demo
一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Pr ...
- Linux中如何让命令在后台运行
1.在下达的命令后面加上&,就可以使该命令在后台进行工作,这样做最大的好处就是不怕被ctrl+c这个中断指令所中断. 2. 那大家可能又要问了,在后台执行的程序怎么使它恢复到前台来运行呢?很简 ...
- Prime Palindromes
题目大意:求出区间[a,b]之间的回文质数. a<=b<=10^8; 解题过程: 1.先打个素数表,新学了个 欧拉筛法,是对普通筛法的改进.普通筛法是每找到一个素数,就把它的所有倍数标记成 ...
- 常州培训 day7 解题报告
最后一天..有些感慨,这七天被虐的感动万分 第一题: 题目大意: 求出 n*i(i=1,2,3....n) mod p的逆元 n<p<=3000000 ,p是质数. 之前写过了,懒得再写 ...
- 使用windows服务和MSMQ和进行日志管理(解决高并发问题)
首先,建立一个windows服务项目 然后进行设计视图 在工作区空白处右属,添加一个安装项目 然后就可以写我们的代码了,我们的服务需要实时监视MSMQ的队列中有没有记录,如果有,就向数据库中插入 核心 ...
- ROS主题发布订阅
节点是一个可执行程序,它连接到了ROS的网络系统中.我们将会创建一个发布者,也就是说话者节点,它将会持续的广播一个信息. 改变目录到之前所建立的那个包下: cd ~/catkin_ws/src/beg ...
- julia解无忧公主的数学时间097.jl
julia解无忧公主的数学时间097.jl #=""" julia解无忧公主的数学时间097.jl http://mp.weixin.qq.com/s?__biz=MzI ...
- Cocoa Drawing
Graphics Contexts Graphics contexts are a fundamental part of the drawing infrastructure in Cocoa ap ...
- 解决C#的64位打包程序,在64位机器上运行出现BadImageFormatException异常。
转载自:http://msdn.microsoft.com/zh-cn/library/system.badimageformatexception%28v=vs.100%29.aspx BadIma ...
- hdu 2055
PS:上课的时候敲的..这道题简单..一次AC,不多说了.. 代码: #include "stdio.h"int main(){ int i,n,y; char x,a[26],b ...