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 ...
随机推荐
- adMob iAd整合,随机根据网络状况自动显示。
最近找整合的代码,找到的都不对,有个大概对的,但要奔溃退出,只要两个单独弄. adMob 下载好sdk,导入进去,iAd的加入iad framework. 使用方法,在viewController v ...
- 关于http断点续传相关的RANGE这个header
<?php //1.txt内容"1234567890" socketData('127.0.0.1','/1.txt',80,"RANGE:bytes=0-0\r\ ...
- caches 文件夹删除
模拟器 可以 删除 真机不行
- Android利用ContentProviderOperation添加联系人
Android添加联系人有两种方式: 1. 直接调用插入语句,先插入一个空Item,得到一个id,然后给这个id对应的插入其他信息,如姓名,号码,邮件等: 2. 利用ContentProviderOp ...
- wpf custom control
最近在做WPF,记录一下自定义控件的制作过程,源码请点击:源码. 1.目标 实现一个如图所示的可增减的数字框: 2.先画Template 可以在Generic.xaml中画,也可以用MergedDic ...
- IT公司100题-19-求Fibonacci数列
问题描述: 定义Fibonacci数列的定义如下: / 0 n=0f(n)= 1 ...
- wp8.1 Study1: 页面导航&页面间值传递
摘要:wp8.1与wp8中很多API是不一样了,wp8.1把以前wp7.x时的api去掉了,更多与win8.1的API相似.比如以下的页面导航和页面之间的值传递 1.页面导航 利用Frame.Navi ...
- SQL Server 索引介绍
数据库索引是对数据表中一个或多个列的值进行排序的结构,就像一本书的目录一样,索引提供了在行中快速查询特定行的能力 详细出处参考:http://www.jb51.net/article/30950.ht ...
- C语言实例代码
绘制余弦曲线和直线 #include #include int main() { double y; int x,m,n,yy; for(yy=0;yy<=20;yy++) {y=0.1*yy; ...
- jQuery Ajax之load()方法
jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是load().$.get()和$.post()方法,第3层是$.getScript()和$.getJ ...