Codeforces Round 917 (Div. 2) 前三题

A. Least Product

题目链接

题目大意

给定一个数组,可以对其中任意元素操作任意次,操作规则:可以用0-a[i]之间的任意数替换a[i],,求使得数组所有元素之积最小的操作次数。

题解

1.积是正数(全正,偶数个负数)时,将其改为0

2.积是负数(奇数个负数)时,不用操作,因为现在已经是最小了

3.有0出现时,不用操作,0就是最小值

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

int main()
{
   int t;cin>>t;
   while(t--)
   {
    int n;
    cin>>n;
    int nf=0,nz=0,n0=0;
    for(int i=1;i<=n;i++)
   {
    	int x;
    	cin>>x;
    	if(x<0)  nf++;
    	if(x>0) nz++;
    	if(x==0) n0++;
    }
    if(n0>0) printf("%d\n",0);
    else
    {
       if(nf%2==0)
       {
    	 printf("%d\n%d %d\n",1,1,0);
    	 continue;
       }
       printf("%d\n",0);
    }
    	
   }	
	
	
	return 0;
}

B. Erase First or Second Letter

题目链接

题目大意

给定一个字符串,每次可以删除字符串第一个字母和第二个字母,可操作任意次。求不同的非空字符串个数。

题解

假设给定一个字符串aabc,那么所有符合条件的字符串为:aabc,abc,bc,c,b,ac,a

可以发现,每个答案都由两部分组成:前缀中的一个字母+后缀,那么枚举每个后缀之前不同的字母个数,再累加即可

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
//#include<queue>
#include<map>
//#include<vector>
//#include<set>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

typedef pair<int,int>PII;

	
string s;
map<char,int>mp;
ll ans,res;
int main()
{
	int t;cin>>t;
	while(t--)
	{
		int n;cin>>n;
		cin>>s;
		mp.clear();
		ans=0,res=0;
		for(auto c:s)
		{
			if(mp[c]==0) res++;
			mp[c]++;
			ans+=res;
		}
		cout<<ans<<endl;
	}
	
	
	return 0;
}

C. Watering an Array

题目链接

题目大意

给定数组a和数组b,其中b数组会循环出现,以及天数d,在接下来d天的第i天必须执行以下一项操作,1.将a数组中下标小于b[i]的元素加一。2.获得a数组中值和下标相等的元素个数的分数,然后将a数组全部置0。求可能获得的最大分数。

题解

可以发现除了第一次执行操作2会获得值和下标相等元素个数的分数之外,我们之后每次执行操作2最多只能获得1分(执行完操作2之后,每次执行操作1最多有一个满足条件的元素),那么我们可以一天执行操作1,下一天执行操作2,每两天获得1分,这是第一种保底的方法。第二种方法则是先执行i天操作1,让满足条件的元素尽可能变多,然后执行操作2,之后就和第一种方法一样每两天获得1分。那么再比较两种方法,取最大值即可

代码

// Problem: C. Watering an Array
// Contest: Codeforces - Codeforces Round 917 (Div. 2)
// URL: https://codeforces.com/contest/1917/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
//#include<queue>
//#include<map>
//#include<vector>
//#include<set>
//#include<deque>
//#include<bitset>
//#include<functional>

#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

typedef pair<int,int>PII;
const int N1=2010;
const int N=1e5+10;
int a[N1],b[N];
int n,k,d;
int ans,res;
	
	
int main()
{
	int t;cin>>t;
	while(t--)
	{
		res=0,ans=0;
		cin>>n>>k>>d;
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=0;i<k;i++) cin>>b[i];
		
		//第一种吃保底获得的分数
		for(int i=1;i<=n;i++) 
		{
			if(a[i]==i) res++;//第一次执行操作2
		}
		res+=(d-1)/2;//每两天获得1分
		
		//第二种,先执行i次操作1,再执行操作2
		for(int i=1;i<=min(2*N1,d-1);i++)//最少要执行d-1次操作1,最多执行4000次
		{
				
			ans=(d-i-1)/2;//执行完i次操作1和1次操作2之后每两天获得1分
			for(int j=1;j<=b[(i-1)%k];j++) a[j]++;//将a数组中前b[j]个元素+1
			for(int j=1;j<=n;j++) //第一次执行操作2可以获得的分数
			{
				if(a[j]==j) ans++;
			}
			
			res=max(ans,res);//取最大值
		}
		   cout<<res<<endl;
		
	}
	
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值