【u211】编码

Time Limit: 1 second
Memory Limit: 128 MB

【问题描述】

假设phi(W)得到是按照以下算法编码后的结果:
1、 如果W的长度为1那么phi(W)即为W;
2、 假设需要编码的单词W = w1w2…w,并且K = N / 2(取下整);
3、 phi(W) = phi(wNwN-1…wK+1) + phi(wKwK-1…w1)
例如,phi(‘Ok’) = ‘kO’, phi(‘abcd’) = ‘cdab’.
你的任务就是,找到wq在经过phi(W)编码后的单词中的位置。

【输入格式】

输入文件encode.in的第1行包含2个正整数N和q,N为单词W的长度。

【输出格式】

输出文件encode.out包含1行,即字母wq在编码后单词phi(W)中的位置。

【数据规模】

30%的数据满足:N<=100; 100%的数据满足:1 <= N <= 109; 1<= q <= N。

Sample Input1

9 4

Sample Output1

8

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=u211

【题解】

比如样例
1 2 3 4 5 6 7 8 9
按照上面那个规则会变成
9 8 7 6 5 + 4 3 2 1
而我们要确定的4号节点已经在9 8 7 6 5 这个字符串的右边了;
所以9 8 7 6 5 再怎么变对4的位置来说也不会有影响了;
所以只要再考虑4 3 2 1就好了
现在问题转换成
n = 4 q=1了;
当然答案先递增n-n/2==5;
然后问题就变成
1 2 3 4 问最后1在哪里
…以此类推进行递归就好了
因为每次都去掉了一半的区间所以复杂度为log2n;

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

void rel(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void rei(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n,q;
LL ans = 0;

void solve(int n,int pos)
{
    if (n==1)
    {
        ans++;
        return;
    }
    int key = n/2;
    if (pos <= key)
    {
        ans += n-key;
        solve(key,key-pos+1);
    }
    else
        solve(n-key,n-pos+1);
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    rei(n);rei(q);
    solve(n,q);
    cout << ans << endl;
    return 0;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(186)  评论(0编辑  收藏  举报