CodeForces - 618B Guess the Permutation(思维+构造)
题目链接:点击查看
题目大意:先给出一个长度为n的序列ai,这个序列是1~n全排列中的其中一种,再给出一个n*n的矩阵,maze[i][j]=val代表min(ai,aj)=val,要求我们构造出原始序列ai
题目分析:其实仔细观察一下给出的样例我们就可以发现:
- 若当前位置应该是1的话,那么当前的一行除了0之外就全是1了(因为1是最小的数字)
- 若当前位置应该是2的话,那么当前的一行除了0之外,有一个1,其余的都是2了
- 若当前位置应该是3的话,那么当前的一行除了0之外,有一个1,一个2,其余的都是3了
以此类推下去,我们就会发现对每一行的权值和排一下序就能得到原序列的位置了,实际上除了n和n-1所在的两行他们的数字表示的都一样之外,其余的数字的位置都是固定的
上面是一种方法,还有一种方法,我相信继续观察一下也是可以看出来的,再看一下上面的那三句话,我们就可以直接得出结论:
当前位置所代表的数,就是当前行中的最大值(除了n和n-1,因为他们两行的最大值都是n-1),这样一来我们只需要对这两行特判一下即可,然后实现就好了
代码:
方法一:权值和排序
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e3+100;struct Node
{int id,val;bool operator<(const Node& a)const{return val<a.val;}
}a[N];int ans[N];int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);int n;scanf("%d",&n);for(int i=1;i<=n;i++){a[i].id=i;for(int j=1;j<=n;j++){int num;scanf("%d",&num);a[i].val+=num;}}sort(a+1,a+1+n);for(int i=1;i<=n;i++)ans[a[i].id]=i;for(int i=1;i<=n;i++)printf("%d ",ans[i]);return 0;
}
方法二:找最大值
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e3+100;int ans[N];int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);int n;scanf("%d",&n);for(int i=1;i<=n;i++){int mmax=-1;for(int j=1;j<=n;j++){int num;scanf("%d",&num);mmax=max(mmax,num);}ans[i]=mmax;}for(int i=1;i<=n;i++)if(ans[i]==n-1){ans[i]=n;break;}for(int i=1;i<=n;i++)printf("%d ",ans[i]);return 0;
}
CodeForces - 618B Guess the Permutation(思维+构造)相关推荐
- CodeForces - 1504C Balance the Bits(思维+构造)
题目链接:https://vjudge.net/problem/CodeForces-1504C 题目大意:给出一个长度为 nnn 的 010101 串,现在要求构造出两个长度为 nnn 的合法括号序 ...
- Educational Codeforces Round 53C(二分,思维|构造)
#include<bits/stdc++.h> using namespace std; const int N=1e6+6; int x[N],y[N]; int sx,sy,n; ch ...
- CodeForces - 1450C2 Errich-Tac-Toe (Hard Version)(思维+构造)
题目链接:点击查看 题目大意:给出一个大小为 n * m 的棋盘,规定不能有三个连续的 ' X ' 或三个连续的 ' O ' ,现在可以通过一次操作将 ' X ' 改成 ' O ' 或者将 ' O ' ...
- codeforces 7.22 E Permutation Shift
codeforces 7.22 E Permutation Shift 给出一个1到n的排列,每次可以交换两个数,问在交换最多m次(m <= n/3)之后能不能得到由1 2 3 - n循环右移所 ...
- Codeforces 1077B Disturbed People(思维题)
Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...
- 【CodeForces - 483C】Diverse Permutation(思维构造)
题干: Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of ndistinct posit ...
- Codeforces Round #624 (Div. 3) E. Construct the Binary Tree 思维 + 构造
传送门 文章目录 题意: 思路: 题意: 给你n,dn,dn,d,让你构造有nnn个点的二叉树,他们每个节点深度和为ddd. n,d≤3000n,d\le 3000n,d≤3000. 思路: 先考虑不 ...
- 【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)
题干: Mike has always been thinking about the harshness of social inequality. He's so obsessed with it ...
- CodeForces - 1343F Restore the Permutation by Sorted Segments(思维)
题目链接:点击查看 题目大意:现在有一个长度为 n 的排列 p ,但排列 p 暂时对我们保密,每个样例将会给出 n - 1 个排好序后的子段,换句话说,对于 r ∈ [ 2 , n ] ,存在一个 l ...
最新文章
- 图像轮廓提取关键函数
- 高兴总结台式故障分析==方案
- 蓝桥杯练习系统习题-算法训练5
- python将姓王的都改成老王_Python老王视频习题答案
- vsftp日志查看_vsftp日志xferlog格式分析(示例代码)
- krpano使用总结-地图路线
- bios文件查看工具_“主板BIOS教程”手把手教你刷bios,多种bios教程
- Quartz的集群模式和单机模式共存-让一个非集群的Quartz与集群节点并行着运行
- 随机抽题软件C语言,ppt VBA 实现随机抽题
- 牛散村期货:3月春风生 第一周非农财经简阅
- C#使用Minidump导出内存快照Minidumper
- php music player,cloudmusic.php
- PHP开源管理系统 Gms,GMS 一个成熟的gms系统,拥有基本的权限管理和分类,后台很好 Windows Develop 249万源代码下载- www.pudn.com...
- 导论计算机论文题目,计算机类论文题目
- 《AWR Adaptive Weighting Regression for 3D Hand Pose Estimation》研读与实践
- python+pygame之飞机大战-第一版源代码分析
- python向网页上传excle、text文件
- 看完了想点赞的文章!| 数据类好文推荐
- 虚拟机之Jvm、dalvik、art联系和区别
- npm install node-sass的时候报错ERR gyp ERR C++