2021HDU多校6 - 7029 Median(思维)
生活随笔
收集整理的這篇文章主要介紹了
2021HDU多校6 - 7029 Median(思维)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:初始時給出 nnn 個數字 1,2,3,...,n{1,2,3,...,n}1,2,3,...,n,再給出 mmm 個中位數,問是否能將 nnn 個數字分到 mmm 個組中,使得每一組的中位數分別為 bib_ibi?
題目分析:分襪子、分鮮花問題的變形:2019ICPC(沈陽) - Flowers
不難發現 mmm 個中位數將 nnn 個數字劃分為了 m+1m+1m+1 段,而任意兩段的數字都是可以相互抵消的,所以轉換模型為:給出 m+1m+1m+1 種顏色不同的鮮花,每種鮮花有 aia_iai? 個,任意兩個不同種類的鮮花都可以相互配對,問所有鮮花是否都能完成配對
很容易想到用 sum?mmaxsum-mmaxsum?mmax 和 mmaxmmaxmmax 去比較大小來判斷。但本題加了一個小變形需要特別實現一下,就是每一組中,中位數后面的數字可以比前面的數字多一個
所以問題就轉換為了 sum?mmax+cnt_midsum-mmax+cnt\_midsum?mmax+cnt_mid 和 mmaxmmaxmmax 的比較了,其中 cnt_midcnt\_midcnt_mid 指的是,區間長度最大的那個區間,前面有多少個中位數。因為最大值的這個區間可以對前面的每個中位數分配一個數字
代碼:
// Problem: Median // Contest: Virtual Judge - HDU // URL: https://vjudge.net/problem/HDU-7029 // Memory Limit: 524 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; bool vis[N]; int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {int n,m;read(n),read(m);memset(vis,false,n+5);for(int i=1,x;i<=m;i++) {read(x);vis[x]=true;}vis[n+1]=true;int cnt=0,cnt_mid=0,mmax=0,mmax_mid=0;for(int i=1;i<=n+1;i++) {if(vis[i]) {if(cnt>=mmax) {mmax=cnt;mmax_mid=cnt_mid;}cnt=0;cnt_mid++;} else {cnt++;}}if(mmax<=n-m-mmax+mmax_mid) {puts("YES");} else {puts("NO");}}return 0; }總結
以上是生活随笔為你收集整理的2021HDU多校6 - 7029 Median(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019ICPC(沈阳) - Flowe
- 下一篇: 2019ICPC(上海) - Spann