Codeforces 699
Problem A Launch of Collider
题目大意
在x轴上有n个点,坐标均为偶数。每个点或向左移动或向右移动,每秒移动距离为1。
使所有点同时开始移动,求最早有点相遇的时间或无解。
解题分析
对于每一个向右移动的点,找右边最近的一个向左的点。向左移动同理。
正反扫两遍即可。
参考程序
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define N 200008
#define V 1008
#define E 60008
#define lson l,m,rt<<1
#define rson m,r+1,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
#define LL long long
//#define debug
const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/
int n;
char s[N];
int a[N],ans[N];
int main(){
scanf("%d",&n);
scanf("%s",s+);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
int x=-;
for (int i=;i<=n;i++)
if (s[i]=='R') x=a[i];
else
{
if (x==-) ans[i]=INF;
else ans[i]=(a[i]-x)/;
}
x=-;
for (int i=n;i>=;i--)
if (s[i]=='L') x=a[i];
else
{
if (x==-) ans[i]=INF;
else ans[i]=(x-a[i])/;
}
int res=INF;
for (int i=;i<=n;i++) res=min(res,ans[i]);
printf("%d\n",res==INF ? - : res); #ifdef debug
system("pause");
#endif
}
Problem B One Bomb
题目大意
有一个n*m的矩形,每一个格子为空地或者为墙壁。 (n,m<=1000)
现在要某点放置一颗炸弹,炸掉所有的墙壁。炸弹的爆炸范围为该点的上下左右两条直线。
给出一种方案或无解。
解题分析
记录一下每行的墙壁数,每列的墙壁数。
枚举每个点是否放置炸弹即可。
参考程序
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define N 1000008
#define V 1008
#define E 60008
#define lson l,m,rt<<1
#define rson m,r+1,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
#define LL long long
//#define debug
const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/ int n,m,num;
int x[V],y[V],mp[V][V];
int main(){
scanf("%d%d",&n,&m);
num=;
for (int i=;i<=n;i++){
char s[V];
scanf("%s",s+);
for (int j=;j<=m;j++)
if (s[j]=='*'){
x[i]++;
y[j]++;
mp[i][j]=;
num++;
}
}
int ok=;
int tx,ty;
for (tx=;tx<=n;tx++){
for (ty=;ty<=m;ty++){
int now;
if (mp[tx][ty]) now=x[tx]+y[ty]-; else now=x[tx]+y[ty];
if (now==num){
ok=;
break;
}
}
if (ok) break;
}
if (ok) printf("YES\n%d %d\n",tx,ty); else printf("NO\n"); #ifdef debug
system("pause");
#endif
}
Problem C Vacations
题目大意
一个小朋友每天可以选择休息或者网上打比赛或者去体育馆玩,但不能两天都刷题或者都玩耍。
给定每天是否网上有比赛,体育馆是否开。
问小朋友n天内最少需要休息几天。
解题分析
dp[i][0~2] 表示第i天干某事的最少休息天数。
分情况讨论转移。
参考程序
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define N 100008
#define V 1008
#define E 60008
#define lson l,m,rt<<1
#define rson m,r+1,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
#define LL long long
//#define debug
const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/
int min(int x,int y,int z){
if (x<=y && x<=z) return x;
if (y<=x && y<=z) return y;
if (z<=x && z<=y) return z;
}
int dp[N][],ok[N][];
int n;
int main(){
scanf("%d",&n);
for (int i=;i<=n;i++){
int x;
scanf("%d",&x);
if (x==){
ok[i][]=;
ok[i][]=;
ok[i][]=;
}
if (x==){
ok[i][]=;
ok[i][]=;
ok[i][]=;
}
if (x==){
ok[i][]=;
ok[i][]=;
ok[i][]=;
}
if (x==){
ok[i][]=;
ok[i][]=;
ok[i][]=;
}
}
for (int i=;i<=n;i++){
if (ok[i][]){
dp[i][]=min(dp[i-][],dp[i-][],dp[i-][])+;
}
else dp[i][]=INF;
if (ok[i][]){
dp[i][]=min(dp[i-][],dp[i-][]);
}
else dp[i][]=INF;
if (ok[i][]){
dp[i][]=min(dp[i-][],dp[i-][]);
}
else dp[i][]=INF;
}
printf("%d\n",min(dp[n][],dp[n][],dp[n][]));
#ifdef debug
system("pause");
#endif
}
Problem D Fix a tree
题目大意
有n个点,给定一个序列f[i]表示i号点的父亲为f[i]。
要求改变最少的f[i],使得这n个点形成一棵树。
解题分析
从每个为搜索过的点开始沿着f[i]开始搜索,如果与之前的点形成了环,说明这些点形成了一个连通块,若形成的环不是自环,则环中的某个点需要改变f[i],从而形成一棵树。
处理出所有的联通块后,假设有x个。
若其中的某个连通块的有自环j,则需改变x-1次,将其他连通块中的某个环中点f[i]改为j。
若没有自环,则需改变x次,将某个环中点改为自环,其他同上处理。
参考程序
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define N 200008
#define V 200008
#define E 60008
#define lson l,m,rt<<1
#define rson m,r+1,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
#define LL long long
//#define debug
const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/
int n,tmp,lx=;
int f[V],dfn[V],instack[V];
int ans[V];
void dfs(int x){
if (dfn[x]){
if (instack[x]==lx) ans[++ans[]]=x;
return;
}
instack[x]=lx;
dfn[x]=++tmp;
dfs(f[x]);
}
int main(){
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&f[i]); for (int i=;i<=n;i++){
lx++;
if (!dfn[i]) dfs(i);
}
lx=-;
for (int i=;i<=ans[];i++)
if (f[ans[i]]==ans[i]) lx=ans[i];
if (lx==-){
printf("%d\n",ans[]);
for (int i=;i<ans[];i++)
f[ans[i]]=ans[i+];
f[ans[ans[]]]=ans[ans[]];
}
else
{
printf("%d\n",ans[]-);
for (int i=;i<=ans[];i++)
f[ans[i]]=lx;
}
for (int i=;i<=n;i++) printf("%d%c",f[i],i==n?'\n':' '); #ifdef debug
system("pause");
#endif
}
Codeforces 699的更多相关文章
- Codeforces Round #699 (Div. 2)
A Space Navigation #include <bits/stdc++.h> using namespace std; typedef long long LL; #define ...
- CF1481X Codeforces Round #699
C Fence Painting(构造) 有用的刷子贪心刷,没用的刷子填在后续的有用/已存在的位置(用个栈记一下就行) D AB Graph(图上构造) 把边当做三种类型,aa bb ab m为奇数时 ...
- CodeForces 698C LRU
吐槽一句:这数据造得真强-. 题意:有一个大小为k的缓存区,每次从n种物品中按照一定的概率选取一种物品尝试放进去.同一个物品每一次选取的概率都是相同的.如果这种物品已经放进去过就不再放进去.如果缓存区 ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- Codeforces Round #363 (Div. 2)
A题 http://codeforces.com/problemset/problem/699/A 非常的水,两个相向而行,且间距最小的点,搜一遍就是答案了. #include <cstdio& ...
- CodeForces - 699B One Bomb
题目地址:http://codeforces.com/contest/699/problem/B 题目大意: 一个矩阵,内容由‘.’和‘*’组成(‘.’ 空,‘*’ 代表墙),墙分布在不同位置,现找出 ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
- Codeforces Round #363
http://codeforces.com/contest/699 ALaunch of Collider 题意:n个球,每个球向左或右,速度都为1米每秒,问第一次碰撞的时间,否则输出-1 贪心最短时 ...
- 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree
题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D ...
随机推荐
- 错误:Error:未定义标识符"_TCHAR"
原因:缺少头文件 解决方案:添加一条 #include <tchar.h>
- JSON Viewer
http://jsonviewer.codeplex.com/ jsoneditor https://github.com/josdejong/jsoneditor
- HTML5 视频规范简介
HTML5 视频规范简介 创建于 2013-02-03, 周日 00:56 作者 白建鹏 HTML 一词是“超文本标记语言”(Hyper-Text Markup Language)的缩写,是用于描 ...
- git的一个merge流程
git merge testSupport 合并testSupport分支代码到当前分支. 若无冲突发生,git commit -m "RM ID:5094",在git push即 ...
- JMETER JDBC操作
本文目标 1.添加测试计划 2.配置JDBC连接 3.插入数据 4.使用控制器 5.查看插入结果 1.添加测试计划 添加mysql驱动 2.添加测试计划 3.添加JDBC连接 在这里JDB ...
- mysql sql 百万级数据库优化方案
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- centos 5.5 安装 lnmp
centos5.5 安装 lnmp,一定要事先选好版本安装,建议自己下载安装. 1.相关文件目录: nginx: /www/nginx/下面mysql: /usr/share/mysql /usr/b ...
- 使用ContentResolver添加数据、查询数据
import java.util.ArrayList;import java.util.HashMap;import java.util.Map; import android.os.Bundle;i ...
- 选择最适合你的Linux学习方法
我们知道Linux只是一个内核,现在的Linux操作系统底层都是用这个内核,包括Android手机,所以Linux操作系统其实是将Linux内核与应用软件做一个打包,我们称之为Linux发行版.现在比 ...
- 喜讯!Ubuntu 16.10(Yakkety Yak) Final Beta发布喽!!!
上月三十日,代号为"Yakkety Yak"的Ubuntu 16.10发行版本的Final Beta正式上线.Canonical的开发者Steve Langasek说道:" ...