/*Решение с обобщения формула Брахмагупты для произвольного четырехугольника. Функция perimeter(double x[], double y[]) возвращает значение периметра, функция area(double x[], double y[]) возвращает значение площади, пример использования и реализация приведены ниже. */
#include <iostream>
#include <math.h>
double perimeter(double x[], double y[]);
double area(double x[], double y[]);
int main()
{
double x[4], y[4];
std::cout << "Quadrangle ABCD\n";
for (auto i = 0; i < 4; i++)
{
std::cout << "Input coordinates of point " << char(i + 'A') << ": ";
std::cin >> x[i] >> y[i];
}
std::cout << perimeter(x, y) << " " << area(x, y);
return 0;
}
double perimeter(double x[], double y[])
{
double a[4], p = 0;
for (auto i = 0; i < 4; i++)
{
a[i] = sqrt((x[i]-x[(i + 1) % 4]) * (x[i]-x[(i + 1) % 4]) + (y[i]-y[(i + 1) % 4]) * (y[i]-y[(i + 1) % 4]));
p += a[i];
}
return p;
}
double area(double x[], double y[])
{
double a[4], p = 0, s = 1, d[2];
for (auto i = 0; i < 4; i++)
{
a[i] = sqrt((x[i]-x[(i + 1) % 4]) * (x[i]-x[(i + 1) % 4]) + (y[i]-y[(i + 1) % 4]) * (y[i]-y[(i + 1) % 4]));
p += a[i];
}
for (auto i = 0; i < 4; i++)
{
s *= (p / 2- a[i]);
}
for (auto i = 0; i < 2; i++)
{
d[i] = sqrt((x[i]-x[i + 2]) * (x[i]-x[i + 2]) + (y[i]-y[i + 2]) * (y[i]-y[i + 2]));
}
s -= (a[0] * a[2] + a[1] * a[3] + d[0] * d[1]) * (a[0] * a[2] + a[1] * a[3] - d[0] * d[1]) / 4;
s = sqrt(s);
return s;
}
// Внимание! Если программа не работает, обновите версию!
const
cunit=1000;
DISCOUNT_PER_UNIT=500;
MAX_DISCOUNT=0.2;
function getTotalCost(firstCost,secondCost,fullUnits:real):real;
begin
var couponSum:=fullUnits*DISCOUNT_PER_UNIT;
var secondCostWithDiscount:=
secondCost-Min(MAX_DISCOUNT*secondCost,couponSum);
Result:=firstCost+secondCostWithDiscount
end;
function solveKnapsack(weights:array of integer; totalWeight:integer):
array of integer;
begin
var maxUnits:=Trunc(totalWeight/cunit+1);
var old:=ArrFill(maxUnits+1,totalWeight);
old[0]:=0;
var cur:=new integer[maxUnits+1];
var n:=weights.Length;
for var pos:=0 to n-1 do begin
cur.Fill(t->totalWeight);
for var units:=0 to maxUnits do begin
cur[units]:=Min(cur[units],old[units]);
var add:=Trunc(weights[pos]/cunit);
if units-add >= 0 then
cur[units]:=Min(cur[units],old[units-add]+weights[pos])
end;
cur.CopyTo(old,0);
end;
Result:=old;
end;
function getSolution(costs:array of integer):real;
begin
var n:=costs.Length;
var totalCost:=0;
for var i:=0 to n-1 do totalCost+=costs[i];
var minForUnits:=solveKnapsack(costs,totalCost);
Result:=totalCost;
var maxUnits:=Trunc(totalCost/cunit+1);
for var units:=0 to maxUnits do begin
var cur:real:=minForUnits[units];
Result:=Min(Result,getTotalCost(minForUnits[units],totalCost-cur,units))
end
end;
begin
Writeln(getSolution(ReadArrInteger(ReadInteger)):0:2)
end.
Пример
15
1131 2764 1249 3885 4971 2526 1506 1919 520 3094 2183 2503 277 2293 4477
30415.40