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

b[i][j]表示i个取j个最大,c[i][j]表示最小,分别把路径记下来即可

Posted by zhangxiao1124 at 2008-05-01 16:14:25 on Problem 3399
#include<iostream>
#include<math.h>
using namespace std;
#define maxn 105
struct Number {
    double exp;
    int flag;
    void Change(double num) {
        if (fabs(num)<1e-6) flag=0;
        else if (num>0) exp=log(num),flag=1;
        else exp=log(-num),flag=-1;
    }
    void Set(int f) {
        flag=f;
        exp=1000000.0;
    }
    void Print() {
        printf("%d %lf ",flag,exp);
    }
} a[maxn],b[maxn][maxn],c[maxn][maxn];
int n,k,num[maxn];
int d[maxn][maxn],e[maxn][maxn];

void Init() {
    scanf("%d%d",&n,&k);
    for (int i=0; i<n; i++) scanf("%d",&num[i]);
    sort(num,num+n);
    for (int i=1; i<=n; i++) a[i].Change((double)num[i-1]);
}

inline int Sign(double x) {
    if (fabs(x)<1e-6) return 0;
    return (x>0)?1:-1;
}

inline int Compare(Number a,Number b) {
    if (a.flag!=b.flag) return a.flag-b.flag;
    if (a.flag>0 && b.flag>0) return Sign(a.exp-b.exp);
    if (a.flag<0 && b.flag<0) return Sign(b.exp-a.exp);
    return 0;                                          
}

inline Number Product(Number a,Number b) {
    a.exp+=b.exp;
    a.flag*=b.flag;
    return a;
}

void Dp() {
    for (int i=0; i<maxn; i++)                            
        for (int j=0; j<maxn; j++)
            b[i][j].Set(-1),c[i][j].Set(1);
    for (int i=0; i<maxn; i++)
        b[i][0].Change(1.0),c[i][0].Change(1.0);
    for (int i=1; i<=n; i++)
        for (int j=1; j<=min(i,k); j++) {
            if (j<i) {
                b[i][j]=b[i-1][j];
                c[i][j]=c[i-1][j];
            }
            Number tmp;
            if (a[i].flag>=0) tmp=Product(a[i],b[i-1][j-1]);
            else tmp=Product(a[i],c[i-1][j-1]);
            if (Compare(tmp,b[i][j])>0) {
                b[i][j]=tmp;
                d[i][j]=1;
            }
            if (a[i].flag>=0) tmp=Product(a[i],c[i-1][j-1]);
            else tmp=Product(a[i],b[i-1][j-1]);
            if (Compare(tmp,c[i][j])<0) {
                c[i][j]=tmp;
                e[i][j]=1;
            }
        }
}

void Print() {
    int now=n,flag=1;
    while (k)
        if (flag) {
            if (d[now][k]) {
                printf("%d ",num[now-1]),k--;
                if (num[now-1]<0) flag^=1;
            }
            now--;
        } else {
            if (e[now][k]) {
                printf("%d ",num[now-1]),k--;
                if (num[now-1]<0) flag^=1;          
            }
            now--;
        }
    printf("\n");
}

int main() {
    Init();
    Dp();
    Print();
    return 0;
}

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