Codeforces Round #725 (Div. 3)

A. Stone Game

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp is playing a new computer game. This game has nn stones in a row. The stone on the position ii has integer power aiai. The powers of all stones are distinct.

Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more.

Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal.

For example, if n=5n=5 and a=[1,5,4,3,2]a=[1,5,4,3,2], then Polycarp could make the following moves:

  • Destroy the leftmost stone. After this move a=[5,4,3,2]a=[5,4,3,2];
  • Destroy the rightmost stone. After this move a=[5,4,3]a=[5,4,3];
  • Destroy the leftmost stone. After this move a=[4,3]a=[4,3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game.

Please note that in the example above, you can complete the game in two steps. For example:

  • Destroy the leftmost stone. After this move a=[5,4,3,2]a=[5,4,3,2];
  • Destroy the leftmost stone. After this move a=[4,3,2]a=[4,3,2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game.

Input

The first line contains an integer tt (1≤t≤1001≤t≤100). Then tt test cases follow.

The first line of each test case contains one integer nn (2≤n≤1002≤n≤100) — the number of stones.

The second line contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the power of the stones.

Output

For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power.

 

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--) 
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int t,n;
int num[105];
int main()
{
	ios
	cin>>t;
	
	while(t--)
	{
		int minn=9999,maxi,mini,maxn=-1;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>num[i];
			if(num[i]>maxn)maxn=num[i],maxi=i;
			if(num[i]<minn)minn=num[i],mini=i;
		}
		int dismax=min(min(max(maxi,mini),max(n-maxi+1,n-mini+1)),min((maxi+n-mini+1),(mini+n-maxi+1)));
		cout<<dismax<<endl;
	}
	return 0;
}

签到题,公式推出来就行。

 

 

B. Friends and Candies

Polycarp has nn friends, the ii-th of his friends has aiai candies. Polycarp's friends do not like when they have different numbers of candies. In other words they want all aiai to be the same. To solve this, Polycarp performs the following set of actions exactly once:

  • Polycarp chooses kk (0≤k≤n0≤k≤n) arbitrary friends (let's say he chooses friends with indices i1,i2,…,iki1,i2,…,ik);
  • Polycarp distributes their ai1+ai2+…+aikai1+ai2+…+aik candies among all nn friends. During distribution for each of ai1+ai2+…+aikai1+ai2+…+aik candies he chooses new owner. That can be any of nn friends. Note, that any candy can be given to the person, who has owned that candy before the distribution process.

Note that the number kk is not fixed in advance and can be arbitrary. Your task is to find the minimum value of kk.

For example, if n=4n=4 and a=[4,5,2,5]a=[4,5,2,5], then Polycarp could make the following distribution of the candies:

  • Polycarp chooses k=2k=2 friends with indices i=[2,4]i=[2,4] and distributes a2+a4=10a2+a4=10 candies to make a=[4,4,4,4]a=[4,4,4,4] (two candies go to person 33).

Note that in this example Polycarp cannot choose k=1k=1 friend so that he can redistribute candies so that in the end all aiai are equal.

For the data nn and aa, determine the minimum value kk. With this value kk, Polycarp should be able to select kk friends and redistribute their candies so that everyone will end up with the same number of candies.

Input

The first line contains one integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

The first line of each test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1040≤ai≤104).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case output:

  • the minimum value of kk, such that Polycarp can choose exactly kk friends so that he can redistribute the candies in the desired way;
  • "-1" if no such value kk exists.

题意为把任意数取出加到其他数里,至少要选择多少个数

思维题,把大于平均数的数都分到不大于平均数的数就行。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--) 
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int t,n;
int num[200005];
 
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
	ios
	cin>>t;
	while(t--)
	{
		cin>>n;
		int tmp=-1,sum=0,flag=0,ans=0,k=1;
		for(int i=1;i<=n;i++)
		{
			cin>>num[i];
			if(tmp==-1)tmp=num[i];
			else if(tmp!=num[i])flag=1;
			tmp  = num[i];
			sum += num[i];
		}
		int avg=sum/n;
		if(!flag)
		{
			cout<<0<<endl;
			continue;
		}
		if(sum%n!=0)
		{
			cout<<-1<<endl;
			continue;
		}
		int cnt=0;
		for(int i=1;i<=n;i++)
		{
			if(num[i]>avg)
			{
				cnt++;
			}
		}
		cout<<cnt<<endl;
	}
	return 0;
}

 

