Codeforces270D Greenhouse Effect【思维+LIS】

解决一个关于植物重新布局的问题,通过移动最少数量的植物来确保不同种类的植物按升序排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Greenhouse Effect
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.

Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.

Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.

Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.

Input

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point.

It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi < xi + 1, 1 ≤ i < n).

Output

Output a single integer — the minimum number of plants to be replanted.

Examples
input
3 2
2 1
1 2.0
1 3.100
output
1
input
3 3
1 5.0
2 5.5
3 6.0
output
0
input
6 3
1 14.284235
2 17.921382
1 20.328172
3 20.842331
1 25.790145
1 27.204125
output
2
Note

In the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.

In the second test case, the species are already in the correct order, so no replanting is needed.



题目大意:


给出N个植物,每个植物都属于一个品种,共计m个品种,分落在不同的位子上(在一个数轴上,而且数轴是无限长度的),保证读入的位子是按照升序读入的。

现在我们可以进行一个操作:取任意一个位子上的植物,移动到任意一个没有植物的位子上去。

问我们最少进行多少次操作,能够使得从左到右,是按照品种升序排列的(1~m),而且每种植物都相邻;


思路:


①仔细读题之后发现,位子是没有用的,那么我们的任务就是挪动最少次数,使得序列品种升序。


②再仔细思考一下,我们挪动到的位子是不限的,那么我们其实问题就是确定最多多少个植物不挪动相对位子,而挪动其他植物即可,

那么问题就是求一个LIS,答案就是n-Lis


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[150000];
int have[150000];
int ned[150000];
int dp[150000];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(have,0,sizeof(have));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            double pos;
            scanf("%d%lf",&a[i],&pos);
        }
        int maxn=0;
        for(int i=1;i<=n;i++)
        {
            dp[i]=1;
            for(int j=i-1;j>=1;j--)
            {
                if(a[i]>=a[j])
                {
                    dp[i]=max(dp[i],dp[j]+1);
                }
            }
            maxn=max(maxn,dp[i]);
        }
        printf("%d\n",n-maxn);
    }
}










当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值