continue

NOIP2015运输计划的一个O(n)解法

偶尔善良 提交于 2019-11-27 10:22:54
一个O(n)的解法。 不难发现有如下性质: 被改造成虫洞的边一定在最长路径上 那么,我们用类似提直径的方法把 这条路径给拎出来 就会形成这样的一棵树。 那么,对于一条边,若其被改成虫洞,最长路径会有如下三种情况: 依然是当前的最长路径 被改造边左端点的最长路径 被改造边右端点的最长路径 难道我们不用考虑经过该边的路径吗?? 当然不用。 我们已经是在 最长路径 上改造边了, 而最长路径是长于任何一条路径的 (废话),那么经过该边的任何路径在此边被改造后 依然短于最长路径 ,故不用考虑。 至于求两端的最长路径,我们可以前缀和后缀分别维护一下。 那么怎么求前缀(后缀)呢? 我们可以在每个点开个vector,对于一条路径,我们把另一端点以及路径的编号加到vector中。 然后,再开个vis数组,表示这个点是否被访问过。 在遍历时,我们访问所有以这一点为一端点的路径标号 \(id\) 和另一端点 \(y\) ,若另 \(vis[y]=true\) ,就拿 \(id\) 的长度去更新即可。(口胡不清,这最好手动模拟一下) 至于求路径长和LCA呢? 额,树剖我是当常数看的。。。 实在不行你可以Tarjan离线预处理啊 代码( 巨丑无比 ): #include<bits/stdc++.h> #define reg register int #define MAXN 300010 using

Using continue in a switch statement

