UVA11039- Building designing

题意:有n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替,且绝对值递增。输出最长序列长度。

思路:其实按照绝对值排序后,只要选出正负号交替最长的序列就可以了。用一个标记来表示下一个要选的是正号还是负号。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int MAXN = 500005;

struct node{
    int x, y;
}a[MAXN];

int arr[MAXN], b[MAXN], order[MAXN];
int n;

int cmp(node a, node b) {
    return a.y < b.y;
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i].x);
            if (a[i].x < 0)
                a[i].y = abs(a[i].x);
            else
                a[i].y = a[i].x;
        }
        sort(a, a + n, cmp);

        int cnt = 1, flag ;
        if (a[0].x == a[0].y)
            flag = 1;
        else
            flag = 0;
        for (int i = 1; i < n; i++) {
            if (a[i].x == a[i].y && !flag) {
                flag = 1;
                cnt++;
            }
            else if (a[i].x != a[i].y && flag) {
                flag = 0;
                cnt++;
            }
        }
        printf("%d\n", cnt);
    }
    return 0;
}

UVA11039- Building designing,布布扣,bubuko.com

时间: 2024-10-12 11:26:20

UVA11039- Building designing的相关文章

UVA 11636 Hello World,LA 3602 DNA Consensus String,UVA 10970 Big Chocolate,UVA 10340 All in All,UVA 11039 Building Designing

课程好紧啊,只能刷点水题了,几乎都是贪心. UVA 11636 Hello World 二的幂答案就是二进制长度减1,不是二的幂答案就是是二进制长度. #include<cstdio> int main() { int n,kas = 0; while(scanf("%d",&n),n>0){ int r = 0; for(n--;n;n>>=1) r++; printf("Case %d: %d\n",++kas,r); }

uva 11039 Building designing (排序)

uva 11039 Building designing 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 addit

11039 - Building designing

  Building designing  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, th

UVA 11039 Building designing 贪心

题目链接:UVA - 11039 题意描述:建筑师设计房子有两条要求:第一,每一层楼的大小一定比此层楼以上的房子尺寸要大:第二,用蓝色和红色为建筑染色,每相邻的两层楼不能染同一种颜色.现在给出楼层数量和每层楼的尺寸(楼层尺寸的大小没有按照顺序给出),求出满足这样要求的最大楼层数. 算法分析:把楼层尺寸按照从大到小排序,然后遍历一次的同时记录相邻楼层所染颜色不同,把不满足要求的楼层去掉即可. 1 #include<iostream> 2 #include<cstdio> 3 #inc

UVa 11039 - Building designing

题目:有n个绝对值不为0的数字,从中找到一个序列,正负交替,绝对值递增,求序列最大长度. 分析:dp,动态规划.因为绝对值要递增,所以先按绝对值排序. 设前k个数组成大的序列最长为f(k),则有如下地推关系: f(k)= f(k-1)        { data[k]*data[k-1] > 0,最后量元素不同时取 } = f(k-1)+ 1  { data[k]*data[k-1] < 0,最后量元素同时取 } (所有数据均不相同,且不为零) 说明:(⊙v⊙). #include <a

UVa 11039 (排序+贪心) Building designing

白书上的例题比较难,认真理解样例代码有助于提高自己 后面的练习题相对简单,独立思考解决问题,增强信心 题意:n个绝对值各不相同的非0整数,选出尽量多的数排成序列,使得该序列正负交错且绝对值递增. 解法:先按绝对值从小到大排序,然后第一个数先入队,然后依次比较后面的数,如果和队尾的数符号相反则入队,直到所有的数遍历完为止 这里用了异或运算,因为这里面没有0,所谓符号相同的两个数异或值为正,不同符号的数异或值为负 1 //#define LOCAL 2 #include <algorithm> 3

UVa 11039 Building designing (贪心+排序+模拟)

题意:给定n个非0绝对值不相同的数,让他们排成一列,符号交替但绝对值递增,求最长的序列长度. 析:我个去简单啊,也就是个水题.首先先把他们的绝对值按递增的顺序排序,然后呢,挨着扫一遍,只有符号不同才计数,easy!!! 代码如下: #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; const int maxn = 5

UVA - 11039 B - Building designing

https://odzkskevi.qnssl.com/516b2b3ad824ada50248f23d20f53083?v=1502089438 1 /* 2 n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增 3 */ 4 #include <iostream> 5 #include <string.h> 6 #include <stdio.h> 7 #include <algorithm> 8 using names

大白书第一章

UVA 11292 The Dragon of Loowater(简单贪心) 题意: n条恶龙,m个勇士,用勇士来杀恶龙.一个勇士只能杀一个恶龙.而且勇士只能杀直径不超过自己能力值的恶龙.每个勇士需要支付能力值一样的金币.问杀掉所有恶龙需要的最少金币. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 20000 + 5; 4 int n, m; 5 int night[maxn], dragon[maxn

Building Maintainable Software-java篇之Separate Concerns in Modules

Building Maintainable Software-java篇之Separate Concerns in Modules   In a system that is both complex and tightly coupled, accidents are inevitable. -Charles Perrow's Normal Accidents theory in one sentence Guideline: ? Avoid large modules in order to