贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门

 1 /*
 2     题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步
 3     贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数,
 4                 其他是2次操作,还要减去k-1个娃娃是只要套上就可以
 5     详细解释:http://blog.csdn.net/firstlucker/article/details/46671251
 6 */
 7 #include <cstdio>
 8 #include <algorithm>
 9 #include <cstring>
10 #include <cmath>
11 using namespace std;
12
13 const int MAXN = 1e5 + 10;
14 const int INF = 0x3f3f3f3f;
15 int a[MAXN];
16
17 int main(void)      //Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
18 {
19     // freopen ("C.in", "r", stdin);
20
21     int n, k;
22     while (scanf ("%d%d", &n, &k) == 2)
23     {
24         int cnt = 0;
25         for (int i=1; i<=k; ++i)
26         {
27             int m;    scanf ("%d", &m);    int p = 0;
28             for (int j=1; j<=m; ++j)
29             {
30                 scanf ("%d", &a[j]);    if (a[j] == 1)    p = j;
31             }
32             if (p)
33             {
34                 for (int j=p+1; j<=m; ++j)
35                 {
36                     if (a[j] == a[j-1] + 1)    cnt++;
37                     else    break;
38                 }
39             }
40         }
41         printf ("%d\n", (n - 1 - cnt) * 2 - (k - 1));
42     }
43
44     return 0;
45 }
时间: 2024-10-14 07:58:14

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas的相关文章

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

题目传送门 1 /* 2 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 3 构造:先求出使第1个指向0要多少步,按照这个次数之后的能否满足要求 4 题目读的好累:( 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #i

Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)

题目地址:传送门 这题虽然是DIV1的C..但是挺简单的..只要用线段树分别维护一下横着和竖着的值就可以了,先离散化再维护.每次查找最大的最小值<=tmp的点,可以直接在线段树里搜,也可以二分去找. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.

Codeforces Round #310 (Div. 1) C Case of Chocolate

思路:对于每个点而言.只与它相邻的两个点有关系.所以可以用stl或者线段树来找到它的相邻点. 代码:187ms(开挂之后貌似是最快的- -) #include <cstdio> #include <map> #include <algorithm> using namespace std; const int N = 200000 + 1; int x[N], y[N], t[N]; //适用于正负整数 template <class T> inline b

水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta&#39;s Gift

题目传送门 1 /* 2 水题:vector容器实现插入操作,暴力进行判断是否为回文串 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <string> 9 #include <vector> 10 using namespace std; 11 12 const int MAXN

Round 310(Div.1) B. Case of Fugitive

Round 310(Div.1) B. Case of Fugitive Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an archipelago of n narrow islands

水题 Codeforces Round #303 (Div. 2) A. Toy Cars

题目传送门 1 /* 2 题意:5种情况对应对应第i或j辆车翻了没 3 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 4 3: if both cars turned over during the collision. 5 是指i,j两辆车,而不是全部 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath> 11 #

水题 Codeforces Round #299 (Div. 2) A. Tavas and Nafas

题目传送门 1 /* 2 很简单的水题,晚上累了,刷刷水题开心一下:) 3 */ 4 #include <bits/stdc++.h> 5 using namespace std; 6 7 char s1[11][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven

水题 Codeforces Round #302 (Div. 2) A Set of Strings

题目传送门 1 /* 2 题意:一个字符串分割成k段,每段开头字母不相同 3 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 using namespace std; 11 12 con