Codeforces Round #137 (Div. 2)

A. Shooshuns and Sequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One day shooshuns found a sequence of n integers, written on a blackboard. The shooshuns can perform one operation with it, the operation consists of two steps:

  1. Find the number that goes k-th in the current sequence and add the same number to the end of the sequence;
  2. Delete the first number of the current sequence.

The shooshuns wonder after how many operations all numbers on the board will be the same and whether all numbers will ever be the same.

Input

The first line contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 105).

The second line contains n space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 105) — the sequence that the shooshuns found.

Output

Print the minimum number of operations, required for all numbers on the blackboard to become the same. If it is impossible to achieve, print -1.

Sample test(s)
input
3 2
3 1 1
output
1
input
3 1
3 1 1
output
-1
Note

In the first test case after the first operation the blackboard will have sequence [1, 1, 1]. So, one operation is enough to make all numbers the same. Thus, the answer equals one.

In the second test case the sequence will never consist of the same numbers. It will always contain at least two distinct numbers 3 and 1. Thus, the answer equals -1.

题目意思:输入n,k 然后输入n个数字,现在的操作是将第k个数字的值,加到最后一个,然后删掉第一个元素,得到还是n个数字的排列,问需要进行多少次操作可以把这个排列的值变成一样,否则输出-1

分析:如果可以n!=k的话,要想达到目的,那么这个排列的最后几个数字应该是全部相等的,当第k位在这个相等的数之间的时候,我们只需要将第k位前,最靠左的第一个不想等那个数字的下标就是需要操作的次数,因为我们只需要删掉它和它前面的那几个数字,而当k不属于那个范围的时候,当然操作就无效了,输出 -1


#include <stdio.h>
const int maxn=100001;
int a[maxn];
int main()
{
    int n,k,i;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(i=n-1;i>=1;i--)
    {
        if(a[i]!=a[i+1])
            break;
    }
    if(k>i)
        printf("%d\n",i);
    else
        printf("-1\n");
    return 0;
}



B. Cosmic Tables
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement.

UCDHP stores some secret information about meteors as an n × m table with integers in its cells. The order of meteors in the Universe is changing. That's why the main UCDHP module receives the following queries:

  • The query to swap two table rows;
  • The query to swap two table columns;
  • The query to obtain a secret number in a particular table cell.

As the main UCDHP module is critical, writing the functional of working with the table has been commissioned to you.

Input

The first line contains three space-separated integers nm and k (1 ≤ n, m ≤ 10001 ≤ k ≤ 500000) — the number of table columns and rows and the number of queries, correspondingly.

Next n lines contain m space-separated numbers each — the initial state of the table. Each number p in the table is an integer and satisfies the inequality 0 ≤ p ≤ 106.

Next k lines contain queries in the format "si xi yi", where si is one of the characters "с", "r" or "g", and xiyi are two integers.

  • If si = "c", then the current query is the query to swap columns with indexes xi and yi (1 ≤ x, y ≤ m, x ≠ y);
  • If si = "r", then the current query is the query to swap rows with indexes xi and yi (1 ≤ x, y ≤ n, x ≠ y);
  • If si = "g", then the current query is the query to obtain the number that located in the xi-th row and in the yi-th column (1 ≤ x ≤ n, 1 ≤ y ≤ m).

The table rows are considered to be indexed from top to bottom from 1 to n, and the table columns — from left to right from 1 to m.

Output

For each query to obtain a number (si = "g") print the required number. Print the answers to the queries in the order of the queries in the input.

Sample test(s)
input
3 3 5
1 2 3
4 5 6
7 8 9
g 3 2
r 3 2
c 2 3
g 2 2
g 3 2
output
8
9
6
input
2 3 3
1 2 4
3 1 5
c 2 1
r 1 2
g 1 3
output
5
Note

Let's see how the table changes in the second test case.

After the first operation is fulfilled, the table looks like that:

2 1 4

1 3 5

After the second operation is fulfilled, the table looks like that:

