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 思维题/推理的更多相关文章

  1. hdu 4883 思维题

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 TIANKENG’s restaurant Time Limit: 2000/1000 MS (Ja ...

  2. Just Random HDU - 4790 思维题(打表找规律)分段求解

    Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In ...

  3. HDU 6464 权值线段树 && HDU 6468 思维题

    免费送气球 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. 朋友HDU - 5963 (思维题) 三种方法

    传送门 题目描述 输入 输出 样例输入 Sample Input 样例输出 Boys win! Girls win! Girls win! Boys win! Girls win! Boys win! ...

  5. Fang Fang HDU - 5455 (思维题)

    Fang Fang says she wants to be remembered. I promise her. We define the sequence FF of strings. F0 = ...

  6. 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 ...

  7. HDU 6140 17多校8 Hybrid Crystals(思维题)

    题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...

  8. HDU 6298.Maximum Multiple-数学思维题(脑子是个好东西,可惜我没有) (2018 Multi-University Training Contest 1 1001)

    暑假杭电多校第一场,这一场是贪心场,很多贪心的题目,但是自己太菜,姿势挫死了,把自己都写吐了... 2018 Multi-University Training Contest 1 HDU6298.M ...

  9. hdu5325 树的思维题

    pid=5325">http://acm.hdu.edu.cn/showproblem.php? pid=5325 Problem Description Bobo has a tre ...

随机推荐

  1. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

  2. Android:ServiceDemo

    效果图: layout的main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  3. 【转】centos安装memcached+php多服务器共享+session多机共享问题

    参考博文: centos安装memcached 源码安装 Yum安装Memcache Memcached内存分配优化及使用问题 <转>php+memcached 实现session共享 P ...

  4. C语言深度剖析--volatile(转载)

    volatile关键字和const一样是一种类型修饰符,用它修饰的变量表示可以被某些编译器未知的因素更改,比如操作系统,硬件或者其他线程等等.遇到这个关键字声明的变量,编译器对访问该变量的代码就不再进 ...

  5. ZOJ 1893 A Multiplication Game 【简单博弈】

    感觉ZJU上有不少博弈的题目. 这道题目还是比较好理解的,题目大概意思是:两人轮流乘一个2-9的数,从1开始乘,求谁的乘积先大于N. 还是寻找必败点必胜点,不过在这个题目里转换成了寻找必败区间必胜区间 ...

  6. php __autoload使用

    官方介绍: void __autoload ( string $class ) 你可以通过定义这个函数来启用类的自动加载. 转载一篇文章: PHP autoload机制详解 (1) autoload机 ...

  7. activity入门2

    1.如何获取其他应用的包名和类名? 点击和查看logcat第一条信息2.第二步Intent intent = new Intent();intent.setClassName("com.an ...

  8. WinForm 小程序 NotePad

    运行效果: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  9. 在Android开发中,定时器一般有以下3种实现方法

    在Android开发中,定时器一般有以下3种实现方法: 原文地址http://www.360doc.com/content/12/0619/13/87000_219180978.shtml 一.采用H ...

  10. 在OpenShift平台开发Node.js程序

    设置process.env方便本地调试 --------------------- 修改~/.profile,增加下面两行,然后执行`. ~/.profile`: export OPENSHIFT_N ...