DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In this problem we will assume, that h(x) = x mod p. Operation a mod b denotes taking a remainder after division a by b.
However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after the i-th insertion, you should output i. If no conflict happens, just output -1.
The first line contains two integers, p and n (2 ≤ p, n ≤ 300). Then n lines follow. The i-th of them contains an integer xi (0 ≤ xi ≤ 109).
Output a single integer — the answer to the problem.
10 5 0 21 53 41 53
4
5 5 0 1 2 3 4
-1
//31 ms 0 KB #include<stdio.h> #include<string.h> int s[307],vis[307]; int main() { int p,n; while(scanf("%d%d",&p,&n)!=EOF) { memset(vis,0,sizeof(vis)); int flag=-1; for(int i=0;i<n;i++) scanf("%d",&s[i]); for(int i=0;i<n;i++) { int a=s[i]%p; if(!vis[a])vis[a]=1; else {flag=i+1;break;} } printf("%d ",flag); } return 0; }
DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter c DZY knows its value wc. For each special string s = s1s2... s|s| (|s| is the length of the string) he represents its value with a function f(s), where
Now DZY has a string s. He wants to insert k lowercase letters into this string in order to get the largest possible value of the resulting string. Can you help him calculate the largest possible value he could get?
The first line contains a single string s (1 ≤ |s| ≤ 103).
The second line contains a single integer k (0 ≤ k ≤ 103).
The third line contains twenty-six integers from wa to wz. Each such number is non-negative and doesn't exceed 1000.
Print a single integer — the largest possible value of the resulting string DZY could get.
abc 3 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
41
In the test sample DZY can obtain "abcbbc", value = 1·1 + 2·2 + 3·2 + 4·2 + 5·2 + 6·2 = 41.
//30 ms 0 KB #include<stdio.h> #include<string.h> char s[1007]; int v[26]; int main() { scanf("%s",s); int n,ans=0,maxx=-1; scanf("%d",&n); for(int i=0;i<26;i++) { scanf("%d",&v[i]); if(v[i]>maxx)maxx=v[i]; } int len=strlen(s); for(int i=0;i<len;i++) ans+=((i+1)*v[s[i]-'a']); for(int i=len+1;i<=len+n;i++) ans+=(i*maxx); printf("%d ",ans); }
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
In a single line print the answer to the problem — the maximum length of the required subsegment.
6 7 2 3 1 5 6
5
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
给你一个序列S。让你能够改变序列中的一个值。使得此序列的一个子序列为上升序列。
求此子序列最长是多少。
设r[i]为在位置i上最长的上升序列个数(从序列头開始),l[i]为在位置i上最长的上升序列的个数(从序列尾開始)。
假设S[i+1]-S[i-1]>1,那么最长可能是max(ans,r[i-1]+l[i+1]+1)
否则最长可能是max(ans,max(r[i-1],l[i+1])+1)
//46 ms 1200 KB #include<stdio.h> #include<algorithm> #define M 100007 using namespace std; int s[M],r[M],l[M]; int main() { int n,ans; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&s[i]); r[1]=1; for(int i=2;i<=n;i++) if(s[i]>s[i-1])r[i]=r[i-1]+1; else r[i]=1; l[n]=1; for(int i=n-1;i>=1;i--) if(s[i]<s[i+1])l[i]=l[i+1]+1; else l[i]=1; ans=max(r[n],l[1])+1; for(int i=2;i<=n-1;i++) if(s[i-1]<s[i+1]-1)ans=max(ans,r[i-1]+l[i+1]+1); else ans=max(ans,max(r[i-1],l[i+1])+1); if(ans>n)ans=n; printf("%d ",ans); }
版权声明:本文博客原创文章,博客,未经同意,不得转载。