Coupons and Discounts(细节题)
来源:互联网 发布:爱淘宝怎么推广 编辑:程序博客网 时间:2024/06/05 07:09
Question:题目详情(http://codeforces.com/problemset/problem/731/B)
题目大意:一个人买披萨,有n天,每天都要ai个披萨,她是一家店的会员,可以在一天同时使用无数张第一种和第二种优惠券,第一种优惠券是一天买两个可用,第二种是连续两天都买一个可用(例如第一天买了一个,第二天也必须买一个才可以用),问他是否在每天都不多买的情况下使用优惠券买到披萨
解题思路:这道题从开始遍历,基本只考虑奇数,如果一个数为奇数,但它前面一个也为奇数,则刚好可以使用第二种优惠券,如果他前面一个数为偶数且后面一个数也为偶数,就用第二种优惠券将后面一个偶数变成奇数,直到最后判断最后一个数是否满足,但注意用这种方法1,0,1也是可以实现输出“YES”但事实上不行,所以‘0’前后都为奇数这种情况是不行的,所以这种情况单独考虑
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>using namespace std;const int INF=0x3f3f3f3f;typedef long long LL;LL a[200010];int main(){ LL n,tp=0,flag=1; //tp表示前面一个是否为奇数 cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; if(a[i]%2) //一个数为奇数,这里没考虑偶数,是因为偶数对结果没影响,例如:偶数前面为偶数用第一种优惠,前一个为奇数,则先与前面共用一个第二种,这就将偶数变为奇数了,其实偶数无影响 { if(tp==1) //如果前面一个为奇数就刚好可以使用第二种优惠 tp=0; else tp=1; //如果前面一个不为奇数,则将这个奇数传给后面 } if(a[i]==0&&tp==1) //单独考虑‘0’前后都为奇数的情况 flag=0; } if(tp==0&&flag==1) printf("YES\n"); else printf("NO\n"); return 0;}
体会:细节题,仔细考虑
0 0
- Coupons and Discounts(细节题)
- Coupons and Discounts(水题)
- B. Coupons and Discounts
- 【贪心】Coupons and Discounts
- Codeforces B. Coupons and Discounts
- CodeForces 731B-Coupons and Discounts(贪心 模拟)
- CodeForces 731 B.Coupons and Discounts(水~)
- 【codeforces 710 B Coupons and Discounts】
- 【50.49%】【codeforces 731B】Coupons and Discounts
- Codeforces 731 B. Coupons and Discounts【贪心】
- Codeforce 731B. Coupons and Discounts
- CodeForces 731B Coupons and Discounts
- CodeForces 731B 之 Coupons and Discounts
- Codeforces 731 B. Coupons and Discounts
- codeforces 731B Coupons and Discounts
- Codeforces Round #376 (Div. 2) B. Coupons and Discounts(贪心)
- Codeforces Round #376 (Div. 2) B. Coupons and Discounts
- Codeforces Round #376 (Div. 2) B. Coupons and Discounts
- Lightoj1014【基础题】
- 【C++】浅拷贝和深拷贝(String类)
- 选择合适的Hadoop数据类型
- Android中IntentService的原理及使用
- [JavaEE - JPA] 6. ORM的核心注解 - 基础类型以及嵌套类型
- Coupons and Discounts(细节题)
- Linux升级内核
- Host 'XXX' is not allowed to connect to this MySQL server 解决方案/如何开启MySQL的远程帐号
- BOOST_STATIC_ASSERT与assert的区别
- 洛谷 P1436 POJ 1191 [NOI1999 D1T2] 棋盘分割
- What is SHA-256?
- 51单片机—独立按键
- 向VS中手动添加研华板卡的控件
- C语言数据类型