Problem E: 穷游中国在统题 优先队列 + 模拟
http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=4
Problem E: 穷游中国在统题
Description
Input
Output
Sample Input
Sample Output
HINT
考虑下这个列子吧
2 4 1 3 5
和
2 4 1 5 3
首先它的数字不是1--n的,而且可能重复。
那么就离散化吧。把他们离散到1--n中。注意,如果数字是2、1、5、4、2
那么离散后的结果是2、1、5、4、3,总是要把她们离散到1---n
至于怎么离散,如果离散成2、1、5、4、2很容易,就是排序 + lowerbound
那么用book[val]表示这个数字出现了多少次,加上权值就可以了。
至于为什么我要离散成这样。就是因为其实这题每次都是要把最小的数字归位。
比如样例1,一开始我肯定是要让最小的那个数字,就是1,归位的吧,所以区间数字的数量最小都是3.
然后归位的时候发现途中有一个4,所以区间数字的数量要更改为4.(如果a[4]是5,那么就要变成5)
然后就在剩下的数字中,(现在只身下5)去找一个最小的数字归位。
其实就是模拟题,只不过每次必须的任务是把最小的数字归位。
离散成这样的目的是相对大小很明显,知道需要比较到那一位数字。
复杂度是O(nlogn)的
#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 = + ;
int b[maxn];
int a[maxn];
int book[maxn];
bool vis[maxn];
struct node {
int val, pos;
bool operator < (const struct node & rhs) const {
if (val != rhs.val) return val > rhs.val;
else return pos > rhs.pos;
}
};
priority_queue<struct node>que;
void work() {
while (!que.empty()) que.pop();
memset(book, , sizeof book);
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &b[i]);
a[i] = b[i];
}
sort(b + , b + + n);
for (int i = ; i <= n; ++i) {
a[i] = lower_bound(b + , b + + n, a[i]) - b;
book[a[i]]++;
a[i] += book[a[i]] - ;
}
// for (int i = 1; i <= n; ++i) {
// printf("%d ", a[i]);
// }
// printf("\n");
memset(vis, false, sizeof vis);
for (int i = ; i <= n; ++i) {
struct node t;
t.pos = i;
t.val = a[i];
que.push(t);
assert(vis[a[i]] == false);
vis[a[i]] = true;
}
int ans = ;
int lef = -inf;
int be = ;
while (!que.empty()) {
struct node t = que.top();
que.pop();
if (t.pos <= lef) continue;
lef = t.pos;
int mx = -inf;
while (true) {
int tmx = mx;
for (int i = be; i <= lef; ++i) {
mx = max(a[i], mx);
}
if (tmx == mx) {
be = mx + ;
lef = mx;
break;
}
be = lef + ;
lef = mx;
}
ans++;
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) {
work();
if (t) printf("\n");
}
return ;
}
Problem E: 穷游中国在统题 优先队列 + 模拟的更多相关文章
- 蚂蜂窝VS穷游最世界-自由行类App分析
很多其它内容请关注博客: http://www.china10s.com/blog/? p=150 一.产品概述 体验环境: 机型:iPhone 6 型号:64G版 系统:iOS9.2 蚂蜂窝APP版 ...
- C. Tourist Problem 2021.3.29 晚vj拉题 cf 1600 纯数学题
拉题链接 https://vjudge.net/contest/430219#overview 原题链接 https://codeforces.com/problemset/problem/340 ...
- poj 1005:I Think I Need a Houseboat(水题,模拟)
I Think I Need a Houseboat Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 85149 Acce ...
- POJ 3468 A Simple Problem with Integers(线段树水题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 135904 ...
- The Chinese Postman Problem HIT - 2739(有向图中国邮路问题)
无向图的问题,如果每个点的度数为偶数,则就是欧拉回路,而对于一个点只有两种情况,奇数和偶数,那么就把都为奇数的一对点 连一条 边权为原图中这两点最短路的值 的边 是不是就好了 无向图中国邮路问 ...
- HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题
首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m & ...
- HDU2426:Interesting Housing Problem(还没过,貌似入门题)
#include <iostream> #include <queue> #include <stdio.h> #include <string.h> ...
- 【莫比乌斯反演+容斥】BZOJ2301-[HAOI2011]Problem b(成为权限狗的第一题纪念!)
[题目大意] 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. [思路] “怎么又是你系列……”思路 ...
- sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)
The Android University ACM Team Selection Contest Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里 ...
随机推荐
- 责任链模式-Chain of Responsibility
责任链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. 责任链模式结构图: 代码实现: 责任链模式 ...
- YARN commands are invoked by the bin/yarn script.
Apache Hadoop 2.9.0 – YARN Commands http://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-sit ...
- 使用libcurl的包装库cpr发起http请求
cpr GitHub地址https://github.com/whoshuu/cpr 简单示例:cpr_http_request.cpp #include <iostream> #incl ...
- CodeForces 559C Gerald and Gia (格路+容斥+DP)
CodeForces 559C Gerald and Gia 大致题意:有一个 \(N\times M\) 的网格,其中有些格子是黑色的,现在需要求出从左上角到右下角不经过黑色格子的方案数(模 \(1 ...
- 配置webpack中externals来减少打包后vendor.js的体积
在日常的项目开发中,我们会用到各种第三方库来提高效率,但随之带来的问题就是打包后的vendor.js体积过大,导致加载时空白页时间过长,给用户的体验太差.为此我们需要减少vendor.js的体积,从本 ...
- ubuntu下tesseract 4.0安装及参数使用
tesseract是一个开源的OCR引擎,最初是由惠普公司开发用来作为其平板扫描仪的OCR引擎,2005年惠普将其开源出来,之后google接手负责维护.目前稳定的版本是3.0.4.0版本加入了基 ...
- mounted
注意 mounted 不会承诺所有的子组件也都一起被挂载.如果你希望等到整个视图都渲染完毕,可以用 vm.$nextTick 替换掉 mounted: mounted: function () { t ...
- 【推荐】 体验SubSonic
SubSonic简介 SubSonic配置 利用sonic.exe来生成代码 通过Substage来生成代码 简单操作示例 1.SubSonic简介 一句讲完就是:SubSonic就是一个ORM开源框 ...
- VC++、MFC
VC++.MFC最好的开源项目 介绍:介绍一下用VC++/MFC写的最好的开源项目. Sourceforge.net中有许多高质量的VC++开源项目,我列举了一些可以作为VC++程序员的参考. 正文: ...
- Vue Router的配置
1.beforeEnter function requireAuth (route, redirect, next) { if (!auth.loggedIn()) { redirect({ path ...