BestCoder Round #61 HDOJ5522 5523 5524题解
2015-10-31 23:46
411 查看
题目链接: 点击打开链接
A: 直接无脑暴力枚举三个数, 或者枚举前两个数, 第三个数二分, 我用了stl的lower_bound()
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 105;
int n, a[MAXN];
int main(int argc, char const *argv[])
{
while(scanf("%d", &n) != EOF) {
bool flag = false;
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
for(int i = 1; i <= n - 2; ++i) {
for(int j = i + 1; j <= n - 1; ++j) {
int x = a[i] + a[j];
int *pos = lower_bound(a + j + 1, a + n + 1, x);
if(*pos == x) {
flag = true;
printf("YES\n");
break;
}
}
if(flag) break;
}
if(!flag) printf("NO\n");
}
return 0;
}
B: 分析后对变量分类讨论即可, n为1或起点与终点在两端则ans为0, 起点与终点重合则ans为-1, 起点与终点相邻或起点在两端ans为1,
其他情况ans为2.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 1e4;
int n, s, t;
int main(int argc, char const *argv[])
{
while(scanf("%d", &n) != EOF) {
scanf("%d%d", &s, &t);
if(n == 1 || (s == 1 && t == n) || (s == n && t == 1)) {
printf("0\n");
continue;
}
if(s == t) {
printf("-1\n");
continue;
}
if(s - t == 1 || s - t == -1) {
printf("1\n");
continue;
}
if(s == 1 || s == n) {
printf("1\n");
continue;
}
printf("2\n");
}
return 0;
}
C: 一棵完全二叉树的左右子树都为完全二叉树, 有一棵子树的最后一层是满的, 每一层的节点子树形态相同, 统计后递归求解另一颗子
树即可得到答案.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "set"
using namespace std;
typedef long long ll;
ll n;
set<ll> s;
void dfs(ll x)
{
if(x == 0 || s.count(x) > 0) return;
s.insert(x);
if((--x & 1) == 0) dfs(x >> 1);
else {
dfs(x >> 1);
dfs((x >> 1) + 1);
}
}
int main(int argc, char const *argv[])
{
while(scanf("%lld", &n) != EOF) {
s.clear();
dfs(n);
printf("%lu\n", s.size());
}
return 0;
}
A: 直接无脑暴力枚举三个数, 或者枚举前两个数, 第三个数二分, 我用了stl的lower_bound()
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 105;
int n, a[MAXN];
int main(int argc, char const *argv[])
{
while(scanf("%d", &n) != EOF) {
bool flag = false;
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
for(int i = 1; i <= n - 2; ++i) {
for(int j = i + 1; j <= n - 1; ++j) {
int x = a[i] + a[j];
int *pos = lower_bound(a + j + 1, a + n + 1, x);
if(*pos == x) {
flag = true;
printf("YES\n");
break;
}
}
if(flag) break;
}
if(!flag) printf("NO\n");
}
return 0;
}
B: 分析后对变量分类讨论即可, n为1或起点与终点在两端则ans为0, 起点与终点重合则ans为-1, 起点与终点相邻或起点在两端ans为1,
其他情况ans为2.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 1e4;
int n, s, t;
int main(int argc, char const *argv[])
{
while(scanf("%d", &n) != EOF) {
scanf("%d%d", &s, &t);
if(n == 1 || (s == 1 && t == n) || (s == n && t == 1)) {
printf("0\n");
continue;
}
if(s == t) {
printf("-1\n");
continue;
}
if(s - t == 1 || s - t == -1) {
printf("1\n");
continue;
}
if(s == 1 || s == n) {
printf("1\n");
continue;
}
printf("2\n");
}
return 0;
}
C: 一棵完全二叉树的左右子树都为完全二叉树, 有一棵子树的最后一层是满的, 每一层的节点子树形态相同, 统计后递归求解另一颗子
树即可得到答案.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "set"
using namespace std;
typedef long long ll;
ll n;
set<ll> s;
void dfs(ll x)
{
if(x == 0 || s.count(x) > 0) return;
s.insert(x);
if((--x & 1) == 0) dfs(x >> 1);
else {
dfs(x >> 1);
dfs((x >> 1) + 1);
}
}
int main(int argc, char const *argv[])
{
while(scanf("%lld", &n) != EOF) {
s.clear();
dfs(n);
printf("%lu\n", s.size());
}
return 0;
}
相关文章推荐
- ubuntu vim
- mysql 从一个表中查数据,插入另一个表
- IntentService报Unable to instantiate service 错误的解决方法
- 15个基础的jQuery面试问题
- 第二百一十二天 how can I 坚持
- spring security使用数据库获取资源、角色和权限保护web应用
- 第四章:Django 的模板系统(转)
- spring security 下web应用安全的关键Filter:FilterSecurityInterceptor
- Android实战-个人App乐逗项目(第一阶段:微信精选文章完成与总结)
- JS模块化历程
- linux 多线程
- LightOJ - 1201 A Perfect Murder(树形DP)
- jdbcTemplate返回简单类型数据
- Python中文全攻略
- 燕过留声:由 Activity 和 Fragment 的通信方法想到的【WIP】
- Veeam Backup & Replication 8 安装和使用
- javascript键盘事件兼容
- android中控制根据是否选中或者按下改变按钮的颜色(通过xml代码实现)
- django 简易博客开发 1 安装、创建、配置、admin使用(转)
- 应用框架图