Assemble
Assemble
Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 42 Solved: 18
Description
Xrdog准备组装一台属于他自己的电脑。
具体来说是这样子的,组装这台电脑需要n个部件(显卡,CPU,硬盘…),每种部件均有k个品牌的产品,不同的产品会有一个性能值来衡量它的性能。Xrdog现在要选择n个产品来组装成他的电脑,这n个产品分别是不同的部件(换言之就是要从每种部件中选择一个),我们定义这台电脑的最终性能值为选择的n个产品的性能值的乘积。
现在Xrdog想最大化他的电脑的性能值,你能帮助一下他吗?
Input
第一行两个正整数整数n,k(n<10,k<10) 接下来n行,每行k个整数,第i行表示的是第i-1个部件的k个品牌的产品的性能值。(|性能值|<10)
Output
输出一个整数,表示电脑的性能值的最大可能值
Sample Input
3 2
2 1
1 2
2 -2
Sample Output
8
Hint
2 * 2 * 2 = 8 即是在每种部件中均选择了性能值为2的产品
最一开始做的时候看到的基本做的人都1A,感觉很简单,自己着手做竟然没头绪,看了有半小时才看出来是dfs ,一激动交了tle了两三次,以为是cincout的问题,结果是没对数据进行预处理,即只需选取每个配件的最大最小,在每层的dfs中遍历这两个最大最小即可,然后又莫名其妙wa了十几发,活久见,让各路大佬帮忙看了好久最后终于发现是自己在每个配件筛选最大最小的时候没有刷新初始值,导致上一个配件的最大最小值被保留了下来。
留个纪念,来提醒自己以后不能犯类似错误。
AC代码:
#include<iostream>
using namespace std;
#define ll long long
int a[15][15];
int n,k;
ll int ans,maxn;
void dfs(int count){
if (count == n)
{
// cout<<ans<<endl;
if (ans >maxn)
{
maxn = ans;
}
return;
}
for (int i = 0; i < 2; ++i)
{
ans*= a[count][i];
// count++;
dfs(count+1);
// count--;
ans/= a[count][i];
}
}
int main(int argc, char const *argv[])
{
// freopen("f.txt","r",stdin);
int temn;
ans = 1,maxn = -1e10;
int temmin = 100,temmax = -100;
// cin>>n>>k;
scanf("%d%d",&n,&k);
for (int i = 0; i < n; ++i)
{
temmin = 100,temmax = -100; //wa了十多发的原因就是少了这一行。。。
for (int j = 0; j < k; ++j)
{
scanf("%d",&temn);
if (temn>= temmax)
{
temmax = temn;
}
if (temn<= temmin)
{
temmin = temn;
}
// scanf("%lld",&a[i][j]);
// cin>>a[i][j];
}
a[i][0] = temmin;
a[i][1] = temmax;
}
// for (int i = 0; i < n; ++i)
// {
// cout<<a[i][0]<<" "<<a[i][1]<<endl;
// }
dfs(0);
printf("%lld\n", maxn);
puts("%lld\n", maxn);
// cout<<maxn<<endl;
return 0;
}