Xtreme 10.0 - Always Be In Control

这是 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:


Control Chart
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:
A single point falls outside the 3-sigma control limits.

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.

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.

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 + A2 Rave
LCLX = Xave - A2 Rave
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[] = {0,0,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-1) / y;
}

bool inControl(vector<int> &p, int group_size) {
    //cout << group_size << endl;
    double center = 0, range = 0;
    int num_group = roundedUp(p.size(), group_size);
    for(int i=0; i<num_group; i++) {
        double mean = 0, min = 20000, max = -20000;
        int g;
        for(g=0; 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 * 2 / 3;
    double UC1 = center + a2[group_size] * range / 3;
    double LC1 = center - a2[group_size] * range / 3;
    double LC2 = center - a2[group_size] * range * 2 / 3;
    double LC3 = center - a2[group_size] * range;
    
    //cout << UC3 << ' ' << center << ' ' << LC3 << endl;
    
    bool inControl = true;
    // A single point falls outside the 3-sigma control limits.
    for(int i=0; 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=0; i<p.size()-2; i++) {
        int countUp = 0, countDown = 0;
        for(int j=0; j<3; j++) {
            if(p[i+j] > UC2) countUp++;
            if(p[i+j] < LC2) countDown++;
        }
        if(countUp >=2 || countDown >= 2) {
            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=0; i<p.size()-4; i++) {
        int countUp = 0, countDown = 0;
        for(int j=0; j<5; j++) {
            if(p[i+j] > UC1) countUp++;
            if(p[i+j] < LC1) countDown++;
        }
        if(countUp >= 4 || countDown >= 4) {
            inControl = false;
        } 
    }
    // At least eight successive values fall on the same side of the center line.
    for(int i=0; i<p.size()-7; i++) {
        int countUp = 0, countDown = 0;
        for(int j=0; j<8; j++) {
            if(p[i+j] > center) countUp++;
            if(p[i+j] < center) countDown++;
        }
        if(countUp == 8 || countDown == 8) {
            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=0; t<T; t++) {
        int N, group_size;
        cin >> N >> group_size;
        
        vector<int> process(N);
        for(int n=0; n<N; n++) {
            cin >> process[n];
        }
        
        if(inControl(process, group_size)) {
            cout << "In Control" << endl;
        }
        else {
            cout << "Out of Control" << endl;
        }
    }
    
    return 0;
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,012评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,628评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,653评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,485评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,574评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,590评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,596评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,340评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,794评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,102评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,276评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,940评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,583评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,201评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,441评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,173评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,136评论 2 352

推荐阅读更多精彩内容