程序思维设计 week-6 模拟
A - 掌握魔法の东东 II
从瑞神家打牌回来后,东东痛定思痛,决定苦练牌技,终成赌神!
东东有 A × B 张扑克牌。每张扑克牌有一个大小(整数,记为a,范围区间是 0 到 A - 1)和一个花色(整数,记为b,范围区间是 0 到 B - 1。
扑克牌是互异的,也就是独一无二的,也就是说没有两张牌大小和花色都相同。
“一手牌”的意思是你手里有5张不同的牌,这 5 张牌没有谁在前谁在后的顺序之分,它们可以形成一个牌型。 我们定义了 9 种牌型,如下是 9 种牌型的规则,我们用“低序号优先”来匹配牌型,即这“一手牌”从上到下满足的第一个牌型规则就是它的“牌型编号”(一个整数,属于1到9):
- 同花顺: 同时满足规则 2 和规则 3.
- 顺子 : 5张牌的大小形如 x, x + 1, x + 2, x + 3, x + 4
- 同花 : 5张牌都是相同花色的.
- 炸弹 : 5张牌其中有4张牌的大小相等.
- 三带二 : 5张牌其中有3张牌的大小相等,且另外2张牌的大小也相等.
- 两对: 5张牌其中有2张牌的大小相等,且另外3张牌中2张牌的大小相等.
- 三条: 5张牌其中有3张牌的大小相等.
- 一对: 5张牌其中有2张牌的大小相等.
- 要不起: 这手牌不满足上述的牌型中任意一个.
现在, 东东从A × B 张扑克牌中拿走了 2 张牌!分别是 (a1, b1) 和 (a2, b2). (其中a表示大小,b表示花色)
现在要从剩下的扑克牌中再随机拿出 3 张!组成一手牌!!
其实东东除了会打代码,他业余还是一个魔法师,现在他要预言他的未来的可能性,即他将拿到的“一 手牌”的可能性,我们用一个“牌型编号(一个整数,属于1到9)”来表示这手牌的牌型,那么他的未来 有 9 种可能,但每种可能的方案数不一样。
现在,东东的阿戈摩托之眼没了,你需要帮他算一算 9 种牌型中,每种牌型的方案数。
题意:瑞神要打牌,从一副牌里抽了已知的两张,然后他想知道,再抽三张牌之后他手中五张牌的各情况可能性。
Input
第 1 行包含了整数 A 和 B (5 ≤ A ≤ 25, 1 ≤ B ≤ 4).
第 2 行包含了整数 a1, b1, a2, b2 (0 ≤ a1, a2 ≤ A - 1, 0 ≤ b1, b2 ≤ B - 1, (a1, b1) ≠ (a2, b2)).
Output
输出一行,这行有 9 个整数,每个整数代表了 9 种牌型的方案数(按牌型编号从小到大的顺序)
Examples:
Input
5 2
1 0 3 1
Output
0 8 0 0 0 12 0 36 0
Input
25 4
0 0 24 3
Output
0 0 0 2 18 1656 644 36432 113344
解题思路:
暴力枚举,然后使用一个check函数,验证每五张牌的情况,用无数个if去判断,注意生成牌的时候要去掉已经有的牌。
#include<cstdio>
#include<algorithm>
using namespace std;
int ans[9]={0};
bool cmp(pair<int,int > x, pair<int,int > y) {
if(x.first == y.first)
return x.second < y.second;
return x.first < y.first;
}
void choose(pair<int,int> *temp){
int flag1=0,flag2=0,flag3=0;
if(temp[0].second==temp[1].second&&temp[1].second==temp[2].second&&temp[2].second==temp[3].second&&temp[3].second==temp[4].second)
flag1=1; //same hua
if(temp[0].first==temp[1].first-1&&temp[1].first==temp[2].first-1&&temp[2].first==temp[3].first-1&&temp[3].first==temp[4].first-1)
flag2=1; //顺子
if(flag1==1&&flag2==1){ans[0]++; return; }//同花顺
else{
if(flag1==1&&flag2==0){ans[2]++; return; }
if(flag1==0&&flag2==1){ans[1]++; return; }
}
if((temp[0].first==temp[1].first&&temp[1].first==temp[2].first&&temp[2].first==temp[3].first)||
(temp[1].first==temp[2].first&&temp[2].first==temp[3].first&&temp[3].first==temp[4].first)){
ans[3]++; return;//炸弹
}
if((temp[0].first==temp[1].first&&temp[2].first==temp[3].first&&temp[3].first==temp[4].first)||
(temp[0].first==temp[1].first&&temp[1].first==temp[2].first&&temp[3].first==temp[4].first)){
ans[4]++; return;
}
if((temp[0].first==temp[1].first&&temp[2].first==temp[3].first)||(temp[0].first==temp[1].first&&
temp[3].first==temp[4].first)||(temp[1].first==temp[2].first&&temp[3].first==temp[4].first)){
ans[5]++; return;//两对
}
if((temp[0].first==temp[1].first&&temp[1].first==temp[2].first)||(temp[1].first==temp[2].first&&
temp[2].first==temp[3].first)||(temp[2].first==temp[3].first&&temp[3].first==temp[4].first)){
ans[6]++; return;//三条
}
if((temp[0].first==temp[1].first)||(temp[1].first==temp[2].first)||(temp[2].first==temp[3].first)||(temp[3].first==temp[4].first)){
ans[7]++; return;//一对
}
ans[8]++; return;//要不起
}
void check(int m, int n,int a,int b,int c ,int d){
int all=m*n-2;
int count=0;
pair<int,int> p1,p2;
p1.first=a;
p1.second=b;
p2.first=c;
p2.second=d;
pair<int,int > p[all];
for(int i=0;i<m;i++)
for(int k=0;k<n;k++){
if(i==a&&k==b) continue;
if(i==c&&k==d) continue;
p[count].first=i;
p[count].second=k;
count++;
}
for(int i=0;i<count;i++)
for(int k=i+1;k<count;k++)
for(int j=k+1;j<count;j++){
pair<int,int> temp[5];
temp[0]=p1;
temp[1]=p2;
temp[2]=p[i];
temp[3]=p[k];
temp[4]=p[j];
sort(temp,temp+5,cmp);
//
// for(int i=0;i<5;i++){
// cout<<"//--"<<temp[i].first<<" "<<temp[i].second<<endl;
//
// }
// cout<<endl;
choose(temp);
}
for(int i=0;i<8;i++){
printf("%d ",ans[i]);
}
printf("%d\n",ans[8]);
}
int main() {
int m,n,a,b,c,d;
scanf("%d %d %d %d %d %d",&m,&n,&a,&b,&c,&d);
check(m,n,a,b,c,d);
return 0;
}
注意点:
- 排序十分关键,重新cmp的时候注意顺序,得排序first,再排序second;
- 注意循环条件,i=k++;
- 判断同花顺的时候得注意;
week 6 A - 氪金带东
实验室里原先有一台电脑(编号为1),最近氪金带师咕咕东又为实验室购置了N-1台电脑,编号为2到N。每台电脑都用网线连接到一台先前安装的电脑上。但是咕咕东担心网速太慢,他希望知道第i台电脑到其他电脑的最大网线长度,但是可怜的咕咕东在不久前刚刚遭受了宇宙射线的降智打击,请你帮帮他。
提示: 样例输入对应这个图,从这个图中你可以看出,距离1号电脑最远的电脑是4号电脑,他们之间的距离是3。 4号电脑与5号电脑都是距离2号电脑最远的点,故其答案是2。5号电脑距离3号电脑最远,故对于3号电脑来说它的答案是3。同样的我们可以计算出4号电脑和5号电脑的答案是4.
Input
输入文件包含多组测试数据。对于每组测试数据,第一行一个整数N (N<=10000),接下来有N-1行,每一行两个数,对于第i行的两个数,它们表示与i号电脑连接的电脑编号以及它们之间网线的长度。网线的总长度不会超过10^9,每个数之间用一个空格隔开。
Output
对于每组测试数据输出N行,第i行表示i号电脑的答案 (1<=i<=N).
Sample Input
5
1 1
2 1
3 1
1 1
Sample Output
3
2
3
4
4
题解
树形dp
由于对于一个节点来说,可能得到的距离最大的值的路径来自他的子树,或者从他的父节点过来,所以用两次DFS。第一次DFS求出所有节点在他的子树范围内到叶子节点距离的最大值和第二大的值,第二次DFS更新从父节点过来的情况就可以了。因为如果只存最大值的话,判断一个点的从父节点过来的最大值,那么如果他的父节点存的最大值正好是从该点过来的,那么就失去了从父节点过来的状态,所以要记录最大的两个值。
代码
#include <iostream>
#include <vector>
using namespace std;
const int ma=10005;
vector < pair<int,int> > edge[ma];
int maxn[ma];
int smax[ma];
int m[ma];
int s[ma];
void DFS1(int t,int p)
{
int i,k;
maxn[t]=0;
smax[t]=0;
for (i=0;i<edge[t].size();i++)
{
k=edge[t][i].second;
if (k==p) continue;
DFS1(k,t);
if (maxn[t]<maxn[k]+edge[t][i].first)
{
smax[t]=maxn[t];
s[t]=m[t];
maxn[t]=maxn[k]+edge[t][i].first;
m[t]=k;
}
else if (maxn[k]+edge[t][i].first>smax[t])
{
smax[t]=maxn[k]+edge[t][i].first;
s[t]=k;
}
}
}
void DFS2(int t,int p,int length)
{
int i,k;
if (t!=1)
{
if (m[p]==t)
{
if (maxn[t]<smax[p]+length)
{
smax[t]=maxn[t];
s[t]=m[t];
maxn[t]=smax[p]+length;
m[t]=p;
}
else if (smax[t]<smax[p]+length)
{
smax[t]=smax[p]+length;
s[t]=p;
}
}
else
{
if (maxn[t]<maxn[p]+length)
{
smax[t]=maxn[t];
s[t]=m[t];
maxn[t]=maxn[p]+length;
m[t]=p;
}
else if (smax[t]<maxn[p]+length)
{
smax[t]=maxn[p]+length;
m[t]=p;
}
}
}
for (i=0;i<edge[t].size();i++)
{
k=edge[t][i].second;
if (k==p) continue;
DFS2(k,t,edge[t][i].first);
}
}
int main()
{
int i,j,n,x,y;
pair <int,int> temp;
while(scanf("%d",&n)!=EOF)
{
for (i=0;i<=n;i++)
{
edge[i].clear();
}
for (i=2;i<=n;i++)
{
scanf("%d%d",&x,&y);
temp.first=y;
temp.second=i;
edge[x].push_back(temp);
temp.second=x;
edge[i].push_back(temp);
}
DFS1(1,-1);
DFS2(1,-1,0);
for (i=1;i<=n;i++)
{
printf("%d\n",max(maxn[i],smax[i]));
}
}
return 0;
}
B-戴好口罩
新型冠状病毒肺炎(Corona Virus Disease 2019,COVID-19),简称“新冠肺炎”,是指2019新型冠状病毒感染导致的肺炎。
如果一个感染者走入一个群体,那么这个群体需要被隔离!
小A同学被确诊为新冠感染,并且没有戴口罩!!!!!!
危!!!
时间紧迫!!!!
需要尽快找到所有和小A同学直接或者间接接触过的同学,将他们隔离,防止更大范围的扩散。
众所周知,学生的交际可能是分小团体的,一位学生可能同时参与多个小团体内。
请你编写程序解决!戴口罩!!
Input
多组数据,对于每组测试数据:
第一行为两个整数n和m(n = m = 0表示输入结束,不需要处理),n是学生的数量,m是学生群体的数量。0 < n <= 3e4 , 0 <= m <= 5e2
学生编号为0~n-1
小A编号为0
随后,m行,每行有一个整数num即小团体人员数量。随后有num个整数代表这个小团体的学生。
Output
输出要隔离的人数,每组数据的答案输出占一行
Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
题解:
一个并查集解决问题,注意压缩路径,还有数组开大亿点点,不然会超时,temp里存放结果
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int ma=100005;
int mark[ma];
int temp[ma];
int find(int a)
{
if (a!=mark[a])
mark[a]=find(mark[a]);
return mark[a];
}
void unit(int p,int q)
{
p=find(p);
q=find(q);
if (p==q)return ;
else
{
mark[p]=q;
temp[q]+=temp[p];
}
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
if (m==0&&n==0)break;
int j,k,l,sum=0;
for (int i=0;i<n;i++)
{
mark[i]=i;
temp[i]=1;
}
for (int i=0;i<m;i++)
{
scanf("%d",&j);
scanf("%d",&k);
for (int i=0;i<j-1;i++)
{
scanf("%d",&l);
unit(k,l);
}
}
printf("%d\n",temp[find(0)]);
}
return 0;
}
C - 掌握魔法の东东 I
东东在老家农村无聊,想种田。农田有 n 块,编号从 1~n。种田要灌氵
众所周知东东是一个魔法师,他可以消耗一定的 MP 在一块田上施展魔法,使得黄河之水天上来。他也可以消耗一定的 MP 在两块田的渠上建立传送门,使得这块田引用那块有水的田的水。 (1<=n<=3e2)
黄河之水天上来的消耗是 Wi,i 是农田编号 (1<=Wi<=1e5)
建立传送门的消耗是 Pij,i、j 是农田编号 (1<= Pij <=1e5, Pij = Pji, Pii =0)
东东为所有的田灌氵的最小消耗
Input
第1行:一个数n
第2行到第n+1行:数wi
第n+2行到第2n+1行:矩阵即pij矩阵
Output
东东最小消耗的MP值
Example
Input
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
Output
9
题解:
给农田浇水,无论是单独浇一块还是从另一块引水同时浇两块地都需要消耗一定的查克拉
现在要求消耗最少的查克拉浇完所有的氵
思路:将单独浇水看作是 从0号地引水,变成了一个n+1条边的无向图,在录入边的时候只需要录左下角一半,使用克里斯算法,将所有的边从小到大排序(相当于压入小根堆),依次用并查集检查是否加入即可
#include<iostream>
#include<algorithm>
using namespace std;
int mark[3000];
int find(int p)
{
if (p!=mark[p])
mark[p]=find(mark[p]);
return mark[p];
}
int unit(int p,int q)
{
p=find(p);
q=find(q);
if (p==q)
return 0;
else
{
mark[p]=q;
return 1;
}
}
struct edge
{
int u,v,w;
bool operator<(edge& j)
{
return w<j.w;
}
} eds[1000000];
int main()
{
int n,ans=0,temp;
scanf("%d",&n);
int m=n+1;
for(int i=0;i<=n;i++)
mark[i]=i;
for(int i=1;i<=n;i++)
{
eds[i].u=0;
eds[i].v=i;
scanf("%d",&eds[i].w);
}
for(int k=1;k<=n;k++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&temp);
if(j<=k) continue;
eds[m].u=k;
eds[m].v=j;
eds[m].w=temp;
m++;
}
}
sort(eds+1,eds+m);
for(int i=1;i<m;i++)
{
if(unit(eds[i].u,eds[i].v))
ans=ans+eds[i].w;
}
printf("%d",ans);
return 0;
}
注意点:
- 把大数组放外面,并且要开大一点点
- 0号就是天上水
- 只要录一半矩阵,不然会重复
- 要相信,他是连通图最后就一定会都连起来,不会漏
D-数据中心
Example
Input
4
5
1
1 2 3
1 3 4
1 4 5
2 3 8
3 4 2
Output
4
题解:
实际上就是找到最小生成树里面最大的边就好,和上一题类似,用并查集和克里斯可算法,只需要改一下算法,只保留最后一条加入的边,一定是最大的
#include<iostream>
#include<algorithm>
using namespace std;
int mark[50000];
int find(int p)
{
if (p!=mark[p])
mark[p]=find(mark[p]);
return mark[p];
}
int unit(int p,int q)
{
p=find(p);
q=find(q);
if (p==q)
return 0;
else
{
mark[p]=q;
return 1;
}
}
struct edge
{
int u,v,w;
bool operator<(edge& j)
{
return w<j.w;
}
} eds[10000000];
int main()
{
int n,m,r,ans=0;
scanf("%d %d %d",&n,&m,&r);
for(int i=0;i<=n;i++)
mark[i]=i;
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&eds[i].u,&eds[i].v,&eds[i].w);
}
sort(eds,eds+m);
for(int i=0;i<m;i++)
{
if(unit(eds[i].u,eds[i].v))
ans=eds[i].w;
}
printf("%d",ans);
return 0;
}
注意:
- 数据量比较大
- 压缩路径
mark[p]=find(mark[p]);
return mark[p];
}
int unit(int p,int q)
{
p=find§;
q=find(q);
if (p==q)
return 0;
else
{
mark[p]=q;
return 1;
}
}
struct edge
{
int u,v,w;
bool operator<(edge& j)
{
return w<j.w;
}
} eds[10000000];
int main()
{
int n,m,r,ans=0;
scanf("%d %d %d",&n,&m,&r);
for(int i=0;i<=n;i++)
mark[i]=i;
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&eds[i].u,&eds[i].v,&eds[i].w);
}
sort(eds,eds+m);
for(int i=0;i<m;i++)
{
if(unit(eds[i].u,eds[i].v))
ans=eds[i].w;
}
printf("%d",ans);
return 0;
}
注意:
1. 数据量比较大
2. 压缩路径
3. 注意边的总条数