codeforces#516 Div2---ABCD
A---Make a triangle!
http://codeforces.com/contest/1064/problem/A
题意:
给定三个整数表示三角形的边。每次给边长可以加一,问至少要加多少才能使这三个边成为一个三角形。
思路:
找到最大的边,然后最大边 + 1减剩下两条边就行了。负数的话就是0。
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<bits/stdc++.h> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int a, b, c; int main(){ while(scanf("%d%d%d", &a, &b, &c) != EOF){
int m = max(a, b);
m = max(m, c);
int ans = ;
if(m == a){
if(b + c > m){
ans = ;
}
else{
ans = m + - b - c;
}
}
else if(m == b){
if(a + c > m){
ans = ;
}
else{
ans = m + - a - c;
}
}
else{
if(a + b > m){
ans = ;
}
else{
ans = m + - a - b;
}
}
printf("%d\n", ans);
}
return ;
}
B---Equations of Mathematical Magic
http://codeforces.com/contest/1064/problem/B
题意:
给定一个a, 使得a - (a ^ x) - x = 0.问能有多少的x。
思路:
如果a的某一位是1, x的这一位是1,那么a^x这一位是0,a-(a^x)-x这一位是0
如果a的某一位是1, x的这一位是0, 那么a^x这一位是1,a-(a^x)-x这一位是0
如果a的某一位是0, x的这一位是1, 那么a^x这一位是1,a-(a^x)-x这一位是1
如果a的某一位是0, x的这一位是0,那么a^x这一位是0,a-(a^x)-x这一位是0
所以统计一下a的1的个数
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<bits/stdc++.h> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int t;
LL a; int main(){ while(scanf("%d", &t) != EOF){
for(int i = ; i < t; i++){
scanf("%I64d", &a);
LL cnt = ;
while(a){
if(a & ){
cnt <<= ;
}
a >>= ;
} printf("%I64d\n", cnt);
} } return ;
}
C---Oh Those Palindromes
http://codeforces.com/contest/1064/problem/C
题意:
给定一个字符串,使得他的是回文的子串数目最多,问应如何重新排列这个字符串。
思路:
让同一个字母都放在一起,输出就行了。sort一下就行,但是我竟然还统计了一下个数,蠢了。
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<bits/stdc++.h> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n;
const int maxn = 1e5 + ;
char str[maxn];
int cnt[]; int main(){ while(scanf("%d", &n) != EOF){
//scanf("%s", str);
cin>>str;
memset(cnt, , sizeof(cnt));
for(int i = ; i < n; i++){
cnt[str[i] - 'a']++;
}
for(int i = ; i < ; i++){
for(int j = ; j < cnt[i]; j++){
printf("%c", i+'a');
}
}
printf("\n");
}
return ;
}
D---Labyrinth
http://codeforces.com/contest/1064/problem/D
题意:
一个n*m的格子,有的格子有障碍不能经过。现在从(r, c)处出发,向左最多只能走x次,向右最多走y次,问可以到达的格子有多少个。
思路:
最开始想的是dfs直接更新啥的。但是由于有先后顺序的问题,所以更新的时候左右的次数是不能直接减1的。需要用数组记录一下最大的左右次数。
而且如果发现vis过了这个格子就不更新了的话,也还是有先后顺序的问题。只有当经过这个格子的左右次数比原来大的时候才继续dfs更新这个格子。
但是这样dfs会T。
czc bfs过了,明天试一下....
先贴一下T了的代码。
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<bits/stdc++.h> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n, m, r, c, x, y, cnt;
const int maxn = ;
bool vis[maxn][maxn];
int leftl[maxn][maxn], leftr[maxn][maxn];
int mvi[] = {, , , -};
int mvj[] = {, -, , };
bool g[maxn][maxn]; void dfs(int posi, int posj)
{
if(!vis[posi][posj]){
vis[posi][posj] = true;
//cout<<posi<<" "<< posj<<endl;
cnt++;
}
for(int k = ; k < ; k++){
int di = posi + mvi[k];
int dj = posj + mvj[k];
int mostl,mostr;
if(di > && dj > && di <= n && dj <= m && g[di][dj]){
if(k == ){//向右
mostr = leftr[posi][posj] - ;
mostl = leftl[posi][posj];
if(mostr > leftr[di][dj] && mostr >= ){
leftr[di][dj] = mostr;
leftl[di][dj] = mostl;
dfs(di, dj);
}
}
else if(k == ){
mostl = leftl[posi][posj] - ;
mostr = leftr[posi][posj];
if(mostl > leftl[di][dj] && mostl >= ){
leftl[di][dj] = mostl;
leftr[di][dj] = mostr;
dfs(di, dj);
}
}
else{
mostl = leftl[posi][posj];
mostr = leftr[posi][posj];
if(mostl > leftl[di][dj]){
leftl[di][dj] = mostl;
leftr[di][dj] = mostr;
dfs(di, dj);
}
}
}
}
} int main(){ while(scanf("%d%d", &n, &m) != EOF){
scanf("%d%d", &r, &c);
scanf("%d%d", &x, &y);
cnt = ;
memset(vis, , sizeof(vis));
memset(leftr, -, sizeof(leftr));
memset(leftl, -, sizeof(leftl)); for(int i = ; i <= n; i++){
char str[maxn];
scanf("%s", str);
for(int j = ; j < m; j++){
if(str[j] == '*'){
g[i][j + ] = false;
}
else{
g[i][j + ] = true;
}
}
} leftl[r][c] = x;
leftr[r][c] = y;
dfs(r, c);
printf("%d\n", cnt);
}
return ;
}
bfs写的 1A代码
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<bits/stdc++.h> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n, m, r, c, x, y, cnt;
const int maxn = ;
bool vis[maxn][maxn];
int leftl[maxn][maxn], leftr[maxn][maxn];
int mvi[] = {, , , -};
int mvj[] = {, -, , };
bool g[maxn][maxn]; void dfs(int posi, int posj)
{
if(!vis[posi][posj]){
vis[posi][posj] = true;
//cout<<posi<<" "<< posj<<endl;
cnt++;
}
for(int k = ; k < ; k++){
int di = posi + mvi[k];
int dj = posj + mvj[k];
int mostl,mostr;
if(di > && dj > && di <= n && dj <= m && g[di][dj]){
if(k == ){//ÏòÓÒ
mostr = leftr[posi][posj] - ;
mostl = leftl[posi][posj];
if(mostr > leftr[di][dj] && mostr >= ){
leftr[di][dj] = mostr;
leftl[di][dj] = mostl;
dfs(di, dj);
}
}
else if(k == ){
mostl = leftl[posi][posj] - ;
mostr = leftr[posi][posj];
if(mostl > leftl[di][dj] && mostl >= ){
leftl[di][dj] = mostl;
leftr[di][dj] = mostr;
dfs(di, dj);
}
}
else{
mostl = leftl[posi][posj];
mostr = leftr[posi][posj];
if(mostl > leftl[di][dj]){
leftl[di][dj] = mostl;
leftr[di][dj] = mostr;
dfs(di, dj);
}
}
}
}
} struct node{
int i, j;
node(){}
node(int _i, int _j):i(_i), j(_j){}
}; void bfs()
{
queue<node>que;
que.push(node(r, c));
vis[r][c] = true;
cnt = ;
while(!que.empty()){
node now = que.front();
que.pop();
for(int k = ; k < ; k++){
int di = now.i + mvi[k], dj = now.j + mvj[k];
if(di > && di <= n && dj > && dj <= m && g[di][dj]){
/*if(!vis[di][dj]){
vis[di][dj] = true;
cnt++;
}*/
if(k == ){
int mostl = leftl[now.i][now.j], mostr = leftr[now.i][now.j] - ;
if(mostr >= && !vis[di][dj]){
vis[di][dj] = true;
cnt++;
}
if(mostr >= && mostr > leftr[di][dj]){
que.push(node(di, dj));
leftl[di][dj] = mostl;
leftr[di][dj] = mostr;
}
}
else if(k == ){
int mostl = leftl[now.i][now.j] - , mostr = leftr[now.i][now.j];
if(mostl >= && !vis[di][dj]){
vis[di][dj] = true;
cnt++;
}
if(mostl >= && mostl > leftl[di][dj]){
que.push(node(di, dj));
leftl[di][dj] = mostl;
leftr[di][dj] = mostr;
}
}
else{
int mostl = leftl[now.i][now.j], mostr = leftr[now.i][now.j];
if(!vis[di][dj]){
vis[di][dj] = true;
cnt++;
}
if(mostl > leftl[di][dj]){
que.push(node(di, dj));
leftl[di][dj] = mostl;
leftr[di][dj] = mostr;
}
}
}
}
}
printf("%d\n", cnt);
} int main(){ while(scanf("%d%d", &n, &m) != EOF){
scanf("%d%d", &r, &c);
scanf("%d%d", &x, &y);
cnt = ;
memset(vis, , sizeof(vis));
memset(leftr, -, sizeof(leftr));
memset(leftl, -, sizeof(leftl)); for(int i = ; i <= n; i++){
char str[maxn];
scanf("%s", str + );
for(int j = ; j <= m; j++){
if(str[j] == '*'){
g[i][j] = false;
}
else{
g[i][j] = true;
}
}
} leftl[r][c] = x;
leftr[r][c] = y;
bfs();
//cout<<"!"<<endl;
//dfs(r, c);
//printf("%d\n", cnt);
}
return ;
}
上了31分,ABC都卡了一下,C还RE了一发,还是太菜了啊。
codeforces#516 Div2---ABCD的更多相关文章
- Codeforces #107 DIV2 ABCD
A #include <map> #include <set> #include <list> #include <cmath> #include &l ...
- [codeforces 516]A. Drazil and Factorial
[codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- CodeForces Round #516 Div2 题解
A. Make a triangle! 暴力... 就是给你三个数,你每次可以选一个加1,问最少加多少次能构成三角形 #include <bits/stdc++.h> #define ll ...
- Codeforces Round #412 div2 ABCD
A 按rank给出每个人的赛前分数和赛后分数 如果一个人打败了比他分数高的人 他的分数必然升高 问比赛rated吗 如果一个人的分数改变了肯定rate 如果全都没改的话 也可能是rated 这时候ch ...
随机推荐
- 转载:【原译】Erlang列表处理(Efficiency Guide)
转自:http://www.cnblogs.com/futuredo/archive/2012/10/22/2734186.html List handling 1 Creating a list ...
- par函数的new 参数-进行图片的叠加
默认调用高级绘图函数的时候,会另外在绘制一副图,为了将两幅图绘制在同一张纸上,可以设置new = TRUE 代码示例: plot(1:5, c(2,3,2,3,2), col = "red& ...
- MySQL 数据库常用命令小结
MySQL 数据库常用命令 1.MySQL常用命令 create database name; 创建数据库 use databasename; 选择数据库 drop database name 直接删 ...
- pclzip 压缩文件与解压
类PclZip.class.php下载:PclZip.rar<?php header("Content-type: text/html; charset=utf-8"); f ...
- iOS 使用AFNetworking 设置cookie
本问题是由于多账号访问统一服务器时, 由于服务器那边接收到sessionid一样, 故无法区分账号信息. 所以需要在移动端请求的时候重新设置cookie, 步骤如下: 1. 在登录的时候, 先将 re ...
- Linux配置防火墙,开启80port、3306port 可能会遇到的小问题
vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(同意80端口通 ...
- port被占用的处理方法
開始--执行--cmd 进入命令提示符 输入netstat -ano 就可以看到全部连接的PID 之后在任务管理器中找到这个PID所相应的程序假设任务管理器中没有PID这一项,能够在任务管理器中选&q ...
- SQL Server 查看数据库在数据缓存(data cache)中占用的空间大小
use master go select * from sys.dm_os_buffer_descriptors go --查看数据库在数据缓存(data cache)中占用的空间大小 --由于每个数 ...
- jQuery页面刷新(局部、全部)问题分析
本文实例分两部分对jquery刷新问题进行介绍,第一部分介绍了呢页面局部刷新:第二部分介绍了页面全部刷新第一:页面局部刷新 jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属 ...
- 搭建LNMP+CI环境
首先搭建 LNMP 的服务器环境 安装 Nginx, MySQL 和 PHP 软件包,执行以下命令 yum install -y nginx mariadb-server mariadb php ph ...