Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

居然是第100道题了呀~贴代码留念~

Posted by chliul at 2014-11-06 23:10:15 on Problem 2886
#include <stdio.h>
using namespace std;

int prime[10] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
int ans_num = 0;
int ans_fnum = 0;
int n;
int k;
void dfs(int num, int fnum, int index, int pnum) {
    if(fnum > ans_fnum) {
        ans_fnum = fnum;
        ans_num = num;
    }
    if(fnum == ans_fnum && num < ans_num) {
        ans_num = num;
    }
    if(index == 10 || pnum == 0) {
        return;
    }
    for(int i = 1, j = 0; num*i <= n && j <= pnum; i *= prime[index+1], j++) {
        dfs(num*i, fnum*(j+1), index+1, j);
    }
}

#define N 500000
struct data {
    int offset;
    char name[11];
};

struct node {
    int l;
    int r;
    int p;
    int empty;
};

data dict[N] = {};
node all[N*2] = {};
int now = 0;

int add_node(int p, int empty) {
    node &n = all[now];
    n.l = -1;
    n.r = -1;
    n.p = p;
    n.empty = empty;
    now++;
    return now-1;
}

int build_tree(int p, int left, int right) {
    int nowi = add_node(p, right-left+1);
    if(left == right) {
        return nowi;
    }
    else {
        int mid = (left+right)/2;
        all[nowi].l = build_tree(nowi, left, mid);
        all[nowi].r = build_tree(nowi, mid+1, right);
        return nowi;
    }
}

int empty_node(int offset, int nowi, int left, int right) {
    node &n = all[nowi];
    n.empty--;
    if(left == right) {
        return left;
    }
    else {
        node &l = all[n.l];
        int mid = (left+right) / 2;
        if(offset < l.empty) {
            return empty_node(offset, n.l, left, mid);
        }
        else {
            return empty_node(offset-l.empty, n.r, mid+1, right);
        }
    }
}

int query_empty(int nowl, int nowr, int nowi, int left, int right) {
    node &n = all[nowi];
    if(nowl == left && nowr == right) {
        return n.empty;
    }
    else {
        int nowmid = (nowl+nowr)/2;
        if(left >= nowmid+1) {
            return query_empty(nowmid+1, nowr, n.r, left, right);
        }
        else if(right <= nowmid) {
            return query_empty(nowl, nowmid, n.l, left, right);
        }
        else {
            return query_empty(nowl, nowmid, n.l, left, nowmid) +
                   query_empty(nowmid+1, nowr, n.r, nowmid+1, right);
        }
    }
}

int get_offset(int le, int re, int nowoff) {
    int sum = le+re;
    if(nowoff > 0) {
        return (le+nowoff-1)%sum;
    }
    else {
        return (sum-1)-(re-nowoff-1)%sum;
    }
}

int main() {
    while(scanf("%d %d", &n, &k) != EOF) {
        ans_num = 1;
        ans_fnum = 1;
        for(int i = 1, j = 0; i <= n; i *= 2, j++) {
            dfs(i, j+1, 0, j);
        }
        for(int i = 0; i < n; i++) {
            scanf("%s %d", dict[i].name, &dict[i].offset);
        }
        int root = build_tree(-1, 0, n-1);
        int abs_off = k-1;
        for(int i = 0; i < ans_num-1; i++) {
            int index = empty_node(abs_off, root, 0, n-1);
            int le = query_empty(0, n-1, root, 0, index);
            int re = query_empty(0, n-1, root, index, n-1);
            int relative_off = dict[index].offset;
            abs_off = get_offset(le, re, relative_off);
        }
        int res = empty_node(abs_off, root, 0, n-1);
        printf("%s %d\n", dict[res].name, ans_fnum);
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator