C - Canvas Line

Your friend Charmion asked you to hang some canvases out to dry on a straight washing line for an art project she has been working on. The canvases are artfully arranged such that none of them overlap, although they may touch along the edges. For stability, each canvas must be held by two pegs, but because the canvases are very rigid, they can be held from anywhere.

Each canvas is an integral number of centimetres wide (at least 10
cm). Each peg is slightly less than 1

cm wide. Canvases and pegs are all placed at integral centimetre positions along the line.

Unnecessary things touching any canvas is a smudge risk, thus every canvas should be held by exactly two pegs, no more and no less. Given all of the pegs that are already attached to the line, place as few as possible additional pegs as necessary to hold all of the canvases.
\includegraphics[width=1.0\textwidth ]{sample}
Figure 1: Illustration of a solution to Sample Input 2. Pre-existing pegs are marked in white.
Input

The input consists of:

One line with an integer n

(1≤n≤103

), the number of canvases on the line.

n
lines, the ith of which contains two integers ℓi and ri (0≤ℓi<ri≤109 and ℓi+10≤ri), the positions of the left and the right end of the i

th canvas in centimetres.

One line with an integer p
(0≤p≤2⋅103

), the number of pegs already used.

One line with p
integers x1,…,xp (0≤xi<xi+1≤109 for each i

), the position of each existing peg in centimetres.

Canvases are given from left to right and may touch only at edges, that is ri≤ℓi+1
for each i

.
Output

If the canvases can be secured, output the smallest number of extra pegs needed to secure all of the canvases while touching each exactly twice. On the next line output the integer positions of all of the new pegs.

Otherwise, output “impossible”.

If there are multiple optimal solutions, you may output any one of them.
ac代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
struct node
{
    int left;
    int right;
    int num;
    bool lflag;
    bool rflag;
} a[1005];
int p[2005];
map<int,int>mp;
vector<int>ans;
int main()
{
    int n,i,j;
    bool flag=0;
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        scanf("%d%d",&a[i].left,&a[i].right);
        a[i].num=0;
        a[i].lflag=0;
        a[i].rflag=0;
    }
    int q;
    scanf("%d",&q);
    for(i=0; i<q; i++)
    {
        scanf("%d",&p[i]);
        mp[p[i]]=1;
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<q; j++)
        {
            if(p[j]<=a[i].right&&p[j]>=a[i].left)
            {
                a[i].num++;
                if(p[j]==a[i].right)
                {
                    a[i].rflag=1;
                }
                if(p[j]==a[i].left)
                {
                    a[i].lflag=1;
                }
            }
        }
    }

    int x,y;
    for(i=0; i<n-1; i++)
    {
        for(j=a[i].left+1; j<a[i].right; j++)
        {
            if(mp[j]==0)
            {
                x=j;
                y=j;
                break;
            }
        }
        if(a[i].num==0)
        {
            if(i==0)
            {
                ans.push_back(a[i].left);
                mp[a[i].left]=1;
                if(a[i].right==a[i+1].left)
                {
                    if(a[i+1].num<2&&a[i+1].lflag==0)
                    {
                        ans.push_back(a[i].right);
                        mp[a[i].right]=1;
                        a[i+1].num++;
                        a[i+1].lflag=1;
                    }
                    else
                    {
                        ans.push_back(y);
                        mp[y]=1;
                    }
                }
                else
                {
                    mp[y]=1;
                    ans.push_back(y);
                }


            }
            else
            {

                ans.push_back(x);
                mp[x]=1;
                if(a[i+1].left==a[i].right)
                {
                    if(a[i+1].num<2&&a[i+1].lflag==0)
                    {
                        ans.push_back(a[i].right);
                        mp[a[i].right]=1;
                        a[i+1].num++;
                        a[i+1].lflag=1;
                    }
                    else
                    {
                        for(j=a[i].left+1; j<a[i].right; j++)
                        {
                            if(mp[j]==0)
                            {
                                x=j;
                                y=j;
                                break;
                            }
                        }
                        ans.push_back(x);
                        mp[x]=1;
                    }
                }
                else
                {
                    for(j=a[i].left+1; j<a[i].right; j++)
                    {
                        if(mp[j]==0)
                        {
                            x=j;
                            y=j;
                            break;
                        }
                    }
                    ans.push_back(y);
                    mp[y]=1;
                }

            }
        }
        else if(a[i].num==1)
        {
            if(a[i+1].left==a[i].right)
            {
                if(a[i+1].num<2&&a[i+1].lflag==0)
                {
                    ans.push_back(a[i].right);
                    mp[a[i].right]=1;
                    a[i+1].num++;
                    a[i+1].lflag=1;
                }
                else
                {
                    ans.push_back(y);
                    mp[y]=1;
                }
            }
            else
            {
                ans.push_back(y);
                mp[y]=1;
            }

        }
        else if(a[i].num>2)
        {
            flag=1;
            break;
        }
    }
    for(j=a[n-1].left+1; j<a[n-1].right; j++)
    {
        if(mp[j]==0)
        {
            y=j;
            break;
        }
    }
    if(a[n-1].num==0)
    {
        ans.push_back(y);
        ans.push_back(a[n-1].right);
    }
    else if(a[n-1].num==1)
    {

        if(a[n-1].rflag==0)
        {
            ans.push_back(a[n-1].right);
        }
        else
        {
            ans.push_back(y);
        }
    }
    else if(a[n-1].num>2)
    {
        flag=1;
    }
    if(flag==1)
        printf("impossible\n");
    else
    {
        int len=ans.size();
        printf("%d\n",len);
        for(i=0; i<len; i++)
        {
            if(i==0)
                printf("%d",ans[i]);
            else
                printf(" %d",ans[i]);
        }
    }

    return 0;
}

挺无语的,大模拟,可能是自己的方法不行吧,,,也算过了,这几天打什么都打不好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

直接AC好吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值