Codeforces 269B.Greenhouse Effect(lis水题)

问把n个东西,每个物品有一个编号,每种必须放在同一个子串中,每次操作可以交换两个物品位置,问最少操作次数

麻了,刚开始一位是每次只能移动一位,胡乱分析了半天,后来发现能直接交换,那问题不就变成了保留最多的原位置的,而且编号要升序

那这个就变成了求最长非下降子序列,看这个数据范围被唬到了呢

#include <bits/stdc++.h>

using namespace std;
#define limit (300000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;

inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}


const ll mod = 1e9 + 7;

ll quickPow(ll base, ll expo) {
    ll ans = 1;
    while (expo) {
        if (expo & 1)(ans *= base) %= mod;
        expo >>= 1;
        base = base * base;
        base %= mod;
    }
    return ans % mod;
}

ll C(ll n, ll m) {
    if (n < m)return 0;
    ll x = 1, y = 1;
    if (m > n - m)m = n - m;
    rep(i, 0, m - 1) {
        x = x * (n - i) % mod;
        y = y * (i + 1) % mod;
    }
    return x * quickPow(y, mod - 2) % mod;
}

ll lucas(ll n, ll m) {
    return !m || n == m ? 1 : C(n % mod, m % mod) * lucas(n / mod, m / mod) % mod;
}
ll fact[limit];
void calc(){
    fact[0] = 1;
    rep(i,1,1e3)fact[i] = (fact[i - 1] * i) % mod;
}
ll A(ll x, ll y){
    return (C(x,y) * fact[y]) % mod;
}
int kase;
int n, m, k;
int a[limit];
double b[limit];
int dp[limit];
void solve() {
    cin>>n>>k;
    rep(i,1,n){
        cin>>a[i]>>b[i];
    }
    int lis = 1;
    dp[1] = a[1];
    rep(i,2,n){
        if(dp[lis] <= a[i]){
            dp[++lis] = a[i];
        }else{
            int pos = upper_bound(dp + 1, dp + 1 + lis,a[i]) - dp;
            dp[pos] = a[i];
        }
    }
    cout<<n-lis<<endl;



}

int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
//    cin >> kase;
//    while (kase--)
        solve();
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
    return 0;
}

 

posted @ 2022-01-09 20:58  tiany7  阅读(51)  评论(0编辑  收藏  举报