1 3 5

2 1 4

So the answer to the third query (the number located in the first row and in the third column) will be 5.


题意:给你m,n,k 输出m*n的矩阵和k个操作,操作为c时交换行的x,y ,操作为r时交换列的x,y ,操作为g时输出坐标为x,y的值

分析:数据范围还好,只是10^6以内的,但是之前做的时候是直接暴力的,TLE了,正确的做法应该是开两个数组,用以保存矩阵下标,所有的操作均在下标上操作。


#include <stdio.h>
const int maxn=1001;
int a[maxn][maxn];
int col[maxn],row[maxn];
int main()
{
    int m,n,k;
    scanf("%d%d%d",&m,&n,&k);
    for(int i=1;i<=m;i++)
    {
        row[i]=i;
        for(int j=1;j<=n;j++)
        {
            col[j]=j;
            scanf("%d",&a[i][j]);
        }
    }
    while(k--)
    {
        char c;
        int x,y;
        getchar();
        scanf("%c%d%d",&c,&x,&y);
        if(c=='c')
        {
            int t=col[x];
            col[x]=col[y];
            col[y]=t;
        }
        else if(c=='r')
        {
            int t=row[x];
            row[x]=row[y];
            row[y]=t;
        }
        else
            printf("%d\n",a[row[x]][col[y]]);
    }
    return 0;
}


C. Reducing Fractions
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractions are represented as two sets of integers. The product of numbers from the first set gives the fraction numerator, the product of numbers from the second set gives the fraction denominator. However, it turned out that the programs that work with fractions in this representations aren't complete, they lack supporting the operation of reducing fractions. Implement this operation and the Empire won't forget you.

Input

The first input line contains two space-separated integers nm (1 ≤ n, m ≤ 105) that show how many numbers the first set (the numerator) and the second set (the denominator) contain, correspondingly.

The second line contains n space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 107) — the numbers that are multiplied to produce the numerator.

The third line contains m space-separated integers: b1, b2, ..., bm (1 ≤ bi ≤ 107) — the numbers that are multiplied to produce the denominator.

Output

Print the answer to the problem in the form, similar to the form of the input data. The number of values in the sets you print nout, moutmust satisfy the inequality 1 ≤ nout, mout ≤ 105, and the actual values in the sets aout, i and bout, i must satisfy the inequality 1 ≤ aout, i, bout, i ≤ 107.

Separate the values in the lines by spaces. The printed fraction must be reduced, that is, there mustn't be such integer x (x > 1), that the numerator and the denominator of the printed fraction are divisible by x. If there are several matching answers, print any of them.

Sample test(s)
input
3 2
100 5 2
50 10
output
2 3
2 1
1 1 1
input
4 3
2 5 10 20
100 1 3
output
1 1
20
3
Note

In the first test sample the numerator equals 1000, the denominator equals 500. If we reduce fraction 1000/500 by the greatest common divisor of the numerator and the denominator (by 500), we obtain fraction 2/1.

In the second test sample the numerator equals 2000, the denominator equals 300. If we reduce fraction 2000/300 by the greatest common divisor of the numerator and the denominator (by 100), we obtain fraction 20/3.


题意:给你两个集合的数,目的是化简,如果一个一个的拆成质因子然后在自由组合的话可能会超,所以用先求所有数的最大质因子,然后能整除的就化简掉,所以我先把两个集合中的元素变成质数的幂分别存在另一个数组里面,然后用一个集合的元素分别和另一个集合中幂的形式的元素进行比较,如果存在就消去,那个质数的幂就减一,然后输出1,否则输出这些不能消去的质数的积。

