这是 meelo 原创的 IEEEXtreme极限编程大赛题解

Xtreme 10.0 - Always Be In Control

题目来源 第10届IEEE极限编程大赛

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/always-be-in-control

Engineers use a technique called "statistical process control" to manage and improve engineering processes. For example, suppose a manufacturing process is producing widgets of some sort and the diameter of a widget, measured in microns, is important to the ability to use that widget in a later assembly. Many things can affect the diameter of a widget (humidity, temperature, quality of raw materials, etc.), so there is going to be some variation in diameter from one set of widgets to the next. Statistical process control would sample the diameter of a widget over time to make sure that the variation is consistent.

One of the core techniques of statistical process control is a control chart, which is used to monitor some aspect of the process over time to see if the process is behaving consistently. A control chart plots the sampled statistic over time and includes upper and lower control limits that describe the variation in the data. Those control limits are called 3-sigma limits as they represent about three standard deviations away from the mean of the data. Here is an example control chart:

A process is considered to be "in control" with respect to a given variable if its variation is predictable. When analyzing a control chart, the process is out of control if any of the following occur:

  1. A single point falls outside the 3-sigma control limits.

  2. At least two out of three successive values fall on the same side of, and more than two sigma units away from, the center line.

  3. At least four out of five successive values fall on the same side of, and more than one sigma unit away from, the center line.

  4. At least eight successive values fall on the same side of the center line.

There are many ways to build control charts and selecting the right one depends on the type of data you have and the question you are trying to answer. For this problem, you are going to build a variation of an Xbar chart, in which we group the data into subgroups of n sequential values. For each subgroup, we compute ri, the range of the values, and Xi, the average of the values. (The range is the maximum value minus the minimum value in the subgroup). The control chart will be a plot of the raw data values (in order). The upper control limit (UCL), lower control limit (LCL), and the center line (CL) are computed as follows:

UCLX = Xave + ARave

LCLX = Xave - ARave

CLX = Xave

Where Xave is the average of the Xi values, Rave is the average of the range values, and A2 is a constant that depends on the size of the groups we created, as shown in the table below.

Size of group (n)      A2
2 1.880
3 1.023
4 0.729
5 0.577
6 0.483
7 0.419
8 0.373
9 0.337
10 0.308

Input Format

The first line of the input will be an integer between 1 and 20, inclusive, that is the number of test cases in the input.

Each test case will be specified by one line of space separated integers. The first will be x, 1 ≤ x ≤ 10,000, the number of data points in the test case. The second will be n, 2 ≤ n ≤ 10, the number of elements in a subgroup. That will be followed by x space separated integers for the test case containing the sequential data gathered from an engineering process. These will be integers with values between -10,000 and 10,000, inclusive.

The last subgroup may be incomplete (i.e. it may not contain n elements). The last subgroup should be treated like a normal subgroup, even if it is incomplete. For example, let's say the subgroup had the entries <1,6,2>. If n = 10, this subgroup is incomplete. The range would be 5 (6 - 1 = 5), and the average would be 3 ((1 + 6 + 2)/3 = 3). If there is only 1 item in this subgroup, the average would be equal to the number, and the range would be 0.

Output Format

You are to calculate the three sigma control limits and then test the data to see if it is in control or out of control. For each test case, output, on a line by itself, either "In Control" or "Out of Control" as appropriate.

Note that the output is case-sensitive.

Sample Input

1
25 5 -13 -18 4 15 -3 10 9 -1 17 -1 -2 20 -20 10 -4 2 2 -5 -1 -14 4 -9 13 4 12

Sample Output

Out of Control

Explanation

The table below shows the necessary calculations for these 25 data points, given that there are 5 items in a subgroup.

DATA     SUBGROUP AVERAGE     SUBGROUP RANGE
-13
-18
4
15
-3 -3 33
10
9
-1
17
-1 6.8 18
-2
20
-20
10
-4 0.8 40
2
2
-5
-1
-14 -3.2 16
4
-9
13
4
12 4.8 22 GRAND AVERAGE 1.24 25.8 UCL 16.1266
CENTER LINE 1.24
LCL -13.6466 SIGMA 4.9622

For these calculations, A2 is 0.577 because we grouped five items in a group. As shown in the table, Xave is 1.24, and Rave is 25.8. Since the control limits are "3-sigma" lines, sigma is one third of the distance between the center line and the upper control limit.

This process would be considered out of control because there are a number of points, e.g. -18 and 20, that are more than three sigma from the center line. Note that in a real world analysis, you would need much more data to draw this conclusion.

题目解析

非常简单的一道题。根据题目的规则计算就行了。

完全不用考虑效率的问题,怎么方便怎么写。

