https://biancheng.love/contest-ng/index.html#/123/problems

如果只是输出最小的值,那么好办,a升序,b降序,这样是最优的。

但是需要次数,这就麻烦了。

但是注意到它说数字互不相同。

那么,用个数组book[a[i]]表示a[i]需要匹配的是那个数字,就是a数组的最小值,需要匹配b数组的最大值。

然后从原数组中模拟。

因为可能并不需要全部都排序的,本来就一一对应的话,就不需要排。

比如

2 1

1 2

也是不需要排的。

然后就是模拟了,对于如果不是一一对应的每一个数,就去找一个和它对应的,交换就行了。只有这样的解了。

1
3
1 2 3
2 1 3

#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>
const int maxn = 3e6 + ;
LL a[maxn];
LL b[maxn];
LL tocmpa[maxn];
LL tocmpb[maxn];
int book[maxn];
int pos[maxn];
const int fix = 1e6;
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%lld", &a[i]);
tocmpa[i] = a[i] + fix;
}
for (int i = ; i <= n; ++i) {
scanf("%lld", &b[i]);
tocmpb[i] = b[i] + fix;
pos[tocmpb[i]] = i;
}
sort(a + , a + + n);
sort(b + , b + + n, greater<LL>());
LL ans = ;
for (int i = ; i <= n; ++i) {
ans += a[i] * b[i];
}
for (int i = ; i <= n; ++i) {
book[a[i] + fix] = b[i] + fix;
}
int anstime = ;
// for (int i = 1; i <= n; ++i) {
// printf("%d ", pos[book[tocmpa[i]]]);
// }
// printf("\n");
for (int i = ; i <= n; ++i) {
if (book[tocmpa[i]] == tocmpb[i]) continue;
anstime++;
int wan = book[tocmpa[i]];
int cur = tocmpb[i];
int poswan = pos[wan];
int poscur = pos[cur];
swap(tocmpb[poswan], tocmpb[poscur]);
swap(pos[wan], pos[cur]);
}
printf("%lld %d\n", ans, anstime);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

这题的原题其实是:

给定两个数组,要使得b数组变成a数组,每次可以交换b数组的任意两个元素,求最小交换次数。

L 裁纸片 贪心 + 模拟的更多相关文章

  1. 贪心+模拟 ZOJ 3829 Known Notation

    题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...

  2. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  3. CodeForces ---596B--Wilbur and Array(贪心模拟)

    Wilbur and Array Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Su ...

  4. UVA 10714 Ants 蚂蚁 贪心+模拟 水题

    题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...

  5. (贪心 模拟?) codeVs1098 均分纸牌

    题目描述 Description 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸 ...

  6. UVA 11776 - Oh Your Royal Greediness! - [贪心/模拟]

    题目链接:https://cn.vjudge.net/problem/UVA-11776 题意: 给出数字n(0<=n<=1000),代表有n个农民,接下来有n行,每行两个数字S和E代表这 ...

  7. Population Size CodeForces - 416D (贪心,模拟)

    大意: 给定$n$元素序列$a$, 求将$a$划分为连续的等差数列, 且划分数尽量小. $a$中的$-1$表示可以替换为任意正整数, 等差数列中必须也都是正整数. 贪心策略就是从前到后尽量添进一个等差 ...

  8. [CSP-S模拟测试]:开心的金明(贪心+模拟)

    题目传送门(内部题117) 输入格式 第一行一个整数$k$,表示需要处理的月份数. 接下来的$k$行,每行$4$个整数,第$1+i$行分别为:$c_i,d_i,m_i,p_i$ 接下来的$k-1$行, ...

  9. Codeforces Round #547 (Div. 3) D. Colored Boots (贪心,模拟)

    题意:有两个字符串,两个字符串中的相同字符可以相互匹配,\(?\)可以和任意字符匹配,输出最大匹配的字符数量和它们分别两个字符串中的位置. 题解:很容易贪心,我们先遍历第一个字符串,然后在第二个字符串 ...

随机推荐

  1. poj 1840 哈希

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14093   Accepted: 6927 Description ...

  2. Spring如何实现IOC和AOP的,说出实现原理。

    用过spring的朋友都知道spring的强大和高深,都觉得深不可测,其实当你真正花些时间读一读源码就知道它的一些技术实现其实是建立在一些最基本的技术之上而已:例如AOP(面向方面编程)的实现是建立在 ...

  3. YTU 2586: 填空题B-字画鉴别

    2586: 填空题B-字画鉴别 时间限制: 1 Sec  内存限制: 128 MB 提交: 509  解决: 131 题目描述 注:本题只需要提交填写部分的代码,请按照C语言方式提交. 古玩店老板小勇 ...

  4. Activity快速入门理解

    ​ 在Java领域,JBPM和Activity是两个主流的工作流系统,而Activity的出现无疑将会取代JBPM(Activity的开发者就是从Jbpm开发者出来的 1. 1个插件 在Eclipse ...

  5. Redis in python, how do you close the connection?

    down voteaccepted Just use redis.Redis. It uses a connection pool under the hood, so you don't have ...

  6. 洛谷P1527 矩阵乘法——二维树状数组+整体二分

    题目:https://www.luogu.org/problemnew/show/P1527 整体二分,先把所有询问都存下来: 然后二分一个值,小于它的加到二维树状数组的前缀和里,判断一遍所有询问,就 ...

  7. python的termcolor模块

    termcolor是一个python包,可以改变控制台输出的颜色,支持各种terminal(WINDOWS的cmd.exe除外). 它支持下列的文字颜色: grey, red, green, yell ...

  8. .NETFramework-Drawing:Font

    ylbtech-.NETFramework-Drawing:Font 1.返回顶部 1. #region 程序集 System.Drawing, Version=4.0.0.0, Culture=ne ...

  9. IJ:IntelliJ IDEA安装

    ylbtech-IJ:IntelliJ IDEA安装 响应速度快 1.返回顶部 1. 2. 3. 4. 5. 6. 7. 2. 接受协议,激活IJ返回顶部 1. 2. 3. 4. 5. 6.0. 6. ...

  10. 【182】SeaDAS 相关处理

    load:Command mode procedure for reading data and data attributes into memory and making it available ...