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

网页设计与制作css西安百度推广优化公司

网页设计与制作css,西安百度推广优化公司,长沙哪里学网站建设,wordpress手机菜单分行Problem - 1579E2 - Codeforces Array Optimization by Deque - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 树状数组解法 将 a i a_i ai​插入到队头,贡献为:原队列中所有比 a i a_i ai​小的数的数量将 a i a_i ai​插入到队尾,贡献为&a…

Problem - 1579E2 - Codeforces

image-20231126234824468

Array Optimization by Deque - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

树状数组解法

  • a i a_i ai插入到队头,贡献为:原队列中所有比 a i a_i ai小的数的数量
  • a i a_i ai插入到队尾,贡献为:原队列中所有比 a i a_i ai大的数的数量

可以发现对于每一次插入a值,影响插入的只跟已经放入的大于a或小于a的数量有关。

需要一个数据结构,满足:动态修改、查询。可以发现树状数组可以做这些操作,不过 a i a_i ai较大,开不下数组,可以离散化。

在离散化后,本题转为:查询 a i a_i ai前面的数量和后面的数量(大于/小于)。如果前面的数量小,就插前面,否则插入后面,将答案进行更新。

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <random>
#include <sstream>
#include <numeric>
#include <stdio.h>
#include <functional>
#include <bitset>
#include <algorithm>
using namespace std;#define Multiple_groups_of_examples
#define int_to_long_long
#define IOS std::cout.tie(0);std::cin.tie(0)->sync_with_stdio(false); // 开IOS,需要保证只使用Cpp io流 *
#define dbgnb(a) std::cout << #a << " = " << a << '\n';
#define dbgtt cout<<" !!!test!!! "<<'\n';
#define rep(i,x,n) for(int i = x; i <= n; i++)#define all(x) (x).begin(),(x).end()
#define pb push_back
#define vf first
#define vs secondtypedef long long LL;
#ifdef int_to_long_long
#define int long long
#endif
typedef pair<int,int> PII;const int INF = 0x3f3f3f3f;
const int N = 2e5 + 21;template <class T>
struct Fenwick { int n;vector<T> a;Fenwick(const int &n = 0) : n(n), a(n, T()) {}void modify(int i, T x) {for (i++; i <= n; i += i & -i) {a[i - 1] += x;}}T get(int i) {T res = T();for (; i > 0; i -= i & -i) {res += a[i - 1];}return res;}T sum(int l, int r) { // [l, r] *这里已经改过return get(r + 1) - get(l);}int kth(T k) {int x = 0;for (int i = 1 << __lg(n); i; i >>= 1) {if (x + i <= n && k >= a[x + i - 1]) {x += i;k -= a[x - 1];}}return x;}
};void inpfile();
void solve() {int n; cin>>n;vector<int> a(n);for(auto &t: a) cin>>t;// 离散化vector<int> id(a);sort(all(id));id.erase(unique(all(id)), id.end());vector<int> last(n);for(int i = 0; i < n; ++i) last[i] = lower_bound(all(id), a[i]) - id.begin() + 1;Fenwick<int> tr(n + 21);int sum = 0;for(int i = 0; i < n; ++i) {// lv表示插入队首,rv表示插入队尾的逆序对值int lv = tr.sum(1, last[i] - 1), rv = tr.sum(last[i] + 1, n + 20);sum += min(lv, rv);tr.modify(last[i], 1);}cout<<sum<<endl;}
#ifdef int_to_long_long
signed main()
#else
int main()
#endif{#ifdef Multiple_groups_of_examplesint T; cin>>T;while(T--)#endifsolve();return 0;
}
void inpfile() {#define mytest#ifdef mytestfreopen("ANSWER.txt", "w",stdout);#endif
}

pbds 解法

pbds库学习笔记(优先队列、平衡树、哈希表) - 知乎 (zhihu.com)

发现核心就是求排名,平衡树即可。这里提供pbds的代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <random>
#include <sstream>
#include <numeric>
#include <stdio.h>
#include <functional>
#include <bitset>
#include <algorithm>// pbds
#include <bits/extc++.h>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using namespace std;#define Multiple_groups_of_examples
#define int_to_long_long
#define IOS std::cout.tie(0);std::cin.tie(0)->sync_with_stdio(false); // 开IOS,需要保证只使用Cpp io流 *
#define dbgnb(a) std::cout << #a << " = " << a << '\n';
#define dbgtt cout<<" !!!test!!! "<<'\n';
#define rep(i,x,n) for(int i = x; i <= n; i++)#define all(x) (x).begin(),(x).end()
#define pb push_back
#define vf first
#define vs secondtypedef long long LL;
#ifdef int_to_long_long
#define int long long
#endif
typedef pair<int,int> PII;const int INF = 0x3f3f3f3f;
const int N = 2e5 + 21;
// pbds
typedef tree<PII, null_type, less<PII>, rb_tree_tag, tree_order_statistics_node_update> Tree;
void inpfile();
void solve() {int n; cin>>n;Tree tr;int sum = 0;for(int i = 0; i < n; ++i) {int t; cin>>t;int lv = tr.order_of_key({t, 0}), rv = i - tr.order_of_key({t, n});sum += min(lv, rv);tr.insert({t, i});}cout<<sum<<endl;
}
#ifdef int_to_long_long
signed main()
#else
int main()
#endif{#ifdef Multiple_groups_of_examplesint T; cin>>T;while(T--)#endifsolve();return 0;
}
void inpfile() {#define mytest#ifdef mytestfreopen("ANSWER.txt", "w",stdout);#endif
}

pbds库学习笔记(优先队列、平衡树、哈希表) - 知乎 (zhihu.com)

http://www.khdw.cn/news/44751.html

相关文章:

  • 韶关做网站需要多少钱南宁seo优化公司排名
  • 装饰工程公司经营范围包括哪些?网站页面优化包括
  • 北京西站疫情防控最新消息青岛网站建设优化
  • 如何开网站网站管理和维护的主要工作有哪些
  • 桂林网站制作哪家公司好爱站网关键词查询网站的工具
  • 新乐市做网站专业seo网络营销公司
  • php 手机网站开发教程网站查询服务器
  • 网站管理难做吗seo关键词推广方式
  • 微信小程序网页制作seo优化自学
  • 婚恋网站里加的人做时时彩直接进网站的浏览器
  • 集团网站建设特点助君爱站长尾词
  • 珠海市网站建设开发公司网络推广员的前景
  • 源代码网站和模板做的区别发布平台
  • 全国最大的设计网站百度人工服务电话
  • 秦皇岛建网站免费网站收录网站推广
  • 做网站的毕业设计郑州seo询搜点网络效果佳
  • 做展示型企业网站seo接单平台有哪些
  • 最好的网站模版windows优化大师如何卸载
  • 基于php网站开发步骤广告推广的软件
  • 汉中站网络营销师证书需要多少钱
  • 内蒙古 网站建设软文推广新闻发布
  • wordpress qvanxian优化方案丛书官网
  • 网站采用哪种开发语言seo兼职招聘
  • 德阳企业品牌网站建设厦门百度关键词优化
  • 代做毕设网站可信么百度pc网页版
  • 动态网站静态发布万网app下载
  • 广州建设网站服务大连seo按天付费
  • 黄冈论坛遗爱网贴吧网站优化入门免费教程
  • 微信公众号做特效的网站百度建站云南服务中心
  • 织梦57网站的友情链接怎么做长春网站制作