题目描述
九条可怜很喜欢 B 站的网综故事王,为了硬核安利这一档节目,可怜决定出一道相关的题目。
在一场比赛中,有 n 名选手 m 名评委。目前比赛已经进入到了最后一轮“结局轮”,当前第 i 位选手的分数是 ai,在结局轮评委一共能给出 n 种可能的分数 bi(保证 bi 两两不同)。
在结局轮的作答结束后,每一名评委需要对每一个位选手给出一个分数,但是同一个评委每一种分数只能给出恰好一次(即重新排列 bi 分别打给每一位选手)。选手的最终得分为 ai 加上最后一轮所有评委给他的分数。最后的排名由分数和选手编号决定:分数高的选手排名靠前,如果分数相同,编号小的选手排名靠前。
不难发现每一个评委的给分有 n! 种可能性,那么最后一轮所有的给分自然有 种可能性。现在你需要对每一对 i,j (1 ≤ i,j ≤ n),计算能让第 i 个选手最后得到第 j 名的给分方案有多少种。输入描述:
第一行输入两个整数 n,m,第二行输入 n 个整数 ai,第三行输入 n 个整数 bi。 的数据,n = 2,m = 1. 的数据,m = 1. 的数据,1 ≤ n ≤ 4,1 ≤ m ≤ 5, ai,bi ≤ 109,保证 bi 两两不同。
输出描述:
输出 n 行每行 n 个整数,第 i 行第 j 个整数的值表示让第 i 个选手获得第 j 名的方案数。
示例1
输入
2 1 1 2 2 1
输出
1 1 1 1
示例2
输入
4 4 1 2 3 4 4 2 3 1
输出
40502 65820 93702 131752 64602 80436 91818 94920 94920 91818 80436 64602 131752 93702 65820 40502
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define LL long long LL zz[10][10],pw[10],n,m,book[10][10]; struct th { LL num,fen; }; th a[10]; LL cmp(th x,th y) { if(x.fen==y.fen) return x.num<y.num;//小于号说明从小到大 return x.fen>y.fen; } void func(th *x) { sort(x+1,x+n+1,cmp); for(LL i=1;i<=n;i++) zz[x[i].num][i]++; } void dfs(LL man,LL k) { if(man==n+1) { dfs(1,k+1); return; } if(k==m+1) { th b[10]; for(LL i=1;i<=n;i++) b[i]=a[i]; func(b); } else { for(LL i=1;i<=n;i++) { if(book[k][i]==0) { book[k][i]=1; a[man].fen+=pw[i]; dfs(man+1,k); book[k][i]=0; a[man].fen-=pw[i]; } } } } int main() { scanf("%lld %lld",&n,&m); for(LL i=1;i<=n;i++) { scanf("%lld",&a[i].fen); a[i].num=i; } for(LL i=1;i<=n;i++) scanf("%lld",&pw[i]); dfs(1,1); for(LL i=1;i<=n;i++) { for(LL j=1;j<=n;j++) { printf("%lld ",zz[i][j]); } printf("\n"); } return 0; }
全排列,next_permutation(p,p+n);当后面没有时,返回0;否则返回1;
sort中的cmp(compare)的详解
struct student {
int grade;
char name[101];
int age;
}stu[1001];
bool cmp(student a,student b)
{ int t = strcmp(a.name,b.name);
if(a.grade != b.grade)
return a.grade < b.grade;
else if(t != 0)
return t < 0;
else return a.age < b.age;
}
sort(stu,stu+n,cmp);
注意概念:
注意这样可以输出正确答案;
#include "iostream"
#include "stdlib.h"
using namespace std;
int* shishi()
{
int *a=(int *)malloc(sizeof(int)*10);//在堆区,除非手动释放内存,否则不会释放
for(int i=0;i<=9;i++)
{
a[i]=i;
}
return a;
}
int main()
{
int *a;
a=shishi();
for(int i=0;i<=9;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
这样会输出错误答案!!!
#include "iostream"
using namespace std;
int* shishi()
{
int a[10];
for(int i=0;i<=9;i++)
{
a[i]=i;
}
return a;
}
int main()
{
int *a;
a=shishi();
for(int i=0;i<=9;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
#include "bits/stdc++.h"
using namespace std;
typedef struct LNode
{
int a;
LNode *next;
}*LinkList;
int main()
{
LinkList head=creat();
while(head!=NULL)//循环终止时head==NULL的这个状态没有走过
{
cout<<head->a<<' ';
head=head->next;
}
head=head->next;//空指向next这种写法的赋值不仅有问题,
//而且不报错,一直不动
return 0;
}