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 ...
随机推荐
- 用Google Analytics跟踪JavaScript Errors (译)
通过custom events来实施 // Track basic JavaScript errors window.addEventListener('error', function(e) { _ ...
- urlrewrite伪静态 及多参数传递-附正则表达式语法 [轉]
首先 加载 urlrewrite包 配置web.xml [list] [*] <error-page> [*] <error-code>404</ ...
- Htmlhelper—CheckBox自动生成两个input
前言 在之前的一篇文章中小猪分享了Htmlhelper的用法.其中有意思的一个就是Checkbox,有必要单独拿出来讲一讲. Htmlhelper—CheckBox 细心的读者一定发现了当使用类似语法 ...
- vbox内部linux :centos5.5与外部ping通(相互),而且域名访问
1 相互ping通:不能使用nat,nat只能单向通,虚拟机不能ping通主机,选择桥接: 如图: 2然后设置 ip:最好设置静态ip这样下次不用再改,这里我们只演示使用eth0网卡,=> vi ...
- java面向对象编程— —第七章 继承
7.1继承的起源 继承(Inheritance),即在面向对象编程中,可以通过扩展(extends)一个已有的类,并继承该类的属性的行为,来创建一个新的类. 已有的类称为父类(也可以称为基类,超类), ...
- 封装自己的JS库
一.基础知识 1.点击计数 第一种: var aBtn=document.getElementsByTagName('input'); var i=0; for(i=0;i<aBtn.lengt ...
- 内工大acm校赛--整理代码
题目:小明搜到一行无缩进无换行代码,请帮小明整理代码.无for语句和case语句,而且只有一个主函数.你只要控制注意“:”“{”“}”这三个符号带来的缩进和换行效果就行. Input: 输入只有一行, ...
- 【C语言学习】-05 二维数组、字符串数组、多维数组
⼆二维数组.字符串数组.多维数组
- win10 用微软账户登录无法访问共享的问题
百度找了一大堆可以解决的,最终最简单的方式(可能是bug): 测试了一下,Win10用微软账户登录的,连局域网共享时,输入用户名的时候,前面加个乱七八糟的域名就可以访问了: 比如: 用户名: ba ...
- Yslow&PageSpeed– 诊断各种缓慢症状
Google的PageSpeed和yahoo的yslow是各位不可少的前端工具(同样也都是firebug的插件,安装了firebug之后才可以拥有她们),当各位无法用三寸不烂之舌收拾产品和各种大佬的时 ...