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的更多相关文章

  1. Codeforces #107 DIV2 ABCD

    A #include <map> #include <set> #include <list> #include <cmath> #include &l ...

  2. [codeforces 516]A. Drazil and Factorial

    [codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define  ...

  3. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  4. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  5. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  6. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  7. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  8. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  9. CodeForces Round #516 Div2 题解

    A. Make a triangle! 暴力... 就是给你三个数,你每次可以选一个加1,问最少加多少次能构成三角形 #include <bits/stdc++.h> #define ll ...

  10. Codeforces Round #412 div2 ABCD

    A 按rank给出每个人的赛前分数和赛后分数 如果一个人打败了比他分数高的人 他的分数必然升高 问比赛rated吗 如果一个人的分数改变了肯定rate 如果全都没改的话 也可能是rated 这时候ch ...

随机推荐

  1. linux下简单好用的端口映射转发工具rinetd 转

    linux下简单好用的工具rinetd,实现端口映射/转发/重定向 官网地址http://www.boutell.com/rinetd 软件下载 wget http://www.boutell.com ...

  2. 接口日志记录AOP实现-LogAspect

    使用spring aop日志记录 所需jar包 pom.xml <!-- logger begin --> <dependency> <groupId>org.sl ...

  3. 使用GitHub和Eclipse进行javaEE开发步骤

    下载Git客户端:链接:http://pan.baidu.com/s/1jIueUEy 密码:7gef; 下载Eclipse javaee客户端:http://www.eclipse.org/down ...

  4. Systemd on ubuntu

    何为 systemd? systemd 是一个 Linux 下的系统和会话管理器,与 SysV 和 LSB 启动脚本兼容.systemd 提供了积极的并行处理能力,使用套接字(socket)和 D-b ...

  5. c#.net常用函数和方法集

    1.DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.D ...

  6. OpenCV学习:Windows+VS2010+OpenCV配置

    OpenCV下载: 百度云下载:https://pan.baidu.com/s/1mhAExdu (2.4.9版本) 下载完成后,双击运行exe,选择输出目录,我选择的是C:\OpenCV 配置环境变 ...

  7. linux常用命令-tar,scp,du

    tar 打包排除指定目录 tar -zcvf afish.tar.gz * --exclude=file1 --exclude=dir1 排除目录注意: 1.--exclude=file1 而不是 - ...

  8. c++虚函数表 Brew VTBL

    参考:http://blog.csdn.net/haoel/article/details/1948051/ BREW VTBL:http://blog.chinaunix.net/uid-51740 ...

  9. JavaScript实现禁用键盘和鼠标的点击事件

    编写自己定义的JavaScript函数maskingKeyboard()和rightKey(); maskingKeyboard():禁用键盘 rightKey():禁用鼠标右键 <script ...

  10. python2.0_day19_充分使用Django_form实现前端操作后台数据库

    在前面的<python2.0_day19_学员管理系统之前端用户交互系统>一节中,我们实现了前端展示customer客户纪录.在<python2.0_day19_前端分页功能的实现& ...