2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest(Gym - 101466)解题报告集合

Gym 101466A Gaby And Addition

#include
using namespace std ;
const int maxn  = 100052;
struct node{
    int val, deep, num;
    node* next[10] ;
    node(){
        for(int i=0;i<10;i++){
            next[i] = NULL;
        }
        num = 0;
    }
    node(int val, int deep){
        this->deep = deep;
        this->val = val;
        for(int i=0;i<10;i++){
            this->next[i] = NULL;
        }
        this->num = 0;
    }
};
node* root;
void initTree(){
    root = new node(0,19);
}
void insertTree(vector<int>&x){
    node* now = root ;
    for(int i=x.size()-1;i>=0;i--){
        now->num++;
        if (now->next[x[i]] == NULL){
            now->next[x[i]] = new node(x[i],i);
        }
        now = now->next[x[i]];
    }
    now->num++;
}

long long int queryMax(vector<int>&x){
    long long int ret = 0 ;
    node* now = root ;
    for (int i=x.size()-1; i>=0; i--){
        for (int j=9-x[i],k=0; k<10; k++,j--){ // 从大到小枚举每一个出口
            if (j<0) j+=10 ;
            if (now->next[j] != NULL){
                now = now->next[j];
                int add = (x[i]+j) %10 ;
                ret = ret *10 +add ;
                break;
            }
        }
    }
    return ret;
}
long long int queryMin(vector<int>&x){
    long long int ret = 0 ;
    node* now = root ;
    for (int i=x.size()-1; i>=0; i--){
        for (int j=10-x[i],k=0; k<10; k++,j++){ // 从小到大枚举每一个出口
            if (j>=10) j-=10 ;
            if (now->next[j] != NULL){
                now = now->next[j];
                int add = (x[i]+j) %10 ;
                ret = ret *10 +add ;
                break;
            }
        }
    }
    return ret;
}
void insertTree(long long int n){
    vector<int> x;
    for(int i=0;i<=18;i++){
        x.push_back(n%10) ;
        n /= 10 ;
    }
    insertTree(x);
}
long long int queryMax(long long int n){
    vector<int> x;
    for(int i=0;i<=18;i++){
        x.push_back(n%10) ;
        n /= 10 ;
    }
    return queryMax(x);
}
long long int queryMin(long long int n){
    vector<int> x;
    for(int i=0;i<=18;i++){
        x.push_back(n%10) ;
        n /= 10 ;
    }
    return queryMin(x);
}

int main(){
    initTree();
    int n;
    scanf("%d",&n);
    long long int ans_max = -1, ans_min = -1 ;
    for(int i=0;i<n;i++){
        long long int temp;
        scanf("%lld",&temp);
        if(i!=0){
            long long int temp_min, temp_max ;
            temp_min = queryMin(temp);
            temp_max = queryMax(temp);
            if(ans_max == -1 || ans_max<temp_max){
                ans_max = temp_max ;
            }
            if(ans_min == -1 || ans_min>temp_min){
                ans_min = temp_min ;
            }
        }
        insertTree(temp);
        //printf(" i = %d\n",i);
    }
    printf("%lld %lld\n", ans_min, ans_max);
    return 0;
}

Gym 101466B Maximum Tree

#include
using namespace std ;
const int maxn  = 100052;
long long int ans = 0;
vector<long long int>a;
int main(){
    long long int n;
    while(cin>>n){
        a.clear();
        for(int i=0;i<n;i++){
            long long int temp;
            scanf("%lld",&temp);
            a.push_back(temp);
        }
        sort(a.begin(),a.end());
        long long int mul = 1, ans = 1 ;
        for(int i=a.size()-1;i>=0;i--){
            mul *= a[i];
            ans += mul;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


Gym 101466C Planet Communcation

#include
using namespace std ;
const int maxn  = 5052;
struct point{
    int x, y, z;
    point(){}
    point(int x, int y, int z){
        this->x = x;
        this->y = y;
        this->z = z;
    }
};
vector <bool> vis;
vector <point> a;
bool in_line(const point &a, const point &b, const point &c){
    return (long long int)(a.x-b.x)*(b.y-c.y) == (long long int)(a.y-b.y)*(b.x-c.x) &&
           (long long int)(a.z-b.z)*(b.y-c.y) == (long long int)(a.y-b.y)*(b.z-c.z) &&
           (long long int)(a.z-b.z)*(b.x-c.x) == (long long int)(a.x-b.x)*(b.z-c.z)  ;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        int temp_x, temp_y, temp_z;
        scanf("%d%d%d", &temp_x, &temp_y, &temp_z);
        a.push_back( point(temp_x, temp_y, temp_z) );
        vis.push_back(0) ;
    }
    vis[0] = 1;
    int res = a.size()-1, ans = 0;
    for(int i=1;i<a.size();i++){
        if (vis[i]) continue ;
        vis[i] = true ; res--; ans++;
        for(int j=i+1;j<n;j++){

            if (vis[j]) continue ;
            if (in_line(a[0],a[i],a[j])){
                vis[j] = true ;
                res--;
            }
            if(res==0)break;
        }
        if(res==0)break;
    }
    printf("%d\n",ans);
    return 0;
}


Gym 101466D Double it

#include
using namespace std ;
const int maxn  = 100052;
int n;
string getWay(int n){
    if(n==1){
        return string("A");
    }
    if(n==2){
        return string("B");
    }
    if(n%2==0){
        return getWay((n-2)/2)+"B";
    }
    else{
        return getWay((n-1)/2)+"A";
    }
}
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        string ans = getWay(n);
        cout<<ans<<endl;
    }
    return 0;
}


