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 有疑问?点这里 ...
随机推荐
- hive增加Update、Delete支持
一.配置hive-site.xml二.建表三.操作四.总结 一.配置hive-site.xml CDH版本先进入Hive配置页 选择高级,找到hive-site.xml 的 Hive 客户端高级配置代 ...
- spring核心框架体系结构(各个jar包作用)
转自:https://blog.csdn.net/sunchen2012/article/details/53939253 弄懂spring就是弄懂spring各个jar包的作用 spring的jar ...
- (linux)SD卡初始化-mmc_sd_init_card函数
为了学习SD/SDIO协议,看了一下linux中初始化SD卡的流程,结合代码更容易SD初始化是怎么做的. 下面图截自:"SD Specifications Part 1 Physical ...
- POJ 3750 小孩报数问题 (线性表思想 约瑟夫问题 数组模拟运算的 没用循环链表,控制好下标的指向就很容易了)
小孩报数问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10423 Accepted: 4824 Descripti ...
- sql server中like无法匹配下划线问题解决方案
在sql server的like中下划线类似于通配符%,所以无法使用like '%_%'来匹配下划线,可以通过以下两种办法实现匹配下划线 1.使用转义字符escape like '%\_%' esca ...
- 获取cookies的简单代码(总结待续)
Cookie[] cookies = request.getCookies(); Cookie cookie = null; for (int i = 0; i < cookies.length ...
- Android的三种主流资源尺寸
Android三种主流资源屏幕尺寸:QVGA.HVGA.WVGA VGA的分辨率是640x480像素 QVGA(Quarter VGA)就是320x240,即VGA分辨率的1/4 HVGA(Half ...
- DataUtils
package com.cc.hkjc.util; import java.text.ParseException;import java.text.SimpleDateFormat;import j ...
- LA-5052 (暴力)
题意: 给[1,n]的两个排列,统计有多少个二元组(a,b)满足a是A的连续子序列,b是B的连续子序列,a,b中包含的数相同; 思路: 由于是连续的序列,且长度相同,可以枚举一个串的子串,找出这个子串 ...
- SPOJ:Another Version of Inversion(二维数组的逆序对)
DCE Coders admins are way much geekier than they actually seem! Kartik has been following that tradi ...