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. js 终止执行的实现方法

    终止JS运行有如下几种可能: 1.终止函数的运行的方式有两种 (1)在函数中使用return,则当遇到return时,函数终止执行,控制权继续向下运行 (2)在函数中使用try-catch异常处理,需 ...

  2. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...

  3. Apach POI 如何拿到有公式的单元格,计算结果

    public static void getFormulaCellValue(){ FileInputStream fis = new FileInputStream("c:/temp/te ...

  4. vue 配置跨域访问

    主要在config->index.js中配置 proxyTable: { ‘/gameapi’: { changeOrigin: true, // target: ‘http://rap.id. ...

  5. (WPF)附加属性

    <Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20 ...

  6. 【iOS系列】- 通知NSNotification的使用

    [iOS系列]- 通知NSNotification的使用 1:属性 通知属性: - (NSString *)name; // 通知的名称 - (id)object; // 通知发布者(是谁要发布通知) ...

  7. 导入项目 R.java没有

    网上一搜,各种 Android tools-fix your porject或者Clean ...不好使 其实可能是由于XML布局文件有错误导致,修改掉这些错误就可以了..

  8. GrideView(三)---编辑功能实现

    GrideView(三)---编辑 法一.(优势:操作数据更加灵活:劣势: 书写较多代码,开发效率低) 通过编辑列---添加超链接(HyperlinkField)字段 ,来跳转页面实现编辑操作: 步骤 ...

  9. CentOS 7下修改rabbitmq打开文件数量方法

    以下为使用systemd的修改方法:   1.系统层修改: 通过修改sysctl配置,提高系统的打开文件数量 vim /etc/sysctl.conf,添加: fs.file-max = 65535 ...

  10. mac系统下命令编译android ndk项目

    1.设置ndk环境变量 2.构造android ndk项目,命令:ndk-build 3.使用ant构造android sdk项目:命令:android update project -p . -n ...