您的位置:首页 > 运维架构

UVa 1523 Helicopter 解题报告(枚举排列)

2014-07-25 15:17 316 查看


1523 - Helicopter

Time limit: 3.000 seconds

Since ancient time, people have been dreaming of flying in the sky. Eventually, the dream was realized in the 20th century. Nowadays, the airplane becomes a useful vehicle that is used frequently in our
daily life.
But before the dream came true, a large number of people had tried to design the aircrafts. One of those aircrafts, which is called ``helicopter" in modern time, can be traced back to the blueprint of
the aircraft designed by Leonardo da Vinci. But the helicopter was not effective enough till this century.
Since the helicopter rises through the updraft generated by the airscrew, it is very important for it to keep balance. Even for the late-model helicopters, the loads are required to be distributed evenly,
so that the center of gravity of the helicopter lies just underneath the airscrew. It is one of the most important requirements for it in flight.
Now, you are requested by an airline company to write a program for a passenger transport helicopter. The program may arrange a seat for each passenger automatically so that the center of gravity of the
helicopter should be located underneath the airscrew as dose as possible. The seats in the helicopter and the airscrew are designed as the figure below.



You may assume the distance of the adjoining seats is 1 unit, and the airscrew occupies a seat. A seat along with a passenger on it will generate a transverse mome nt and a longitudinal moment. The transverse
moment, Mvi, is the weight of a passenger on the seat multiplied by the transverse distance from the seat to the airscrew. The longitudinal moment, Mhi, is the weight
of a passenger on the seat multiplied by the longitudinal distance. If the transverse moments generated by the passengers on the left are assumed to be positive, the moments by the passengers on the right will be negative. Also, if the longitudinal moments
generated by the passengers in front are assumed to be positive, the moments by the passengers on the back will be negative. That is, the moments may counteract with each other. You may use the formula below to figure out the composition of moments.

M = 


If M = 0, the center of gravity of the helicopter lies just underneath the airscrew.
You are required to arrange the seats of 8 passengers according to their weights to locate the center of gravity underneath the airscrew as far as possible. That is, the value of M should
be minimum.

Input 

The input file may contain several test cases. Each test case consists of eight integers in lines, which represent the weights of those passengers. The end of input is signified by the test case in which
the weights are all 0. And this test case should not be processed.

Output 

The output for each test case should include a line contains a real number which tells the composition of moments, M, in the optimal arrangement. The output of the composition
of moments should be accurate to 3 decimal places. And you should not print any more white spaces or blank lines in the output.

Sample Input 

1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0


Sampple Output 

0.000


    解题报告: 一共8个位置,给定8个整数,求某个排列使公式值最小。

    直接暴力。当然,排列中存在重复,可以先求出所有不重复的排列,这样会快一点。

    代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif // ACM
work();
}

/***************************************************/

int per[504000][8];
int tot;

void init()
{
string order = "12345678";
set<string> ss;

do
{
bool flag = true;
for(int i=0;i<8;i+=2) if(ss.count(order.substr(i)+order.substr(0, i)))
flag = false;

string rev = order.substr(1);
reverse(rev.begin(), rev.end());
rev = order.substr(0, 1) + rev;
for(int i=0;i<8;i+=2) if(ss.count(rev.substr(i)+rev.substr(0, i)))
flag = false;

if(flag) ss.insert(order);
} while(next_permutation(order.begin(), order.end()));

for(set<string>::iterator it = ss.begin();it!=ss.end();it++, tot++)
ff(i, 8) per[tot][i] = (*it)[i] - '1';
}

int num[8];

void work()
{
init();

while(scanf("%d%d%d%d%d%d%d%d", num, num+1, num+2, num+3, num+4, num+5, num+6, num+7) == 8)
{
bool allzero = true;
ff(i, 8) if(num[i]) allzero = false;
if(allzero) break;

double ans = 1e30;
ff(i, tot)
{
double h = (double)num[per[i][0]] + num[per[i][1]] + num[per[i][2]]
- num[per[i][4]] - num[per[i][5]] - num[per[i][6]];

double v = (double)num[per[i][0]] + num[per[i][7]] + num[per[i][6]]
- num[per[i][2]] - num[per[i][3]] - num[per[i][4]];

ans = min(ans, sqrt(h*h + v*v));
}
printf("%.3lf\n", ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: