Median
来源:互联网 发布:梦三国矩阵密保卡下载 编辑:程序博客网 时间:2024/04/24 05:06
题目链接
题意:给你一个包含n个数的序列和m个询问,每个询问包含两个区间,求出这两个区间包含的所有数的中位数。
思路:分两种情况讨论即可,一种是两区间没有交集,一种是有交集。
代码:
/// He renders landscapes with great skill and artistry.#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#include <iterator>#include <cctype>#include <sstream>#include <string>#include <vector>#include <set>#include <map>#include <stack>#include <deque>#include <queue>#include <list>#include <functional>#include <ctime>#include <bitset>//#pragma comment(linker, "/STACK:102400000, 102400000")#define debug puts("+******************************************************+")#define Min(a, b) ( (a < b) ? a : b )#define Max(a, b) ( (a > b) ? a : b )#define lc o<<1#define rc o<<1|1#define lson L, M, lc#define rson M + 1, R, rc#define mem0(x) memset(x, 0, sizeof x)#define mem1(x) memset(x, -1, sizeof x)#define memf(x) memset(x, false, sizeof x)#define pb push_back#define pf push_front#define LB lower_bound#define UB upper_bound#define PQ priority_queue#define fr(x) freopen("x", "r", stdin )#define fw(x) freopen("x", "w" , stdout)#define all(a) a.begin(), a,end()#define X first#define Y second#define MP make_pair#define Abs(x) ( x >= 0 ) ? x : ( -x )#define MAXS 50000 + 8#define MAXT 10000 + 8#define MAXL 500000 + 8#define INF 0x3f3f3f3f#define INFL 1000000000000000000#define inf -(1<<30)#define EPS 1e-10#define PI acos(-1.0)#define sqr(x) (x * x)using namespace std;typedef long long LL;typedef unsigned long long uLL;typedef double DB;typedef long double LD;typedef pair<int, int > pii;typedef pair<LL, LL> pll;const int MOD = 1e9;const int N = 1e5 + 8;const int maxn = 1e3 + 8;const int dx[] = { -1, 1, 0, 0 };const int dy[] = { 0, 0, -1, 1 };struct Node{ LL x; int id; char ch; bool operator < ( const Node & n) const { return x < n.x; }} no[N];int sum;int l1, r1, l2, r2;int n, m;double a[N];double solve(int len){ if ( r1 < l2 ) { if ( l1 + len - 1 <= r1 ) return a[l1 + len - 1]; else { int t = l1 + len - 1 - r1; return a[ l2 + t - 1 ]; } } else { if ( l1 + len - 1 < l2 ) return a[l1 + len - 1]; else if ( l2 - 1 + (r1 - l2 + 1 ) * 2 <= l1 + len - 1 ) { int t = l1 + len - 1 - r1; return a[l2 + t - 1]; } else{ int t = l1 + len - 1 - (l2 - 1); if ( t & 1 ) return a[ l2 + t / 2 ]; else return a[l2 + t / 2 - 1 ]; } }}int len;int main(){ //freopen("codecoder.in", "r", stdin); //freopen("out.txt", "w", stdout); // ios::sync_with_stdio(false); int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lf", &a[i]); for (int i = 0; i < m; i++) { scanf("%d%d%d%d", &l1, &r1, &l2, &r2); if ( l1 > l2 ) swap(l1, l2); if ( r1 > r2 ) swap(r1, r2); len = r1 - l1 + 1 + r2 - l2 + 1; if ( len & 1 ) printf("%.1lf\n", 1.0 * solve(len / 2 + 1)); else printf("%.1lf\n", 0.5 * solve(len / 2) + 0.5 * solve(len / 2 + 1)); } } return 0;}
队内赛自己分情况讨论的时候把这题搞复杂了,改了好长时间没改出来,坑队友了。
附上比赛时的坑码(已AC), 留个教训。
/// He renders landscapes with great skill and artistry.#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#include <iterator>#include <cctype>#include <sstream>#include <string>#include <vector>#include <set>#include <map>#include <stack>#include <deque>#include <queue>#include <list>#include <functional>#include <ctime>#include <bitset>//#pragma comment(linker, "/STACK:102400000, 102400000")#define debug puts("+******************************************************+")#define Min(a, b) ( (a < b) ? a : b )#define Max(a, b) ( (a > b) ? a : b )#define lc o<<1#define rc o<<1|1#define lson L, M, lc#define rson M + 1, R, rc#define mem0(x) memset(x, 0, sizeof x)#define mem1(x) memset(x, -1, sizeof x)#define memf(x) memset(x, false, sizeof x)#define pb push_back#define pf push_front#define LB lower_bound#define UB upper_bound#define PQ priority_queue#define fr(x) freopen("x", "r", stdin )#define fw(x) freopen("x", "w" , stdout)#define all(a) a.begin(), a,end()#define X first#define Y second#define MP make_pair#define Abs(x) ( x >= 0 ) ? x : ( -x )#define MAXS 50000 + 8#define MAXT 10000 + 8#define MAXL 500000 + 8#define INF 0x3f3f3f3f#define INFL 1000000000000000000#define inf -(1<<30)#define EPS 1e-10#define PI acos(-1.0)#define sqr(x) (x * x)using namespace std;typedef long long LL;typedef unsigned long long uLL;typedef double DB;typedef long double LD;typedef pair<int, int > pii;typedef pair<LL, LL> pll;const int MOD = 1e9;const int N = 1e5 + 8;const int maxn = 1e3 + 8;const int dx[] = { -1, 1, 0, 0 };const int dy[] = { 0, 0, -1, 1 };struct Node{ LL x; int id; char ch; bool operator < ( const Node & n) const { return x < n.x; }} no[N];int sum;int l1, r1, l2, r2;int n, m;double a[N];int z;void solve1(){ // debug; z = sum / 2; int zh = l1 + z; //printf("%d------\n", zh); if ( r1 < l2 ) { if ( zh <= r1 ) printf("%.1lf\n", a[zh] ); else { int t = zh - r1; printf("%.1lf\n", a[ l2 + t - 1 ] ); } } else if ( r1 >= l2 && r1 <= r2 ) { int t = r1 - l2 + 1; int tz = l2 - l1; int tx = z + 1 - tz; ///个数 if ( zh < l2 ) { } else { if ( l1 + tz - 1 + (r1 - l2 + 1) * 2 >= zh ) zh = l2 + ( (tx % 2 == 0) ? (tx / 2 - 1) : (tx / 2) ); else { int tt = zh - r1; zh = l2 + tt - 1; } } //printf("%d %d %d++++\n", t, tx, zh); printf("%.1lf\n", a[zh] ); } else if ( r1 > r2 ){ int t = r1 - l2 + 1; int tz = l2 - l1; int tx = zh - tz; if ( l1 + tz - 1 + (r2 - l1 + 1) * 2 < zh ) { int tt = zh - l1 + 1 - tz - (r2 - l2 + 1) * 2; zh = r2 + tt - 1; } else { zh = l2 + ( (tx % 2 == 0) ? (tx / 2 - 1) : (tx / 2) ); } printf("%.1lf\n", a[zh] ); }}double f(int zh){ // printf("%d+++++++\n", zh); int t = r1 - l2 + 1; int tz = l2 - l1; int tx = zh - l1 + 1 - tz; ///个数 if ( zh < l2 ) { } else { if ( l1 + tz - 1 + (r1 - l2 + 1) * 2 >= zh ) zh = l2 + ( (tx % 2 == 0) ? (tx / 2 - 1) : (tx / 2) ); else { int tt = zh - r1; zh = l2 + tt - 1; } } return a[zh]; //printf("%d %d %d++++\n", t, tx, zh); // printf("%.1lf\n", a[zh] );}double ff(int zh){ // printf("%d+++++++\n", zh); int t = r1 - l2 + 1; int tz = l2 - l1; int tx = zh - tz; if ( zh < l2) { } else { if ( l1 + tz - 1 + (r2 - l1 + 1) * 2 < zh ) { int tt = zh - l1 + 1 - tz - (r2 - l2 + 1) * 2; zh = r2 + tt - 1; } else { zh = l2 + ( (tx % 2 == 0) ? (tx / 2 - 1) : (tx / 2) ); } } return a[zh];}void solve2(){ z = sum / 2; int zh = l1 + z - 1; if ( r1 < l2 ) { if ( zh < r1 ) { printf("%.1lf\n", ( a[zh] + a[zh + 1] )/ 2 ); } else if ( zh == r1) { printf("%.1lf\n", ( a[zh] + a[l2] ) / 2 ); } else { int t = zh - r1; zh = l2 + t - 1; printf("%.1lf\n", ( a[zh] + a[zh + 1] )/ 2 ); } } else if ( r1 >= l2 && r1 <= r2) { double pp = f(zh); double qq = f(zh + 1); //printf("%lf %lf+++++++++++\n", pp, qq); printf("%.1lf\n", ( pp + qq ) / 2 ); } else if ( r1 > r2 ){ //debug; double pp = ff(zh); double qq = ff(zh + 1); // printf("%lf %lf+++++++++++\n", pp, qq); printf("%.1lf\n", ( pp + qq ) / 2 ); }}int main(){ //freopen("codecoder.in", "r", stdin); //freopen("out.txt", "w", stdout); // ios::sync_with_stdio(false); int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lf", &a[i]); for (int i = 0; i < m; i++) { scanf("%d%d%d%d", &l1, &r1, &l2, &r2); if (l1 > l2) swap(l1, l2); if (r1 > r2) swap(r1, r2); sum = r1 - l1 + 1 + r2 - l2 + 1; if ( sum & 1 ) solve1(); else solve2(); } } return 0;}
0 0
- Median
- Median
- Median
- median
- Median
- Median
- Median
- median
- median
- Median
- Median Filter
- M Median
- Find Median
- ZOJ3612 Median
- Median Filter
- zju2011 Median
- 1029. Median
- PAT_1029: Median
- PAT A1010. Radix PAT和牛客网全A思路以及PAT上测试例10的讨论
- Centos7 搭建 V为P屁N恩
- ava IO的File类常用方法总结三
- effective java 读书笔记---第三章对于所有对象都通用的方法
- 2016东莞市特长生考试 游戏问题
- Median
- Android日历界面MaterialCalendarView的使用
- Android ListView的使用
- html5中用audio插入背景音乐
- [LeetCode]42. Trapping Rain Water
- awk的使用方法点滴
- 机器学习实践
- LeetCode 240. Search a 2D Matrix II
- Introduction to the CTC loss