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:
- Find the number that goes k-th in the current sequence and add the same number to the end of the sequence;
- 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.
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.
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.
3 2 3 1 1
1
3 1 3 1 1
-1
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;
}
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.
The first line contains three space-separated integers n, m and k (1 ≤ n, m ≤ 1000, 1 ≤ 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 xi, yi 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.
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.
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
8 9 6
2 3 3 1 2 4 3 1 5 c 2 1 r 1 2 g 1 3
5
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;
}
#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;
}
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.
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.
Print two space-separated integers — the best and the worst place Vasya could have got on the Olympiad.
5 2 1 1 1 1 1 1 1 1 1 1
1 5
6 7 4 3 5 6 4 4 8 6 0 4 3 4
1 5
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;
}
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.
The first line contains three space-separated integers n, m, k (1 ≤ n ≤ 1015, 1 ≤ m ≤ 52, 0 ≤ 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 cin, cout streams or the %I64dspecifier.
Print a single integer — the sought number modulo 1000000007 (109 + 7).
3 3 2 ab ba
17
3 3 0
27
2 1 1 aa
0
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;
}