D. Dirty Deeds Done Dirt Cheap(思维)

题目链接:

https://codeforces.com/problemset/problem/1148/D

题目大意:

给你n个数对,让你构造成题目中的样例,尽可能多的长度。

具体思路:

对式子化简,在分类的前提下(x和y的大小关系),只和x有关。

x1>y1

x2>y1

x2>y2

这样的话,只要保持x递增就可以了,

AC代码:

#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define LL_inf (1ll<<60)
# define inf 0x3f3f3f3f3
const int maxn = 2e6+20;
const int mod = 1e9+7;
struct node
{
    int x,y, id;
    node() {}
    node(int xx,int yy,int zz)
    {
        x=xx;
        y=yy;
        id=zz;
    }
} q_add[maxn],q_dec[maxn];
bool cmp_add(node t1,node t2)
{
    return t1.x>t2.x;
}
bool cmp_dec(node t1,node t2)
{
    return t1.x<t2.x;
}
int main()
{
    int n,st,ed;
    scanf("%d",&n);
    int num_add=0,num_dec=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d %d",&st,&ed);
        if(st<ed)
        {
            q_add[++num_add]=node(st,ed,i);
        }
        else
        {
            q_dec[++num_dec]=node(st,ed,i);
        }
    }
    sort(q_add+1,q_add+num_add+1,cmp_add);
    sort(q_dec+1,q_dec+num_dec+1,cmp_dec);
    printf("%d
",max(num_add,num_dec));
    if(num_add>num_dec)
    {
        for(int i=1; i<=num_add; i++)
        {
            printf("%d ",q_add[i].id);
        }
        printf("
");
    }
    else
    {
        for(int i=1; i<=num_dec; i++)
        {
            printf("%d ",q_dec[i].id);
        }
        printf("
");
    }
}

 

原文地址:https://www.cnblogs.com/letlifestop/p/11007412.html