USACO Section2.2 Party Lamps - Xavier's Blog



USACO Section2.2 Party Lamps - Xavier's Blog

一开始是想,给定某一盏灯的最终状态,就可知道相关按钮拨动次数的奇偶。比如,state[1] = 0,我就可以知道B1+B2+B4为偶数。因此,对于给出的部分灯的最终状态,就可得到一系列的关于按钮的约束条件(可能有重复),再枚举B1~B4的情况,使其既满足B1+B2+B3+B4 = C,又满足上述约束条件,就可以得到合理的按钮拨动次数的集合了。但编程实现下来,发现C4无法满足时限,而且代码想到丑陋!

后来通过网上一些文章的提示,想到某一按钮按两次跟没有按是同样的情况,所以每种按钮只有按0次和按1次这两种情况,复杂度一下就降到24了。对于按钮的每种情况,分析以下属性:

  • 按1次按钮的总个数CBn。它代表了按钮至少按下了CBn次,因此因满足条件C >= CBn

  • CBn的奇偶性。它应该跟C的奇偶性相同

  • 已确定的部分灯的状态是否与其相符

如果满足了这三个约束条件,就是答案之一了。

////////////////////////////////////////////////////////////////////////////////////////////

看了USACO的官方分析后,发现题目中还有许多巧妙的地方:

  • 无论n和c为多大,最终灯的状态都是以长度6为循环的

  • 大于4且为偶数的c,可以转化为4;大于3且为奇数的c,可以转化为3

对于c=4,可分为:

  • 4个不同的按钮按下;

  • 2个不同的按钮按下;

  • 0个按钮按下。

对于c=3,可分为:

  • 3个不同的按钮按下;

  • 1个不同的按钮按下。

对于c=2:

  • 2个不同按钮按下;

  • 0个不同按钮按下。

对于c=1:

  • 1个不同按钮按下

Read full article from USACO Section2.2 Party Lamps - Xavier's Blog


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts