贪心 --- 巧妙解法

Moving Tables


Time
Limit: 2000/1000 MS (Java/Others)    Memory Limit:
65536/32768 K (Java/Others)
Total Submission(s):
18319    Accepted Submission(s):
6268

Problem Description

The famous ACM (Advanced Computer Maker) Company has
rented a floor of a building whose shape is in the following figure.



The floor
has 200 rooms each on the north side and south side along the corridor.
Recently the Company made a plan to reform its system. The reform includes
moving a lot of tables between rooms. Because the corridor is narrow and all
the tables are big, only one table can pass through the corridor. Some plan is
needed to make the moving efficient. The manager figured out the following
plan: Moving a table from a room to another room can be done within 10 minutes.
When moving a table from room i to room j, the part of the corridor between the
front of room i and the front of room j is used. So, during each 10 minutes,
several moving between two rooms not sharing the same part of the corridor will
be done simultaneously. To make it clear the manager illustrated the possible
cases and impossible cases of simultaneous moving.



For
each room, at most one table will be either moved in or moved out. Now, the
manager seeks out a method to minimize the time to move all the tables. Your
job is to write a program to solve the manager’s problem.

Input

The input consists of T test cases. The number of
test cases ) (T is given in the first line of the input. Each test case begins
with a line containing an integer N , 1<=N<=200 , that represents the
number of tables to move. Each of the following N lines contains two positive
integers s and t, representing that a table is to move from room number s to
room number t (each room number appears at most once in the N lines). From the
N+3-rd line, the remaining test cases are listed in the same manner as
above.

Output

The output should contain the minimum time in minutes
to complete the moving, one per line.

Sample Input

3 4 10 20 30 40 50
60 70 80 2 1 3 2 200 3 10 100 20 80 30 50

Sample Output

10 20 30

【题目来源】

Asia
2001, Taejon (South Korea)

题目意思很容易理解,这里就不过多说了。

【题目分析】

贪心算法,我看很多人用区间标记的方法写了一百多行,其实根本不用,直接就统计排序就行,不仅好理解,而且代码也很精简。


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int cnt[200];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int T,n,a,b,l,r;
scanf("%d",&T);
while(T--)
{
memset(cnt,0,sizeof(cnt));
scanf("%d",&n);
for(int i=0;i<n;++i)
{
scanf("%d%d",&a,&b);
l=min(a,b);
r=max(a,b);
l=(l+1)/2;
r=(r+1)/2;
for(int j=l;j<=r;++j)
cnt[j]++;
}
sort(cnt,cnt+200,cmp);
printf("%d\n",cnt[0]*10);
}
return 0;
}

贪心 --- 巧妙解法

时间: 2024-10-10 17:09:21

贪心 --- 巧妙解法的相关文章

POJ 2182 Lost Cows 巧妙解法

题目 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11766   Accepted: 7570 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'wat

POJ 2109 巧妙解法

Int最大是10^9.所以一般思路是二分+高精度.但是double 范围是10^(-307)-10^308所以可以用double型.k^n=p.所以有k=p^(1/n). 见代码: #include<stdio.h>#include<string.h>#include<iostream>using namespace std;#include<math.h> int main(){     double n, p;     while(cin >>

巧妙解法:构建乘积数组

给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不能使用除法. class Solution { public: vector<int> multiply(const vector<int>& A) { int n = A.size(); vector<int> B; if(n == 0)return B; B[0] = 1

【hoj】2160 bin packing 二分、贪心

这个题是在二分的题单上的,可是依据二分法写出来的会在oj上超时.依据题目以下给出的提示能够发现能通过贪心法每次都找最能满足的情况去填充每个包,这样就能保证使用的包的数量是最少的 二分法解法: #include <iostream> #include <stdio.h> #include <cstring> #include <algorithm> #define MAX 100000 using namespace std; int n,length; in

HDU 2037 今年暑假不AC(贪心,区间更新,板子题)

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 55813    Accepted Submission(s): 30009 Problem Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...”确实如此,世界杯来了,球迷的节日也来了,估计很多AC

【17贪心算法】 剪绳子

这题和我之前做的https://www.cnblogs.com/Jun10ng/p/12363679.html 是同一个题目,但是现在多了一个条件 1<=n<=1000 如果还是用dp的话,dp数组就要用大数类BigInteger 但是,还有一种解法,贪心算法 题目 同上 思路 原本打算用大数类的dp数组 但是看了下贪心的解法,也很容易理解 就是一直乘3, 收获 大数类的使用 头文件是 java.math.BigInteger 初始化函数是BigInteger(String类的数字) 代码(贪

【转】深入浅出PageRank算法

原文链接 http://segmentfault.com/a/1190000000711128 PageRank算法 PageRank算法是谷歌曾经独步天下的“倚天剑”,该算法由Larry Page和Sergey Brin在斯坦福大学读研时发明的, 论文点击下载: The PageRank Citation Ranking: Bringing Order to the Web. 本文首先通过一些参考文献引出问题,然后给出了PageRank的几种实现算法, 最后将其推广至在MapReduce框架下

HDOJ-ACM1021(JAVA)

题意: 斐波拉契数列的另外一个变型,如果F(n)能被3整除,则输出yes,否则输出no.(n<1000000) 解题思路: 看到(n<1000000)这个条件,有点感觉递归量有点大,因此要将递归转为循环~不过有没更巧妙地做法呢,还是暴力破解暂且不知. 递归java代码实现:(结果当然是Time Limit Exceeded) import java.util.*; import java.io.*; public class Main{ public static void main(Stri

CCF题目:相邻数对

问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一个整数,表示值正好相差1的数对的个数. 样例输入 6 10 2 6 3 7 8 样例输出 3 样例说明 值正好相差1的数对包括(2, 3), (6, 7), (7, 8). 评测用例规模与约定 1<=n<=1000,给定的整数为不超过10000的非负整数. -----------------------------