您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: