UVA - 11039 Building designing
来源:互联网 发布:怎么在u盘中装ubuntu 编辑:程序博客网 时间:2024/06/15 19:28
题目大意:搭建楼层,下一层比上一层大且颜色交替(即正负交替),问最多能搭几层
解题思路:正负数分别存于两个数组,轮流在两个数组取合适的数。模拟过程好复杂而且 WA 好几次。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>using namespace std;int neg[1000000], pos[1000000];int ans1[1000000], ans2[1000000];int t1, t2, tag, tot1, tot2;int cmp(const void*a, const void*b) { return *(int*)a-*(int*)b;}int solve(int ans[]) { int i; for (i = 1; t1 <= tot1 && t2 <= tot2; i++) { if (tag == 1) { tag = 2; while (t2 < tot2 && neg[t2] < ans[i-1]) t2++; if (neg[t2] > ans[i-1]) ans[i] = neg[t2++]; else break; } else{ tag = 1; while (t1 < tot1 && pos[t1] < ans[i-1]) t1++; if (pos[t1] > ans[i-1]) ans[i] = pos[t1++]; else break; } } return i;}int main() { int p, n, t; scanf("%d", &p); while (p--) { memset(ans1, 0, sizeof(ans1)); memset(ans2, 0, sizeof(ans2)); memset(pos, 0, sizeof(pos)); memset(neg, 0, sizeof(neg)); scanf("%d", &n); tot1 = 0, tot2 = 0; while (n--) { scanf("%d", &t); if (t > 0) pos[tot1++] = t; else neg[tot2++] = -t; } qsort(pos, tot1, sizeof(int), cmp); qsort(neg, tot2, sizeof(int), cmp);/*for (int i = 0; i < tot1; i++) cout<<pos[i]<<" ";cout<<endl;for (int i = 0; i < tot2; i++) cout<<neg[i]<<" ";cout<<endl;*/ t1 = t2 = 0; ans1[0] = pos[t1++]; tag = 1; int m = solve(ans1); t1 = t2 = 0; ans2[0] = neg[t2++]; tag = 2; int n = solve(ans2);/*for (int j = 0; j < m; j++) cout<<ans1[j]<<" ";cout<<endl;for (int j = 0; j < n; j++) cout<<ans2[j]<<" ";cout<<endl;*/ printf("%d\n", max(m, n)); } return 0;}
以下是大神的思路:绝对值排序,相邻两个数相乘小于零说明正负交替,注意相乘可能会爆,要用 long long。比上面自己想的简单的多qwq。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespace std;long long flor[600000];bool cmp(int a, int b) { return abs(a)>abs(b);}int main() { int p, n; scanf("%d", &p); while (p--) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &flor[i]); sort(flor, flor+n, cmp); int cnt = 1; for (int i = 1; i < n; i++) if (flor[i] * flor[i-1] < 0) cnt++; printf("%d\n", cnt); } return 0;}
0 0
- UVA 11039 Building designing
- UVA 11039 - Building designing
- uva 11039 - Building designing
- Uva-11039-Building designing
- UVa 11039 - Building designing
- Uva - 11039 - Building designing
- UVA 11039 - Building designing
- uva 11039 Building designing
- Uva 11039 - Building designing
- UVA 11039 - Building designing
- UVA - 11039 Building designing
- UVA 11039 Building designing
- UVA 11039 Building designing
- UVA - 11039 Building designing
- UVa 11039 - Building Designing
- UVa 11039 - Building designing
- UVA - 11039 Building designing
- UVA 11039 Building Designing
- SVD & PCA
- Binary Tree Level Order Traversal
- throw throws 区别
- HDU1022——Train Problem I(栈 stack)
- SGU 106 The equation(扩欧)
- UVA - 11039 Building designing
- 计算思维实践之路(五)
- 前端SEO
- YoMail批量删除邮件
- Android中CoordinatorLayout(协调布局)的使用
- 杭电2033问题
- post乱码、get乱码问题如何解决?
- Activity的启动模式
- Android Studio: Plugin with id 'android-library' not found