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 ...
随机推荐
- 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 修改Linux系统时区
修改配置文件来修改时区1.修改/etc/sysconfig/clock ZONE=Asia/Shanghai 2.rm /etc/localtime3.链接到上海时区文件 ln -sf ...
- C#与Java中相等关系
最近在学习java的语法,发现java中两个字符串某些情况下不能用==这个来判断,比如自己定义的类中有String属性,那么就不能用==判断两个是否相等.这才开始比较C#和Java在这里的区别,最后发 ...
- Centos 下搭建SVN + Apache 服务器(转载)
安装软件包 ? 1 # yum install httpd ? 1 # yum install mod_dav_svn ? 1 # yum install subversion 2. 验证安装 ? ...
- GFS: Evolution on Fast-forward
GFS: Evolution on Fast-forward by Marshall Kirk McKusick, Sean Quinlan | August 7, 2009 A discussion ...
- Json文件放入Assets文件,读取解析并且放入listview中显示。
package com.lixu.TestJson; import android.app.Activity; import android.content.Context; import andro ...
- Unity中的Path对应各平台中的Path
OS: Application.dataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.a ...
- 用PDB库调试Python程序
Python自带的pdb库,发现用pdb来调试程序还是很方便的,当然了,什么远程调试,多线程之类,pdb是搞不定的. 用pdb调试有多种方式可选: 1. 命令行启动目标程序,加上-m参数,这样调用my ...
- ARM2440换lcd
将原来的3.5寸分辨率为240x320换为480x272所需要修改的地方 时序设置: CLKVAL=4 (VCLK =10) 5< VCLK <12 每个点扫描周期 ...
- centos=>gsutil,iptables
sudo apt-get remove --purge gsutil sudo easy_install -U pip sudo pip2 install gsutil gsutil ls gs:/ ...