C. Number of Pairs

You are given an array aa of nn integers. Find the number of pairs (i,j)(i,j) (1≤i<j≤n1≤i<j≤n) where the sum of ai+ajai+aj is greater than or equal to ll and less than or equal to rr (that is, l≤ai+aj≤rl≤ai+aj≤r).

For example, if n=3n=3, a=[5,1,2]a=[5,1,2], l=4l=4 and r=7r=7, then two pairs are suitable:

  • i=1i=1 and j=2j=2 (4≤5+1≤7 4≤5+1≤7);
  • i=1i=1 and j=3j=3 (4≤5+2≤7 4≤5+2≤7).

Input

The first line contains an integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

The first line of each test case contains three integers n,l,rn,l,r (1≤n≤2⋅1051≤n≤2⋅105, 1≤l≤r≤1091≤l≤r≤109) — the length of the array and the limits on the sum in the pair.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

It is guaranteed that the sum of nn overall test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output a single integer — the number of index pairs (i,j)(i,j) (i<ji<j), such that l≤ai+aj≤rl≤ai+aj≤r.

 

题目意思大致为给你一个数列,让你求 l ≤ai+aj ≤r 的数对有多少对(要求i<j)

我们可以转化为两个问题,找ai+aj<=r的数对有多少对 ai+aj<l的数对有多少对

然后将他们相减就是每个区间有多少对数。

 

如果暴力开始做,枚举左端点,枚举右端点,将公式转换为 l-a[i]<=a[j]<=r-a[i] ,左端点为i,右端点为j,注意到每个数都被统计了两次,将数除以二可以得到最终结果。

那么我们可以得到二分的思路,枚举左端点,二分符合要求的l-a[i]<=a[j]<=r-a[i]的右区间端点。注意特判数对的两个数都是他自己的情况,即 l-a[j]<=a[j]  2a[j]>=l ,2a[j]<=r时,要对ans去重,减掉其中一种情况。最终得数/2即可得到结果。

差点忘了,不要忘记开long long。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn=200005;
ll T,n,lll,rrr,num[maxn];
bool check(int a,int l,int r,int i)
{
	if(num[i]<=r-num[a])return 1;
	return 0;
}
bool checkk(int a,int l,int r,int i)
{
	if(num[i]<l-num[a])return 1;
	return 0;
}
int main()
{
	ios
	cin>>T;
	while(T--)
	{
		ll cnt=0;
		cin>>n>>lll>>rrr;
		for(int i=1;i<=n;i++)
		{
			cin>>num[i];
		}
		sort(num+1,num+n+1);
		for(int i=1;i<=n;i++)
		{
			//cout<<i<<' ';
			ll l=1,r=n,mid,ans=0;
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(check(mid,lll,rrr,i))
				{
					ans=mid;
					l=mid+1;
				}
				else r=mid-1;
			}
			cnt+=ans;
			//cout<<ans<<" ";
			//cout<<'a'<<num[ans]<<endl;
			l=1,r=n,mid,ans=0;
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(checkk(mid,lll,rrr,i))
				{
					ans=mid;
					l=mid+1;
				}
				else r=mid-1;
			}
			cnt-=ans;
			//cout<<ans<<" "<<endl;
			//cout<<ans-i<<' ';cout<<endl;
			if(num[i]*2>=lll&&num[i]*2<=rrr)cnt--;
		}
		cout<<cnt/2<<endl;
	}
	return 0;
}

 

D. Another Problem About Dividing Numbers

input

standard input

output

standard output

You are given two integers aa and bb. In one turn, you can do one of the following operations:

  • Take an integer cc (c>1c>1 and aa should be divisible by cc) and replace aa with acac;
  • Take an integer cc (c>1c>1 and bb should be divisible by cc) and replace bb with bcbc.

