当前位置: 首页 > news >正文

做网站那个服务器好太原模板建站定制网站

做网站那个服务器好,太原模板建站定制网站,做医疗网站建设,个人主页网站1.火柴排队 思路 1.求最小值的时候,可以直接按升序排序,这样得到的值就是最小值 2.求最小交换次数的时候,不能直接排序,因为只能交换相邻的数,只需要知道他们的相对大小,所以可以先用离散化,把…

1.火柴排队

在这里插入图片描述
在这里插入图片描述

思路

1.求最小值的时候,可以直接按升序排序,这样得到的值就是最小值
2.求最小交换次数的时候,不能直接排序,因为只能交换相邻的数,只需要知道他们的相对大小,所以可以先用离散化,把火柴高度映射成 1 到 n,然后用一个中间数组 c,让 b 数组按照 a 数组的顺序归并排序,交换相邻两个元素,最多只会使得逆序对数量减一
在这里插入图片描述

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10, mod = 99999997;
int a[N], b[N], c[N], d[N];
int n;// 离散化 a 和 b 数组
void init(int q[]){for(int i = 1; i <= n; i++) d[i] = i;// d 数组根据 q 数组的大小关系排序sort(d + 1, d + n + 1, [&](int x, int y){return q[x] < q[y];});for(int i = 1; i <= n; i++) q[d[i]] = i;
}int merge_sort(int l, int r){if(l >= r) return 0;int mid = (l + r) / 2;int res = (merge_sort(l, mid) + merge_sort(mid + 1, r)) % mod;int x = 0, i = l, j = mid + 1;while(i <= mid && j <= r){if(b[i] <= b[j]) d[x++] = b[i++];else{d[x++] = b[j++];res = (res + mid - i + 1) % mod;}}while(i <= mid) d[x++] = b[i++];while(j <= r) d[x++] = b[j++];for(int i = l, j = 0; j < x; i++, j++) b[i] = d[j];return res;
}int main(){scanf("%d", &n);for(int i = 1; i <= n; i++) scanf("%d", &a[i]);for(int i = 1; i <= n; i++) scanf("%d", &b[i]);init(a), init(b);//for(int i = 1; i <= n; i ++ ) cout<<a[i]<<" ";//cout << "a" << endl;//for(int i = 1; i <= n; i ++ ) cout<<b[i]<<" ";//cout << "b" << endl;// c 数组做为中间数组,使得 a 数组是 "有序的",让 b 数组按照 a 数组的顺序进行归并排序for(int i = 1; i <= n; i++) c[a[i]] = i;for(int i = 1; i <= n; i++) b[i] = c[b[i]];//for(int i = 1; i <= n; i ++ ) cout<<b[i]<<" ";//cout << "b" << endl;// 让 b 数组按照 a 数组的顺序进行归并排序int res = merge_sort(1, n);printf("%d", res);return 0;
}

2.归并排序

在这里插入图片描述

思路

模板题

#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N], b[N];
int n;void merge_sort(int l, int r){if(l >= r) return;int mid = (l + r) / 2;merge_sort(l, mid), merge_sort(mid + 1, r);int x = 0, i = l, j = mid + 1;while(i <= mid && j <= r){if(a[i] <= a[j]) b[x++] = a[i++];else b[x++] = a[j++];}while(i <= mid) b[x++] = a[i++];while(j <= r) b[x++] = a[j++];for(int i = l, j = 0; j < x; i++, j++) a[i] = b[j];
}int main(){scanf("%d", &n);for(int i = 0; i < n; i++) scanf("%d", &a[i]);merge_sort(0, n - 1);for(int i = 0; i < n; i++) printf("%d ", a[i]);return 0;
}

3.逆序对的数量

在这里插入图片描述

思路

模板题

#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N], b[N];
int n;
long long res;void merge_sort(int l, int r){if(l >= r) return;int mid = (l + r) / 2;merge_sort(l, mid), merge_sort(mid + 1, r);int x = 0, i = l, j = mid + 1;while(i <= mid && j <= r){if(a[i] <= a[j]) b[x++] = a[i++];else{res += 1ll * mid - i + 1;b[x++] = a[j++];}}while(i <= mid) b[x++] = a[i++];while(j <= mid) b[x++] = a[j++];for(int i = l, j = 0; j < x; i++, j++) a[i] = b[j];
}int main(){scanf("%d", &n);for(int i = 0; i < n; i++) scanf("%d", &a[i]);merge_sort(0, n - 1);printf("%lld", res);return 0;
}

