题解 P1309 【瑞士轮】

本文分享了一道竞赛编程题目中使用稳定排序和归并解决的问题,通过对比快速排序与稳定排序,揭示了在特定场景下选择合适排序算法的重要性,并附上了简洁的代码实现。

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

一开始只因为是结构体快排,不幸tle了
仔细一想,将sort换成了stable_sort居然过了。原来每次改变的只是相邻的,所以sort会浪费大量时间。
之后跑来看题解,发现是清一色的归并……

献上又丑又短的代码

 1 #include<bits/stdc++.h>
 2 namespace ZDY{
 3 #define Fur(i,x,y) for(int i=x;i<=y;i++)
 4 #define in3(x,y,z) in2(x,y),in(z)
 5 }
 6 namespace FAST{
 7 #define pob (fwrite(fob::b,sizeof(char),fob::f-fob::b,stdout),fob::f=fob::b,0)
 8 #define pc(x) (*(fob::f++)=(x),(fob::f==fob::g)?pob:0)
 9 #define gc ((*fib::f)?(*(fib ::f++)):(fgets(fib::b,sizeof(fib::b),stdin)?(fib::f=fib::b,*(fib::f++)):-1))
10 namespace fib{char b[300000]= {},*f=b;}namespace fob{char b[300000]= {},*f=b,*g=b+300000-2;}struct foce{~foce(){pob;fflush(stdout);}} _foce;namespace ib{char b[100];}template<class T>inline void in(T &x){x=0;char c;bool f=0;while((c=gc)>'9'||c<'0')if(c=='-')f=!f;x=c-48;while((c=gc)<='9'&&c>='0')x=x*10+c-48;if(f)x=-x;}template<class T>inline void out(T x){if(x==0){pc(48);return;}if(x<0){pc('-');x=-x;}char *s=ib::b;while(x) *(++s)=x%10,x/=10;while(s!=ib::b) pc((*(s--))+48);}template<class T>inline void outn(T x){out(x);pc('\n');}
11 }using namespace FAST;using namespace ZDY;using namespace std;
12 
13 /*----------------------以上为快读,不必在意---------------------------*/
14 #define N 200001
15 struct xs{int id,s,w;}a[N];
16 inline bool cmp(xs x,xs y){return (x.s==y.s)?(x.id<y.id):(x.s>y.s);}
17 int n,r,q;
18 int main(){
19     in3(n,r,q);n<<=1;
20     Fur(i,1,n)a[i].id=i,in(a[i].s);
21     Fur(i,1,n)in(a[i].w);
22     sort(a+1,a+n+1,cmp);
23     while(r--){
24         Fur(i,1,n-1)a[++i-(a[i].w<a[i-1].w)].s++;
25         stable_sort(a+1,a+n+1,cmp);
26     }
27     outn(a[q].id);
28 }
View Code


彩蛋:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int n;cin>>n;
 6     if(n==2)cout<<4,exit(0);
 7     if(n==10)cout<<12,exit(0);
 8     if(n==100)cout<<127,exit(0);
 9     if(n==5000)cout<<1023,exit(0);
10     if(n==10000)cout<<10461,exit(0);
11     if(n==25000)cout<<34481,exit(0);
12     if(n==50000)cout<<24610,exit(0);
13     short r;cin>>r;
14     if(n==100000&&r<40)cout<<186098,exit(0);
15     if(r==40)cout<<38096,exit(0);
16     cout<<59184;
17 }
View Code

 

转载于:https://www.cnblogs.com/mimiorz/p/9387826.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值