#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 10000010
using namespace std;
int prime[maxn];
int a[maxn],b[maxn];
int aa[maxn],bb[maxn];
int main()
{
    memset(prime,0,sizeof(prime));
    memset(aa,0,sizeof(aa));
    memset(bb,0,sizeof(bb));
    for(int i=2;i<=maxn;i++)
        if(!prime[i])
        {
            prime[i]=i;
            for(int j=2*i;j<=maxn;j+=i)
                prime[j]=i;//求最小质因子
        }
    int m,n;
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&b[i]);
    printf("%d %d\n",m,n);
    for(int i=0;i<m;i++)
        for(int j=a[i];j>1;j/=prime[j])
            aa[prime[j]]++;
    for(int i=0;i<n;i++)
        for(int j=b[i];j>1;j/=prime[j])
            bb[prime[j]]++;
    int ans;
    for(int i=0;i<m;i++)
    {
        ans=1;
        for(int j=a[i];j>1;j/=prime[j])
            if(bb[prime[j]]>0)
                bb[prime[j]]--;
            else
                ans*=prime[j];
        if(i==0)
            printf("%d",ans);
        else
            printf(" %d",ans);
    }
    printf("\n");
    for(int i=0;i<n;i++)
    {
        ans=1;
        for(int j=b[i];j>1;j/=prime[j])
            if(aa[prime[j]]>0)
                aa[prime[j]]--;
            else
                ans*=prime[j];
        if(i==0)
            printf("%d",ans);
        else
            printf(" %d",ans);
    }
    printf("\n");
    return 0;
}


D. Olympiad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A boy named Vasya has taken part in an Olympiad. His teacher knows that in total Vasya got at least x points for both tours of the Olympiad. The teacher has the results of the first and the second tour of the Olympiad but the problem is, the results have only points, no names. The teacher has to know Vasya's chances.

Help Vasya's teacher, find two numbers — the best and the worst place Vasya could have won. Note that the total results' table sorts the participants by the sum of points for both tours (the first place has the participant who has got the most points). If two or more participants have got the same number of points, it's up to the jury to assign places to them according to their choice. It is guaranteed that each participant of the Olympiad participated in both tours of the Olympiad.

Input

The first line contains two space-separated integers n, x (1 ≤ n ≤ 105; 0 ≤ x ≤ 2·105) — the number of Olympiad participants and the minimum number of points Vasya earned.

The second line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the participants' points in the first tour.

The third line contains n space-separated integers: b1, b2, ..., bn (0 ≤ bi ≤ 105) — the participants' points in the second tour.

The participants' points are given in the arbitrary order. It is guaranteed that Vasya was present in the Olympiad — there are two integersi, j (1 ≤ i, j ≤ n) such, that ai + bj ≥ x.

Output

Print two space-separated integers — the best and the worst place Vasya could have got on the Olympiad.

Sample test(s)
input
5 2
1 1 1 1 1
1 1 1 1 1
output
1 5
input
6 7
4 3 5 6 4 4
8 6 0 4 3 4
output
1 5
Note

In the first text sample all 5 participants earn 2 points each in any case. Depending on the jury's decision, Vasya can get the first (the best) as well as the last (the worst) fifth place.

In the second test sample in the best case scenario Vasya wins again: he can win 12 points and become the absolute winner if the total results' table looks like that — {4:8, 6:4, 3:6, 4:4, 4:3, 5:0}.

In this table all participants are sorted by decreasing points and we can see how much a participant earned in the first and in the second tour.

In the worst case scenario Vasya can get the fifth place if the table looks like that — {4:8, 4:6, 6:4, 5:4, 4:3, 3:0}, and he earned 4 and 3 points in the first and second tours, correspondingly.


题意:已知某人参加了比赛,已知2场比赛的n个人的分数,现在要求其最好的排名和最坏的排名,已保证所有人的分数都会大于等于x

分析:最好的排名当然是第一名,至于最坏的,贪心做法,某人肯定是分数和大于等于x里面分数最低的,先将两场比赛的分数进行排序,一个升序,一个降序,贪心。

#include <stdio.h>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int a[100001],b[100001];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&b[i]);
    sort(a+1,a+n+1);
    sort(b+1,b+n+1,cmp);
    int ans=1;
    for(int i=1;i<=n;i++)
        if(a[i]+b[ans]>=k)
            ans++;
    printf("1 %d",ans-1);
    return 0;
}


