当前位置:网站首页>Luogu p1309 Swiss wheel
Luogu p1309 Swiss wheel
2022-07-04 01:05:00 【How many degrees does it stop raining】
Take notes again
Topic link :https://www.luogu.com.cn/problem/P1309
Title Description
2×N The name number is 1∼2N All of the contestants have R round . Before the start of each round , And after all the Games , Will be in accordance with the total score from high to low on a player ranking . The total score of a player is the initial score before the first round plus the scores of all competitions he has participated in and . The total score is the same , The player with the smaller number is in the top of the list .
The match arrangement of each round is related to the ranking before the start of the round : The first 1 Name and number 2 name 、 The first 3 Name and number 4 name 、……、 The first 2K−1 Name and number 2K name 、…… 、 The first 2N−1 Name and number 2N name , One game each . The winner of every game gets 1 branch , The loser is the winner 0 branch . That is to say, in addition to the first round , The arrangement of other rounds can't be determined in advance , It depends on the player's performance in the previous competition .
Now give each player's initial score and strength value , Try to calculate in R After the round , Ranking the first QQ What's the player number of . Let's assume that the strength values of the players are different , And in every game, the one with higher strength always wins .
sample input
2 4 2
7 6 6 7
10 5 20 15
sample output
1
explain :
** After reading this topic emm To understand it roughly is : First of all, there is 2*n A contestant , To carry out r round , We want to output the ranking after the game q People who Each player has his own strength , Initial integral , Number ( You have to use a structure ) The order of the game is to let the first play the second , Third, fourth … Yes, of course , Of course, the ranking should be updated after the change of points after each competition ( This has to be sorted every time ?) **
Then look at the data range and time limit , Glancing at the time ,0.5s Ah, there are trees ! This is a bit of a mess
It's impossible to directly simulate the fast line after every game
Think about where our direct sorting is slow , The winner of the game will add one point , Failure does not change , If someone was in front of you , You both win or both lose , Isn't he still in front of you ?
so ga! But what's the use of that ?
This seems to mean that if we put the winners in a group , Fail to put a group , Then these two groups are still in order !! In fact, what we need to do is to combine two arrays that are already ordered !
Hoarse This is the idea of merging and sorting
Let's start with the main idea
#include <bits/stdc++.h>
using namespace std;
#define N 3000000
int n,r,q;
struct player
{
int num;
int able;
int score;
} a[N],win[N],lose[N];
int main()
{
cin>>n>>r>>q;
for(int i=1; i<=2*n; i++)a[i].num=i; // Number initialization
for(int i=1; i<=2*n; i++)scanf("%d",&a[i].score);
for(int i=1; i<=2*n; i++)scanf("%d",&a[i].able); // Read in the data
sort(a+1,a+1+2*n,cmp);// Initialize the ranking before the game
while(r--)// Simulation Competition
{
for(int i=1; i<=n; i++)
{
if(a[2*i-1].able>a[2*i].able)// Compare according to strength
{
a[2*i-1].score++;
win[i]=a[2*i-1];// Winners and losers are stored separately to ensure order
lose[i]=a[2*i];
}
else// There was no draw in the game
{
a[2*i].score++;
win[i]=a[2*i];
lose[i]=a[2*i-1];
}
}
my_merge();// Handwriting merge Function to merge winners and losers
}
cout<<a[q].num<<endl;
return 0;
}
There are two functions used One cmp Comparison function , Yeah , You make people sort You have to give a sorting standard to arrange the structure
int cmp(player x,player y)// Comparison function
{
if(x.score!=y.score)return x.score>y.score;
else return x.num<y.num;// If the scores are the same, the number of small ones is in front
}
This is easy to say , Points are in front of many rows , The same is better than the number , In front of the number row
There's another one my_merge Function? , To put two arrays win,lose Synthesis of a a If this function doesn't work well, Barbie Q 了 , I have this card wa Half an hour , card tle I didn't know what happened to the kidney until I read other people's solutions for half an hour
Synthesis , Simple , acutely Go straight up
void my_merge()
{
int k=0,i,j;
i=j=1;
while(k<2*n)
{
if(win[i].score>lose[j].score)
{
a[++k]=win[i];
i++;
}
else if(win[i].score<lose[j].score)
{
a[++k]=lose[j];
j++;
}
else// Compare numbers with the same scores
{
if(win[i].num<lose[j].num)
{
a[++k]=win[i];
i++;
}
else// The contestant number is unique
{
a[++k]=lose[j];
j++;
}
}
}
}
The cyclic condition is a The array is full Then according to the comparison standard from win and lose Choose from the inside , Submit !!
Listen to wa A sound. Is dubious
In this way, we ignore a problem In comparison win[i] lose[j] It doesn't have to be , You put it one by one In case win Everything inside has gone in i still +, Then no md fuck*running time error 了 ( However, I'm used to the fact that if the array is opened a little larger, it won't cross the boundary ) The answer is wrong
well Change !
void my_merge()
{
int k=0,i,j;
i=j=1;
while(k<2*n)
{
while(cmp(win[i],lose[j])&&i<=n)
{
a[++k]=win[i];
i++;
}
while(cmp(lose[j],win[i])&&j<=n)
{
a[++k]=lose[j];
j++;
}
}
}
Hey I changed it directly to while loop i If it is greater than n You can't get in That's not only j 了 Tact as I It's too early to be happy
This is a special change tle ah Or five points It's better not to change ! Don't be impatient. Don't be impatient What 0.5s Really?
Try fast reading ?
inline int qread(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
But it is of no damn use .. I want to know The amount of data read into this topic is not large , Adding a quick read doesn't work at all Of course, if the data is big, fast reading is still very delicious Then I ran to see others' solutions
After modification
void my_merge()
{
int k=0,i,j;
i=j=1;
while(k<2*n)
{
if(i>n)
{
while(j<=n)
{
a[++k]=lose[j];
j++;
}
break;
}
if(j>n)
{
while(i<=n)
{
a[++k]=win[i];
i++;
}
break;
}
if(win[i].score>lose[j].score)
{
a[++k]=win[i];
i++;
}
else if(win[i].score<lose[j].score)
{
a[++k]=lose[j];
j++;
}
else// Compare numbers with the same scores
{
if(win[i].num<lose[j].num)
{
a[++k]=win[i];
i++;
}
else// The contestant number is unique
{
a[++k]=lose[j];
j++;
}
}
}
}
In fact, it's just adding if Special judgement But I didn't figure out why I was so slow
Complete code ( There is also a debugging code )
#include <bits/stdc++.h>
using namespace std;
#define N 3000000
int n,r,q;
struct player
{
int num;
int able;
int score;
} a[N],win[N],lose[N];
int cmp(player x,player y)// Comparison function
{
if(x.score!=y.score)return x.score>y.score;
else return x.num<y.num;// If the scores are the same, the number of small ones is in front
}
void my_merge()
{
int k=0,i,j;
i=j=1;
while(k<2*n)
{
if(i>n)
{
while(j<=n)
{
a[++k]=lose[j];
j++;
}
break;
}
if(j>n)
{
while(i<=n)
{
a[++k]=win[i];
i++;
}
break;
}
if(win[i].score>lose[j].score)
{
a[++k]=win[i];
i++;
}
else if(win[i].score<lose[j].score)
{
a[++k]=lose[j];
j++;
}
else// Compare numbers with the same scores
{
if(win[i].num<lose[j].num)
{
a[++k]=win[i];
i++;
}
else// The contestant number is unique
{
a[++k]=lose[j];
j++;
}
}
}
}
/*void debug() { printf(" At present, the points of each player are ranked as follows :\n Number integral power \n"); for(int i=1; i<=2*n; i++) { printf("%d %d %d\n",a[i].num,a[i].score,a[i].able); } }*/
int main()
{
cin>>n>>r>>q;
for(int i=1; i<=2*n; i++)a[i].num=i; // Number initialization
for(int i=1; i<=2*n; i++)scanf("%d",&a[i].score);
for(int i=1; i<=2*n; i++)scanf("%d",&a[i].able); // Read in the data
sort(a+1,a+1+2*n,cmp);// Initialize the ranking before the game
while(r--)// Simulation Competition
{
for(int i=1; i<=n; i++)
{
if(a[2*i-1].able>a[2*i].able)// Compare according to strength
{
a[2*i-1].score++;
win[i]=a[2*i-1];// Winners and losers are stored separately to ensure order
lose[i]=a[2*i];
}
else// There was no draw in the game
{
a[2*i].score++;
win[i]=a[2*i];
lose[i]=a[2*i-1];
}
}
my_merge();// Handwriting merge Function to merge winners and losers
}
cout<<a[q].num<<endl;
return 0;
}
Let out a sigh
The notes are written here
I wrote Luo Gu and roast by the way codefore This website The question is really difficult Slow reaction , You have to jump in a daze everywhere
边栏推荐
- It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
- 功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
- How to be a professional software testing engineer? Listen to the byte five year old test
- Function: write function fun to find s=1^k+2^k +3^k ++ The value of n^k, (the cumulative sum of the K power of 1 to the K power of n).
- PMP 考试常见工具与技术点总结
- Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing
- Leetcode 121 best time to buy and sell stock (simple)
- 数据库表外键的设计
- 长文综述:大脑中的熵、自由能、对称性和动力学
- In the process of seeking human intelligent AI, meta bet on self supervised learning
猜你喜欢
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
The FISCO bcos console calls the contract and reports an error does not exist
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
[dynamic programming] leetcode 53: maximum subarray sum
机器学习基础:用 Lasso 做特征选择
功能:求5行5列矩阵的主、副对角线上元素之和。注意, 两条对角线相交的元素只加一次。例如:主函数中给出的矩阵的两条对角线的和为45。
What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
1-Redis架构设计到使用场景-四种部署运行模式(上)
随机推荐
Future source code view -juc series
技術實踐|線上故障分析及解决方法(上)
GUI 应用:socket 网络聊天室
The difference between objects and objects
老姜的特点
All in one 1407: stupid monkey
Introduction to unity shader essentials reading notes Chapter III unity shader Foundation
The first training of wechat applet
What is the GPM scheduler for go?
In the process of seeking human intelligent AI, meta bet on self supervised learning
求esp32C3板子连接mssql方法
Future源码一观-JUC系列
A malware detection method for checking PLC system using satisfiability modulus theoretical model
MPLS experiment
Function: find the approximate value of the limit of the ratio of the former term to the latter term of Fibonacci sequence. For example, when the error is 0.0001, the function value is 0.618056.
Network layer - routing
Why use get/set instead of exposing properties
Msp32c3 board connection MSSQL method
Struct in linked list
Pratique technique | analyse et solution des défaillances en ligne (Partie 1)