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\n",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("\n");
    }
    else
    {
        for(int i=1; i<=num_dec; i++)
        {
            printf("%d ",q_dec[i].id);
        }
        printf("\n");
    }
}

 

 

原文链接: https://www.cnblogs.com/letlifestop/p/11007412.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    D. Dirty Deeds Done Dirt Cheap(思维)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/296622

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午6:04
下一篇 2023年2月15日 下午6:05

相关推荐