E. Decoding Genome
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently a top secret mission to Mars has taken place. As a result, scientists managed to obtain some information about the Martian DNA. Now we know that any Martian DNA contains at most m different nucleotides, numbered from 1 to m. Special characteristics of the Martian DNA prevent some nucleotide pairs from following consecutively in this chain. For example, if the nucleotide 1 and nucleotide 2 can not follow consecutively in the Martian DNA, then the chain of nucleotides [1, 2] is not a valid chain of Martian DNA, but the chain of nucleotides [2, 1] can be a valid chain (if there is no corresponding restriction). The number of nucleotide pairs that can't follow in the DNA chain consecutively, is k.

The needs of gene research required information about the quantity of correct n-long chains of the Martian DNA. Your task is to write a program that will calculate this value.

Input

The first line contains three space-separated integers n, m, k (1 ≤ n ≤ 10151 ≤ m ≤ 520 ≤ k ≤ m2).

Next k lines contain two characters each, without a space between them, representing a forbidden nucleotide pair. The first character represents the first nucleotide in the forbidden pair, the second character represents the second nucleotide.

The nucleotides with assigned numbers from 1 to 26 are represented by English alphabet letters from "a" to "z" (1 is an "a", 2 is a "b",..., 26 is a "z"). Nucleotides with assigned numbers from 27 to 52 are represented by English alphabet letters from "A" to "Z" (27 is an "A", 28 is a "B", ..., 52 is a "Z").

It is guaranteed that each forbidden pair occurs at most once in the input. It is guaranteed that nucleotide's numbers in all forbidden pairs cannot be more than m. Note that order is important in nucleotide pairs.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cincout streams or the %I64dspecifier.

Output

Print a single integer — the sought number modulo 1000000007 (109 + 7).

Sample test(s)
input
3 3 2
ab
ba
output
17
input
3 3 0
output
27
input
2 1 1
aa
output
0
Note

In the second test case all possible three-nucleotide DNAs are permitted. Each nucleotide can take one of three values, thus in total there are 27 distinct three nucleotide DNAs.

In the third test sample we cannot make any DNA of two nucleotides — the only possible nucleotide "a" cannot occur two times consecutively.


题意:给你 n m k  表示m个长度为n的矩阵串,现在告诉你k种,不能相邻的情况,现在问这种排列会有多少种

分析:先转换成矩阵,然后就是矩阵的快速幂问题。


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

typedef struct
{
    long long matrix[55][55];
}Matrix;

long long mod=1000000007;
int m;
Matrix Mul(Matrix A,Matrix B)
{
    Matrix C;
    for(int i=0;i<m;i++)
        for(int j=0;j<m;j++)
        {
            C.matrix[i][j]=0;
            for(int k=0;k<m;k++)
                C.matrix[i][j]=(C.matrix[i][j]+(A.matrix[i][k]*B.matrix[k][j])%mod)%mod;
        }
    return C;
}
int num(char c)
{
    if(c>='a'&&c<='z')
        return c-'a';
    else
        return c-'A'+26;
}
Matrix A,B;
int main()
{
    char str[3];
    long long n,k;
    scanf("%I64d%d%I64d",&n,&m,&k);
    for(int i=0;i<m;i++)
    {
        B.matrix[i][0]=1;
        for(int j=0;j<m;j++)
        {
            A.matrix[i][j]=1;
        }
    }
    while(k--)
    {
        scanf("%s",str);
        A.matrix[num(str[0])][num(str[1])]=0;
    }
    n--;
    while(n)
    {
        if(n&1) B=Mul(A,B);
        A=Mul(A,A);
        n>>=1;
    }
    __int64 ans=0;
    for(int i=0;i<m;i++)
    {
        ans=(ans+B.matrix[i][0])%mod;
    }
    printf("%I64d\n",ans);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值