zoukankan      html  css  js  c++  java
  • oj1025

    方案:二分查找+动态规划
    Problem Description
    JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines.

    Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities). Each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. You may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource. 

    With the development of industry, poor cities wanna import resource from rich ones. The roads existed are so small that they're unable to ensure the heavy trucks, so new roads should be built. The poor cities strongly BS each other, so are the rich ones. Poor cities don't wanna build a road with other poor ones, and rich ones also can't abide sharing an end of road with other rich ones. Because of economic benefit, any rich city will be willing to export resource to any poor one.

    Rich citis marked from 1 to n are located in Line I and poor ones marked from 1 to n are located in Line II. 

    The location of Rich City 1 is on the left of all other cities, Rich City 2 is on the left of all other cities excluding Rich City 1, Rich City 3 is on the right of Rich City 1 and Rich City 2 but on the left of all other cities ... And so as the poor ones. 

    But as you know, two crossed roads may cause a lot of traffic accident so JGShining has established a law to forbid constructing crossed roads.

    For example, the roads in Figure I are forbidden.



    In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^
     
    Input
    Each test case will begin with a line containing an integer n(1 ≤ n ≤ 500,000). Then n lines follow. Each line contains two integers p and r which represents that Poor City p needs to import resources from Rich City r. Process to the end of file.
     
    Output
    For each test case, output the result in the form of sample. 
    You should tell JGShining what's the maximal number of road(s) can be built. 
     
    Sample Input
    2
    1 2
    2 1
    3
    1 2
    2 3
    3 1
     
    Sample Output
    Case 1: My king, at most 1 road can be built.
     
    Case 2: My king, at most 2 roads can be built.
     
     
     
    题意:
    有一个富国和穷国,每个穷国都需要一个富国的东西,所以要修路,但路不能交叉,求最多能修几条路
     
     
     1 #include<iostream>    
     2 using namespace std;
     3 const int MAX=500001;
     4 int dp[MAX];
     5 int a[MAX];
     6 
     7 int find(int num, int len)
     8 {
     9     int right, left, mid;
    10     left = 1;
    11     right = len;
    12     mid = (left + right) / 2;
    13     while (left<=right)
    14     {
    15         if (dp[mid] == num)
    16             return mid;
    17         else if (dp[mid] > num)
    18             right = mid - 1;
    19         else
    20             left = mid + 1;
    21         mid = (left + right) / 2;
    22     }
    23     return left;
    24 }
    25 
    26 void main()
    27 {
    28     int count = 0;
    29     int n;
    30     cin >> n;
    31     while (n != 0)
    32     {
    33         for (int i = 1; i <= n; i++)
    34             dp[i] = 0;
    35         count++;
    36         int len = 1;
    37         int j;
    38         for (int i = 1; i <= n; i++)
    39         {
    40             cin >> j;
    41             cin >> a[j];
    42         }
    43         dp[1] = a[1];
    44         for (int i = 2; i <= n; i++)
    45         {
    46             j = find(a[i], len);
    47             dp[j] = a[i];
    48             if (j > len)
    49                 len = j;
    50         }
    51         cout << "case " << count<<":";
    52         if (len == 1)
    53             cout << "My king, at most 1 road can be built." << endl;
    54         else
    55             cout << "My king, at most " << len << " roads can be built." << endl;
    56     }
    57 }
     
  • 相关阅读:
    096实战 在windows下新建maven项目
    095实战 ETL的数据来源,处理,保存
    094实战 关于js SDK的程序,java SDK的程序
    093实战 Nginx日志切割,以及脚本上传nginx的切割日志
    092实战 数据收集(各种事件)
    091实战 Nginx配置(日志服务器中关于日志的产生)
    android64位机子兼容32位.so库文件
    给 Android 初学者的 Gradle 知识普及
    Android重力感应开发
    随笔之Android平台上的进程调度探讨
  • 原文地址:https://www.cnblogs.com/zhengzhe/p/6616076.html
Copyright © 2011-2022 走看看