http://codeforces.com/contest/765/problem/D

这题的化简,不能乱带入,因为复合函数的带入,往往要严格根据他们的定义域的

题目要求出下面两个函数

g[h(x)] = x。这个函数的值域[1, m],定义域[1, n]

h[g(x)] = f(x)。这个函数的值域[1, n],定义域[1, m]

设任意一个数t、和A。使得g[t] = A,也就是有h[A] = t了。

h[A] = h[g(t)] = f(t),那么就是f(t) = t的时候,会使得g[t] = A,h[A] = t

A的值从1...n模拟过去。

这样就能得到g[]和h[]

当然,g[]有些是0,也就是空的,这个时候去找h[]任何一个满足g[pos] = x。就行。

其他的就是细节了。

 8
1 2 3 2 1 2 3 2
 
 5
1 2 2 1 1 
 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e6 + ;
int f[maxn];
int g[maxn];
int h[maxn];
int ans[maxn];
int mph[maxn];
void work() {
int n;
cin >> n;
for (int i = ; i <= n; ++i) cin >> f[i];
int m = ;
for (int i = ; i <= n; ++i) {
if (f[i] == i) {
g[i] = ++m;
h[m] = i;
mph[i] = m;
}
}
if (m == ) {
cout << "-1" << endl;
return;
}
// for (int i = 1; i <= n; ++i) {
// if (g[i] == 0) g[i] = 1;
// }
for (int i = ; i <= n; ++i) {
if (g[i] != && h[g[i]] != f[i]) {
cout << - << endl;
return;
}
if (g[i] == ) {
if (!mph[f[i]]) {
cout << - << endl;
return;
}
g[i] = mph[f[i]];
}
if (i <= m && g[h[i]] != i && g[h[i]] != ) {
cout << - << endl;
return;
}
if (i <= m && g[h[i]] == ) {
g[h[i]] = i;
i--;
}
// i--;
}
cout << m << endl;
for (int i = ; i <= n; ++i) {
cout << g[i] << " ";
}
cout << endl;
for (int i = ; i <= m; ++i) {
cout << h[i] << " ";
}
cout << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

D. Artsem and Saunders 数学题的更多相关文章

  1. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造

    D. Artsem and Saunders 题目连接: http://codeforces.com/contest/765/problem/D Description Artsem has a fr ...

  2. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders

    地址:http://codeforces.com/contest/765/problem/D 题目: D. Artsem and Saunders time limit per test 2 seco ...

  3. D. Artsem and Saunders

    一个变换题 给定f(x),[1,n]->[1,n] 构造g(x),h(x)满足: g(h(x))=x [1,n]->[1,m] h(g(x))=f(x) [1,m]->[1,n] 根 ...

  4. Codeforces Round #397 Div. 2 D. Artsem and Saunders

    http://codeforces.com/problemset/problem/765/D 题意: 有一个函数f,f: [n] → [n] 的意思就是定义域为[1,n],每个x值对应于[1,n]内的 ...

  5. 【构造】Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders

    根据那两个式子 g(h(x))=x h(g(x))=f(x) 可以推出来两个新的式子 g(f(x))=g(x) h(x)=f(h(x)) 于是,我们先找到f(x)的所有不动点,有几个不动点,m就是多少 ...

  6. 【codeforces 765D】Artsem and Saunders

    [题目链接]:http://codeforces.com/contest/765/problem/D [题意] 给你一个函数f(x); 要让你求2个函数g[x]和h[x],使得g[h[x]] = x对 ...

  7. Codeforces_765_D. Artsem and Saunders_(数学)

    D. Artsem and Saunders time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  8. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

    运气好,分到的房里我最先开始Hack C题,Hack了12个,听说F题沙雕莫队但我不会,最后剩不到15分钟想出E题做法打了一波结果挂了,最后虽然上分了但总有点不甘心. 最后A掉ABCD Hack+12 ...

  9. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造

    A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...

随机推荐

  1. LoadRunner 比较字符串是否相等

    int strcmp ( const char *string1, const char *string2 );大小写敏感.int stricmp ( const char *string1, con ...

  2. java 生成压测数据

    询价接口压测,需要批量生成数据, 数据包括4个字段(车牌号,车架号,发动机号,支付号)licenseNo,vehicleFrameNo,engineNo,payFlowId 需符合LoadRunner ...

  3. rocketmq消费队列代码

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(Constant.operationLogGroup); try { consum ...

  4. Making User-Managed Backups-17.3、Making User-Managed Backups of Offline Tablespaces and Datafiles

    17.3.Making User-Managed Backups of Offline Tablespaces and Datafiles 备份离线的表空间时.须要注意下面指导原则: (1)不能离线s ...

  5. 设计模式C++实现_2_简单工厂模式

    简单工厂模式 主要用于创建对象. 新加入类时. 不会影响曾经的系统代码. 核心思想是用一个工厂来依据输入的条件产生不同的类,然后依据不同类的 virtual 函数得到不同的结果. 以下以苹果手机的生产 ...

  6. Android中View窗口getWidth和getMeasuredWidth的差别

    今天在研究自己定义listview的下拉刷新的效果.想移植到项目需求中,再看自己定义源代码时发现了一个问题就是getWidth和getMeasuredWidth两个方法有什么差别,求教万能的百度,经调 ...

  7. nhibernate实体类主键ID赋值问题

    有个同事忽然来找我,说他遇到了一个问题,在调用nhibernate 进行update数据的时候报错,说是有数据行锁定. 看代码,没啥问题. 直接在PL/SQL developer里对数据库进行插入,也 ...

  8. Linq:int类型使用Contains方法

    获取的是前台传过来的String类型的值,例如:1,123,44,59 具体代码如下 if (!string.IsNullOrEmpty(str)) { string[] strArr = str.S ...

  9. 系统队列中的Windows错误报告

  10. fscanf()函数详解【转】

    本文转载自:http://blog.csdn.net/liangxanhai/article/details/8026496 以前解析有规律的文件的时候要么用正则表达式,要么就是傻傻的自己写程序来解析 ...