我已经采取了icpc2013问题的代码H:matryoshkas娃娃它正常工作,但我不了解它的功能,请帮助我理解它。 [英] I have taken a code for icpc2013 problem H: matryoshkas dolls it works correctly but I am not understanding its functioning , please help me understand it.
问题描述
<pre>#include <cstring>
#include <iostream>
#include <set>
using namespace std;
#define MAXN 1000
int N;
int doll[MAXN+1], dollv[2014], retcost[MAXN+1];
int cost[MAXN+1][MAXN+1], mx[MAXN+1][MAXN+1];
main()
{
cout<<"\n\033[1m\033[32m Enter the number of test cases: ";
while (cin >> N)
{
cout<<"\n\033[1m\033[33mEnter the doll sizes: ";
for (int i = 0; i < N; i++) cin >> doll[i];
memset(cost, 63, sizeof(cost));
memset(mx, 0, sizeof(mx));
for (int i = N-1; i >= 0; i--)
{
cost[i][i+1] = 0;
memset(dollv, 0, sizeof(dollv));
set<int> sorted;
for (int j = i; j < N; j++)
{
if (dollv[doll[j]]) break;
dollv[doll[j]] = true;
mx[i][j+1] = max(mx[i][j], doll[j]);
sorted.insert(doll[j]);
set<int>::iterator smallest = sorted.end();
--smallest;
int seqsize1 = sorted.size(), seqsize2 = 0;
for (int k = j+1; k < N; k++)
{
if (dollv[doll[k]]) break;
while (seqsize1 && doll[k] < *smallest)
{
--seqsize1;
if (smallest != sorted.begin()) --smallest;
}
if (!seqsize1 && doll[k] < *smallest)
{
++seqsize2;
}
cost[i][k+1] = min(cost[i][k+1], cost[i][j+1] + cost[j+1][k+1] +(k+1-i) - max(seqsize1, seqsize2));
}
}
}
memset(retcost, 63, sizeof(retcost));
retcost[0] = 0;
for (int i = 0; i < N; i++) if (retcost[i] < 1000000000)
{
for (int j = i; j < N; j++) if (mx[i][j+1] == j+1-i)
{
retcost[j+1] = min(retcost[j+1], retcost[i] + cost[i][j+1]);
}
}
if (retcost[N] < 1000000000)
{
cout<<"\n\033[1m\033[34mNumber of openings: ";
cout << retcost[N] << endl;
cout<<"\n";
return 0;
}
else
{
cout << "\t\n\033[1m\033[31mImpossible!!\n" << endl;
return 0;
}
}
}
我尝试了什么:
测试条件:
1)
测试用例数:7
1 2 3 2 4 1 5
开口数量:7
2)
测试案例数量:7
1 2 1 2 4 3 3
不可能!!
What I have tried:
test conditions:
1)
number of test cases: 7
1 2 3 2 4 1 5
number of openings: 7
2)
number of test cases: 7
1 2 1 2 4 3 3
Impossible!!
推荐答案
你知道解释代码行的工作量是多少吗?线是?
每一行都需要一段解释!例如:
Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();
创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。
可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?
不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 你想坐下45分钟然后输入逐行描述没有充分理由吗?
如果你不明白它,或者自己写,或者回到你从中得到代码的地方并在那里问:我们不是来向你解释你的作业!
Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.
Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?
No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
If you don't understand it, either write it yourself, or go back to where you got the code from and ask there: we are not here to explain your homework to you!
这篇关于我已经采取了icpc2013问题的代码H:matryoshkas娃娃它正常工作,但我不了解它的功能,请帮助我理解它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!