UVA 11039(模拟,贪心)
来源:互联网 发布:r语言与数据挖掘 谢 编辑:程序博客网 时间:2024/04/29 12:42
An architect wants to design a very high building. The building will consist of some floors, and each
floor has a certain size. The size of a floor must be greater than the size of the floor immediately
above it. In addition, the designer (who is a fan of a famous Spanish football team) wants to paint
the building in blue and red, each floor a colour, and in such a way that the colours of two consecutive
floors are different.
To design the building the architect has n available floors, with their associated sizes and colours.
All the available floors are of different sizes. The architect wants to design the highest possible building
with these restrictions, using the available floors.
Input
The input file consists of a first line with the number p of cases to solve. The first line of each case
contains the number of available floors. Then, the size and colour of each floor appear in one line.
Each floor is represented with an integer between -999999 and 999999. There is no floor with size 0.
Negative numbers represent red floors and positive numbers blue floors. The size of the floor is the
absolute value of the number. There are not two floors with the same size. The maximum number of
floors for a problem is 500000.
Output
For each case the output will consist of a line with the number of floors of the highest building with
the mentioned conditions.
Sample Input
2
5
7
-2
6
9
-3
8
11
-9
2
5
18
17
-15
4
Sample Output
2
floor has a certain size. The size of a floor must be greater than the size of the floor immediately
above it. In addition, the designer (who is a fan of a famous Spanish football team) wants to paint
the building in blue and red, each floor a colour, and in such a way that the colours of two consecutive
floors are different.
To design the building the architect has n available floors, with their associated sizes and colours.
All the available floors are of different sizes. The architect wants to design the highest possible building
with these restrictions, using the available floors.
Input
The input file consists of a first line with the number p of cases to solve. The first line of each case
contains the number of available floors. Then, the size and colour of each floor appear in one line.
Each floor is represented with an integer between -999999 and 999999. There is no floor with size 0.
Negative numbers represent red floors and positive numbers blue floors. The size of the floor is the
absolute value of the number. There are not two floors with the same size. The maximum number of
floors for a problem is 500000.
Output
For each case the output will consist of a line with the number of floors of the highest building with
the mentioned conditions.
Sample Input
2
5
7
-2
6
9
-3
8
11
-9
2
5
18
17
-15
4
Sample Output
2
5
题意:给你一堆数字,让你构造一个新数列,要求是绝对值递增,而且正负值交替
题解:先绝对值排序。明显第一个数字是将绝对值较小的那个作为数列的开头,然后直接模拟就好啦。
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<string>#include<algorithm>#include<set>#include<cmath>#include<map>#define LL long long using namespace std;vector<int>a,b;int solve(){int f,tmp,t1=0,t2=0;if(a[0]<b[0]){f=1;tmp=a[0];}else{f=0;tmp=b[0];}int ans=1;int over=0;while(t2<a.size()&&t1<b.size()){if(f){while(tmp>b[t1]){t1++;if(t1==b.size()){over=1;break;}}f=0;if(!over){tmp=b[t1];ans++;}else return ans;}else{while(tmp>a[t2]){t2++;if(t2==a.size()){over=1;break;}}f=1;if(!over){tmp=a[t2];ans++;}else return ans;}}return ans;}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endif int t,n,x;scanf("%d",&t);while(t--){a.clear();b.clear();scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&x);if(x>0)a.push_back(x);else b.push_back(-x);}sort(a.begin(),a.end());sort(b.begin(),b.end());printf("%d\n",solve());} return 0;}
0 0
- UVA 11039(模拟,贪心)
- UVA 12545 Bits Equalizer(模拟+贪心)
- UVA 311 Packets(贪心 + 模拟)
- UVA 311 Packets 贪心+模拟
- uva--1368(贪心,字符串模拟)
- uva - 10716 - Evil Straw Warts Live(贪心、模拟)
- UVA-11054-Wine trading in Gergovia(模拟+贪心)
- UVA-10716 Evil Straw Warts Live(贪心+模拟)
- Uva---11520Problem A Fill the Square (贪心+模拟)
- uva 11054 Wine trading in Gergovia (贪心 + 模拟)
- UVA 10714 Ants 蚂蚁 贪心+模拟 水题
- UVA 10366 - Faucet Flow(贪心+模拟细节)
- uva 11039 - Building designing(贪心)
- UVa 11039 - Building designing(贪心)
- uva 11039 - Building designing(贪心)
- uva 11039 贪心法
- uva 11389(贪心)
- uva - 10037(贪心)
- Java中数组赋值的三种方式
- AndroidEditText的边框颜色及有无焦点下不同状态
- 【NanoPi2试用体验】扩容SD卡,利用gparted将剩余空间利用起来
- SPL常用数据结构
- SDUT 数据结构实验之排序六:希尔排序
- UVA 11039(模拟,贪心)
- C#英汉翻译
- android开发那些事儿(二)--Drawable资源
- os函数 lua
- 支付宝集成
- android:ToolBar详解
- C++学习:构造函数
- 《jQuery基础教程》第四版第七章课后练习——使用插件
- LeetCode 053