Half thirds Study Notes
Front
No front
Bipartite
Half the maximum value of the subject is usually described using the minimum or maximum required minimum
Some specific idea is to keep a range is divided into two sections, divided into two sections continue to take legal
Constantly close to a very small range, the minimum range is the answer
But the point is usually either a legal or illegal, do not have points down
There are two binary model, type 1100 and type 0011 (a method, not legal 0)
A simple example to understand what
This question is a typical 0011 type, to two points as the legitimate product is less than zero for some interval condition
If mid ~ r l = mid legitimate
Otherwise, r = mid
Then two time-card bit precision to
Code
#include <cstdio>
#include <algorithm>
#define maxn 500010
using namespace std;
const double eps = 1e-4;
double a, b, o, d, mid;
double c(double x)
{
return x * x * x * a + x * x * b + x * o + d;
}
int main() {
scanf("%lf%lf%lf%lf", &a, &b, &o, &d);
double l, r;
for(int i = -100; i <= 100; i++)
{
l = i;
r = i + 1;
if(c(l) == 0)
{
printf("%.2f ", l);
continue;
}
if(c(l) * c(r) < 0)
{
while(r - l > eps)//卡精度
{
mid = (l + r) / 2;
if(c(mid) * c(r) <= 0) l = mid;//合法or不合法
else r = mid;
}
printf("%.2f ", l);
}
}
return 0;
}
A little difficult
P1182 series segments Section II
Half of the main difficulties in determining the legality
This question our mid maximum current
If the maximum number of groups in order to drop the sum <= m is legitimate
Otherwise illegal
Therefore, this type entitled 0011
#include <cstdio>
#include <algorithm>
#define maxn 500010
using namespace std;
int l, r, n, m, a[maxn], now, sum;
bool pd(int mid)
{
now = mid;
sum = 1;//初始化
for(int i = 1; i <= n; i++)
{
if(now < a[i])//如果现在的组数数值最大值不够a[i],就开一个新的组
{
sum++;
now = mid - a[i];
}
else now -= a[i];
}
return sum <= m;
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
l = max(l ,a[i]);
r += a[i];
}
int mid;
while(l < r)
{
mid = (l + r) >> 1;
if(pd(mid)) r = mid;
else l = mid + 1;
}
printf("%d", l);
return 0;
}
P2985 eating chocolate Chocolate Eating
This question refers to the mid value most unhappy happy day
Enumerate every day, today, when the value is less than happy eating a chocolate mid to determine when, if you still eat chocolate less than the mid to explain illegal
Note that the current record can not eat chocolate when looking for a minimum order value happy, because the program will all possible cases are down
We should be in the final again pitted pd, this time in order to write down
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 50010
typedef int int_ ;
#define int long long
using namespace std;
int val[maxn], n, d, l, r, vis[maxn], ans;
bool pd(int mid)
{
int now = 0, last = 0;
for(int j = 1; j <= d; j++)
{
last /= 2;
while(last < mid)
{
last += val[++now];
if(now > n) return 0;
if(mid && mid == ans)vis[now] = j;
}
}
return 1;
}
int_ main() {
scanf("%lld%lld", &n, &d);
for(int i = 1; i <= n; i++)
{
scanf("%lld", &val[i]);
r += val[i];
}
int l = 0;
while(l <= r)
{
int mid = (l + r) >> 1;
if(pd(mid))
{
l = mid + 1;
ans = mid;
}
else r = mid - 1;
}
pd(ans);//最后对答案跑一遍记录顺序
printf("%lld\n", ans);
for(int i = 1; i <= n; i++)
{
if(vis[i] == 0) vis[i] = d;
printf("%lld\n", vis[i]);
}
return 0;
}
Thirds
#include <cstdio>
#include <algorithm>
#define maxn 500010
using namespace std;
const double eps = 1e-7;
int n;
double l, r, xs[110];
double f(double x)
{
double ret = 0;
for(int i = 0; i <= n; i++)
{
double cf = 1;
for(int j = 1; j <= i; j++) cf *= x;
ret += xs[i] *cf;
}
return ret;
}
int main() {
scanf("%d%lf%lf", &n, &l, &r);
for(int i = n; i >= 0; i--) scanf("%lf", &xs[i]);
while(r - l > eps)
{
double mid = (l + r) / 2.0;
double mmid = (mid + r) / 2.0;
if(f(mid) > f(mmid)) r = mmid;
else l = mid;
}
printf("%.5lf", l);
return 0;
}
As the title. . . Thirds
We have continued to compare the size of the corresponding function and mid mmid see who greater
Application and unimodal function
(It can be used on the line)