[ACM] SDUT 2886 Weighted Median (省賽最悲劇的一道題)
Weighted Median
Time Limit: 2000ms Memory limit: 65536K 有疑問?點這裡^_^
題目描述
For n elements x1,?x2,?...,?xn with positive integer weights w1,?w2,?...,?wn. The weighted median is the element xk satisfying
and
,
S indicates
Can you compute the weighted median in O(n) wZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcnN0LWNhc2U/CiAKCjxoMj4KyuTI6zwvaDI+CgpUaGVyZSBhcmUgc2V2ZXJhbCB0ZXN0IGNhc2VzLiBGb3IgZWFjaCBjYXNlLCB0aGUgZmlyc3QgbGluZSBjb250YWlucyBvbmUgaW50ZWdlciBuKDE/odw/P24/odw/MTBeNykgoaogdGhlIG51bWJlciBvZiBlbGVtZW50cyBpbiB0aGUgc2VxdWVuY2UuIFRoZSBmb2xsb3dpbmcgbGluZSBjb250YWlucyBuIGludGVnZXIgbnVtYmVycyB4aSAoMD+h3D94aT+h3D8xMF45KS4gVGhlIGxhc3QgbGluZQogY29udGFpbnMgbiBpbnRlZ2VyIG51bWJlcnMgd2kgKDA/PD93aT88PzEwXjkpLgogCgo8aDI+Csrks/Y8L2gyPgoKT25lIGxpbmUgZm9yIGVhY2ggY2FzZSwgcHJpbnQgYSBzaW5nbGUgaW50ZWdlciBudW1iZXKhqiB0aGUgd2VpZ2h0ZWQgbWVkaWFuIG9mIHRoZSBzZXF1ZW5jZS4KIAoKPGgyPgrKvsD9yuTI6zwvaDI+Cgo8cHJlIGNsYXNzPQ=="brush:java;">7
10 35 5 10 15 5 20
10 35 5 10 15 5 20
示例輸出
20
提示
The S which indicates the sum of all weights may be exceed a 32-bit integer. If S is 5,
equals 2.5.
來源
2014年山東省第五屆ACM大學生程序設計競賽
解題思路:
當時最後半個多小時三個人死活沒想出來怎麼做,現在拿出這道題一看十幾分鐘就解決了。。。現場的時候心態實在是太重要了,一慌腦子就容易空白。。。
代碼:
#include
#include
#include
using namespace std;
const int maxn=1e7+2;
int n;
struct Node
{
int x,w;
}node[maxn];
bool cmp(Node a,Node b)
{
if(a.x