题意 :

  • 构造一个n(偶数)的全排列,使得全排列的前半部分最小值等于a,后半部分最大值等于b,如果不能构造,则输出-1

思路一 :

  • 直接构造,将a放最前,b放最后,中间从大到小放,然后构造后判断是否合法,不合法则直接-1
  • *min_element表示最小元素,记得加头文件
  • cout << p[i] << " \n"[i == n - 1] 表示只有在i == n - 1才输出\n否则输出空格
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <vector>
#define pb push_back
#define fi first;
#define se second;using namespace std;void solve()
{int n, a, b;cin >> n >> a >> b;vector<int> p{a};for (int i = n; i >= 1; i -- ){if (i == a || i == b) continue;p.pb(i);}p.pb(b);if (*min_element(p.begin(), p.begin() + n / 2) == a && *max_element(p.begin() + n / 2, p.end()) == b){for (int i = 0; i < n; i ++ )cout << p[i] << " \n"[i == n - 1];}else{cout << -1 << endl;}
}int main()
{ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int _ = 1;cin >> _;while (_ -- ){solve();}return 0;
}

思路二 :

  • 就en构造,发现这个做法太累赘了,比赛的时候边界还卡了很久
  • 算边界的时候,忘记减去边界中的另一个空点了
#include <iostream>
#include <unordered_map>
#include <vector>
#define pb push_back
#define fi first;
#define se second;using namespace std;void solve()
{int n, a, b;cin >> n >> a >> b;if (a > b){if (b != n / 2 || n - a + 1 != n / 2){cout << -1 << endl;return ;}for (int i = a; i <= n; i ++ ) cout << i << ' ';for (int i = 1; i <= b; i ++ ) cout << i << ' ';cout << endl;}else{if (n - a < n / 2 || b - 1 < n / 2){cout << -1 << endl;return ;}for (int i = a; i <= a - n / 2 + b - 1; i ++ ) cout << i << ' ';for (int i = b + 1; i <= n; i ++ ) cout << i << ' ';for (int i = 1; i <= a - 1; i ++ ) cout << i << ' ';for (int i = a - n / 2 + b; i <= b; i ++ ) cout << i << ' ';cout << endl;}
}int main()
{ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int _ = 1;cin >> _;while (_ -- ){solve();}return 0;
}

Special Permutation 构造,min_element,贪心(900)相关推荐

  1. Special Permutation CodeForces - 1352G(构造)

    思路:一开始想复杂了,直接搞的图论,TLE了.后来发现其实可以直接构造.前四个我们可以构造出2 3 1 4 的形式,如果n>=4的话,那么可以左右来回放置,这样就可以构造成功.只有n<=3 ...

  2. CodeForces - 287C Lucky Permutation(构造)

    题目链接:点击查看 题目大意:构造一个合法的排列,满足 ppi=n−i+1p_{p_{i}}=n-i+1ppi​​=n−i+1 题目分析:因为第四个样例的存在降低了本题的难度,不然感觉还是有点难度的一 ...

  3. Minimize the Permutation CodeForces - 1256(贪心)

    题意: q次询问,每次询问给你长度为n的排列,然后你每次可以选择一个位置i和i+1的数字进行交换.但是每个位置只能交换一次,问你反转若干次后,这个排列最小是多少? 题目: You are given ...

  4. 【ZOJ - 3963】Heap Partition (STLset,二叉树的性质,构造,贪心,思维)

    题干: A sequence S = {s1, s2, ..., sn} is called heapable if there exists a binary tree Twith n nodes ...

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

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

  6. Permutation(构造+思维)

    A permutation p is an ordered group of numbers p1,   p2,   ...,   pn, consisting of ndistinct positi ...

  7. K Co-prime Permutation 构造,gcd,互质(2020.12.南京)

    题意 : 求构造一个n的排列,满足其中恰好k个元素与下标互质,如果不能则输出-1 思路 : 让前k个与下标偏移1,后面的都与下标相同即可 当且仅当k=0时,无解,输出-1 #include <i ...

  8. Polycarp Recovers the Permutation 构造(1000)

    题意 : 对于一个长度为n的排列和一个空序列ans,每次取当前最左边或者最右边中最小的值,如果取的是最左边,插入序列ans的最左边,然后在排列中删除这个数,如果取的是最右边,插入最右边,删除 现在给出 ...

  9. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

最新文章

  1. php mysql 时间差_PHP中计算时间差的方法
  2. 2017-7-6日记
  3. div中定义局部变量_说说 Go 中的变量(附粗制滥造面试题)
  4. poj2002 STL set
  5. 腾讯大数据高级产品总监洪桃李:决胜未来的4大关键能力
  6. 行政编码json_基于FME国内县级及以上网络公开行政区划边界的获取
  7. keil接收别人发的工程打不开_STM32工程模板建立之 KEIL 开发平台设置(一)
  8. 实用网址(平时记下的)
  9. 支付业务与技术架构学习总结(8)——支付系统设计包含:账户,对账,风控...
  10. js对象写入键值对_js对象添加键值对
  11. 解放前端工程师——手把手教你开发自己的自定义列表和自定义表单系列之三表格
  12. 串行接口的工作原理和实现
  13. 如何制作手机自适应网页
  14. 【linux内核分析与应用-陈莉君】设备驱动模型
  15. 初中地理老师教师资格证考试成功通过前辈备考经验分享
  16. 区块链baas平台告警方案
  17. 键盘输入一个高精度的正整数n,去掉任意s个数字后剩下的数字按原左右次序将组成一个新的正整数。编程对给定的n和s,寻找一种方案,使得剩下的数最小
  18. python分析谷歌浏览器的历史记录
  19. mysql 连续天数_mysql计算连续天数,mysql连续登录天数,连续天数统计
  20. C++多态——静态多态与动态多态

热门文章

  1. ABAP销售合同删除Bapi
  2. PP生产订单的BADI增强 WORKORDER_UPDATE
  3. Lead time 在ERP 中怎么填
  4. 启动物料帐后不可更改物料价格的处理方法
  5. 2014公积金新政策对你买房有什么好处?
  6. ABAP中的动态运算函数
  7. SAP外币评估 fagl_fc_val 多评估与少评估问题
  8. 用户选择屏幕搜索帮助的代码实现
  9. AgilePoint BPMS 业务流程
  10. 重新洗牌的网约车,谁在接管下沉市场?