Educational Codeforces Round 145 (Rated for Div. 2) A-D题解

比赛地址

A. Garland

复制代码
 1 void solve()
 2 {
 3     for(int i=1;i<=4;i++)
 4     {
 5         b[i]=a[i]=0;
 6     }
 7     int cnt=0;
 8     string t;cin>>t;
 9     set<int>st;
10     for(int i=0;i<4;i++)
11     {
12         if(!st.count(t[i]-'0'))
13         {
14             a[++cnt]=t[i]-'0';
15             b[cnt]=1;
16         }else
17         {
18             for(int j=1;j<=cnt;j++)
19             {
20                 if(a[j]==t[i]-'0')b[j]++;
21             }
22         }
23         st.insert(t[i]-'0');
24  
25     }
26     if(st.size()==1)cout<<"-1\n";
27     else if(st.size()==2)
28     {
29         if(b[1]==b[2])
30         {
31             cout<<"4\n";
32             return;
33         }else
34         {
35             cout<<"6\n";
36             return;
37         }
38     }else cout<<"4\n";
39 }
View Code
复制代码

B.Points on Plane

题意:在平面上放置n个整点,两两之间距离大于1,求出max(|xi|+|yi|),

Solution

对于ans=2的情况,放在(2,0)(0,2)(-2,0)(0,-2),(1,1)(1,-1)(-1,1)(-1,-1)以及(0,0),这样会呈现一个正方形,能容得下(2+1)*(2+1)个点

以此类推,对于ans=k的情况,能容下(k+1)^2个点

复制代码
 1 void solve()
 2 {
 3     int n;cin>>n;
 4  
 5     int l=1,r=sqrt(n)+1;
 6     while(l<r)
 7     {
 8         int mid=(l+r)>>1;
 9         int cnt=(mid+1);
10         if(cnt*cnt<n)l=mid+1;
11         else r=mid;
12     }
13     
14     int cnt=(l-1)*2-1;
15     if(n==1)cout<<"0\n";
16     else cout<<l<<"\n";
17 }
View Code
复制代码

C.Sum on Subarrays

题意:构造一个长度为n数组,使得有k个子区间和为正数,剩下的为负数,并且-1000<=a[i]<=1000

Solution

PS:赛时没发现答案的和里面不能有0

构造方法很多了,这里写一种

以pos为左端点有n-pos+1个子区间,如果最左边的数大于右边所有负数和,那么res+=n-pos+1

很显然,只要找出所有pos使得res=k即可,并且这里我们是从左边遍历到右边的,所有最后正数区间一定是[1,x]与y

在这之后,负数都改为-1,正数都改为n-pos+1,y-1的位置如果是负数就改为-(n-pos+1),就一定能保证答案的正确性

例如n=7,k=16,构造出来的是7,6,-1,-4,3,-1,-1,保证以负数为左端点的子区间和均为负数

复制代码
 1 void solve()
 2 {
 3     memset(a,-1,sizeof(a));
 4     int n,k;cin>>n>>k;
 5     //cout<<n<<k<<"\n";
 6     for(int i=n;i>=1;i--)a[i]=-1;
 7     int cnt=min(n,k);
 8     int pos=n-cnt+1;
 9     while(cnt<=k&&k!=0)
10     {
11         //cout<<cnt<<" "<<pos<<"\n";
12         a[pos]*=-1;
13         k-=cnt;
14         cnt--;
15         pos++;
16         while(cnt>k&&k!=0)
17         {
18             cnt--;
19             pos++;
20         }
21     }
22     int sum=0;
23     int last=-1;
24     for(int i=n;i>=1;i--)
25     {
26         if(a[i]>0)a[i]=n-i+1;
27         else if(a[i]<0)
28         {
29             if(i==n)continue;
30             if(a[i+1]>0)a[i]=-(n-i+1);
31         }
32     }
33     for(int i=1;i<=n;i++)cout<<a[i]<<" ";
34     cout<<"\n";
35     
36 }
View Code
复制代码

D.Binary String Sorting

题意:对01串进行任意次两种操作,使得01串变为不降序列,并要求代价最小:

1:交换s[i]和s[i+1],代价为1e12

2:删除s[i],代价为1e12+1

Solution

因为对于每一个0或1,如果需要交换的次数大于1,显然是不如删除的,遍历01串分界点,删除分界点右边的0和左边的1,如果当前为10,则交换一次,可以减少两次删除操作

可以使用前缀和统计当前位置的0,1个数

复制代码
 1 void solve()
 2 {
 3     string s;cin>>s;
 4     int ans=1e18;
 5     for(int i=1;i<=s.length();i++)
 6     {
 7         a[i]=a[i-1];
 8         b[i]=b[i-1];
 9         if(s[i-1]=='0')a[i]++;
10         if(s[i-1]=='1')b[i]++;
11     }
12     
13     for(int i=1;i<=s.length()+1;i++)
14     {
15         int pos=i-1;
16         int cnt0=a[s.length()]-a[pos];
17         int cnt1=b[pos];
18         int res=(cnt0+cnt1)*(1e12+1);
19         //cout<<res<<"\n";
20         if(i!=1&&s[i-2]=='1'&&s[i-1]=='0')
21         {
22             res-=(1e12+1)*2-1e12;
23         }
24         //cout<<res<<"\n\n";
25         ans=min(res,ans);
26     }
27     cout<<ans<<"\n";
28     //cout<<"\n";
29 }
View Code
复制代码

 

posted @   HikariFears  阅读(163)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示