Codeforces Round #560 (Div. 3) Editorial

A:Remainder

遍历一遍就可以了。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 char s[200010];
 5 int main() {
 6     int n,x,y;
 7     scanf("%d%d%d",&n,&x,&y);
 8     scanf("%s",s);
 9     int ans=0;
10     int temp=n-y-1;
11     if(s[temp]!='1') ans++;
12     for(int i=n-x;i<temp;i++) {
13         if(s[i]!='0') ans++;
14     }
15     for(int i=temp+1;i<n;i++) {
16         if(s[i]!='0') ans++;
17     }
18     printf("%d",ans);
19 }
View Code

B: Polycarp Training

排序后,遍历,逐渐增加答案

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int a[200010];
 5 int main() {
 6     int n;
 7     scanf("%d",&n);
 8     for(int i=0;i<n;i++) scanf("%d",&a[i]);
 9     sort(a,a+n);
10     int pos=1;
11     for(int i=0;i<n;i++) {
12         if(a[i]>=pos) pos++;
13     }
14     printf("%d\n",pos-1);
15 }
View Code

C: Good String
比赛时,就是不懂怎么搞,看了题解,发现特别简单。维护结果串,偶数长度直接放,奇数的话,判断是否与最后一个字符一样。最后注意答案长度的奇偶。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main() {
 5     int n;
 6     string s,ans;
 7     cin>>n>>s;
 8     char c='A';
 9     for(int i=0;s[i];i++) {
10         if(ans.size() %2==0||s[i]!=c)
11             ans+=s[i];
12             c=s[i];
13     }
14     string res;
15     if(ans.size()%2) {
16         for(int i=0;i<ans.size()-1;i++) res+=ans[i];
17     } else res=ans;
18     cout<<n-res.size() <<endl<<res<<endl;
19 }
View Code

D:Almost All Divisors

其实是挺简答的一题。排序后,用最小 * 最大得到结果数,然后得到所有因数,然后判断即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 
 5 int _,n;
 6 vector<ll> a,b;
 7 int main() {
 8     for(scanf("%d",&_);_;_--) {
 9         a.clear();b.clear();
10         scanf("%d",&n);
11         ll x;
12         for(int i=0;i<n;i++) {
13             scanf("%lld",&x);
14             a.push_back(x);
15         }
16         sort(a.begin(),a.end());
17         ll val=a[0]*a[n-1];
18         for(int i=2;i*1ll*i<=val;i++) {
19             if(val%i==0) {
20                 b.push_back(i);
21                 if(i!=val/i) b.push_back(val/i);
22             }
23         }
24         sort(b.begin(),b.end());
25         if(a==b) printf("%lld\n",val);
26         else printf("-1\n");
27     }
28 }
View Code

E:Two Arrays and Sum of Functions

分析对结果的贡献,a_i * b_i 会出现 i * (n-i+1)次,而 a_i * b_i *i *(n-i+1) 中,只有b_i 是变量。要想结果最小,应该用最大 *最小。例 n = 3

会出现以下(l,r)的情况

(1,1)  (1,2)  (1,3) --> (1,1) + (1,1)+(2,2) +(1,1)+(2,2)+(3,3)

    (2,2)  (2,3) --> (2,2) + (2,2) +(3,3)

      (3,3) --> (3,3) 

刚好对应红色的公式。可以理解为   行数*列数 

比如 a_1 * b_1 出现了  1*(3-1+1)次  

 1  #include<bits/stdc++.h>
 2 using namespace std;
 3 const int mod=998244353;
 4 
 5 int a[200010],b[200010];
 6 
 7 bool cmp(int x,int y) {
 8     return x>y;
 9 }
10 int main() {
11     int n;
12     scanf("%d",&n);
13     for(int i=0;i<n;i++) scanf("%d",&a[i]);
14     for(int i=0;i<n;i++) scanf("%d",&b[i]);
15     vector<pair<long long,int> > t;
16     for(int i=0;i<n;i++) {
17         t.push_back(make_pair((i+1)*1ll*(n-i)*a[i],i));
18     }
19     sort(b,b+n,cmp);
20     sort(t.begin(),t.end());
21     int ans=0;
22     for(int i=0;i<n;i++) {
23         ans=(ans+t[i].first%mod*1ll*b[i]%mod)%mod;
24     }
25     printf("%d\n",ans);
26 }
View Code

                 

猜你喜欢

转载自www.cnblogs.com/ACMerszl/p/10883297.html