Code Jam 2017 Qualification Round Problem A. Oversized Pancake Flipper
来源:互联网 发布:订货软件 编辑:程序博客网 时间:2024/06/07 22:51
Problem A. Oversized Pancake Flipper
题意
给定字符串 S (仅由 +
或 -
组成)和 K ,存在一种操作:将串 S 中连续 K 个字符进行反转( +
变成 -
以及 -
变成 +
)。问最少进行多少次上述操作,可以把串 S 变成全由 +
组成。
解法
首先考虑对于任意区间 [i, i+K-1]
,作奇数次操作与作 1 次操作的结果相同,作偶数次操作与不作操作结果相同。
对区间 [i, i+K-1]
和区间 [j, j+K-1]
,其操作的先后次序不会对最后结果产生任何影响。
因此考虑从左到右顺序处理,能对 位置 0
产生影响的仅有区间 [0, 0+K-1]
,而将 位置 0
处理结束后,位置 1
的状态将只与区间 [1, 1+K-1]
有关…
当对于最后一个区间 [n-K+1,n]
,判断区间内的数是否均为 +
, 若存在 -
,则表示无解 - IMPOSSIBLE
。
代码
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<fstream>using namespace std;char s[1010];int k, a[1010], flg[1010], n;int solve(){ int i, sum = 0, cnt = 0; memset(flg, 0, sizeof(flg)); for(i=0;i<n-k+1;i++) { if(i-k>=0) sum -= flg[i-k]; if(a[i] == 0 && sum%2 == 0) { flg[i] = 1; sum += flg[i]; cnt++; } else if(a[i] == 1 && sum % 2 == 1) { flg[i] = 1; sum += flg[i]; cnt++; } } for(i;i<n;i++) { if(i-k>=0) sum -= flg[i-k]; if(sum % 2 == 0 && a[i] == 0) return -1; else if(sum % 2 == 1 && a[i] == 1) return -1; } return cnt;}int main(){ freopen("G:\\A.in", "r", stdin); freopen("G:\\A.out", "w", stdout); int T; scanf("%d",&T); for(int ica=1;ica<=T;ica++) { scanf(" %s %d",s, &k); n = strlen(s); for(int i=0;i<n;i++) a[i] = s[i] == '+' ? 1 : 0; int ans = solve(); if(ans == -1) printf("Case #%d: IMPOSSIBLE\n", ica); else printf("Case #%d: %d\n", ica, ans); }}
0 0
- Code Jam 2017 Qualification Round Problem A. Oversized Pancake Flipper
- 【google code jam Qualification Round 2017】【Oversized Pancake Flipper】【贪心】
- Problem A. Oversized Pancake Flipper(googlejam资格赛)
- Google Code Jam 2014 Qualification Round:Problem A. Magic Trick
- Google Code Jam 2015 Qualification Round: Problem A. Standing Ovation
- Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation
- Code Jam 2017 Qualification Round Problem B. Tidy Numbers
- Code Jam 2017 Qualification Round Problem C. Bathroom Stalls
- Code Jam 2017 Qualification Round Problem D. Fashion Show
- Google Code Jam 2010 Qualification Round 资格赛 Problem A. Snapper Chain 问题A.按扣链条
- google code jam 2011 Qualification Round 资格赛 Problem A. Bot Trust
- Google Code Jam Qualification Round 2014 Problem A. Magic Trick 题解
- Google Code Jam 2010 Qualification Round 2012 Problem B && C
- Google Code Jam 2014-Qualification Round-Problem C. Minesweeper Master
- Google Code Jam 2015 Qualification Round: Problem C. Dijkstra
- Google Code Jam 2014 Qualification Round 2014 A B D
- Google Code Jam 2011 Qualification Round 资格赛 Problem A. Closing the Loop 问题A.关闭的环
- [Google Code Jam 2017] Round 2 problem A
- Part15
- 代码风格
- 查询表空间使用率的sql
- 单调队列优化DP
- 二叉搜索树-《算法导论》学习笔记十一
- Code Jam 2017 Qualification Round Problem A. Oversized Pancake Flipper
- 路径中 使用斜杠/和反斜杠\的区别
- [unity3D基础篇02]必备的3D数学基础1
- C++ #pragma pack()解析
- python中flask 常见问题
- Java异常 相关问题
- Code Jam 2017 Qualification Round Problem B. Tidy Numbers
- 篇章结构类型、层次及分析模式研究
- NGUI所见即所得之UITweener