Your goal is to make aa equal to bb using exactly kk turns.

For example, the numbers a=36a=36 and b=48b=48 can be made equal in 44 moves:

  • c=6c=6, divide bb by cc ⇒⇒ a=36a=36, b=8b=8;
  • c=2c=2, divide aa by cc ⇒⇒ a=18a=18, b=8b=8;
  • c=9c=9, divide aa by cc ⇒⇒ a=2a=2, b=8b=8;
  • c=4c=4, divide bb by cc ⇒⇒ a=2a=2, b=2b=2.

For the given numbers aa and bb, determine whether it is possible to make them equal using exactly kk turns.

Input

The first line contains one integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

Each test case is contains three integers aa, bb and kk (1≤a,b,k≤1091≤a,b,k≤109).

Output

For each test case output:

  • "Yes", if it is possible to make the numbers aa and bb equal in exactly kk turns;
  • "No" otherwise.

The strings "Yes" and "No" can be output in any case.

问给你两个数,问你他们除k次能不能让他们相同(除数不能为1)

分解质因数,如果a能整除B或者B能整除A,那k=1就可以。

k>2时分解质因数,若k小于他们质因数的和,则可以。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--) 
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int a,b,k,t;
int main()
{
	ios
	cin>>t;
	while(t--)
	{
		cin>>a>>b>>k;
		if(k==1)
		{
			if((a%b==0||b%a==0)&&a!=b)cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
		else
		{
			int cnta=0,cntb=0;
			for(int i=2;i*i<=a;i++)
			{
				while(a%i==0)
				{
					cnta++;
					a/=i;
				}
			}
			if(a>1)cnta++;
			//cout<<cnta<<endl;
			for(int i=2;i*i<=b;i++)
			{
				while(b%i==0)
				{
					cntb++;
					b/=i;
				}
			}
			if(b>1)cntb++;
			if(k<=(cnta+cntb))cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}

                                           F. Interesting Function

You are given two integers ll and rr, where l<rl<r. We will add 11 to ll until the result is equal to rr. Thus, there will be exactly r−lr−l additions performed. For each such addition, let's look at the number of digits that will be changed after it.

For example:

  • if l=909l=909, then adding one will result in 910910 and 22 digits will be changed;
  • if you add one to l=9l=9, the result will be 1010 and 22 digits will also be changed;
  • if you add one to l=489999l=489999, the result will be 490000490000 and 55 digits will be changed.

Changed digits always form a suffix of the result written in the decimal system.

Output the total number of changed digits, if you want to get rr from ll, adding 11 each time.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

Each test case is characterized by two integers ll and rr (1≤l<r≤1091≤l<r≤109).

Output

For each test case, calculate the total number of changed digits if you want to get rr from ll, adding one each time.

注意到每个100会让数位变化111次,1000会让数位变化1111次,则枚举每个位数,加上所对应的变化次数即可

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--) 
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn=200005;
int T,n,lll,rrr,num[maxn];
bool check(int a,int l,int r,int i)
{
	if(num[i]<=r-num[a])return 1;
	return 0;
}
bool checkk(int a,int l,int r,int i)
{
	if(num[i]>=l-num[a])return 1;
	return 0;
}
int main()
{
	ios
	cin>>T;
	int cnt=0;
	while(T--)
	{
		cin>>n>>lll>>rrr;
		for(int i=1;i<=n;i++)
		{
			cin>>num[i];
		}
		sort(num+1,num+n+1);
		for(int i=1;i<=n;i++)
		{
			int l=i,r=n,mid,ans=0;
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(check(mid,lll,rrr,i))
				{
					ans=mid;
					r=mid-1;
				}
				else l=mid+1;
			}
			cnt+=ans-i;
			
			l=i,r=n,mid,ans=0;
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(check(mid,lll,rrr,i))
				{
					ans=mid;
					l=mid+1;
				}
				else r=mid-1;
			}
			cnt-=ans-i;
		}
		cout<<cnt<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值