贪心 没什么可以说的,还是要注意细节

Moving Tables

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

就是移动桌子,移动一次10min,我就排序Y大到小,一样在排X。

需要注意的是4 5 6 7算两次因为5和6在同一个地方详细的见上图和表格

#include<stdio.h>
#include<string.h>
main()
{
int i,n,m,g,t,sum,max,min;
int x[300];
int y[300];
int z[300];
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
sum=0;
memset(z,0,sizeof(z));
for(i=1;i<=m;i++)
{scanf("%d %d",&x[i],&y[i]);
if(x[i]>y[i])
{
t=x[i];
x[i]=y[i];
y[i]=t;
}}
for(i=1;i<m;i++)
for(g=i+1;g<=m;g++)
{
if(y[i]<y[g])
{ t=x[i];
x[i]=x[g];
x[g]=t;
t=y[i];
y[i]=y[g];
y[g]=t; }
}
for(i=1;i<m;i++)
for(g=i+1;g<=m;g++)
{
if(y[i]==y[g]&&x[i]<x[g])
{ t=x[i];
x[i]=x[g];
x[g]=t;}
}
for(i=1;i<m;i++)
{max=x[i];
if(z[i]==0){
for(g=i+1;g<=m;g++)
{
if(y[g]<=max&&z[g]==0){
if(max-y[g]==1&&y[g]%2==1)
z[g]=0;
else{z[g]=1;max=x[g];}
}
}
sum++; } }
if(z[m]==0)
sum++;
printf("%d\n",sum*10);
}
}

贪心 没什么可以说的,还是要注意细节

时间: 2024-08-29 15:07:47

贪心 没什么可以说的,还是要注意细节的相关文章

1-22

今天考了场试.....虽然题好像很简单......但还是只拿了一半的分数..... 其中一个是没有加algorithm库............ 今天试题感觉营养并不高(???)...感觉跟usaco银组差不多....... 一个贪心(模拟),一个基本数论(刚好符合复杂度的因数分解),一个分治(??并不懂......反正跟本月usaco月赛银组t3差不多{当时还想了好久的说.},一个不让懒人得分的找规律...... 贪心没什么好说的....(然鹅爆零了#algorithm) 在打数论的时候原本以

CF735C 数论\平衡树叶子节点的最大深度\贪心\斐波那契\条件归一化

http://codeforces.com/problemset/problem/735/C 题意..采用淘汰赛制..只要打输就退出比赛..而且只有两个选手打过的场数 相差不超过1才能比赛..最后问你..最多打几场比赛能决出冠军 那么这个题的做法是..画图..观察..分析 Tip:首先我们观察未知量的形式..它是一个复合函数的形式..max(决出冠军的所有可能的比赛方式的场数) 那么我们首先要求括号最里面的东西..即符合条件的所有可能的比赛方式的场数 那么我们可以画一画 我一开始为了保证比赛能正

贪心的几种题型

序: 我不是大牛,也只是个初学者,写这些也只是为了回顾一下自己所学的知识,当然不会的也求教大牛了,大家对我的要求不要太高,希望大牛多多指点. 正文: 一.算法思路:尽可能的最优解. —不能能保证最后的解为最优解: —不能用来求最大或最小解: —只能用来求满足某些约束条件的可行解的范围. 二.题型: (1)背包问题:有一个背包,背包容量是M=150.有7个物品,物品可以分割成任意大小.要求尽可能让装入背包中的物品总价值最大,但不能超过总容量 等对背包问题多种要求详细理解后再以专题形式总结. (2)

HDU 4070 + 赤裸裸的贪心~~

J - Phage War Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Phage War is a little flash game. In this game, we want infect all cells by the transmission and breed of phages. Originally, there is a cell infected by phage

POJ_2376_Cleaning Shifts(贪心)

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12788   Accepted: 3312 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

刷题向》关于一道奇怪的贪心(田忌赛马)BZOJ1034

这道题一看就是一道贪心,比较简单,但是越容易的题考试的时候越容易错... 没什么好说的,一遍SORT之后,直接强行田忌赛艇(滑稽脸)就好啦. 注意在对比大小的时候不仅要从前还要从后同时,不过这两个情况的优先级(亲测)没什么差别. 传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1034 直接甩出题目&代码 Description 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选

【读书笔记/复健向】算法竞赛入门经典训练指南1.1贪心部分

例题一(UVa11292) 基础贪心,没什么需要多讲的,直接放上代码.有一点要注意的是我第一遍写的时候竟然犯了两个错误. 错误点 将dragon.knight与i.j对应错误了,仔细一想人有先后对应的天性,下次设置i.j时还是老老实实根据输入顺序来,避免出错 第23行遗漏了(j<n)这个条件,使得在龙已经全被砍头的情况下却雇佣了剩余的骑士. 本题重点 砍龙头的时候设置两个指针,分别移动,使用频率挺高的一个小技巧,不难,但是挺重要的. 1 #include<iostream> 2 #inc

贪心法——活动选择问题和背包问题

今天上午听了米老师讲的算法,感觉收获很多,对于算法更加有信心了.首先,先来宏观看一下: 这三种算法总的来说,刚开始看的时候不知道怎么下手,但是看多了也会有那么一点儿感觉.分治法是这三种算法里面都有的思想,动态规划和贪心都是将问题分解成子问题求解,但动态规划里面的子问题都带有联系,而贪心算法里面的子问题都相对独立,唯一不同的是,贪心算法要首先想出一个解决方案来构造求解最优解的过程. 宏观介绍下算法后,来看看贪心算法的两个实例. 一,活动选择问题 解决方案: 对于活动的选择问题,我们求解过程是这样的

【BZOJ1692】[Usaco2007 Dec]队列变换 后缀数组+贪心

[BZOJ1692][Usaco2007 Dec]队列变换 Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的"全美农场主大奖赛".在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席前依次走过. 今年,竞赛委员会在接受队伍报名时,采用了一种新的登记规则:他们把所有队伍中奶牛名字的首字母取出,按它们对应奶牛在队伍中的次序排成一列(比如说,如果FJ带去的奶牛依次为Bessie.Sylvia.Dora,登记人员就把