折月煮酒 提交于 2019-11-27 10:13:47
问题 I want to jump from the middle of a switch statement, to the loop statement in the following code: while (something = get_something()) { switch (something) { case A: case B: break; default: // get another something and try again continue; } // do something for a handled something do_something(); } Is this a valid way to use continue ? Are continue statements ignored by switch statements? Do C and C++ differ on their behaviour here? 回答1: It's fine, the continue statement relates to the

[HDU多校]Ridiculous Netizens

别等时光非礼了梦想. 提交于 2019-11-27 08:44:11
[HDU多校]Ridiculous Netizens 点分治 分成两个部分:对某一点P,连通块经过P或不经过P. 经过P采用树形依赖背包 不经过P的部分递归计算 树型依赖背包 v点必须由其父亲u点转移过来 即必须经过P点 \(dp[v][s*a[v]]+=dp[u][s]\) 第二维代表连通块的乘积 第一维代表经过该点并且一定经过P点的方案数 所以最后父节点还要加上子节点的方案数 空间优化 第二维不能开这么大 稍稍转变含义,改成还能"装下"多少体积 \(\lfloor \frac{m}{s}\rfloor\) 就可以划分很多个等价的状态 #include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = 2010; const int mod = 1e9+7; struct edge{ int to,next; }e[maxn<<1]; ll val[maxn],kuai[maxn]; ll dp[maxn][maxn]; int nowN,rt,maxP,head[maxn],tol,cnt,n; int Size[maxn]; bool vis[maxn<<1]; ll ans; int m; inline void Mod(ll &a,ll b){ a=a+b<mod?a+b

余数算法

≯℡__Kan透↙ 提交于 2019-11-27 08:12:51
一筐鸡蛋: 1个1个拿,正好拿完。 2个2个拿,还剩1个。 3个3个拿,正好拿完。 4个4个拿,还剩1个。 5个5个拿,还剩4个。 6个6个拿,还剩3个。 7个7个拿,正好拿完。 8个8个拿,还剩1个。 9个9个拿,正好拿完。 问筐里最少有多少鸡蛋? 先简化算法: 第一个条件忽略, 是8的倍数一定是4的倍数,也一定是2的倍数 是9的倍数一定是3的倍数, 是3的倍数,而且是奇数,被6除一定余3, 所以,可以归纳为: 5个5个拿,还剩4个。 7个7个拿,正好拿完。 8个8个拿,还剩1个。 9个9个拿,正好拿完。 static void Main(string[] args) { for (int i = 0; i < 10000; i++) { if ((i - i / 5 * 5) != 4) continue; if ((i - i / 7 * 7) != 0) continue; if ((i - i / 8 * 8) != 1) continue; if ((i - i / 9 * 9) != 0) continue; Console.Write(i); Console.ReadLine(); return; } } i=1449 来源: http://www.cnblogs.com/gobuild/p/5142346.html

Equivalent of “continue” in Ruby

拈花ヽ惹草 提交于 2019-11-27 04:56:37
问题 In C and many other languages, there is a continue keyword that, when used inside of a loop, jumps to the next iteration of the loop. Is there any equivalent of this continue keyword in Ruby? 回答1: Yes, it's called next . for i in 0..5 if i < 2 next end puts "Value of local variable is #{i}" end This outputs the following: Value of local variable is 2 Value of local variable is 3 Value of local variable is 4 Value of local variable is 5 => 0..5 回答2: next also, look at redo which redoes the

Skip multiple iterations in loop python

跟風遠走 提交于 2019-11-27 04:11:34
I have a list in a loop and I want to skip 3 elements after look has been reached. In this answer a couple of suggestions were made but I fail to make good use of them: song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life'] for sing in song: if sing == 'look': print sing continue continue continue continue print 'a' + sing print sing Four times continue is nonsense of course and using four times next() doesn't work. The output should look like: always look aside of life for uses iter(song) to loop; you can do this in your own code and then advance the iterator inside the loop;

label语句

十年热恋 提交于 2019-11-27 04:02:48
在javascript中,我们可能很少会去用到 Label 语句,但是熟练的应用 Label 语句,尤其是在嵌套循环中熟练应用 break, continue 与 Label 可以精确的返回到你想要的程序的位置。 label语句语法 label:statement 示例: start:for(var i = 0; i < 5; i++){ console.log(i) } 举个栗子:(没添加label) var num = 0; for(var i = 0; i < 10; i++){ for(var j = 0; j < 10; j++){ if(i == 5 && j ==5){ break; } num++; } } console.log(num); //95 这里当 i 和 j 等于5的时候只退出了 j 循环 ,输出结果是95 添加了label var num = 0; outermost: for(var i = 0; i < 10; i++){ for(var j = 0; j < 10; j++){ console.log('i',i,'j',j,'num',num) if(i == 5 && j ==5){ console.log('等于5了') break outermost; } num++; } } console.log(num); //55

Please explain the usage of Labeled Statements

穿精又带淫゛_ 提交于 2019-11-27 01:54:18
Is breaking and continuing the only uses of labeled statements in Java? When have you used Labeled Statements in your programs? Sorry the code snippet has been deleted. I am splitting the question JLS 14.7 Labeled statements (edited for clarity) Statements may have label prefixes ( Identifier : Statement ). The Identifier is declared to be the label of the immediately contained Statement . Unlike C and C++, the Java programming language has no goto statement; identifier statement labels are used with break ( §14.15 ) or continue ( §14.16 ) statements appearing anywhere within the labeled

POWER

你说的曾经没有我的故事 提交于 2019-11-27 00:08:18
源程序名 输入文件名 输出文件名 时间限制 内存限制 power. pas /power. cpp power.in power.out 1s 128M FJ 的奶牛想要快速计算整数 P 的幂 (1 <= P <=20,000),它们需要你的帮助。因为计算 极大数的幂,所以它们同一时间仅能使用 2 个存储器,每个存储器可记录某个结果值。 第一件工作是初始化存储器内的值一个为底数 x, 另一个为 1。 奶牛可以相乘或相除 2 个存储器中的值,并把结果存在其中某个存储器内,但所有存储的结果必须是整数。 例如, 如果他们想计算 x^31, 一种计算方法是: WV1 WV2 开始: x 1 存储器 1 和存储器 1 相乘,结果存于存储器 2: x x^2 存储器 2 和存储器 2 相乘,结果存于存储器 2: x x^4 存储器 2 和存储器 2 相乘,结果存于存储器 2: x x^8 存储器 2 和存储器 2 相乘,结果存于存储器 2: x x^16 存储器 2 和存储器 2 相乘,结果存于存储器 2: x x^32 存储器 2 和存储器 1 相除,结果存于存储器 2: x x^31 因此, x^31 可以通过 6 次计算得出。给出要计算的幂次,要求求出最少需要几次计算。 输入 仅一个整数: P。 输出 仅一个整数:最少计算次数。 样例 power.in 31 power.in 6 A

Continue in nested while loops

我的未来我决定 提交于 2019-11-27 00:05:46
问题 In this code sample, is there any way to continue on the outer loop from the catch block? while { // outer loop while { // inner loop try { throw; } catch { // how do I continue on the outer loop from here? continue; } } } 回答1: UPDATE: This question was inspiration for my article on this subject. Thanks for the great question! "continue" and "break" are nothing more than a pleasant syntax for a "goto". Apparently by giving them cute names and restricting their usages to particular control