您的位置:首页 > 其它

CodeForces 567C Case of Matryoshkas map+递推

2015-08-06 10:25 417 查看
原题: http://codeforces.com/contest/556/problem/C

题目:

Case of Matryoshkas

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.

The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.

In one second, you can perform one of the two following operations:

Having a matryoshka a that isn’t nested in any other matryoshka and a matryoshka b, such that b doesn’t contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;

Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.

According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → … → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

Input

The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.

The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, …, aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn’t nested into any other matryoshka).

It is guaranteed that m1 + m2 + … + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.

Output

In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.

Sample test(s)

input

3 2

2 1 2

1 3

output

1

input

7 3

3 1 3 7

2 2 5

2 4 6

output

10

Note

In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.

In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.

思路:

n个数,长度为3以k为公比的等比数列有多少。

因为数据比较大,开数组显然是不可能的,这里我们用map代替数组。

我们用3个map数组,分别代表当前状态下i有多少个,以i为等比数列第二项的个数,以i为等比数列第三项的个数。

显然,我们加入一个新值x进去,如果它的1/k(x%k==0)存在,那么他可以作为以x/k为首项的第二项,同理,如果x/k作为第二项的时候存在,那么第三项也存在,一直累加就可以得到结果。

代码:

#include <iostream>
#include"string.h"
#include"cstdio"
#include"stdlib.h"
#include"map"
using namespace std;

typedef __int64 lint;

int main()
{
lint n,k;
while(scanf("%I64d %I64d",&n,&k)!=EOF)
{
map<lint,lint> mp1;
map<lint,lint> mp2;
map<lint,lint> mp3;
map<lint,lint>::iterator it;
while(n--)
{
lint x;
scanf("%I64d",&x);
if(x%k==0)
{
mp3[x]=mp3[x]+mp2[x/k];
mp2[x]=mp2[x]+mp1[x/k];
}
mp1[x]++;
}
lint ans=0;
for(it=mp3.begin(); it!=mp3.end(); it++)
{
lint an=it->second;
ans=ans+an;
}
printf("%I64d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: