gym102035

E New Max
n=400暴力求解 k可以为0 0次操作旧的最大值等于新的最大值
或者二分lower_bound
Kamal 'D has an array of size N.

He can do at most K operations on it, in each operation he can choose any element in the array and change its value to any other value X, (1≤x≤400).

Kamal 'D wants to know if he can make the maximum element in the new array equal to M.
输入
The first line of input will contain 3 integers N, M and K (1≤N≤400) (0≤K≤400) (1≤M≤400) which are the size of the array, the new maximum value and the number of operations.

The second line will contain n integers, the ith one is ai which is the ith element in array a, (1≤ai≤400)
输出
For each test case, print “YES” if Kamal 'D can make the maximum element equals to M, print “NO” otherwise .
Input
6 4 3
1 5 3 4 2 6
Output
YES
Input
6 4 2
5 5 5 5 1 2
Output
NO

1.暴力

#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
typedef long long ll;
typedef pair pii;
typedef pair pll;
const int maxn=1e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f; 
const ll mod=1e9+7;
ll gcd(ll a,ll b){return b!=0?gcd(b,a%b):a;}
int a[405];
int main()
{
	std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
    int n,m,k,ans=0,maxx=0;
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	 } 
	for(int i=1;i<=n;i++)
	{
		if(a[i]>m)
			ans++;//比新最大值m大 全部变成m新的最大值 
		maxx=max(maxx,a[i]);
	}
	if(ans>k  || (k==0 && maxx!=m))//反面     k=0 旧的最大值==新的最大值 无论数组多长
		puts("NO");
	else
		puts("YES");
	return 0;
} 

2.二分

#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
typedef long long ll;
typedef pair pii;
typedef pair pll;
const int maxn=1e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f; 
const ll mod=1e9+7;
ll gcd(ll a,ll b){return b!=0?gcd(b,a%b):a;}
int a[405];
int main()
{
	std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
    int n,m,k;
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	 } 
	int p=lower_bound(a+1,a+n+1,m)-a;//不存在 越界a数组开的大 
	
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>m)
			ans++;
	}//ans若为0个 无m还得改为m  1 1 1 1  m=2 n=4 k=0
	if(a[p]

I
同余
回文串对应位置差值取gcd即可
找到任意一个数和他对应数字的因子的差,模差值同余
然后枚举它的因子作为mod能否形成回文串/直接对于所有数字和其对应的数字的差,取gcd即可

Abu Tahun loves palindromes.

An array is palindrome if it reads the same backwards and forwards, for example arrays {1}, {1,1,1}, {1,2,1} , {1,3,2,3,1} are palindrome, but arrays {11,3,5,11}, {1,12} are not.

Abu Tahun has an array of n integers A. He wants his array to be palindrome. He can choose an integer m, then change the value of all Ai (1≤i≤n) to (Ai mod m).

what is the maximum value of m he can choose, such that the array becomes palindrome?
input
The first line of input contains a single integer n (1≤n≤105)
The second line contains integers A1,A2,…,An (1≤Ai≤109)
output
Print the maximum value of m Abu Tahun can choose, if m is arbitrarily large print -1.

input
4
1 1 1 1
Output
-1

4
1 2 3 4

1

3
8 12 16

8

#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
typedef long long ll;
typedef pair pii;
typedef pair pll;
const int maxn=1e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f; 
const ll mod=1e9+7;
ll gcd(ll a,ll b){return b!=0?gcd(b,a%b):a;}
int n;
ll a[maxn];
int check(int n)//判断回文串
{
	int flag=1;
	for(int i=1;i<=n/2;i++)
	{
		if(a[i]!=a[n-i+1])
			flag=0;
	}
	return flag;
}
int main()
{
	std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    	cin>>a[i];
	}
	if(check(n))
	{
		puts("-1");
		return 0;
	}
	ll ans=0;
	for(int i=1;i<=n/2;i++)
	{
		if(a[i]==a[n-i+1])//此对称位置可以任意取m 
			continue;
		ans=gcd(ans,abs(a[i]-a[n-i+1]));//对应位置差值 取模差值则同余
	}
	cout<

J Negative effect 前缀和+特判
题意枚举替换正整数数组每一个位置为负数m 替换后求所有子数组和最大值
注意特判 n=1情况 前缀后缀为0 ans=max(0,0+m负数)=0 而一个数替换后 和应为m 前缀和爆int 用ll
Abu tahun has an array a of n positive integers, and a negative integer m.

Abu Tahun defines the beauty of the array as the maximum summation of a sub-array in that array.

A sub-array of an array is a sequence of consecutive integers in the array.

For example :

The beauty of array {1,−5,7} equals to 7.

And the beauty of array {6,−5,7} equals to 8.

in the second example we have the sub-arrays :

{6} sum = 6, {−5} sum = -5, {7} sum = 7, {6,−5} sum = 1, {−5,7} sum = 2, {6,−5,7} sum = 8.

You should tell Abu Tahun, for every index if he replaced the number in that index with m, what is the beauty of the resulting array.
Input
First line of input contains two integers n and m (1≤n≤105) (−109≤m<0)
The second line will contain n integers, the ith one is ai, which is the ith element in the array, (1≤ai≤109).

Output
You should print n integers, the ith one should be the beauty of the given array after replacing the ith integer with m.
Input
4 -3
1 2 3 4
Output
9 7 4 6

#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
typedef long long ll;
typedef pair pii;
typedef pair pll;
const int maxn=2e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f; 
const ll mod=1e9+7;
ll gcd(ll a,ll b){return b!=0?gcd(b,a%b):a;}
ll a[maxn],pre[maxn],suf[maxn];//n=1e5 ai=1e9前缀和 1e14 
int main()
{
	std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
    	cin>>a[i];
	}
	pre[0]=0;
	for(int i=1;i<=n;i++)
	{
		pre[i]=pre[i-1]+a[i]; 
	}
	suf[n+1]=0;
	for(int i=n;i>=1;i--)
	{
		suf[i]=suf[i+1]+a[i];
	}//预处理
	ll ans=0;
	if(n==1)//m=-3 n=1 代替自身 -3 前缀后缀为0,ans=0 ans=max(0,0+-3)=0 而应-3 
	{
		cout<

B STL map 求余数组num存不下
只有当剩下1个 或 每一对差值求余m余数为0才结束 即剩下的同余

As Ayoub was going home from Mahmoud’s house, he remembered that he forgot his hard disk, which contains the solutions to n problems from the JU Flash contest. The ith problem has size ai.

While panicking, he remembered that he installed a software that prevented the user from copying any files and only allowed the files to be cut from the hard disk in any order the user wants.

As a security measure, the software will stop the transfer if the number of remaining files is 1, or the absolute difference between the sizes ofevery pair of problems left on the hard disk is divisible by m.

Ayoub knows the permutation b, which represents the order of the problems Mahmoud will attempt to cut from the hard disk.

Since Ayoub is a bad programmer, he asks for your help to tell him how many problems Mahmoud will steal until the transfer stops.

Input
The first line contains two integer n and m (1≤n≤105) (1≤m≤109)
The second line contains integers a1,a2,…,an (1≤ai≤109)
The third line contains a permutation b , b1,b2,…,bn (1≤bi≤105)

Output
print exactly one integer − the number of codes Mahmoud can steal

Input
3 2
2 1 4
2 1 3
Output
1
Input
3 2
2 1 4
3 2 1
Output
2
Input
5 2
7 9 5 33 77
5 3 4 2 1
Output
0

#in]clude
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
typedef long long ll;
typedef pair pii;
typedef pair pll;
const int maxn=1e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f; 
const ll mod=1e9+7;
ll gcd(ll a,ll b){return b!=0?gcd(b,a%b):a;}
int a[maxn],b[maxn];
map mm;//余数值出现次数 0-1e9-1 1e5次 
int main()
{

	std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    	cin>>a[i],a[i]=a[i]%m;
    for(int i=1;i<=n;i++)
    	cin>>b[i];
//剩下的数 任意一对之差能被m整除 (a-b)%m==0 a,b求余m有相同的余数 同余 num数组存不下1-1e9 
	int ans=-1;
	for(int i=1;i<=n;i++)
	{
		//余数种类只剩下1个或1种 按照序列b顺序 
		if(mm.size()==1)
		{
			ans=i-1;
			break;
		 }
		 mm[a[b[i]]]--;
		 if(mm[a[b[i]]]==1)
		 	mm.erase(a[b[i]]);
		
	}
	
	/*
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		if(mm.find(a[b[i]])==mm.end())
			mm[a[b[i]]]=1,sum++;
		else
			mm[a[b[i]]]++;
	}
	for(int i=1;i<=n;i++)
	{
		if(sum==1)
		{
			ans=i-1;
			break;
		}
		
	//	if(sum==1 && ans==-1) 第一次更新 不用break 
	//		ans=i-1;
	
		mm[a[b[i]]]--;
		if(mm[a[b[i]]]==0)
			 sum--;
	}*/
	cout<

你可能感兴趣的:(训练)