hdu 5014 思维题/推理
http://acm.hdu.edu.cn/showproblem.php?pid=5014
从小数開始模拟找方法规律,然后推广,尤其敢猜敢尝试,错了一种思路继续猜-----这是一种非常重要的方法啊
这道题还是从小数開始模拟,我是依据16以内的找的规律
依据
2^k---2^k-1
2^k+1---2^k-2
...
这样陪下去
当2^k==n的时候,
从2^(k-1)按相同的方法配下去,
WA了非常久,是lower_bound用错了 只是没明确为啥 换成upper_bound也能够AC....但还不明确为啥lower_bound WA......
目的是找以一个<=n的下标
int id=lower_bound(mi,mi+17,n)-mi;
if(mi[id]>n)id--;
跟
int id=16;
while(mi[id]>n && id>=0)id--;
不一样么?后者AC 前者WA。。
以下是AC代码
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std; #define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const double pi = acos(-1.0);
const int INF = 100000000; const int MAXN = 1e5+50;
ll a[MAXN],ans[MAXN];
const ll mi[20]={1,2,4,8,16,
32,64,128,256,512,
1024,2048,4096,8192,16384,
32768,65536}; ll n;
void print()
{
ll out=0;
out=ans[a[0]]^a[0];
for(ll i=1;i<=n;i++)
out+=ans[a[i]]^a[i];
printf("%I64d\n",out);
printf("%I64d",ans[a[0]]); for(ll i=1;i<=n;i++)
printf(" %I64d",ans[a[i]]);
putchar('\n');
} int main()
{
//OUT("hdu5014.txt");
//IN("hdu5014.txt");
while(~scanf("%I64d",&n))
{
for(ll i=0;i<=n;i++)
scanf("%I64d",&a[i]),ans[i]=i; //int id=lower_bound(mi,mi+17,n)-mi; // id>=1 由于n>=1
int id=16;
while(mi[id]>n && id>=0)id--;
if(id == 0) //说明n == 1
{
ans[0]=1;
ans[1]=0;
print();
continue;
}
if(mi[id]>n)id--;
ll cnt=0,last=0,ls=n;
while(id>=0&& ls>=0)
{
ll k;
for(k=0;mi[id]-k-1>=0 && k+mi[id]<=ls;k++)
{
//printf("id=%d mi[id]+k=%d mi[id]-k-1=%d\n",id,mi[id]+k,mi[id]-k-1);
swap(ans[mi[id]+k],ans[mi[id]-k-1]);
//cnt+=2;
last=mi[id]-k-1;
}
ls=last-1;
id--;
}
if(ans[0]==0 && ans[1]==1)swap(ans[0],ans[1]);
print();
}
return 0;
}
hdu 5014 思维题/推理的更多相关文章
- hdu 4883 思维题
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 TIANKENG’s restaurant Time Limit: 2000/1000 MS (Ja ...
- Just Random HDU - 4790 思维题(打表找规律)分段求解
Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In ...
- HDU 6464 权值线段树 && HDU 6468 思维题
免费送气球 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 朋友HDU - 5963 (思维题) 三种方法
传送门 题目描述 输入 输出 样例输入 Sample Input 样例输出 Boys win! Girls win! Girls win! Boys win! Girls win! Boys win! ...
- Fang Fang HDU - 5455 (思维题)
Fang Fang says she wants to be remembered. I promise her. We define the sequence FF of strings. F0 = ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
- HDU 6140 17多校8 Hybrid Crystals(思维题)
题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...
- HDU 6298.Maximum Multiple-数学思维题(脑子是个好东西,可惜我没有) (2018 Multi-University Training Contest 1 1001)
暑假杭电多校第一场,这一场是贪心场,很多贪心的题目,但是自己太菜,姿势挫死了,把自己都写吐了... 2018 Multi-University Training Contest 1 HDU6298.M ...
- hdu5325 树的思维题
pid=5325">http://acm.hdu.edu.cn/showproblem.php? pid=5325 Problem Description Bobo has a tre ...
随机推荐
- HDU2527:Safe Or Unsafe(哈弗曼树)
Problem Description Javac++ 一天在看计算机的书籍的时候,看到了一个有趣的东西!每一串字符都可以被编码成一些数字来储存信息,但是不同的编码方式得到的储存空间是不一样的!并且当 ...
- 初识Channel
java.nio.channels 中的接口和类. A channel represents an open connection to an entity such as a hardware de ...
- 【集训笔记】【大数模板】特殊的数 【Catalan数】【HDOJ1133【HDOJ1134【HDOJ1130
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3324 http://blog.csdn.net/xymscau/artic ...
- java基础之注解
包含成员变量及方法的注解称之为元数据, 不包含任何成员的注解称之为标记. 用@interface定义注解, 使用类似于接口方法的方式来定义注解的属性,其中返回值成为属性的类型,方法名为属性的名称. p ...
- pojg487-3279电话号码转换(字符映射)
http://poj.grids.cn/practice/2974 注意输入中连字符可以任意添加和删除. 描述企业喜欢用容易被记住的电话号码.让电话号码容易被记住的一个办法是将它写成一个容易记住的单词 ...
- [置顶] 自定义java Annotation快速入门与提高
我们先来看看一个简单的注解类定义 import java.lang.annotation.Documented; import java.lang.annotation.Retention; impo ...
- 2008r2 显示桌面图标
- SICP中sqrt(开方)的实现(附C#实现)
#lang racket (define (square x) (* x x)) (define (sqrt-iter guess x) (if (good-enough? guess x) gues ...
- 基于Zlib算法的流压缩、字符串压缩源码
原文:基于Zlib算法的流压缩.字符串压缩源码 Zlib.net官方源码demo中提供了压缩文件的源码算法.处于项目研发的需要,我需要对内存流进行压缩,由于zlib.net并无相关文字帮助只能自己看源 ...
- linux: /usr/bin/ld: cannot find -lloc
/usr/bin/ld: cannot find -lloc ld链接库的时候没发现loc这个库-lloc本事不是文件名字,要去找这个库就搜索libloc, loc, 不能搜索lloc. /usr1/ ...