浪在ACM 集训队第八次测试赛
A和B题没a的请自行回顾补题
C. Minimizing the String
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
You are given a string s consisting of n lowercase Latin letters.
You have to remove at most one (i.e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.
String s=s1s2…sn is lexicographically smaller than string t=t1t2…tm if n<m and s1=t1,s2=t2,…,sn=tn or there exists a number p such that p≤min(n,m) and s1=t1,s2=t2,…,sp−1=tp−1 and sp<tp.
For example, “aaa” is smaller than “aaaa”, “abb” is smaller than “abc”, “pqr” is smaller than “z”.
Input
The first line of the input contains one integer n (2≤n≤2⋅105) — the length of s.
The second line of the input contains exactly n lowercase Latin letters — the string s.
Output
Print one string — the smallest possible lexicographically string that can be obtained by removing at most one character from the string s.
Examples
input
3
aaa
output
aa
input
5
abcda
output
abca
Note
In the first example you can remove any character of s to obtain the string “aa”.
In the second example “abca” < “abcd” < “abcda” < “abda” < “acda” < “bcda”.
解析
即求该字符串最长可以不连续的上升序列,当s[i]<s[i-1]时,就令t=i-1,即将破坏递增序列的字母删除.如果没有,就把最后一个删除.
证明不会
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;
char mp[200010];
int n,t=0;
int main()
{
cin >> n >> mp;
for (int i = 1; i < n; i++)
if (mp[i] < mp[i - 1]) {
t = i - 1;
break;
}
for (int i = 0; i < t; i++)
cout << mp[i];
for (int i = t + 1; i < n; i++)
cout << mp[i];
cout << endl;
return 0;
}
D. Divisor Subtraction
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
You are given an integer number n. The following algorithm is applied to it:
if n=0, then end algorithm;
find the smallest prime divisor d of n;
subtract d from n and go to step 1.
Determine the number of subtrations the algorithm will make.
Input
The only line contains a single integer n (2≤n≤1010).
Output
Print a single integer — the number of subtractions the algorithm will make.
Examples
input
5
output
1
input
4
output
2
Note
In the first example 5 is the smallest prime divisor, thus it gets subtracted right away to make a 0.
In the second example 2 is the smallest prime divisor at both steps.
解析
题意很坑的一道题.
1.如果n=0则结束,输出计算次数,否则进行2
2.寻找n的最小质因数d(翻译总会出错,说成最小的素数)
3.用n减去d,跳回1
一开始想用线性筛,1010,哦哄,mle,ce…
如果这个数是个偶数,那么最小的质因数必为2,n-2后,依然是偶数,然后持续-2.
所以该数为偶数时,输出n/2
如果这个数为素数,那么最小的质因数必为其本身,于是,输出1
如果这个数为合数,那么减去它的最小质因子后,就变成了偶数~~(为啥,我不知道)~~,然后就变成了第一种情况,输出(n-i)/2+1
于是简短代码…
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;
int main()
{
long long n;
cin >> n;
for(long long i=2;i*i<=n;i++)
if (n%i == 0) {
cout << 1 + (n - i) / 2 << endl;
return 0;
}
cout << 1 << endl;
system("pause");
return 0;
}