题目链接:点击查看

题目大意:先给出一个长度为n的序列ai,这个序列是1~n全排列中的其中一种,再给出一个n*n的矩阵,maze[i][j]=val代表min(ai,aj)=val,要求我们构造出原始序列ai

题目分析:其实仔细观察一下给出的样例我们就可以发现:

  1. 若当前位置应该是1的话,那么当前的一行除了0之外就全是1了(因为1是最小的数字)
  2. 若当前位置应该是2的话,那么当前的一行除了0之外,有一个1,其余的都是2了
  3. 若当前位置应该是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(思维+构造)相关推荐

  1. CodeForces - 1504C Balance the Bits(思维+构造)

    题目链接:https://vjudge.net/problem/CodeForces-1504C 题目大意:给出一个长度为 nnn 的 010101 串,现在要求构造出两个长度为 nnn 的合法括号序 ...

  2. 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 ...

  3. CodeForces - 1450C2 Errich-Tac-Toe (Hard Version)(思维+构造)

    题目链接:点击查看 题目大意:给出一个大小为 n * m 的棋盘,规定不能有三个连续的 ' X ' 或三个连续的 ' O ' ,现在可以通过一次操作将 ' X ' 改成 ' O ' 或者将 ' O ' ...

  4. codeforces 7.22 E Permutation Shift

    codeforces 7.22 E Permutation Shift 给出一个1到n的排列,每次可以交换两个数,问在交换最多m次(m <= n/3)之后能不能得到由1 2 3 - n循环右移所 ...

  5. Codeforces 1077B Disturbed People(思维题)

    Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...

  6. 【CodeForces - 483C】Diverse Permutation(思维构造)

    题干: Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of ndistinct posit ...

  7. Codeforces Round #624 (Div. 3) E. Construct the Binary Tree 思维 + 构造

    传送门 文章目录 题意: 思路: 题意: 给你n,dn,dn,d,让你构造有nnn个点的二叉树,他们每个节点深度和为ddd. n,d≤3000n,d\le 3000n,d≤3000. 思路: 先考虑不 ...

  8. 【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)

    题干: Mike has always been thinking about the harshness of social inequality. He's so obsessed with it ...

  9. CodeForces - 1343F Restore the Permutation by Sorted Segments(思维)

    题目链接:点击查看 题目大意:现在有一个长度为 n 的排列 p ,但排列 p 暂时对我们保密,每个样例将会给出 n - 1 个排好序后的子段,换句话说,对于 r ∈ [ 2 , n ] ,存在一个 l ...

最新文章

  1. 图像轮廓提取关键函数
  2. 高兴总结台式故障分析==方案
  3. 蓝桥杯练习系统习题-算法训练5
  4. python将姓王的都改成老王_Python老王视频习题答案
  5. vsftp日志查看_vsftp日志xferlog格式分析(示例代码)
  6. krpano使用总结-地图路线
  7. bios文件查看工具_“主板BIOS教程”手把手教你刷bios,多种bios教程
  8. Quartz的集群模式和单机模式共存-让一个非集群的Quartz与集群节点并行着运行
  9. 随机抽题软件C语言,ppt VBA 实现随机抽题
  10. 牛散村期货:3月春风生 第一周非农财经简阅
  11. C#使用Minidump导出内存快照Minidumper
  12. php music player,cloudmusic.php
  13. PHP开源管理系统 Gms,GMS 一个成熟的gms系统,拥有基本的权限管理和分类,后台很好 Windows Develop 249万源代码下载- www.pudn.com...
  14. 导论计算机论文题目,计算机类论文题目
  15. 《AWR Adaptive Weighting Regression for 3D Hand Pose Estimation》研读与实践
  16. python+pygame之飞机大战-第一版源代码分析
  17. python向网页上传excle、text文件
  18. 看完了想点赞的文章!| 数据类好文推荐
  19. 虚拟机之Jvm、dalvik、art联系和区别
  20. npm install node-sass的时候报错ERR gyp ERR C++

热门文章

  1. MySQL 高级 - 存储过程 - 概述
  2. RocketMQ 消息持久化机制
  3. 大型项目中会出现的一些问题:
  4. R eentrantLock的源码分析
  5. MyBatis相应API
  6. 使用RSA算法生成令牌
  7. AOP日志-域对象创建与基本操作介绍
  8. 发布订阅之direct
  9. 数据库-日期计算-获取年月日
  10. 守护线程与非守护线程