hihocoder 2015ACM-ICPC 北京赛区 Problem A. Xiongnu's Land

原创 2015年11月18日 23:33:44

Description

Wei Qing (died 106 BC) was a military general of the Western Handynasty whose campaigns against the Xiongnu earned him great acclaim. He was arelative of Emperor Wu because he was the younger half-brother of Empress WeiZifu (Emperor Wu's wife) and the husband of Princess Pingyang. He was also theuncle of Huo Qubing, another notable Han general who participated in thecampaigns against the Xiongnu and exhibited outstanding military talent even asa teenager..

Defeated by Wei Qingand Huo Qubing, the Xiongnu sang:“Losing my Qilian Mountains, made my cattleunthriving; Losing my Yanzhi Mountains, made my women lacking rouge.”

 

The text aboveis digested from Wikipedia. Since Wei and Huo's distinguished achievements,Emperor Wu decided to give them some awards --- a piece of land taken by themfrom Xiongnu. This piece of land was located in a desert, and there were manyoases in it. Emperor Wu wanted to draw a straight south-to-north dividing line todivide the land into two parts, and gave the western part to Wei Qing whilegave the eastern part to Huo Qubing.

There are two rules about the landdividing:

 

1.  The total area of the oases lay in Wei's land must be larger orequal to the total area of the oases lay in Huo's land, and the difference mustbe as small as possible.

2.  Emperor Wu wanted Wei's land to be as large as possible withoutviolating the rule 1. 

  

To simplify theproblem, please consider the piece of land given to Wei and Huo as a square ona plane. The coordinate of its left bottom corner was (0,0) and the coordinateof its right top corner was (R,R).  Eachoasis in this land could also be considered as a rectangle which was parallelto the coordinate axes. The equation of the dividing line was like x = n, and nmust be an integer. If the dividing line split an oasis, then Wei owned thewestern part and Huo owned the eastern part. Please help Emperor Wu to find outhow to draw the dividing line.

Input

The first line of theinput is an integer K meaning that there are K (1 <= K <=15) test cases.

For each test case:

The first line is an integer R, indicatingthat the land's right top corner was at (R,R) (1 <

= R <= 1,000,000)

Then a line containingan integer N follows, indicating that there were N (0 <N <= 10000)

oases.

Then N lines follow,each contains four integers  L,T, W andH, meaning that there was an oasis whose coordinate of the left top corner was(L,T), and its width was W and height was H. (0<=L,T <= R, 0<W,H <=R). No oasis overlaps. 

 

output

For each testcase, print an integer n, meaning that Emperor Wu should draw a dividing linewhose equation is x = n. Please note that, in order to satisfy the rules , Emperormight let Wei get the whole land by drawing a line of x = R if he had to.  

Sample Input

2

1000

2

1 1 2 1

5 1 2 1

1000

1

1 1 2 1

Sample Output

5

2

 

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;

ll R, sum;
struct Node
{
    ll w, h;
    ll a, b;
} f[10009];
int n;

ll fun(ll x)
{
    ll tmp = 0;
    for(int i = 0; i < n; i++)
    {
        tmp += f[i].h * max(0LL, min(f[i].w, x - f[i].a));
    }
    return tmp;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%I64d", &R);
        scanf("%d", &n);
        sum = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%I64d%I64d%I64d%I64d", &f[i].a, &f[i].b, &f[i].w, &f[i].h);
            sum += (ll)(f[i].w * f[i].h);
        }

        ll le = 0, ri = R + 1;
        ll mid;
        while(le < ri)
        {
            mid = (le + ri) / 2;
            //cout<<"cnt="<<cnt++<<endl;
            if(fun(mid) * 2 < sum)
                le = mid + 1;
            else
                ri = mid;
        }

         ll ans = fun(ri);
        le = 0, ri = R + 1;
        while(le < ri)
        {
            mid = (le + ri) / 2;
            if(fun(mid) <= ans) le = mid + 1;//再次二分找到使ans最大的位置,此时左边尽量分的多
            else ri = mid;
        }
        int ans1 = le-1;

        printf("%lld\n",le-1);
    }
    return 0;
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

UVALive 7261 A - Xiongnu's Land

Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against th...

2015ACM-ICPC 北京赛区 Problem A. Xiongnu's Land

Description Wei Qing (died 106 BC) was a military general of the Western Handynasty whose campaig...

【hihocoder1249 2015北京赛区A】【打标记线性扫描法】 Xiongnu's Land 土地划分使得左边比右边大且尽可能接近

#include #include typedef long long LL; const int N=1e6+10; int n,g; int y,x,w,h; int a[N]; int main...

hihoCoder 1227 The Cats' Feeding Spots && ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

题目链接: http://hihocoder.com/problemset/problem/1227 解题思路: 题目大意: 平面上有m个点,要从这m个点当中找出n个点,使得包含这n个点的圆的半径(...

hihoCoder 1234 Fractal——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

H Fractal——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

hihoCoder 1233 Boxes(状态压缩 + bfs)——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

hihoCoder 1233 Boxes(状态压缩 + bfs)——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

hihoCoder 1227 The Cats' Feeding Spots(暴力)——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

A The Cats' Feeding Spots(暴力)——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

HDU 5124 hihocoder 1249 Xiongnu's Land (2015北京区域赛A)

扫描线

2015ACM-ICPC 北京赛区 Problem I. Snake Carpet

Description    In school of EECS of Peking University, there is a homework for all freshman -- the ...

2015ACM-ICPC 北京赛区 Problem C. Today Is a Rainy Day

Description      Today is a rainy day. The temperature is apparently lower than yesterday. Winter is...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)