第二题dfs直接就过了,毕竟范围很小; 和昨天米哈游笔试的难度比一个天上一个地下。。。 贴个代码: #include <iostream> using namespace std; int value[101] = {}; int dist[101] = {}; int n, m, times; int maxnum = 0; void dfs(int nowdist, int lefttime,int laststep,int totalvalue) { if (lefttime == 0) { if (totalvalue > maxnum)maxnum = totalvalue; return; } if (laststep == n || (dist[laststep + 1] - dist[laststep]) > m) { if (totalvalue > maxnum)maxnum = totalvalue; return; } for (int i = laststep + 1; i <= n && (dist[i] - dist[laststep]) <= m; i++) { dfs(dist[i], lefttime - 1, i, totalvalue + value[i]); } return; } int main() { cin >> n >> m >> times; for (int i = 1; i <= n; i++) { cin >> dist[i] >> value[i]; } if (n == 1 ) { cout << value[1]; return 0; } if (n == 0) { cout << 0; return 0; } dfs(0,times,1,value[1]); cout << maxnum; return 0; }

相关推荐

牛客热帖

牛客网
牛客企业服务