日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

G.Eating Together(LIS,LDS)

發布時間:2024/9/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 G.Eating Together(LIS,LDS) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

牛客假日團隊賽7:
鏈接:https://ac.nowcoder.com/acm/contest/997/G
來源:牛客網

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
The cows are so very silly about their dinner partners. They have organized themselves into three groups (conveniently numbered 1, 2, and 3) that insist upon dining together. The trouble starts when they line up at the barn to enter the feeding area.
Each cow i carries with her a small card upon which is engraved Di (1 ≤ Di ≤ 3) indicating her dining group membership. The entire set of N (1 ≤ N ≤ 30,000) cows has lined up for dinner but it’s easy for anyone to see that they are not grouped by their dinner-partner cards.
FJ’s job is not so difficult. He just walks down the line of cows changing their dinner partner assignment by marking out the old number and writing in a new one. By doing so, he creates groups of cows like 111222333 or 333222111 where the cows’ dining groups are sorted in either ascending or descending order by their dinner cards.
FJ is just as lazy as the next fellow. He’s curious: what is the absolute mminimum number of cards he must change to create a proper grouping of dining partners? He must only change card numbers and must not rearrange the cows standing in line.
輸入描述:

  • Line 1: A single integer: N
  • Lines 2…N+1: Line i describes the i-th cow’s current dining group with a single integer: Di
    輸出描述:
  • Line 1: A single integer representing the minimum number of changes that must be made so that the final sequence of cows is sorted in either ascending or descending order
    示例1
    輸入
    復制
513211

輸出
復制

1

說明
We would need at least two changes to turn this into an increasing sequence (changing both non-1’s to a 1).
However, changing the first “1” to a “3” yields a decreasing sequence with just one change, which is optimal.
題意:
一個只由1,2,3組成的序列,改變最少的數使序列成為上升或下降序列。
很明顯就是求一個最長上升子序列和最長下降子序列,需要修改的數量ans = N -兩個子序列中長度的較大值。
N達到3e4,所以用基于貪心的O(NlogN)解法
AC_code:

#include <bits/stdc++.h> using namespace std; const int N = 3e4+4; int a[N]; int st[N]; int main() {int n;cin>>n;int t = 0;st[t] = 0;for(int i = 1; i <= n; i++){cin>>a[i];if(a[i] >= st[t]){st[++t] = a[i];}else{int l = 1,r = t;int res = 0;while(l <= r){int mid = (l+r)>>1;if(st[mid] > a[i]){res = mid;r = mid - 1;}else l = mid+1;}st[res] = a[i];}}int maxx = t;t = 0;st[t] = 10;for(int i = 1; i <= n; i++){if(a[i] <= st[t]){st[++t] = a[i];}else{int l = 1,r = t;int res = 0;while(l <= r){int mid = (l+r)>>1;if(st[mid] < a[i]){res = mid;r = mid - 1;}else l = mid+1;}st[res] = a[i];}}maxx = max(maxx,t);cout<<(n-maxx)<<endl;return 0; }

總結

以上是生活随笔為你收集整理的G.Eating Together(LIS,LDS)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。