您的位置:首页 > 其它

HDU 5432 BC - Pyramid Split(二分 + 模拟)

2015-09-12 21:41 543 查看
新生如何加入ACM集训队?

Pyramid Split

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 56 Accepted Submission(s): 29



Problem Description

Xiao Ming is a citizen who's good at playing,he has lot's of gold cones which have square undersides,let's call them pyramids.

Anyone of them can be defined by the square's length and the height,called them width and height.

To easily understand,all the units are mile.Now Ming has n pyramids,there
height and width are known,Xiao Ming wants to make them again to get two objects with the same volume.

Of course he won't simply melt his pyramids and distribute to two parts.He has a sword named "Tu Long" which can cut anything easily.

Now he put all pyramids on the ground (the usdersides close the ground)and cut a plane which is parallel with the water level by his sword ,call this plane cutting plane.

Our mission is to find a cutting plane that makes the sum of volume above the plane same as the below,and this plane is average cutting plane.Figure out the height of average cutting plane.

Input

First line: T,
the number of testcases.(1≤T≤100)

Then T testcases
follow.In each testcase print three lines :

The first line contains one integers n(1≤n≤10000),
the number of operations.

The second line contains n integers A1,…,An(1≤i≤n,1≤Ai≤1000) represent
the height of the ith pyramid.

The third line contains n integers B1,…,Bn(1≤i≤n,1≤Bi≤100) represent
the width of the ith pyramid.

Output

For each testcase print a integer - **the height of average cutting plane**.

(the results take the integer part,like 15.8 you should output 15)

Sample Input

2
2
6 5
10 7
8
702 983 144 268 732 166 247 569
20 37 51 61 39 5 79 99


Sample Output

1
98这道题目可以直接二分也可以模拟通过,这里的是二分代码


#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))

typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " ") {
int d = p < q ? 1 : -1;
while(p != q) {
cout << *p;
p += d;
if(p != q) cout << Gap;
}
cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
int len = bes.length();
if(len >= 2)cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
}

const int INF = 0x3f3f3f3f;
const int MAXM = 1e5;
const int MAXN = 1e4 + 5;
const double eps = 1e-4;
int T, n, A[MAXN], B[MAXN],MAXH;

bool C(double m) {
double Mm = 0, M = 0;
for(int i = 0; i < n; i ++) {
if(m > A[i]) {
M += B[i] * B[i] * A[i] * 1.0 / 3.0;
}
else {
double Bi = (A[i] - m) * 1.0 / A[i];
M += B[i] * B[i] * A[i] * 1.0 / 3.0;
Mm += B[i] * Bi * B[i] * Bi * (A[i] - m) / 3.0;
}
}
if(M >= 2.0 * Mm) return true;
return false;
}

int main() {
cin >> T;
while(T --) {
cin >> n;
MAXH = 0;
for(int i = 0; i < n; i ++) {
cin >> A[i];
MAXH = max(MAXH, A[i]);
}
for(int i = 0; i < n; i ++) {
cin >> B[i];
}
double lb = 0, ub = MAXH;
while(ub - lb > eps) {
double mid = (ub + lb) / 2.0;
if(C(mid)) ub = mid;
else lb = mid;
}
printf("%d\n", (int)ub);
}

return 0;
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: