UVA1523-Helicopter(暴力+全排列)

题目链接

题意:有八个乘客坐在直升机上,求重心M最小值。

思路:根据题目所给的公式,我们可以知道要使得M最小,也就是要使得Mv和Mh的和最小,我们可以使用全排列,分别将每个值放在各个位子上,然后更新最小值。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;

int a[8];

int main() {
    while (1) {
        for (int i = 0; i < 8; i++)
            scanf("%d", &a[i]);
        if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5] && !a[6] && !a[7])
            break;
        sort(a, a + 8);
        double Min = INF;
        do{
            double mv, mh, m;
            double x1, x2, y1, y2;
            x1 = a[5] + a[6] + a[7];
            x2 = a[0] + a[1] + a[2];
            y1 = a[2] + a[4] + a[7];
            y2 = a[0] + a[3] + a[5];
            mv = pow((x1 - x2), 2);
            mh = pow((y1 - y2), 2);
            m = sqrt(mv + mh);
            if (Min > m)
                Min = m;
        } while(next_permutation(a, a + 8));
        printf("%.3lf\n", Min);
    }
    return 0;
}

UVA1523-Helicopter(暴力+全排列)

时间: 2024-08-02 03:04:35

UVA1523-Helicopter(暴力+全排列)的相关文章

hdu 5727 Necklace 阴阳珠 二分图匹配+暴力全排列

Necklace Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2462    Accepted Submission(s): 775 Problem Description SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang e

HDU 2616 Kill the monster (暴力搜索 || 终极暴力全排列)

题目链接:HDU 2616 Kill the monster 题意:有N个技能去打HP有M的怪兽,技能(A,M),技能伤害为A,当怪兽HP<=M时伤害为2*A.求打死怪兽(HP<=0)用的最少技能 方法一:将技能全排列,计算伤害,得到答案. 方法二:搜索,具体看代码. 全排列AC代码: #include<stdio.h> #include<algorithm> using namespace std; struct node { int p,v; }; struct n

lightoj-1021 - Painful Bases(状压+数位dp)

1021 - Painful Bases PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBAs you know that sometimes base conversion is a painful task. But still there are interesting facts in bases. For convenience let's assume that we are deali

【暴力,STL,水】UVa 1523 - Helicopter

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 l

uv216 暴力枚举全排列

数据范围较小,暴力枚举全排列就行 // // main.cpp // uva216 // // Created by Fangpin on 15/3/8. // Copyright (c) 2015年 FangPin. All rights reserved. // #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <vector&

牛客网NowCoder 2018年全国多校算法寒假训练营练习比赛(第四场)A.石油采集(dfs) B.道路建设(最小生成树prim) C.求交集(暴力) F.Call to your teacher(迪杰斯特拉乱用) H.老子的全排列呢(dfs)

菜哭了... A.石油采集 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 链接:https://www.nowcoder.com/acm/contest/76/A来源:牛客网 题目描述 随着海上运输石油泄漏的问题,一个新的有利可图的行业正在诞生,那就是撇油行业.如今,在墨西哥湾漂浮的大量石油,吸引了许多商人的目光.这些商人们有一种特殊的飞机,可以一瓢略过整个海面20米乘10米这么大的长方形.(上下相

uva 140 Bandwidth (全排列+暴力枚举)

uva 140 Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it

POJ2718 Smallest Difference (暴力搜索+全排列)

题目链接:传送门 题意: 给你一个长度为n的数列 ,0<=a[i]<=9;用他们组成两个数 然后使两个数的差最小,求这个数.不能有前导0. 分析: 要使差最小肯定要使两个数的长度尽量相等,一个为n/2,另 一个为n-n/2.然后dfs搜一下选的数,然后另一个数用剩下的 数做一个全排列就可以了. 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <algorit

暴力搜索专题小结:全排列及可重集排列生成算法

1~n的全排列 (1)思路:按照递归的思想,初始化集合S中含有1~n所有元素.如果1~n的集合S为空,那么输出全排列:否则从小到大依次考虑每个元素i,在A的末尾添加i后,集合S变为S-{i}.这里我们不需要集合S,只需要利用一个变量cur表示当前位要填的数即可.那么A中没有出现过的元素均可以选择. #define N 100 int A[N]; void print_permutation(int n, int*A, int cur) { if (cur == n) { for (int i =