Codeforces Round #262 (Div. 2) 二分+贪心

时间:2023-12-09 17:00:37

题目链接

Little Dima and Equation

题意:给a, b,c 给一个公式,s(x)为x的各个位上的数字和,求有多少个x.

分析:直接枚举x肯定超时,会发现s(x)范围只有只有1-81,所以枚举一下就行。

在做题的时候,用了pow()错了3次,反正以后不用pow了,还是手写吧。会有误差。pow返回的是double型的。

昨天在b题耽误了好多时间,先是提交错第一组,然后又被人cha了。注意在x在1-10^9之间。

 #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL __int64
const int maxn = +;
using namespace std;
LL y[maxn];
int check(LL x)
{
int sum = ;
while(x)
{
sum += x%;
x /= ;
}
return sum;
}
LL pow_m(int a, int b)
{
LL ret = ;
for(int i = ; i <= b; i++)
ret *= a;
return ret;
}
int main()
{
LL a, b, c, i;
int cnt;
LL tmp;
while(~scanf("%I64d%I64d%I64d", &a, &b, &c))
{
cnt = ;
memset(y, , sizeof(y));
for(i = ; i <= ; i++)
{
//tmp = (LL)pow(i, a); //用这个我的程序跑的数据对,但是测试数据不对
tmp = pow_m(i, a);
tmp = (LL)tmp*b + (LL)c;
if(check(tmp)==i)
{
if(tmp > && tmp < )
y[cnt++] = tmp;
}
}
sort(y, y+cnt);
printf("%d\n", cnt);
for(i = ; i < cnt; i++)
{
if(i==cnt-)
printf("%I64d\n", y[i]);
else
printf("%I64d ", y[i]);
}
}
return ;
}

Present

题意:给一串n个数字,可以对连续的w个数字增加1,共增加m次,问增加完以后最小的数字是多少,让求所有方法里最小数字的最大值。

分析:

对结果二分就行了,即二分最小的值,然后都符合的话,往上加一个。

这个题和poj计划上的那两个二分题差不多,比赛的时候因为前面的b题实在是脑残了,做这个题没时间了,当时思路也不是很好,没写出来。

 #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL __int64
const int maxn = +;
using namespace std;
int a[maxn], f[maxn]; int main()
{
int i, n, m, w, y;
int low, mid, hig, tmp, ans, t2;
while(~scanf("%d%d%d", &n, &m, &w))
{
for(i = ; i < n; i++)
{
scanf("%d", &a[i]);
if(i==) low = a[i];
else if(a[i]<low)
low = a[i];
}
hig = m+low;
while(hig>=low)
{
y = m;
tmp = ;
memset(f, , sizeof(f));
mid = (low+hig)/;
for(i = ; i < n; i++)
{
t2 = a[i];
tmp -= f[i]; //先减去已经在增加范围之外的。
t2 += tmp;
if(t2 < mid)
{
y -= mid-t2;
if(i+w<n)
f[i+w] += mid-t2; //f数组记录到w个以后的不会加上mid-t2。
tmp += mid-t2; //记录前面增加的值
}
if(y < ) break;
}
if(y>=)
{
low = mid+;
ans = mid; //记录下合法的答案,一直到最高点
}
else
hig = mid-; //减去1
}
printf("%d\n", ans);
}
return ;
}