Gym 101466E Text Editor

#include
using namespace std ;
const int maxn  = 100052;

char a[maxn], b[maxn];
int na, nb, limit, next_[maxn];
void get_next(char* str2, int len, int* next){//next数组保存了以i结尾的字符串的最长公共前缀和后缀的起始坐标
    int i,j;
    next[0] = j = -1;
    i = 0;
    while(i < len){
        while(j!=-1&&str2[j]!=str2[i])//自身和自身进行匹配
            j = next[j];
        next[++i] = ++j;
    }
}
int kmp(char *str1, char* str2, int len, int *next){
    int ans = 0;
    int i,j;
    i = j = 0;
    while(i < na){//注意和返回下标的区别
        while(j!=-1&&str1[i]!=str2[j]){
            j = next[j];
        }
        if(j == len-1){
           ans ++;
           j = next[j];
        }
        i++;    j++;
    }
    return ans;//返回匹配次数
}
int getNum(char *a, char *b, int len){
    get_next(b, len, next_);
    return kmp(a, b, len, next_);
}
int main(){
    char now = 0;
    na = nb = 0;
    while(now != '\n'){
        scanf("%c", &now);
        a[na++] = now ;
    }
    now = 0;
    while(now != '\n'){
        scanf("%c", &now);
        b[nb++] = now ;
    }
    scanf("%d",&limit);
    int flag = getNum(a,b,1);
    if (flag < limit){
        printf("IMPOSSIBLE\n");
        return 0;
    }
    int l = 1, r = nb;
    while(l <= r){
        int mid = (l+r)/2;
        if(getNum(a,b,mid) >= limit){
            l = mid +1 ;
        }
        else {
            r = mid -1 ;
        }
    }
    for(int i=0;i<l-1;i++){
        printf("%c",b[i]);
    }
    printf("\n");
    return 0;
}

Gym 101466F Polygon Triangles

#include
using namespace std ;
const int maxn  = 100052;

int main(){
    int n;
    scanf("%d",&n);
    int flag = 1;
    while(n--){
        int a[3];
        scanf("%d%d%d", &a[0], &a[1], &a[2]);
        sort(a,a+3);
        if (a[0]+a[1] <= a[2]) flag = 0;
    }
    if (flag == 1){
        printf("YES\n");
    }
    else {
        printf("NO\n");
    }
    return 0;
}


Gym 101466G Generative Model


Gym 101466H Logo

#include
using namespace std ;
const int maxn  = 100052;

void draw(int n){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i==n-1 || j==0 || j==n-1){
                printf("*");
            }
            else{
                printf(" ");
            }
        }
        printf(" ");
        for(int j=0;j<n;j++){
            if(i==0 || j==0 || j==n-1){
                printf("*");
            }
            else{
                printf(" ");
            }
        }
        printf("\n");
    }
}
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        draw(n);
    }
    return 0;
}

Gym 101466I Math Class


Gym 101466J Jeronimo’s List

#include
using namespace std ;
const int maxn  = 100052;
int n, m, q;
const int MOD = 30000000;
int a[MOD+4], b[MOD+4];
int inline mod(int x){return x>=MOD?x-MOD:x;}
int main(){
    scanf("%d%d%d", &n, &m, &q);
    for(int i=0;i<m;i++){
        int temp ;
        scanf("%d",&a[i]);
        b[a[i]]++;
    }
    for(int i=m;i<n;i++){
        a[i] = mod(a[i-m]+a[i-m+1]) ;
        b[a[i]]++;
    }
    int now = 0 ;
    for(int i=0;i<MOD;i++){
        while(b[i]--){
            a[now++] = i;
        }
    }
    while(q--){
        int temp ;
        scanf("%d",&temp);
        printf("%d\n",a[temp-1]);
    }
    return 0;
}
/*
30000000 4 3
1 2 9 10
1 5 10
*/

