1. 程式人生 > >浪在ACM 集訓隊第八次測試賽

浪在ACM 集訓隊第八次測試賽

浪在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;
}