2020-06-21 12:06

# 这题怎么做啊？求C++代码

【题目背景】

【输入格式】

【输出格式】

【样例 1 输入】

3

5 1

1 4

2 2

【样例 1 输出】

2

• 写回答
• 好问题 提建议
• 关注问题
• 收藏
• 邀请回答

#### 1条回答默认 最新

• threenewbee 2020-06-21 13:59
已采纳
``````#include <iostream>
#include <stdlib.h>
using namespace std;

int minr;
int * p;
int * t;

int calc(int * seed, int sn)
{
int mr = 0;
int pt = 0;
for (int i = 0; i < sn; i++)
{
if (mr < p[seed[i]] * pt) mr = pt - p[seed[i]];
pt += t[seed[i]];
}
return mr;
}

void arrange(int * seed, int sn, int n)
{
if (sn == n)
{
int mr = calc(seed, sn);
if (mr < minr) minr = mr;
return;
}
for (int i = 0; i < n; i++)
{
int used = 0;
for (int j = 0; j < sn; j++)
{
if (seed[j] == i)
{
used = 1;
break;
}
}
if (!used && calc(seed, sn) < minr)
{
int * seed1 = new int[n];
memcpy(seed1, seed, sizeof(int) * sn);
seed1[sn] = i;
arrange(seed1, sn + 1, n);
delete[] seed1;
}
}
}

int main()
{
minr = 0x7fffffff;
int n;
cin >> n;
p = new int[n];
t = new int[n];
for (int i = 0; i < n; i++)
{
cin >> t[i];
cin >> p[i];
}
int * seed = new int[n];
arrange(seed, 0, n);
delete[] seed;
delete[] p;
delete[] t;
cout << minr;
return 0;
}

``````

已采纳该答案
评论
解决 无用
打赏 举报