CodeForces Round #278 (Div.2) (待续)
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个从小到大排列的正整数,x1 x2 x3 x4 ,他们满足下面三个数相等:
现在丢了4-n的数,只剩下其中的n个数(0≤n≤4)
问能否找到4-n个正整数,使得这四个数重新满足上面的条件。
分析:
这道题思路很简单,就是比较繁琐,看到有人写了100多甚至200行代码,所以我还是觉得有必要把我的思路详细分析一下的。
对条件变一下形,等价于下面两个条件:
- x4 = 3x1
- x1 + x4 = x2 + x3
我们对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,然而根据我们的条件有x4 = 3x1,即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 =
, (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, ATKM - DEFY}, 同样地, 杨大师对怪兽造成的伤害为 max{0, ATKY - DEFM}
如果某一时刻HPY > 0 且 HPM ≤ 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
分析:
从第一个数开始向左右两个方向延伸(第一个数只能往右延伸),求得一个最大的闭区间[left1, right1],使得这个区间里的数都满足第一个条件 max - min ≤ s.
如果区间的长度小于l,则无解
如果长度大于等于l,我们则求出了第一段纸片右端点的一个范围[l, right1]
第二段纸片从第 right + 1 个数向两侧延伸,这时向左延伸就有了一个界限,因为要保证第一段纸片至少有l个数,所以第二段纸片最多只能向左延伸到第l+1个数。
记此时的区间为[left2, right2],同样地,看区间的长度是否≥l来判断是否有解。这样一来,为了保证在第三段纸片延伸的时候第二段至少有l个数,所以第三段纸片至多向左延伸到left2+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 填充当前的数。
这道题先留着,等我比较系统地学习数论以后在给出严格证明以及代码。
CodeForces Round #278 (Div.2) (待续)的更多相关文章
- Codeforces Round #278 (Div. 2)
题目链接:http://codeforces.com/contest/488 A. Giga Tower Giga Tower is the tallest and deepest building ...
- Brute Force - B. Candy Boxes ( Codeforces Round #278 (Div. 2)
B. Candy Boxes Problem's Link: http://codeforces.com/contest/488/problem/B Mean: T题目意思很简单,不解释. ana ...
- Codeforces Round #278 (Div. 1) B. Strip multiset维护DP
B. Strip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/487/problem/B De ...
- Codeforces Round #278 (Div. 1) A. Fight the Monster 暴力
A. Fight the Monster Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/487/ ...
- Codeforces Round #278 (Div. 1)
A A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang an ...
- codeforces 487a//Fight the Monster// Codeforces Round #278(Div. 1)
题意:打怪兽.可增加自己的属性,怎样在能打倒怪兽的情况下花费最少? 这题关键要找好二分的量.一开始我觉得,只要攻击到101,防御到100,就能必胜,于是我对自己的三个属性的和二分(0到201),内部三 ...
- Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp
D. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Round #278 (Div. 1) D - Conveyor Belts 分块+dp
D - Conveyor Belts 思路:分块dp, 对于修改将对应的块再dp一次. #include<bits/stdc++.h> #define LL long long #defi ...
- Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列
B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...
随机推荐
- SQL SERVER时间函数
本篇文章还是学习<程序员的SQL金典>内容的记录,此次将讲解的是SQL SERVER的时间函数. 本文只讲SQL SERVER支持的时间函数(其它数据库这里就不罗列了,想看更多的可以关注& ...
- C# 客户端判断服务器连接已断开
问题描述: 在C# Socket编程中,服务器端已经断开连接(发送数据方),客户端接收服务器端发送数据,在客户端使用client.Recieve()中,服务器端断开连接,客户端任然显示已 ...
- 解决Ubuntu开机自动挂载硬盘回收站不可用等权限问题
1.修改fstab sudo gedit /etc/fstab 2.添加如下代码 #Entry for /dev/sdb7 : UUID=78A675EB46D703C4 /media/anseey/ ...
- 【POJ】【2151】Check the difficulty of problems
概率DP kuangbin总结中的第8题 一开始题目看错导致想转移方程想错了……想成f[i][j]表示前 i 个队伍中最多的做出来 j 道题的概率……sigh 看了下题解……其实是对于每个队伍 i 单 ...
- 使用文本文件(.txt)进行数据存取的技巧总结(相当的经典)
使用文本文件(.txt)进行数据存取的技巧总结(相当的经典) 使用文本文件(.txt)进行数据存取的技巧总结 由于本帖内容较多,部分转自他人的心得,因此,凡转贴的地方仅用“----转----”标注,原 ...
- Iptables DDOS/CC 自动屏蔽脚本
Iptables DDOS/CC 自动屏蔽脚本 May 20, 2013 最近不停地被 CC (DDOS的一种)频繁干扰,分享一个 iptables 屏蔽 DDOS 的脚本.让 crond 每分钟运行 ...
- MongoDB 性能优化五个简单步骤
MongoDB 一直是最流行的 NoSQL,而根据 DB-Engines Ranking 最新的排行,时下 MongoDB 已经击败 PostgreSQL 跃居数据库总排行的第四位,仅次于 Oracl ...
- [转载]Spring Bean Configuration Inheritance
转自: http://www.mkyong.com/spring/spring-bean-configuration-inheritance/ In Spring, the inheritance i ...
- hdu 1162 Eddy's picture(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...
- hdu 4417 Super Mario 离线线段树
思路:将点按值从小到大排序,询问按h从小到大排序. 在建立线段树,按h的大小更新树并得到该次查询的结果! 代码如下: #include<iostream> #include<stdi ...