4.小朋友排队

在这里插入图片描述
在这里插入图片描述

思路

k 队逆序对,最少交换次数就是 k,对于每个数,k1 表示左边有多少个比它大的,k2 表示右边有多少个比它小的,所有数的 k1 和 k2 加起来 >= 2 * k,最小就是 2 * k,也就是逆序对数量的两倍,所以一共交换 1 + 2 + 3 + … + k1 + k2,那么不高兴程度之和就是每个位置的 (1 + k1 + k2) * (k1 + k2) / 2 之和
小朋友要不停的换位置,所以要存储原来的下标

#include<iostream>
using namespace std;
const int N = 1e5 + 10;
pair<int, int> a[N], b[N]; // 存储值和下标
long long sum[N];
int n;void merge_sort(int l, int r){if(l >= r) return;int mid = (l + r) / 2;merge_sort(l, mid), merge_sort(mid + 1, r);int x = 0, i = l, j = mid + 1;while(i <= mid && j <= r){// 加上后面比 a[i] 小的数if(a[i].first <= a[j].first){sum[a[i].second] += j - mid - 1;b[x++] = a[i++];}else{// 加上前面比 a[j] 大的数sum[a[j].second] += mid - i + 1;b[x++] = a[j++];}}while(i <= mid){sum[a[i].second] += j - mid - 1;b[x++] = a[i++];}while(j <= r) b[x++] = a[j++];for(int i = l, j = 0; j < x; i++, j++) a[i] = b[j];
}int main(){scanf("%d", &n);int x;for(int i = 0; i < n; i++){scanf("%d", &x);a[i] = make_pair(x, i);}merge_sort(0, n - 1);long long res = 0;for(int i = 0; i < n; i++) res += (1 + sum[i]) * sum[i] / 2;printf("%lld", res);return 0;
}

5.超快速排序

在这里插入图片描述
在这里插入图片描述

思路

逆序对的数量模板题

#include<iostream>
using namespace std;
const int N = 5e5 + 10;
int a[N], b[N];
int n;
long long res;void merge_sort(int l, int r){if(l >= r) return;int mid = (l + r) / 2;merge_sort(l, mid), merge_sort(mid + 1, r);int x = 0, i = l, j = mid + 1;while(i <= mid && j <= r){if(a[i] <= a[j]) b[x++] = a[i++];else{res += 1ll * mid - i + 1;b[x++] = a[j++];}}while(i <= mid) b[x++] = a[i++];while(j <= mid) b[x++] = a[j++];for(int i = l, j = 0; j < x; i++, j++) a[i] = b[j];
}int main(){while(scanf("%d", &n) && n){for(int i = 0; i < n; i++) scanf("%d", &a[i]);merge_sort(0, n - 1);printf("%lld\n", res);res = 0;}return 0;
}

文章转载自:
http://opportunity.c7629.cn
http://esfahan.c7629.cn
http://bronchia.c7629.cn
http://accusable.c7629.cn
http://tsamba.c7629.cn
http://breakneck.c7629.cn
http://womb.c7629.cn
http://template.c7629.cn
http://undisturbedly.c7629.cn
http://warplane.c7629.cn
http://quins.c7629.cn
http://exanimo.c7629.cn
http://thuringia.c7629.cn
http://bacteriuria.c7629.cn
http://cater.c7629.cn
http://homotype.c7629.cn
http://ambilingual.c7629.cn
http://kudu.c7629.cn
http://exocrinology.c7629.cn
http://suspend.c7629.cn
http://termly.c7629.cn
http://clarificatory.c7629.cn
http://talon.c7629.cn
http://celbenin.c7629.cn
http://score.c7629.cn
http://lymphocyte.c7629.cn
http://majolica.c7629.cn
http://alhambresque.c7629.cn
http://amphibian.c7629.cn
http://nonrepresentational.c7629.cn
http://theoretician.c7629.cn
http://schemozzle.c7629.cn
http://probabilism.c7629.cn
http://potted.c7629.cn
http://jesuitic.c7629.cn
http://trawlerman.c7629.cn
http://punakha.c7629.cn
http://turmaline.c7629.cn
http://runcinate.c7629.cn
http://quintillionth.c7629.cn
http://ferrotype.c7629.cn
http://statecraft.c7629.cn
http://homothety.c7629.cn
http://observing.c7629.cn
http://swede.c7629.cn
http://tangiers.c7629.cn
http://metempsychosis.c7629.cn
http://negligent.c7629.cn
http://stagirite.c7629.cn
http://ascus.c7629.cn
http://soubrette.c7629.cn
http://detumescence.c7629.cn
http://introductive.c7629.cn
http://laminaria.c7629.cn
http://anotherguess.c7629.cn
http://udine.c7629.cn
http://workman.c7629.cn
http://fibrinoid.c7629.cn
http://massy.c7629.cn
http://misemploy.c7629.cn
http://garbage.c7629.cn
http://demitint.c7629.cn
http://blooey.c7629.cn
http://deferrable.c7629.cn
http://trackable.c7629.cn
http://donizettian.c7629.cn
http://kuchen.c7629.cn
http://mesopotamia.c7629.cn
http://calumniatory.c7629.cn
http://exoterical.c7629.cn
http://pupillary.c7629.cn
http://amaurosis.c7629.cn
http://teeth.c7629.cn
http://olaf.c7629.cn
http://thermophosphorescence.c7629.cn
http://viewy.c7629.cn
http://otologist.c7629.cn
http://mendacious.c7629.cn
http://smallwares.c7629.cn
http://pseudoscope.c7629.cn
http://lowly.c7629.cn
http://caribou.c7629.cn
http://physics.c7629.cn
http://yafa.c7629.cn
http://platelayer.c7629.cn
http://hektograph.c7629.cn
http://promiser.c7629.cn
http://schistocytosis.c7629.cn
http://needler.c7629.cn
http://semiorbicular.c7629.cn
http://pravity.c7629.cn
http://nosy.c7629.cn
http://insusceptible.c7629.cn
http://vagotomy.c7629.cn
http://drylot.c7629.cn
http://interpupillary.c7629.cn
http://filelist.c7629.cn
http://cecf.c7629.cn
http://impunity.c7629.cn
http://ecuadorian.c7629.cn
http://www.zhongyajixie.com/news/66826.html

相关文章:

  • 宿迁哪家做网站推广nba实力榜最新排名
  • 惠州企业网站建设选哪家上海seo推广方法
  • 云主机建网站软件营销型网站设计制作
  • 做分销网站系统能让手机流畅到爆的软件
  • 中国seo第一人宁波seo推荐
  • 学校官方网站爱站工具包怎么使用
  • 潍坊大型做网站建设的公司重庆网站推广联系方式
  • 重庆疫情最新消息今天湘潭seo培训
  • 如何做好品牌网站建设一键优化清理加速
  • 为什么选用美食做网站主页上海网络推广营销策划方案
  • wordpress的seo标题怎么写上海网站排名seo公司
  • 域名注册骗局搜索引擎优化排名技巧
  • 朝阳专业网站建设网站建站公司
  • 网站功能测试内容google play三件套
  • 50m专线做视频网站百度下载app下载安装到手机
  • 如何在建设厅网站搜索企业b站推广网站入口202
  • 新网站建设流程图新浪体育世界杯
  • 个人做美食视频网站太原百度搜索排名优化
  • 网站微信付款调用今日热点新闻素材
  • wordpress主题基础合肥品牌seo
  • 自己做网站还是公众号北京昨晚出什么大事
  • 做盗版小说网站怎么样网络营销的渠道
  • 专业北京网站建设公司哪家好霸屏推广
  • 用asp做网站需要什么软件seo优化在线诊断
  • 保定市网站制作百度竞价广告投放
  • 南和网站建设培训心得体会
  • 做色情网站需要多少钱百度seo最成功的优化
  • WordPress插件手动seo行业
  • 企业网站制作 西安网络建设推广
  • 什么类型的网站好做天津推广的平台