编程介的小学生 2019-04-25 22:38 采纳率: 20.5%
浏览 200

复杂度的一个控制的算法的问题,怎么采用的C语言的程序的编写的过程来实现的?

Problem Description
LL is very sensitive to the difficulty of ACM problems. He can assign each problem a positive integer from [1,10000000] to denote its difficulty ( although he has only ACed about 1000 problems ^_^ ). Now, there are N problems in his mind. He want to choose some of them to hold a contest and the total difficulty of all problems in the contest is as near as possible to M.

Input
For each case, the first line is two positive integers N and M (1<=N<=26). Followed by N lines. Each line contains the problem's ID ( a single capital letter ) and its difficulty.

Output
For each case, ouput how many and which problems he should choose in the format of the sample. If more than one combinations of problems reach the request, output the one which comes earliest in lexicography order.

Sample Input
3 10
A 4
B 12
C 8

Sample Output
2
A C

  • 写回答

1条回答 默认 最新

  • Italink 2019-04-27 02:49
    关注
    #include<stdio.h>
    #include<math.h>
    int dfc[27] = { 0 }, top = -1, visited[27] = { 0 }, sum = 0, n, m;
    char stk[30],res[30];
    int mindfs=10000001;
    void dfs(int i) {
        for (int j = 0; j < 26; ++j) {
            if (dfc[j] && !visited[j]) {
                visited[j] = 1;
                sum += dfc[j];
                stk[++top] = 'A' + j;
                if (abs(sum - m) < mindfs) {
                    mindfs = abs(sum - m);
                    for (int k = 0; k <= top; k++)
                        res[k] = stk[k];
                    res[top+1] = '\0';
                }
                dfs(j);
                --top;
                sum -= dfc[j];
                visited[j] = 0;
            }
        }
    }
    int main() {
        char ch;
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; ++i) {
            getchar();
            scanf("%c", &ch);
            scanf("%d", &dfc[ch-'A']);
        }
        dfs(27);
        printf("%d\n", mindfs);
        for (char* ch = res; *ch != '\0'; ++ch)
            printf("%s%c", ch == res ? "" : " ", *ch);
        return 0;
    }
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!