程序

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; const double a2[] = {,,1.880,1.023,0.729,0.577,0.483,0.419,0.373,0.337,0.308}; // x/y rounded up
int roundedUp(int x, int y) {
return (x+y-) / y;
} bool inControl(vector<int> &p, int group_size) {
double center = , range = ;
int num_group = roundedUp(p.size(), group_size);
for(int i=; i<num_group; i++) {
double mean = , min = , max = -;
int g;
for(g=; g<group_size; g++) {
int index = i * group_size + g;
if(index >= p.size()) break; mean += p[index];
if(p[index] < min) min = p[index];
if(p[index] > max) max = p[index];
}
center += mean / g;
range += (max - min);
}
center /= num_group;
range /= num_group; double UC3 = center + a2[group_size] * range;
double UC2 = center + a2[group_size] * range * / ;
double UC1 = center + a2[group_size] * range / ;
double LC1 = center - a2[group_size] * range / ;
double LC2 = center - a2[group_size] * range * / ;
double LC3 = center - a2[group_size] * range; bool inControl = true;
// A single point falls outside the 3-sigma control limits.
for(int i=; i<p.size(); i++) {
if(p[i] > UC3 || p[i] < LC3) {
inControl = false;
}
}
// At least two out of three successive values fall on the same side of, and more than two sigma units away from, the center line.
for(int i=; i<p.size()-; i++) {
int countUp = , countDown = ;
for(int j=; j<; j++) {
if(p[i+j] > UC2) countUp++;
if(p[i+j] < LC2) countDown++;
}
if(countUp >= || countDown >= ) {
inControl = false;
}
}
// At least four out of five successive values fall on the same side of, and more than one sigma unit away from, the center line.
for(int i=; i<p.size()-; i++) {
int countUp = , countDown = ;
for(int j=; j<; j++) {
if(p[i+j] > UC1) countUp++;
if(p[i+j] < LC1) countDown++;
}
if(countUp >= || countDown >= ) {
inControl = false;
}
}
// At least eight successive values fall on the same side of the center line.
for(int i=; i<p.size()-; i++) {
int countUp = , countDown = ;
for(int j=; j<; j++) {
if(p[i+j] > center) countUp++;
if(p[i+j] < center) countDown++;
}
if(countUp == || countDown == ) {
inControl = false;
}
} return inControl;
} int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int T;
cin >> T;
for(int t=; t<T; t++) {
int N, group_size;
cin >> N >> group_size; vector<int> process(N);
for(int n=; n<N; n++) {
cin >> process[n];
} if(inControl(process, group_size)) {
cout << "In Control" << endl;
}
else {
cout << "Out of Control" << endl;
}
} return ;
}

博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址

IEEEXtreme 10.0 - Always Be In Control的更多相关文章

  1. IEEEXtreme 10.0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  2. IEEEXtreme 10.0 - Painter's Dilemma

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...

  3. IEEEXtreme 10.0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  4. IEEEXtreme 10.0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  5. IEEEXtreme 10.0 - Checkers Challenge

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  6. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  7. IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...

  8. IEEEXtreme 10.0 - Full Adder

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...

  9. IEEEXtreme 10.0 - N-Palindromes

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...

随机推荐

  1. Python之旅:装饰器

    装饰器就是闭包函数的一种应用场景 一.为何要用装饰器 开放封闭原则:软件一旦上线后,就应该遵循开放封闭原则,即对修改源代码是封闭的,对功能的扩展是开放的 也就是说我们必须找到一种解决方案:能够在不修该 ...

  2. Linux之多线程20160705

    简单介绍一下多线程的API,线程的概念类似与一个任务或者说一个函数,线程一旦被创建就会运行,具体使用方法可以在Linux下使用man 命令查看: pthread_t:线程ID pthread_attr ...

  3. maven 启动 tomcat 及 跳过 test 安装

    1.先在pom文件中配置 tomcat插件 <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload< ...

  4. Spring MVC @ModelAttribute详解

    被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立 ...

  5. (二)SMO算法

    11 SMO优化算法(Sequential minimal optimization) SMO算法由Microsoft Research的John C. Platt在1998年提出,并成为最快的二次规 ...

  6. 电商网站中价格的精确计算(使用BigDecimal进行精确运算(实现加减乘除运算))

    使用BigDecimal的String的构造器.商业计算中,使用bigdecimal的String构造器,一定要用. 重要的事情说三遍: 商业计算中,使用bigdecimal的String构造器! 商 ...

  7. FreeRTOSv9.0.0在STM32F103RCT6上的移植

    1.去官网下载源代码(FreeRTOSv9.0.0.exe) 2.取出Source文件夹,根据单片机和编译器不同,删除不需要的文件,如下图 3.在CORTEX_STM32F103_IAR文件夹中取出P ...

  8. Javascript判断Chrome浏览器

    今天分享一下如何通过Javascript来判断Chrome浏览器,这里是通过userAgent判断的,检测一下userAgent返回的字符串里面是否包含“Chrome”, 具体怎么检测是通过index ...

  9. ueditor和thinkphp框架整合修改版

    基于tp官网上的一篇文章修改的  因为tp中所有目录其实都是性对于入口文件的 在原来的基础上略做修改后 已经做到 无论项目放在www下的任何位置 图片在编辑器中回填后都能正常显示! http://fi ...

  10. Perl6 必应抓取(1):测试版代码

    一个相当丑漏的代码, 以后有时间再优化了. 默认所有查找都是15页, 如果结果没有15页这么多估计会有重复.速度还是很快的. sub MAIN() { my $fp = open 'bin_resul ...