int main(){ scanf("%d%d",&jewelNum,&keep); for(int i=0;i<jewelNum;i++){ jewels[i].index=i; scanf("%d%d",&jewels[i].value,&jewels[i].weight); } if(jewelNum!=keep){ averageValue=getAverageValue(); while(true){ std::nth_element(jewels,jewels+keep,jewels+jewelNum); double newAverageValue=getAverageValue(); if(fabs(averageValue-newAverageValue)<=1e-8) break; averageValue=newAverageValue; } } /* printf("%d",jewels[0].index+1); for(int i=1;i<keep;i++) printf(" %d",jewels[i].index+1); */ for(int i=0;i<keep;i++) printf("%d ",jewels[i].index+1); printf("\n"); return 0; }
/** * Simulates all Temperature Sensors * * Formula: * Oldvalue + [1,15% of average Radiation] + * [-1..1] - [2% of average Flow] */ static void SimulateTemperature(Trie*const sensorbox) { if(!sensorbox)return; if(!sensorbox->e)return; { int newValue, averageRadiation, averageFlow; Sensor*const sensor = sensorbox->e; iSensor*const isensor = (iSensor*)sensor; int*p = malloc(sizeof*p); if(!p) { Log(LOGT_SERVER, LOGL_SERIOUS_ERROR, "Out of memory!"); return; } // Set current value in history queue *p = isensor->value; sensor->delta = AutoQadd(sensor->delta, p); averageRadiation = getAverageValue(RadiationTable); averageFlow = getAverageValue(FlowTable); // Get new value and set as current value newValue = (isensor->value) + ((averageRadiation/83) + (multirandom(2)-1) - (averageFlow/49)); if( (newValue < 18) && (!(singlerandom(12) == 12)) ) newValue += singlerandom(10); if( (newValue > 70) && (!(singlerandom(12) == 12)) ) newValue -= singlerandom(10); if( (newValue > isensor->lbound) && (newValue < isensor->ubound) && Templock && (TempSens == isensor) ) { Templock = 0; TempSens = 0; } else if( (newValue < isensor->lbound) && Templock && !(TempSens == isensor) ) newValue += 10; else if( (newValue > isensor->ubound) && Templock && !(TempSens == isensor) ) newValue -= 10; else if( (newValue < isensor->lbound) && !Templock ) { Templock = 1; TempSens = isensor; } else if((newValue > isensor->ubound) && !Radlock) { Templock = 1; TempSens = isensor; } //Log(LOGT_SERVER, LOGL_DEBUG, "%s: %d",sensor->name,newValue); isensor->value = newValue; PushS(sensor); } }
/** * Simulates all Pressure Sensors * * Formula: * Startvalue + ( [0..1200] * [average Temperature] * * (( [amount of true in FnSet1] / 2) + 1) ) - * ( [0..1200] * [5% of average Flow] * * (( [amount of true in FnSet2] / 2) + 1) ) */ static void SimulatePressure(Trie*const sensorbox) { if(!sensorbox)return; if(!sensorbox->e)return; { int newValue, averageTemperature, averageFlow, halfFnSet, amountFnSet1, amountFnSet2; Sensor*const sensor = sensorbox->e; iSensor*const isensor = (iSensor*)sensor; int*p = malloc(sizeof*p); if(!p) { Log(LOGT_SERVER, LOGL_SERIOUS_ERROR, "Out of memory!"); return; } // Set current value in history queue *p = isensor->value; sensor->delta = AutoQadd(sensor->delta, p); averageTemperature = getAverageValue(TemperatureTable); averageFlow = getAverageValue(FlowTable); halfFnSet = (countTrie(FullnessTable)/2); amountFnSet1 = countTrueInSet(FullnessTable, 0, &halfFnSet); amountFnSet2 = countTrueInSet(FullnessTable, &halfFnSet, &halfFnSet); // Get new value and set as current value newValue = ( (isensor->startvalue) + ( singlerandom(1200) * averageTemperature * ((amountFnSet1 / 2) + 1) ) - ( singlerandom(1200) * (averageFlow / 20) * ((amountFnSet2 / 2) + 1) ) ); if( (newValue > isensor->lbound) && (newValue < isensor->ubound) && Presslock && (PressSens == isensor) ) { Presslock = 0; PressSens = 0; } else if( (newValue < isensor->lbound) && Presslock && !(PressSens == isensor) ) newValue += 10000; else if( (newValue > isensor->ubound) && Presslock && !(PressSens == isensor) ) newValue -= 10000; else if( (newValue < isensor->lbound) && !Presslock ) { Presslock = 1; PressSens = isensor; } else if((newValue > isensor->ubound) && !Presslock) { Presslock = 1; } isensor->value = newValue; PushS(sensor); } }
BOOL waitForSTBToGoIDLE(int reads) { //one read is ~1,6 s int counter = 0; while (getAverageValue() > TRESHOLD_OFF) { counter++; if (reads > 0 && counter > reads) { printf("Timeout idle\r\n"); return FALSE; } } return TRUE; }
BOOL waitForSTBToStart(int reads) { //one read is ~1,6 s int counter = 0; while (getAverageValue() < TRESHOLD_ON) { counter++; if (reads > 0 && counter > reads) { printf("Timeout start\r\n"); return FALSE; } } return TRUE; }
void Analog::update() { if (!board.analogDataAvailable()) return; addAnalogSamples(); if (!analogValuesSampled()) return; uint16_t analogData; //check values for (int i=0; i<MAX_NUMBER_OF_ANALOG; i++) { //don't process component if it's not enabled if (!database.read(CONF_BLOCK_ANALOG, analogEnabledSection, i)) continue; if (database.read(CONF_BLOCK_ANALOG, analogTypeSection, i) != aType_button) { analogData = getAverageValue(i); analogType_t type = (analogType_t)database.read(CONF_BLOCK_ANALOG, analogTypeSection, i); switch(type) { case aType_potentiometer: checkPotentiometerValue(i, analogData); break; case aType_fsr: checkFSRvalue(i, analogData); break; default: break; } } else { analogData = board.getAnalogValue(i); bool state = analogData > DIGITAL_VALUE_THRESHOLD; buttons.processButton(i+MAX_NUMBER_OF_BUTTONS, state, false); } } resetSamples(); }
void appMain(void) { Stdev_t stdev = stdevInit(8); uint16_t i = 2; addStdev(&stdev, &i); // 2 i=4; addStdev(&stdev,&i); // 4 addStdev(&stdev,&i); // 4 addStdev(&stdev,&i); // 4 i=5; addStdev(&stdev,&i); // 5 addStdev(&stdev,&i); // 5 i=7; addStdev(&stdev,&i); // 7 i=9; addStdev(&stdev,&i); // 9 // Should print avg = 5, stdev = 2 PRINTF("Data = {2,4,4,4,5,5,7,9}, avg = %u, stdev = %u\n", getAverageValue(&stdev.average), getStdevValue(&stdev)); PRINTF("Done!"); }
/** Reajust actor position in scene according with brick shape bellow actor @param brickShape Shape of brick bellow the actor */ void reajustActorPosition(int32 brickShape) { int32 brkX, brkY, brkZ; if (!brickShape) { return; } brkX = (collisionX << 9) - 0x100; brkY = collisionY << 8; brkZ = (collisionZ << 9) - 0x100; // double-side stairs if (brickShape >= kDoubleSideStairsTop1 && brickShape <= kDoubleSideStairsRight2) { switch (brickShape) { case kDoubleSideStairsTop1: if (processActorZ - collisionZ <= processActorX - collisionX) { brickShape = kStairsTopLeft; } else { brickShape = kStairsTopRight; } break; case kDoubleSideStairsBottom1: if (processActorZ - collisionZ <= processActorX - collisionX) { brickShape = kStairsBottomLeft; } else { brickShape = kStairsBottomRight; } break; case kDoubleSideStairsLeft1: if (512 - processActorX - collisionX <= processActorZ - collisionZ) { brickShape = kStairsTopLeft; } else { brickShape = kStairsBottomLeft; } break; case kDoubleSideStairsRight1: if (512 - processActorX - collisionX <= processActorZ - collisionZ) { brickShape = kStairsTopRight; } else { brickShape = kStairsBottomRight; } break; case kDoubleSideStairsTop2: if (processActorX - collisionX >= processActorZ - collisionZ) { brickShape = kStairsTopRight; } else { brickShape = kStairsTopLeft; } break; case kDoubleSideStairsBottom2: if (processActorZ - collisionZ <= processActorX - collisionX) { brickShape = kStairsBottomRight; } else { brickShape = kStairsBottomLeft; } break; case kDoubleSideStairsLeft2: if (512 - processActorX - collisionX <= processActorZ - collisionZ) { brickShape = kStairsBottomLeft; } else { brickShape = kStairsTopLeft; } break; case kDoubleSideStairsRight2: if (512 - processActorX - collisionX <= processActorZ - collisionZ) { brickShape = kStairsBottomRight; } else { brickShape = kStairsTopRight; } break; default: if (cfgfile.Debug == 1) { printf("Brick Shape %d unsupported\n", brickShape); } break; } } if (brickShape >= kStairsTopLeft && brickShape <= kStairsBottomRight) { switch (brickShape) { case kStairsTopLeft: processActorY = brkY + getAverageValue(0, 0x100, 0x200, processActorX - brkX); break; case kStairsTopRight: processActorY = brkY + getAverageValue(0, 0x100, 0x200, processActorZ - brkZ); break; case kStairsBottomLeft: processActorY = brkY + getAverageValue(0x100, 0, 0x200, processActorZ - brkZ); break; case kStairsBottomRight: processActorY = brkY + getAverageValue(0x100, 0, 0x200, processActorX - brkX); break; default: break; } } }
double RecordEntry::getValue(string estimationMethod) { double value = 0; if (estimationMethod == "last") { value= getLatestValue(); } if (estimationMethod == "decay") { value = getDecayValue(); } if (estimationMethod == "average") { int nowSeconds = static_cast<int>(Simulator::Now().GetSeconds()); int delay = memoryLength; int min = nowSeconds - memoryLength; int max = min + memoryLength; value = getAverageValue(min, max); // if (times.size() > 0) { // cout << Simulator::Now().GetSeconds() << " estimationMethod " << estimationMethod << " " << value << " " << min << "-" << max << " times.size " << times.size() << endl; // } int eraseTo = 0; min = min - memoryLength; int i = 0; for (vector<double>::iterator it = times.begin(); it != times.end(); ++it) { if (*it >= min && *it < max) { eraseTo = i; break; } ++i; } if (eraseTo > 0) { // cout << Simulator::Now().GetSeconds() << " Erasing " << times.size() << " from " << times[0] << " to " << times[eraseTo]; times.erase(times.begin() + 1, times.begin() + eraseTo + 1); values.erase(values.begin() + 1, values.begin() + eraseTo + 1); packetIds.erase(packetIds.begin() + 1, packetIds.begin() + eraseTo + 1); // cout << " after " << times.size() << endl; } } if (estimationMethod == "tmc") { int nowSeconds = static_cast<int>(Simulator::Now().GetSeconds()); int delay = memoryLength; int min = nowSeconds / memoryLength * memoryLength - delay; int max = min + memoryLength; value = getAverageValue(min, max); // if (times.size() > 0) { // cout << Simulator::Now().GetSeconds() << " estimationMethod " << estimationMethod << " " << value << " " << min << "-" << max << " times.size " << times.size() << endl; // } // remove int eraseTo = 0; int i = 0; for (vector<double>::iterator it = times.begin(); it != times.end(); ++it) { if (*it >= min && *it < max) { eraseTo = i; break; } ++i; } if (eraseTo > 0) { // cout << Simulator::Now().GetSeconds() << " Erasing " << times.size() << " from " << times[0] << " to " << times[eraseTo]; times.erase(times.begin() + 1, times.begin() + eraseTo + 1); values.erase(values.begin() + 1, values.begin() + eraseTo + 1); packetIds.erase(packetIds.begin() + 1, packetIds.begin() + eraseTo + 1); // cout << " after " << times.size() << endl; } } return value; }
float VoltageSensor::getCellVoltage(Cell cell) { // TODO: fix this for the new ADC return getAverageValue(cell) * VOLTAGE_PER_BIT; }
float VoltageSensor::getTotalVoltage() { float factor = settings_manager->get_total_voltage_factor(); return getAverageValue(CELL0) * VOLTAGE_PER_BIT * factor; }