Codeforces Round #553 (Div. 2)

题目链接:

https://codeforces.com/contest/1151

A. Maxim and Biology

题解:

水题直接上代码

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,ans=123456789;
 4 string ss,s="ACTG";
 5 
 6 int main()
 7   {
 8     #ifndef ONLINE_JUDGE
 9     freopen("aa.in","r",stdin);
10     #endif
11     ios::sync_with_stdio(false);
12     cin>>n;
13     //for(int i=1;i<=n;i++)
14     cin>>ss;
15     for(int i=0;i<=n-4;i++)
16       {
17     int tp=0;
18     for(int j=i;j<=i+3;j++)
19       {
20         int x=fabs(ss[j]-s[j-i]);
21         tp+=min(x,26-x);
22       }
23     ans=min(ans,tp);
24       }
25     cout<<ans;
26   }
View Code

B. Dima and a Bad XOR

题意:

给你一个n*m的矩阵,每一行选一个数,使他们的异或和大于0.

题解:

这题惨遭hack,其实不难,还是太菜了,分类讨论能力不够导致思维有些小混乱。

因为只需要大于零,我们可以枚举每一位二进制位。

对于某一位某一行只有三种情况:

(1) 这一行每个数这一位都为1

(2)这一位都为0

(3)这一位既有0又有1

显然如果某一行满足(3),就一定有解(想一想为什么),然后我们先假定所有满足(3)的行都选择放0,最后如果结果是0就将任意一行改成放1就行。

如果没有(3),则每一行这一位都是固定的,算一下等不等于1就行了。

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define N 600
 4 int n,m,ans=123456789;
 5 int mp[N][N];
 6 int a[N][20],b[N][20];
 7 int flag[N],c[N];
 8 int main()
 9 {
10 #ifndef ONLINE_JUDGE
11   freopen("aa.in","r",stdin);
12 #endif
13   ios::sync_with_stdio(false);
14   cin>>n>>m;
15   for(int i=1;i<=n;i++)
16     for(int j=1;j<=m;j++)
17       cin>>mp[i][j];
18   for(int k=0;k<=9;k++)
19     {
20       for(int i=1;i<=n;i++)
21     {
22       for(int j=1;j<=m;j++)
23         {
24            
25           if ((mp[i][j]>>k)&1)
26         a[i][k]=j;else b[i][k]=j;
27         }
28       if (a[i][k]>0&&b[i][k]>0)flag[k]=1;
29       if (a[i][k]>0&&b[i][k]==0)c[k]^=1;
30     }
31     }
32   for(int k=0;k<=9;k++)
33     {
34       if (flag[k]==1||c[k]==1)
35     {
36       cout<<"TAK"<<endl;
37       for(int i=1;i<=n;i++)
38         {
39           if (b[i][k]==0)cout<<a[i][k]<<" ";
40           if (a[i][k]==0)cout<<b[i][k]<<" ";
41           if (a[i][k]>0&&b[i][k]>0)
42         {
43           if (c[k]==0)cout<<a[i][k]<<" ";
44           if (c[k]==1)cout<<b[i][k]<<" ";
45           c[k]=1;
46         }
47         }
48       return 0;
49     }
50     }
51   cout<<"NIE";
52    
53 }
View Code

C. Problem for Nazar

题解

直接模拟即可,但是要注意取模,之前有个地方报数据范围了调了好久,果然菜是原罪

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll unsigned long long
 4 const ll mo=1e9+7;
 5 const ll maxn=(1e18)/2+2;
 6 int main()
 7 {
 8 #ifndef ONLINE_JUDGE
 9   freopen("aa.in","r",stdin);
10 #endif
11   ios::sync_with_stdio(false);
12   ll l,r;
13   cin>>l>>r;
14   l--;
15   ll js=0,sum1=0,sum2=0,ansl=0,ansr=0;
16   for(ll k=1;;)
17     {
18       js++;
19       if (sum1+sum2+k>=l)
20     {
21       ll x=l-sum1-sum2;
22       if (js%2==1) sum1+=x;
23       else  sum2+=x;
24       ansl=sum1%mo*(sum1%mo)%mo+(sum2+1)%mo*((sum2)%mo)%mo;
25       ansl%=mo;
26       break;
27     }
28       if (js%2==1)sum1+=k;
29       if (js%2==0)sum2+=k;
30       if (k<maxn)k=k*2;
31       else k=maxn;
32     }
33   sum1=0;sum2=0;js=0;
34   for(ll k=1;;)
35     {
36       js++;
37       if (sum1+sum2+k>=r)
38     {
39       ll x=r-sum1-sum2;
40       if (js%2==1) sum1+=x;
41       else  sum2+=x;
42       ansr=sum1%mo*(sum1%mo)%mo+(sum2+1)%mo*((sum2)%mo)%mo;
43       ansr%=mo;
44       break;
45     }
46       if (js%2==1)sum1+=k;
47       if (js%2==0)sum2+=k;
48       if (k<maxn)k=k*2;
49       else k=maxn;
50     }
51   cout<<(ansr+mo-ansl)%mo;
52 }
View Code

D. Stas and the Queue at the Buffet

写完c题才发现,D题过的人比c题多多了,真是悲伤。

这题直接排序就行,排序的方式,很简单,自己想想吧,或者直接看我代码就能看懂。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long 
 4 struct nn{
 5   int x;int y;
 6   bool operator <(const nn &b)const 
 7   {
 8     return x-y>b.x-b.y;
 9   } 
10 }a[1000050];
11 
12 int main()
13 {
14   int n;
15   cin>>n;
16   for(int i=1;i<=n;i++)
17     {
18       cin>>a[i].x;
19       cin>>a[i].y;
20     }
21   sort(a+1,a+n+1);
22   ll ans;
23   for(ll i=1;i<=n;i++)
24     ans+=a[i].x*(i-1)+a[i].y*(n-i);
25   cout<<ans;
26 }
View Code

E. Number of Components 

比赛的时候因为做的太慢,所以根本没有看,但是赛后看了半天还是不会,毫无思路。

虽然对于这种统计所有区间的题目,通常是枚举每个点的贡献,但是我依然不会。

看了题解后豁然开朗,对于固定的(l,r)就是把在区间里的赋值为1,把不在区间里的赋值成0,然后求连续的1的个数。

其实就等于求连续两位为01的个数。这样就只用看相邻两个数了。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define N 100050
 4 #define ll long long
 5 ll n,a[N];
 6 ll ans;
 7 template<typename T>void read(T&x)
 8 {
 9   int k=0;char c=getchar();
10   x=0;
11   while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
12   if (c==EOF)exit(0);
13   while(isdigit(c))x=x*10+c-'0',c=getchar();
14   x=k?-x:x;
15 }
16 int main()
17 {
18   #ifndef ONLINE_JUDGE
19   freopen("aa.in","r",stdin);
20   #endif
21   read(n);
22   for(int i=1;i<=n;i++)read(a[i]);
23   for(int i=1;i<=n;i++)
24     {
25       if (a[i-1]<a[i])
26     ans+=(a[i]-a[i-1])*(n-a[i]+1);
27       else
28     ans+=a[i]*(a[i-1]-a[i]);
29     }
30   printf("%I64d
",ans);
31 }
View Code
原文地址:https://www.cnblogs.com/mmmqqdd/p/10735374.html