演算法 Activity Selection Problem

 avatar
user_6817964
c_cpp
2 years ago
1.1 kB
1
Indexable
Never
int main()
{
    int n, s[101], f[101], s_ori[101];
    scanf("%d", &n);
    for (int k = 1; k <= n; k++) {
        scanf("%d", &s[k]);
        s_ori[k] = k;
    }
    for (int k = 1; k <= n; k++) {
        scanf("%d", &f[k]);
    }
    
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            if (s[j] > s[i]) {
                int temp = s[i];
                s[i] = s[j];
                s[j] = temp;

                temp = f[i];
                f[i] = f[j];
                f[j] = temp;

                temp = s_ori[i];
                s_ori[i] = s_ori[j];
                s_ori[j] = temp;
            }
        }
    }

    int total = 1;
    int get[101];
    get[1] = n;
    for (int i = n - 1, j = n; i >= 1; i--) {
        if (f[i] <= s[j]) {
            total++;
            get[total] = i;
            j = i;
        }
    }
    
    printf("%d\n", total);
    printf("(%d", s_ori[get[total]]);
    for (int k = total - 1; k >= 1; k--) {
        printf(",%d", s_ori[get[k]]);
    }
    printf(")");
}