E. Champions' League
In the autumn of this year, two Russian teams came into the group stage of the
most prestigious football club competition in the world — the UEFA Champions
League. Now, these teams have already started to play in the group stage and
are fighting for advancing to the playoffs. In this problem we are interested in the
draw stage, the process of sorting teams into groups.
The process of the draw goes as follows (the rules that are described in this problem,
are somehow simplified compared to the real life). Suppose n teams will take part
in the group stage (n is divisible by four). The teams should be divided into groups
of four. Let's denote the number of groups as m (). Each team has a rating
— an integer characterizing the team's previous achievements. The teams are
sorted by the rating's decreasing (no two teams have the same rating).
After that four "baskets" are formed, each of which will contain m teams: the first
m teams with the highest rating go to the first basket, the following m teams go to
the second one, and so on.
Then the following procedure repeats m - 1 times. A team is randomly taken from
each basket, first from the first basket, then from the second, then from the third,
and at last, from the fourth. The taken teams form another group. After that, they
are removed from their baskets.
The four teams remaining in the baskets after (m - 1) such procedures are performed,
form the last group.
In the real draw the random selection of teams from the basket is performed by people
— as a rule, the well-known players of the past. As we have none, we will use a random
number generator, which is constructed as follows. Its parameters are four positive
integers x, a, b, c. Every time there is a call to the random number generator, it produces
the following actions:
- calculates ;
- replaces parameter x by value y (assigns );
- returns x as another random number.
Operation means taking the remainder after division: , .
A random number generator will be used in the draw as follows: each time we need
to randomly choose a team from the basket, it will generate a random number k. The
teams that yet remain in the basket are considered numbered with consecutive
integers from 0 to s - 1, in the order of decreasing rating, where s is the current size
of the basket. Then a team number is taken from the basket.
Given a list of teams and the parameters of the random number generator, determine
the result of the draw.
Input
The first input line contains integer n (4 ≤ n ≤ 64, n is divisible by four) — the number
of teams that take part in the sorting. The second line contains four space-separated
integers x, a, b, c (1 ≤ x, a, b, c ≤ 1000) — the parameters of the random number
generator. Each of the following n lines describes one team. The description consists
of the name of the team and its rating, separated by a single space. The name of a
team consists of uppercase and lowercase English letters and has length from 1 to 20
characters. A team's rating is an integer from 0 to 1000. All teams' names are distinct.
All team's ratings are also distinct.
Output
Print the way the teams must be sorted into groups. Print the groups in the order, in
which they are formed in the sorting. Number the groups by consecutive uppercase
English letters, starting from letter 'A'. Inside each group print the teams' names one
per line, in the order of decreasing of the teams' rating. See samples for a better
understanding of the output format.
Examples
input
8 1 3 1 7 Barcelona 158 Milan 90 Spartak 46 Anderlecht 48 Celtic 32 Benfica 87 Zenit 79 Malaga 16output
Group A: Barcelona Benfica Spartak Celtic Group B: Milan Zenit Anderlecht MalagaNote
In the given sample the random number generator will be executed four times:
- ,
- ,
- ,
- .
题意:
n支队伍,n是4的倍数,现在将这N支队伍分为4组,第一组每一个的权值都大于第二组
每一个etc,每次操作从4组中各选择一支队伍组合(选择方法通过上述函数),
问最终组合的结果。
思路:
可以数组模拟,但比较麻烦。
STL操作简单一些,注意一些技巧的使用。
代码实现:(数组模拟)
#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e5+100;
const int M=4e5+100;
struct Node{
string name;
int val;
}arr[N];
bool cmp(Node aa,Node bb){
return aa.val>bb.val;
}
int cal(int x,int a,int b,int c){
return (x*a+b)%c;
}
struct result{
int id,val;
string name;
}ans[N];
int vis[N];
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n,a,b,c,x,tot;
while(cin>>n){
tot=0;
cin>>x>>a>>b>>c;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
cin>>arr[i].name>>arr[i].val;
}
sort(arr+1,arr+1+n,cmp);
for(int i=1;i<=(n-4)/4;i++){
for(int j=1;j<=4;j++){
int y=cal(x,a,b,c);
int tmp=y%(n/4-i+1);
int cnt=0;
for(int k=(j-1)*n/4+1;k<=j*(n/4);k++){
if(vis[k]==0){
cnt++;
if(cnt==tmp+1){
tmp=k;
vis[k]=1;
break;
}
}
}
ans[++tot].id=i;
ans[tot].name=arr[tmp].name;
ans[tot].val=arr[tmp].val;
x=y;
}
}
for(int i=1;i<=n;i++){
if(vis[i]==0){
ans[++tot].val=arr[i].val;
ans[tot].id=n/4;
ans[tot].name=arr[i].name;
}
}
for(int i=1;i<=n/4;i++){
cout<<"Group "<<char('A'+i-1)<<":"<<endl;
for(int j=1;j<=4;j++){
cout<<ans[(i-1)*4+j].name<<endl;
}
}
}
return 0;
}
代码实现(Vector技巧):
#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e5+100;
const int M=4e5+100;
struct Node{
string name;
int val;
}arr[N];
bool cmp(Node aa,Node bb){
return aa.val>bb.val;
}
int cal(int x,int a,int b,int c){
return (x*a+b)%c;
}
vector<int>vc[N];
vector<int>res[N];
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n,a,b,c,x;
while(cin>>n){
for(int i=0;i<n;i++){
vc[i].clear();
res[i].clear();
}
int m=n/4;
cin>>x>>a>>b>>c;
for(int i=0;i<n;i++){
cin>>arr[i].name>>arr[i].val;
}
sort(arr,arr+n,cmp);
for(int i=0;i<n;i++){
vc[i/m].push_back(i);//i/m技巧
}
for(int i=0;i<m-1;i++){
for(int j=0;j<4;j++){
int y=cal(x,a,b,c);
x=y;
int id=vc[j][y%vc[j].size()];
//erase的使用
vc[j].erase(vc[j].begin()+y%vc[j].size());
res[i].push_back(id);
}
}
for(int i=0;i<4;i++){
res[m-1].push_back(vc[i][0]);
}
for(int i=0;i<m;i++){
cout<<"Group "<<char('A'+i)<<":"<<endl;
for(int j=0;j<4;j++){
cout<<arr[res[i][j]].name<<endl;
}
}
}
return 0;
}
THE END