Link: codeforces. comcontest447A. Large, numberedfrom0top? -? 1. He
Match link: http://codeforces.com/contest/447. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p? -? 1. He
Match link: http://codeforces.com/contest/447
A. DZY Loves Hash
Time limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
DZY has a hash tablePBuckets, numbered from 0P? -? 1. He wants to insertNNumbers, in the order they are given, into the hash table. ForI-Th numberXI, DZY will put it into the bucket numberedH(XI), WhereH(X) Is the hash function. In this problem we will assume, thatH(X)? =?X mod p. OperationA mod BDenotes taking a remainder after pisionAByB.
However, each bucket can contain in 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 afterI-Th insertion, you shoshould outputI. If no conflict happens, just output-1.
Input
The first line contains two integers,PAndN(2? ≤?P,?N? ≤? 300). ThenNLines follow.I-Th of them contains an integerXI(0? ≤?XI? ≤? 109 ).
Output
Output a single integer-the answer to the problem.
Sample test (s)
Input
10 5021534153
Output
4
Input
5 501234
Output
-1
Link: http://codeforces.com/contest/447
Find the location where hash conflicts occur for the first time.
Solution: use an array to indicate whether the elements after hash are stored. 0 indicates that this position has not been used, 1 indicates that there is a hash element in this position (that is, a conflict occurs ).
Code:
#include
#include
const int MAXN = 305;int a[MAXN], p, n, ans = -1;int main(){ bool flag = true; scanf("%d%d", &p, &n); for(int i = 0; i < n; i++) { int x; scanf("%d", &x); if(flag) { if(0 == a[x % p]) { a[x % p] = 1; } else { ans = i + 1; flag = false; } } } printf("%d\n", ans); return 0;}
B. DZY Loves Strings
Time limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
DZY loves collecting special strings which only contain lowercase letters. For each lowercase letterCDZY knows its valueWC. For each special stringS? =?S1S2...S|S| (|S| Is the length of the string) he represents its value with a functionF(S), Where
Now DZY has a stringS. He wants to insertKLowercase 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 cocould get?
Input
The first line contains a single stringS(1? ≤? |S|? ≤? 103 ).
The second line contains a single integerK(0? ≤?K? ≤? 103 ).
The third line contains twenty-six integers fromWAToWZ. Each such number is non-negative and doesn' t exceed 1000.
Output
Print a single integer-the largest possible value of the resulting string DZY cocould get.
Sample test (s)
Input
abc31 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
Output
41
Note
In the test sample DZY can obtain "abcbbc ",Value? =? 1-1? +? 2 · 2? +? 3 · 2? +? 4 · 2? +? 5 · 2? +? 6 · 2? =? 41.
Link: http://codeforces.com/contest/447/problem/ B
Add k characters to the given string to obtain the maximum weight and sum.
Solution: Find the maximum bit right, put k characters with the maximum bit right at the end of the original string, and evaluate the sum of values. Ps: The answer may exceed int. Use long.
Code:
#include
#include
using namespace std;int main(){ string s; int k, a[27], imax = -1; cin >> s >> k; for(int i = 0; i < 26; i++) { cin >> a[i]; if(a[i] > imax) { imax = a[i]; } } long long ans = 0; int len = s.length(); for(int i = 0; i < len; i++) { ans += a[s[i] - 'a'] * (i + 1); } ans += (long long)imax * k * (2 * len + k + 1) / 2; cout << ans << endl;}
C. DZY Loves Sequences
Time limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
DZY has a sequenceA, ConsistingNIntegers.
We'll call a sequenceAI,?AI? +? 1 ,?...,?AJ(1? ≤?I? ≤?J? ≤?N) A subsegment of the sequenceA. The value (J? -?I? +? 1) denotes the length of the subsegment.
Your task is to find the longest subsegmentA, 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.
Input
The first line contains integerN(1? ≤?N? ≤? (105). The next line containsNIntegersA1 ,?A2 ,?...,?AN(1? ≤?AI? ≤? 109 ).
Output
In a single line print the answer to the problem-the maximum length of the required subsegment.
Sample test (s)
Input
67 2 3 1 5 6
Output
5
Note
You can choose subsegmentA2 ,?A3 ,?A4 ,?A5 ,?A6 and change its 3rd element (that isA4) to 4.
Link: http://codeforces.com/contest/447/problem/C
Select a substring from a number to change the value of a number in the substring to a new substring, and increase the length of the new substring to the maximum.
Solution:
Splits the original array into ascending substrings and records the start and end positions and lengths of each substring. Next we will discuss the situation in several ways: 1. after two adjacent substrings change a number, they can be merged to form a new incrementing substring. the length of the adjacent three substrings is 1. After the numbers in the middle are changed, a new ascending substring can be formed. adjacent substrings cannot be merged to form new incremental substrings, but can be based on the original strings, get a new incrementing substring with a length increase of 1 (there is a number before the starting position of the substring, or there is a number after the ending position ).
Code:
#include
#include
#include using namespace std;const int MAXN = 100010;int a[MAXN];struct P{ int l, len, r;};P p[MAXN];int n;int main(){ memset(p, 0, sizeof(p)); scanf("%d", &n); int t = 0; for(int i = 0; i < n; i++) { scanf("%d", &a[i]); if(!i) { p[t].len++; p[t].l = p[t].r = i; continue; } if(a[i] <= a[i - 1]) { t++; } if(0 == p[t].len) { p[t].l = i; } p[t].len++; p[t].r = i; } int ans = p[0].len < n ? p[0].len + 1 : p[0].len; for(int i = 1; i <= t; i++) { ans = max(ans, p[i].len + 1); if(a[p[i].l] > a[p[i - 1].r - 1] + 1 || a[p[i].l + 1] > a[p[i - 1].r] + 1) { ans = max(ans, p[i].len + p[i - 1].len); } if(i >= 2 && 1 == p[i - 1].len && a[p[i].l] > a[p[i - 2].r + 1]) { ans = max(ans, p[i].len + p[i - 2].len + 1); } // printf("%d \n", p[i].len); } printf("%d\n", ans); return 0;}