欢迎访问 生活随笔!

生活随笔

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

编程问答

2021HDU多校6 - 7029 Median(思维)

发布时间:2024/4/11 编程问答 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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-mmaxsummmaxmmaxmmaxmmax 去比较大小来判断。但本题加了一个小变形需要特别实现一下,就是每一组中,中位数后面的数字可以比前面的数字多一个

所以问题就转换为了 sum−mmax+cnt_midsum-mmax+cnt\_midsummmax+cnt_midmmaxmmaxmmax 的比较了,其中 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(思维)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。