12-6——12-12ACM笔记

A. Make Even

Polycarp has an integer n that doesn’t contain the digit 0. He can do the following operation with his number several (possibly zero) times:

Reverse the prefix of length l (in other words, l leftmost digits) of n. So, the leftmost digit is swapped with the l-th digit from the left, the second digit from the left swapped with (l−1)-th left, etc. For example, if n=123456789 and l=5, then the new value of n will be 543216789.
Note that for different operations, the values of l can be different. The number l can be equal to the length of the number n — in this case, the whole number n is reversed.

Polycarp loves even numbers. Therefore, he wants to make his number even. At the same time, Polycarp is very impatient. He wants to do as few operations as possible.

Help Polycarp. Determine the minimum number of operations he needs to perform with the number n to make it even or determine that this is impossible.

You need to answer t independent test cases.

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
string n;
cin>>t;
int q;
int p;
int i;
while(t--)
{cin>>n;q=1;p=1;
if(n[n.length()-1]=='2'||n[n.length()-1]=='4'||n[n.length()-1]=='6'||n[n.length()-1]=='8')
{cout<<"0"<<endl;p=0;}
else
if(n[0]=='2'||n[0]=='4'||n[0]=='6'||n[0]=='8')
{cout<<"1"<<endl;p=0;}
else
for(i=1;i<n.length()-1;i++)
if(n[i]=='2'||n[i]=='4'||n[i]=='6'||n[i]=='8')
{q=0;p=0;}
 
if(q==0)
cout<<"2"<<endl;
if(p==1)
cout<<"-1"<<endl;
 
 
 
 
}
 
 
 
 
 
return 0;
}

B. Team Composition: Programmers and Mathematicians

The All-Berland Team Programming Contest will take place very soon. This year, teams of four are allowed to participate.

There are a programmers and b mathematicians at Berland State University. How many maximum teams can be made if:

each team must consist of exactly 4 students,
teams of 4 mathematicians or 4 programmers are unlikely to perform well, so the decision was made not to compose such teams.
Thus, each team must have at least one programmer and at least one mathematician.

Print the required maximum number of teams. Each person can be a member of no more than one team.

#include <bits/stdc++.h>
using namespace std;
int main(){
  int t;
  cin >> t;
  for (int i = 0; i < t; i++){
    int a, b;
    cin >> a >> b;
    int tv = 0, fv = min(a, b) + 1;
    while (fv - tv > 1){
      int mid = (tv + fv) / 2;
      int a2 = a - mid;
      int b2 = b - mid;
      if (a2 + b2 >= mid * 2){
        tv = mid;
      } else {
        fv = mid;
      }
    }
    cout << tv << endl;
  }
}

本来我的思路是把两组量分成3:1和2:2然后求最大选法,但想到诸如21 11 这种情况下,3:1分完后还要分2:2,所以重新选择了思路。
C. Polycarp Recovers the Permutation

Polycarp wrote on a whiteboard an array p of length n, which is a permutation of numbers from 1 to n. In other words, in p each number from 1 to n occurs exactly once.

He also prepared a resulting array a, which is initially empty (that is, it has a length of 0).

After that, he did exactly n steps. Each step looked like this:

Look at the leftmost and rightmost elements of p, and pick the smaller of the two.
If you picked the leftmost element of p, append it to the left of a; otherwise, if you picked the rightmost element of p, append it to the right of a.
The picked element is erased from p.
Note that on the last step, p has a length of 1 and its minimum element is both leftmost and rightmost. In this case, Polycarp can choose what role the minimum element plays. In other words, this element can be added to a both on the left and on the right (at the discretion of Polycarp).

Let’s look at an example. Let n=4, p=[3,1,4,2]. Initially a=[]. Then:

During the first step, the minimum is on the right (with a value of 2), so after this step, p=[3,1,4] and a=[2] (he added the value 2 to the right).
During the second step, the minimum is on the left (with a value of 3), so after this step, p=[1,4] and a=[3,2] (he added the value 3 to the left).
During the third step, the minimum is on the left (with a value of 1), so after this step, p=[4] and a=[1,3,2] (he added the value 1 to the left).
During the fourth step, the minimum is both left and right (this value is 4). Let’s say Polycarp chose the right option. After this step, p=[] and a=[1,3,2,4] (he added the value 4 to the right).
Thus, a possible value of a after n steps could be a=[1,3,2,4].

You are given the final value of the resulting array a. Find any possible initial value for p that can result the given a, or determine that there is no solution.

#include<iostream>
using namespace std;
 
int t,n,a[2000010];
 
int main()
{
	cin >> n;
	while(n--) {
		cin >> t;
		  for (int i=1; i<=t; i++) cin >> a[i];
      if (a[1]==t || a[t]==t) {
        for(int i=t; i>=1; i--) cout << a[i] << " "; }
		       else cout<<-1;
		cout<<"\n";
	}
	return 0;
}

DIV3的题前三个都不难,能在限定时间内做出来。D没想明白,但看了别人的代码感觉好短,再努努力可以在限定时间内碰一碰

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值