Codeforces Round #363
http://codeforces.com/contest/699
题意:n个球,每个球向左或右,速度都为1米每秒,问第一次碰撞的时间,否则输出-1
贪心最短时间一定在RL中,R右边一定有L,L左边一定有R
// #pragma comment(linker, "/STACK:102c000000,102c000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N = 1e5+;
const int M = 1e6+;
const int MOD = 1e9+;
#define LL long long
#define mi() (l+r)>>1
double const pi = acos(-);
const double eps = 1e-;
void fre() {
freopen("in.txt","r",stdin);
} // inline int r() {
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
// } int a[];
int main(){
int n;
string s;
scanf("%d",&n);
cin>>s;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
int st=s.find('R');
if(st==-){
printf("-1\n");
exit();
}
int ans=inf;
bool flag=false;
for(int i=st+;i<(int)s.size();i++){
if(s[i]=='L'){
ans=min(ans,(a[i]-a[st])/);
flag=true;
}
else
st=i;
}
if(!flag) printf("-1\n");
else printf("%d\n",ans);
return ;
}
题意:给你一个n*m的图 问你能不能站在一个点上就把所有*覆盖完
思路:注意全是.的情况
开两个数组一个记录每行的*,另一个记录每列的*
// #pragma comment(linker, "/STACK:102c000000,102c000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N = 1e5+;
const int M = 1e6+;
const int MOD = 1e9+;
#define LL long long
#define mi() (l+r)>>1
double const pi = acos(-);
const double eps = 1e-;
void fre() {
freopen("in.txt","r",stdin);
}
// inline int r() {
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
// }
char g[][];
int x[],y[];
int main(){
// fre();
int n,m;
int sum=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {
scanf("%s",g[i]+);
for(int j=;j<=m;j++){
if(g[i][j]=='*'){
x[i]++,y[j]++,sum++;
}
}
}
int ansx,ansy;
bool flag=false;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if((x[i]+y[j]-(g[i][j]=='*'))==sum){
ansx=i,ansy=j;
flag=true;
break;
}
}
if(flag) break;
}
if(flag){
puts("YES");
printf("%d %d\n",ansx,ansy);
}
else printf("NO\n");
return ;
}
题意:0代表必须休息,1代表只能做运动,2代表只能写作业,3代表两种都可以,然后不能连续两天做同样的事情,除了休息,问你最少休息多少天
思路:贪心。。。。
// #pragma comment(linker, "/STACK:102c000000,102c000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N = 1e5+;
const int M = 1e6+;
const int MOD = 1e9+;
#define LL long long
#define mi() (l+r)>>1
double const pi = acos(-);
const double eps = 1e-;
void fre() {
freopen("in.txt","r",stdin);
}
// inline int r() {
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
// }
int a[];
int main(){
int n;
int ans=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]==) ans++;
}
for(int i=;i<=n;i++){
if(a[i]==) continue;
else if((a[i]==&&a[i-]==)||(a[i]==&&a[i-]==)) continue;
else if(a[i]==&&a[i-]==){ a[i]=;ans++;continue;}
else if(a[i]==&&a[i-]==){a[i]=;ans++;continue;}
else if(a[i]==&&a[i-]==){a[i]=;continue;}
else if(a[i]==&&a[i-]==){a[i]=;continue;}
}
printf("%d\n",ans);
return ;
}
题意:n个数,ai是i的祖先,问最小的步数把图变成一棵树
思路:
并查集判断
首先孤点可以形成根;对于环,我们把最后一条边连到根上;全是环,任选一个环上点当作根;最小步数一定是cnt-1
// #pragma comment(linker, "/STACK:102c000000,102c000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N = 2e5+;
const int M = 1e6+;
const int MOD = 1e9+;
#define LL long long
#define mi() (l+r)>>1
double const pi = acos(-);
const double eps = 1e-;
void fre() {
freopen("in.txt","r",stdin);
}
// inline int r() {
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
// }
int fa[N];
int a[N];
int find(int x){ return fa[x]==x?x:fa[x]=find(fa[x]);}
int main(){
int n;
int cnt=,root=-;
scanf("%d",&n);
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]==i){root=i;cnt++;}
else{
int x=find(i),y=find(a[i]);
if(x==y){
cnt++;
a[i]=i;
}
else
fa[x]=y;
}
}
if(root==-){
for(int i=;i<=n;i++){
if(a[i]==i){
root=i;
cnt++;
break;
}
}
}
printf("%d\n",cnt-);
for(int i=;i<=n;i++){
if(a[i]==i) a[i]=root;
printf("%d%c",a[i],i==n?'\n':' ');
}
return ;
}
Codeforces Round #363的更多相关文章
- Codeforces Round 363 Div. 1 (A,B,C,D,E,F)
Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...
- Codeforces Round #363 (Div. 2)
A题 http://codeforces.com/problemset/problem/699/A 非常的水,两个相向而行,且间距最小的点,搜一遍就是答案了. #include <cstdio& ...
- Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环
题目链接:http://codeforces.com/problemset/problem/698/B题意:告诉你n个节点当前的父节点,修改最少的点的父节点使之变成一棵有根树.思路:拆环.题解:htt ...
- Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...
- Codeforces Round #363 (Div. 2) B. One Bomb —— 技巧
题目链接:http://codeforces.com/contest/699/problem/B 题解: 首先统计每行每列出现'*'的次数,以及'*'出现的总次数,得到r[n]和c[m]数组,以及su ...
- Codeforces Round #363 (Div. 2) C. Vacations —— DP
题目链接:http://codeforces.com/contest/699/problem/C 题解: 1.可知每天有三个状态:1.contest ,2.gym,3.rest. 2.所以设dp[i] ...
- Codeforces Round #363 (Div. 2)A-D
699A 题意:在一根数轴上有n个东西以相同的速率1m/s在运动,给出他们的坐标以及运动方向,问最快发生的碰撞在什么时候 思路:遍历一遍坐标,看那两个相邻的可能相撞,更新ans #include< ...
- Codeforces Round #363 LRU(概率 状压DP)
状压DP: 先不考虑数量k, dp[i]表示状态为i的概率,状态转移方程为dp[i | (1 << j)] += dp[i],最后考虑k, 状态表示中1的数量为k的表示可行解. #incl ...
- Codeforces Round #363 Fix a Tree(树 拓扑排序)
先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cs ...
随机推荐
- FireMonkey支持的机型
酷派5890(android 4.1.2) 从截图上看,正常.不知道为啥说不行.海信 T96(android 4.0.3) CPU 不支持 NEON.没辙.摩托罗拉XT885(android 4.0. ...
- PowerDesigner连接Oracle数据库建表序列号实现自动增长
原文:PowerDesigner连接Oracle数据库建表序列号实现自动增长 创建表就不说了.下面开始介绍设置自动增长列. 1 在表视图的列上创建.双击表视图,打开table properties — ...
- 修改 eclipse 文件编码格式
如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简 ...
- 58. Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- C++:静态成员
3.7.1 静态数据成员对象是类的一个实例,每个对象都具有自己的数据成员.例如,学生类张三或李四都具有自己的学号,姓名和平均成绩.在实际使用时,常常还需要一些其他的数据项,比如学生人数.总成绩.平均成 ...
- Android ListView高度自适应和ScrollView冲突解决
在ScrollView中嵌套使用ListView,ListView只会显示一行到两行的数据.起初我以为是样式的问题,一直在对XML文件的样式进行尝试性设置,但始终得不到想要的效果.后来在网上查了查,S ...
- 转ATL对象类型
http://hi.baidu.com/rural_child/item/d91ce5d8fba9c8e73cc2cbf9 1.Objects a.Simple Object:用于实现业务逻辑,无用户 ...
- 【Spring】如何在单个Boot应用中配置多数据库?
原创 BOOT 为什么需要多数据库? 默认情况下,Spring Boot使用的是单数据库配置(通过spring.datasource.*配置具体数据库连接信息).对于绝大多数Spring Boot应用 ...
- kafka的环境搭建
kafka是一个高吞吐量的消息系统.隔离消息接收和处理过程(可理解为一个缓存) 1.kafka伪分布的部署 1.1.下载并解压 1.2.启动zk bin/zookeeper-server-start. ...
- 自定义View(3)关于canas.drawText
本文以Canvas类的下面这个函数为基础,它用来在画布上绘制文本. public void drawText(String text, float x, float y, Paint paint) 效 ...