Gym 101466K Random Numbers

// 2 3 5 7 11 13
#include
using namespace std ;
const int maxn  = 100052;
const int MOD = 1000000007;
const int ppos[15] = {-1, -1, 0, 1, -1, 2, -1, 3, -1, -1, -1, 4, -1, 5, -1 } ;
const int prime[6] = {2, 3, 5, 7, 11, 13 } ;
inline mod(long long int x){return x%MOD;}

struct node{
    long long int mul, num[6] ;
    node(){
        memset(num, 0, sizeof(num));
        this->mul = 1 ;
    }
    node(long long int mul){
        memset(num, 0, sizeof(num));
        this->mul = mul;
        for(int i=0;i<6;i++){
            while(mul%prime[i]==0){
                mul /= prime[i];
                num[i]++;
            }
        }
    }
    long long int get_val(){
        long long int ret = 1 ;
        for (int i=0; i<6; i++){
            ret = mod(ret *(num[i]+1));
        }
        return ret ;
    }
};
node operator + (const node &l, const node &r){
    node ret = l;
    for (int i=0; i<6; i++){
        ret.num[i] += r.num[i];
    }
    ret.mul = mod(ret.mul *r.mul) ;
    return ret ;
}

node tr[maxn<<2] ;
vector <int> u[maxn] ;
int pos[maxn] ; // tree_id -> dfs_id
int s[maxn] ; // 子树大小
long long int a[maxn]; // 线段树维护的dfs序列
int dfs_p = 1 ;

void build(int nl, int nr, int now){
    if (nl == nr){
        tr[now] = node(a[nl]);
        return ;
    }
    int mid = (nl+nr) /2 ;
    build(nl, mid, now<<1);
    build(mid+1, nr, now<<1|1);
    tr[now] = tr[now<<1] +tr[now<<1|1] ;
    return ;
}
void updatat(int pos, long long int x, int nl, int nr, int now){
    if(nl == nr){
        tr[now] = tr[now]+node(x);
        return ;
    }
    int mid = (nl+nr) /2 ;
    if (pos <= mid){
        updatat(pos, x, nl, mid, now<<1) ;
    }
    else {
        updatat(pos, x, mid+1, nr, now<<1|1) ;
    }
    tr[now] = tr[now<<1] + tr[now<<1|1] ;
    return ;
}
node query(int l, int r, int nl, int nr, int now){
    //printf("%d %d %d %d %d\n",l,r,nl,nr,now);
    node ret = node(1);
    if (l<=nl && nr<=r){
        return tr[now] ;
    }
    int mid = (nl+nr) /2 ;
    if (l <= mid) {
        ret = ret +query(l, r, nl, mid, now<<1);
    }
    if (mid+1 <= r) {
        ret = ret +query(l, r, mid+1, nr, now<<1|1);
    }
    return ret ;
}

int dfs(int x, int fa){
    int ret = 1;
    pos[x] = dfs_p++ ;
    for (int i=0; i<u[x].size(); i++){
        if (u[x][i] == fa) continue ;
        ret += dfs(u[x][i], x) ;
    }
    return s[x] =  ret ;
}
int main(){
    int n;
    scanf("%d",&n);
    for (int i=0; i<n-1; i++) {
        int temp_st, temp_ed;
        scanf("%d%d", &temp_st, &temp_ed ) ;
        u[temp_st].push_back(temp_ed) ;
        u[temp_ed].push_back(temp_st) ;
    }
    dfs(0, -1);
    for (int i=0; i<n; i++) {
        long long int temp ;
        scanf("%lld", &temp);
        a[pos[i]] = temp ;
    }
    build(1,n,1);
    int q;
    scanf("%d",&q);
    while(q--){
        char temp[100];
        scanf("%s", temp);
        if (temp[0] == 'R'){
            int root;
            scanf("%d", &root);
            node ans = query(pos[root], pos[root]+s[root]-1, 1, n, 1);
            printf("%lld %lld\n",ans.mul, ans.get_val());
        }
        else{
            int p, v;
            scanf("%d%d", &p, &v);
            updatat(pos[p], v, 1, n, 1);
        }
    }
    return 0;
}


你可能感兴趣的:(题解集合)