uva11039 -Building designing
2016-02-13 13:09
435 查看
题意:
n个绝对值不同的非0整数,求正负交叉绝对值递增能排出的最长序列长度。
思路:
将序列按绝对值排序后再控制正负号即可
代码如下:
n个绝对值不同的非0整数,求正负交叉绝对值递增能排出的最长序列长度。
思路:
将序列按绝对值排序后再控制正负号即可
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int N = 500005; int arr ; int cmp(int a, int b){ return abs(a)<abs(b); } int main() { int cas; scanf("%d", &cas); while (cas--){ int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } sort(arr, arr + n, cmp); int ans = 1, t; if (arr[0]<0) t = -1; else t = 1; for (int i = 1; i < n; i++){ if (arr[i] * t<0){ ans++; t = -t; } } printf("%d\n", ans); } }
相关文章推荐
- UIImagePickerController页中,按钮显示中文问题
- [SPOJ375]Query on a tree
- Building Maintainable Software-java篇之Write Code Once
- MASQUERADE 和 SNAT的区别
- UI基础-01第一个iOS应用程序
- uva1121 - Subsequence
- Key-Value Observing
- Sidelinesoft NL5 Circuit Simulator 2.1.1
- poj2127Greatest Common Increasing Subsequence【LICS】
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
- UITableView如何撤销移动操作
- UITableView如何撤销移动操作
- UITableView如何撤销移动操作
- Codeforces Round #342 (Div. 2) 625A Guest From the Past(贪心)
- 【SoapUI】在soapui中使用Groovy脚本常用方法
- 【SoapUI】自动化测试实施方法
- how to install cmake-gui on ubuntu
- 分治算法 Divide and Conquer Algorithm
- SpatialHadoop 中的 Operations 层 之 Range Query
- LeetCode Count Univalue Subtrees