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. COCOS2DX学习之Box2d物理引擎使用之------动态物体的创建

    1.创建一个物理世界 首先要引入一个头文件#include "Box2D\Box2D.h" 之后利用b2word创建一个对象,而且指定这个物理世界中的加速度方向. word = n ...

  2. Android Studio一些简单设置

          简单设置   1.默认主题设置 默认的 Android Studio 为灰色界面,能够选择使用炫酷的黑色界面. Settings --> Appearance --> Them ...

  3. 创业公司做数据分析(四)ELK日志系统

      作为系列文章的第四篇.本文将重点探讨数据採集层中的ELK日志系统.日志.指的是后台服务中产生的log信息,一般会输入到不同的文件里.比方Django服务下,一般会有nginx日志和uWSGI日志. ...

  4. hdu 3183 A Magic Lamp 贪心

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  5. EXTJS 4 树形表格组件使用演示样例

    EXTJS 4 树形表格组件使用演示样例 一.总体效果图 version=1&modificationDate=1412058826000&api=v2" alt=" ...

  6. ubuntu安装jdk 1.6

    linux下安装JDK1.6 1. 去http://java.sun.com/j2se/1.4.2/download.html 下载一个Linux Platform的JDK,建议下载RPM自解压格式的 ...

  7. asp.net listview 实现分页浏览效果

    页面代码: <div style="margin-top:0px;">共<asp:Label ID="lb_count" runat=&quo ...

  8. 信息检索导论的课件——http://home.ustc.edu.cn/~zhufengx/ir/pdf/

    http://home.ustc.edu.cn/~zhufengx/ir/pdf/ 中科大

  9. 并不对劲的bzoj5020:loj2289:p4546:[THUWC2017]在美妙的数学王国中畅游

    题目大意 有一个n(\(n\leq 10^5\))个点的森林,每个点\(u\)上有个函数\(f_u(x)\),是形如\(ax+b\)或\(e^{ax+b}\)或\(sin(ax+b)\)的函数,保证当 ...

  10. 【SCOI 2005】 繁忙的都市

    [题目链接] 点击打开链接 [算法] 题目描述比较繁琐,但细心观察后,发现其实就是用kruskal算法求最小生成树 [代码] #include<bits/stdc++.h> using n ...