PDA

View Full Version : مفهوم کد C++ زیر چیه ؟



farzaneh.p
شنبه 22 خرداد 1389, 03:10 صبح
سلام . این کد برای کتاب طرخی الگوریتم با کد C++‎ .
کد ها برام نامفهومن .اگه می شه در موردش توضیح بدین برام .یه توضیحاتی هم خودش داده اما من در مورد کدش مشکل دارم. نمیفهمم . 2 شنبه باید ارائه بدم به استادمون .

مرسی.

Is Bigger Smarter?



PC/UVa IDs: 111101/10131, Popularity: B, Success rate: high Level: 2

Some people think that the bigger an elephant is, the smarter it is. To disprove this,

you want to analyze a collection of elephants and place as large a subset of elephants

as possible into a sequence whose weights are increasing but IQ’s are decreasing.



Input

The input will consist of data for a bunch of elephants, at one elephant per line terminated

by the end-of-file. The data for each particular elephant will consist of a pair of

integers: the first representing its size in kilograms and the second representing its IQ

in hundredths of IQ points. Both integers are between 1 and 10,000. The data contains

information on at most 1,000 elephants. Two elephants may have the same weight, the

same IQ, or even the same weight and IQ.



Output

The first output line should contain an integer n, the length of elephant sequence

found. The remaining n lines should each contain a single positive integer representing

an elephant. Denote the numbers on the ith data line as W[i] and S[i]. If these sequence

of n elephants are a[1], a[2],..., a[n] then it must be the case that

W[a[1]]<W[a[2]] < ... < W[a[n]] and S[a[1]] > S[a[2]] > ... > S[a[n]]i

In order for the answer to be correct, n must be as large as possible. All inequalities

are strict: weights must be strictly increasing, and IQs must be strictly decreasing.

Your program can report any correct answer for a given input.



#include <iostream> // Accepted in 0.000 and 8 on the ranklist!
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

struct ELEPHANT {
int sn; // serial number
int weight;
int iq;
friend bool operator > (ELEPHANT t1, ELEPHANT t2){ return (t1.weight > t2.weight)&&(t1.iq < t2.iq); }
};

void merge_sort(ELEPHANT array[], int begin_p, int end_p)
{
if(begin_p >= end_p -1) return;

int temp = (end_p + begin_p) / 2, i, j, k;
ELEPHANT array2[end_p-begin_p];

merge_sort(array, begin_p, temp);
merge_sort(array, temp, end_p);

for(i = begin_p, j = temp, k = 0; (i < temp)&&(j < end_p); k++)
{
if(array[i].weight <= array[j].weight) array2[k] = array[i++];
else array2[k] = array[j++];
}

while(i < temp) array2[k++] = array[i++];
while(j < end_p) array2[k++] = array[j++];

copy(array2, array2+end_p-begin_p, array+begin_p);
}

int main()
{
ELEPHANT array[1000];
int elephant_num; // number of the elephants

for(elephant_num = 0; cin >> array[elephant_num].weight >> array[elephant_num].iq; elephant_num++)
array[elephant_num].sn = elephant_num+1;

// weight in increasing order
merge_sort(array, 0, elephant_num);

int last[elephant_num];
int len[elephant_num];
int max_len = 0, ptr = -1;

fill(last, last+elephant_num, -1);
fill(len, len+elephant_num, 1);

for(int i = 1; i < elephant_num; i++)
{
for(int j = 0; j < i; j++)
{
if(array[i] > array[j])
{
if(len[j]+1 > len[i])
{
len[i] = len[j] + 1, last[i] = j;

if(len[i] > max_len)
max_len = len[i], ptr = i;
}
}
}
}

cout << max_len << endl;

vector<int> temp;

while(ptr != -1)
{
temp.push_back(array[ptr].sn);
ptr = last[ptr];
}

for(int i = temp.size()-1; i >= 0; i--)
cout << temp[i] << endl;

return 0;

}