karel

ICPC World Finals 2019 题解

谁都会走 提交于 2021-01-04 08:51:22
【A】Azulejos 题意简述: 有两排瓷砖,每排都有 $n$ 个,每个瓷砖有高度 $h_i$ 和价格 $p_i$ 两种属性。 你需要分别重新排列这两排瓷砖,使得同一排的瓷砖满足价格不降,后一排的瓷砖的高度严格大于前一排对应瓷砖的高度。 判断无解或输出一种合法方案。 题解: 首先要满足价格不降,那么先把两排瓷砖分别按照 $p_i$ 排序。 如果同一排中的两个瓷砖 $p_i$ 不同,那么顺序已经确定。但是如果 $p_i$ 相同,就可以任意交换顺序。 也就是说,前后两排中的瓷砖都被划分为了若干区间,每个区间中的瓷砖的 $p_i$ 都相同,而且可以任意交换顺序。 让我们从两排的第一个区间开始考虑,这时有两种情况。 第一种是前一排的区间长度较短,那么此时前排的区间中的每个瓷砖都需要与后排的区间中的某个瓷砖配对。 那么在保证尽量能构造出解的前提下,最好把更多更优($h_i$ 尽量大)的后排的瓷砖留给后续考虑。 这引出一个贪心策略,使用 set 按照 $h_i$ 为关键字维护瓷砖, 对于前排的每个瓷砖,在后排中寻找一个 $h_i$ 尽量小,但是比当前瓷砖大的瓷砖与其配对,这样可以保证留给后面的瓷砖尽量优。 第二种是后一排的区间长度较短,相反地,我们对于后排的每个瓷砖寻找前排中 $h_i$ 尽量大,但是比当前瓷砖小的瓷砖与其配对即可。 这种策略可以保证留给后续考虑的瓷砖尽量优

How does Karel run without “main” method?

一曲冷凌霜 提交于 2019-12-02 05:53:57
问题 I was wondering in the program like Karel the Robot runs because it does not used main() method. Instead it used run() method: import stanford.karel.Karel; public class CollectNewspaperKarel extends Karel { public void run() { move(); } } How does it work? 回答1: The actual main method is somewhere else. For example, in the KarelRunner class. When java executes the program, it's actually executing the main method in the runner class. Your own run method is called from that runner code. 回答2: a

How does Karel run without “main” method?

自闭症网瘾萝莉.ら 提交于 2019-12-02 02:02:32
I was wondering in the program like Karel the Robot runs because it does not used main() method. Instead it used run() method: import stanford.karel.Karel; public class CollectNewspaperKarel extends Karel { public void run() { move(); } } How does it work? The actual main method is somewhere else. For example, in the KarelRunner class . When java executes the program, it's actually executing the main method in the runner class. Your own run method is called from that runner code. a "main" method is the starting point of every java program. What is going on with this class is that its not a