A

这么简单的题直接贴代码好了。

 #include <cstdio>
#include <cmath>
using namespace std; bool islucky(int a)
{
a = abs(a);
while(a)
{
if(a % == ) return true;
a /= ;
}
return false;
} int main(void)
{
int a, b = ;
scanf("%d", &a);
while(!islucky(a + b)) b++;
printf("%d\n", b); return ;
}

代码君

B

题意:

有4个从小到大排列的正整数,x x x x ,他们满足下面三个数相等:

  • CodeForces Round #278 (Div.2) (待续)-LMLPHP
  • CodeForces Round #278 (Div.2) (待续)-LMLPHP
  • CodeForces Round #278 (Div.2) (待续)-LMLPHP

现在丢了4-n的数,只剩下其中的n个数(0≤n≤4)

问能否找到4-n个正整数,使得这四个数重新满足上面的条件。

分析:

这道题思路很简单,就是比较繁琐,看到有人写了100多甚至200行代码,所以我还是觉得有必要把我的思路详细分析一下的。

对条件变一下形,等价于下面两个条件:

  • x = 3x
  • x + x = x + x

我们对n不同的情况来考虑

  • n=0,直接随便输出一组解就好了,比如 1 1 3 3
  • n=1,比如所给的数为a, a  a 3a 3a 就是一组解
  • n=2,所给的数为a b(a ≤ b), 我们断言b ≤ 3a时,才有解,为 a b (4a-b) 3a 。 首先若b ≤ 3a,前面所给的解是满足条件的。  若 b > 3a,则四个数中最小的一定是a,最大的一定是b,然而根据我们的条件有x = 3x,即b = 3a,导出矛盾,所以无解。
  • n=3,设所给的三个数为a、b、c。我们只枚举有解的情况,其他情况则无解:  
    • 若c = 3a,则 a  b  (4a-b)  3a 是一组解
    • 若c ≤ 3a 且 b + c = 4a, 这时 a  b  c  3a 是一组解
    • 若c是3的倍数 且 a + b = CodeForces Round #278 (Div.2) (待续)-LMLPHP,  (c / 3)  a  b  c,是一组解
  • n=4直接判断是否满足题目要求条件就好了

代码也比较短,只有50多行。

 #include <cstdio>
#include <algorithm>
using namespace std; int n, a[], ans[]; bool solve()
{
switch(n)
{
case :
ans[] = ans[] = , ans[] = ans[] = ;
return true;
case :
ans[] = a[], ans[] = ans[] = * a[];
return true;
case :
if(a[] <= a[] * )
{
ans[] = a[] * ;
ans[] = a[] * - a[];
return true;
}
return false;
case :
if(a[] == a[] * )
{ ans[] = a[] * - a[]; return true; }
if(a[] <= a[] * && a[] + a[] == a[] * )
{ ans[] = a[] * ; return true; }
if(a[] % == && a[] + a[] == a[]/*)
{ ans[] = a[] / ; return true; }
break;
case :
if(a[] + a[] == a[] + a[] && a[] * == a[])
return true;
}
return false;
} int main(void)
{
scanf("%d", &n);
for(int i = ; i < n; ++i) scanf("%d", &a[i]);
sort(a, a + n);
if(solve())
{
printf("YES\n");
for(int i = ; i < -n; ++i) printf("%d\n", ans[i]);
}
else printf("NO\n"); return ;
}

代码君

C(暴力)

题意:

Master Yang 要去打败一个怪兽(Monster), 他和怪兽都有血量HP,攻击力ATK和防御力DEF。

每个时刻,怪兽使杨大师血量减少 max{0, ATK - DEF}, 同样地, 杨大师对怪兽造成的伤害为 max{0, ATK - DEF}

如果某一时刻HP > 0 且 HP ≤ 0,则视为将怪兽打败。

为了能够打败怪兽,杨大师可以去商店购买HP、ATK和DEF。

现给出杨大师和怪兽的HP、ATK和DEF,和购买每点HP、ATK和DEF所需要消耗的金币,求能够打败怪兽所需要的最少的金币。

分析:

因为数据范围很小,所以暴力是完全可以的。

我们枚举可能购买的攻击力和防御力,然后算出打败怪兽后的剩余HP,如果HP少于1,那么就要购买相应的补回来。然后取每次总花费的最小值。

 #include <cstdio>
#include <algorithm>
using namespace std; int main(void)
{
int y[], m[], price[], ans = ;
for(int i = ; i < ; ++i) scanf("%d", &y[i]);
for(int i = ; i < ; ++i) scanf("%d", &m[i]);
for(int i = ; i < ; ++i) scanf("%d", &price[i]); for(int buyatk = ; buyatk <= ; ++buyatk)
for(int buydef = ; buydef <= ; ++buydef)
{
int crtatk = y[] + buyatk;
int crtdef = y[] + buydef;
int hurty = max(, m[] - crtdef); //杨大师收到的伤害
int hurtm = max(, crtatk - m[]); //怪兽收到的伤害
if(hurtm == ) continue; //打不掉怪兽的血,跳过循环
int k = m[] % hurtm == ? m[]/hurtm : m[]/hurtm + ;
int LeftHP = y[] - hurty * k;
int cost = price[] * buyatk + price[] * buydef + ( - LeftHP > ? ( - LeftHP) * price[] : );
ans = min(ans, cost);
} printf("%d\n", ans); return ;
}

代码君

D

像这种贪心贪不出来的都感觉是DP。

在CF上扒了一个46ms的代码,也总算是看懂了。

题意:

纸条上有n个数,可以将纸条剪为若干小段(只有一个数字也可以),使得每段上的数字满足:

  • 最大值与最小值之差不超过s
  • 数字个数不少于l

分析:

从第一个数开始向左右两个方向延伸(第一个数只能往右延伸),求得一个最大的闭区间[left, right],使得这个区间里的数都满足第一个条件 max - min ≤ s.

如果区间的长度小于l,则无解

如果长度大于等于l,我们则求出了第一段纸片右端点的一个范围[l, right]

第二段纸片从第 right + 1 个数向两侧延伸,这时向左延伸就有了一个界限,因为要保证第一段纸片至少有l个数,所以第二段纸片最多只能向左延伸到第l+1个数。

记此时的区间为[left, right],同样地,看区间的长度是否≥l来判断是否有解。这样一来,为了保证在第三段纸片延伸的时候第二段至少有l个数,所以第三段纸片至多向左延伸到left+l+1

所以我们用last来记录每次向左延伸的界限

 #include <cstdio>
#include <algorithm>
using namespace std; const int maxn = + ;
long long a[maxn], last = ;
bool flag = true; int main(void)
{
//freopen("Din.txt", "r", stdin); long long n, s, l, ans = ;
scanf("%I64d%I64d%I64d", &n, &s, &l);
for(int i = ; i < n; ++i) scanf("%I64d", &a[i]); long long pos, left, right;
for(pos = ; pos < n; ++pos) //从pos位置开始延伸
{
long long maxm, minm, cnt = ; //区间延伸过程中的最小值和最大值以及区间中元素的个数 maxm = a[pos], minm = a[pos];
right = pos + ;
while(right < n)
{
maxm = max(maxm, a[right]);
minm = min(minm, a[right]);
if(maxm - minm <= s) cnt++;
else break;
right++;
}
right--; //maxm = a[pos], minm = a[pos];
left = pos - ;
while(left >= last)
{
maxm = max(maxm, a[left]);
minm = min(minm, a[left]);
if(maxm - minm <= s) cnt++;
else break;
left--;
}
left++; if(cnt < l)
{
flag =false;
break;
}
ans++;
last = left + l;
pos = right;
} if(!flag) ans = -;
printf("%I64d\n", ans); return ;
}

代码君

E(数论+构造)

题意:

给出一个正整数n,问是否存在一个 1~n的排列,使得前i个数的乘积模上n的余数得到的序列是0~n-1的一个排列。

分析:

当n为1、4和素数的时候有解,构造方法是用 (i+1)*(i的逆元)%n 填充当前的数。

这道题先留着,等我比较系统地学习数论以后在给出严格证明以及代码。

05-11 21:46