101 Hack 50

101 Hack 50 闲来无事。也静不下心,打个代码压压压惊

Hard Questions

Vincent and Catherine are classmates who just took an exam in Math 55. The exam consists of  multiple-choice questions. Each question has  choices, each of which is represented by a single capital letter ABCD and E. Each question has exactly one correct answer. A student's score is equal to the number of questions he/she correctly answered.

This was the hardest exam they've ever taken! No one was ever sure of their answer even after the exam, and some students weren't even able to answer all the questions. The questions were so hard that Vincent and Catherine strongly believe that they can't both be correct in any question. In other words, for each question, they believe that one or both of them must be incorrect.

Now, Vincent wants to know how well he could have performed in the exam. Given the answers of Vincent and Catherine, find the maximum score that Vincent could have gotten, assuming that they can't both have gotten the correct answer to any particular question.

Input Format

The first line contains a single integer , the number of questions. 
The second line contains a string of length  denoting the answers of Vincent. 
The third line contains a string of length  denoting the answers of Catherine.

Each answer string consists of only the characters ABCDE and . (dot character).

  • If the 'th character is ABCD or E, then this character represents the student's answer for the 'th question.
  • If the 'th character is ., then this means the student gave no answer for the 'th question.

Constraints

 

Output Format

Print a single line containing a single integer denoting the maximum score that Vincent could have gotten assuming that they can't both have gotten the correct answer to any particular question.

Sample Input 0

24
CCACCBAEBAAAAAAAA.......
CCACCBAEBAAAAAAAA.......

Sample Output 0

0

Explanation 0

In this case, Vincent and Catherine answered exactly the same for the whole exam. Since they can't both be correct in any question, it means they are both incorrect in every question. Hence, they both score .

Sample Input 1

7
ACCEDED
DECADE.

Sample Output 1

4

Explanation 1

In this case, the answer sheet could have been ACBEABD, in which Vincent scores . However, one can also show that Vincent cannot get a higher score than  assuming Vincent and Catherine can't both be correct in any question. Hence, the answer is .

The following diagram illustrates this case:

 

 

Sample Input 2

11
BEE..ADDED.
CAB.DAD.DEE

Sample Output 2

6

Explanation 2

In this case, the answer sheet could have been BEEADEBDEDE, in which Vincent scores . However, one can also show that Vincent cannot get a higher score than  assuming Vincent and Catherine can't both be correct in any question. Hence, the answer is .

The following diagram illustrates this case:

 

 这是一道难(简单)题,有两个人在做一个数学测试,因为太难两个人不可能都做对,如果是"."就说明他没做,所以要你贪心下第一个人最多可以对几个

这种题可以拿来锻炼英语,做起来是没什么价值的,就是两个人不一样,而且不是“.”呗

#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
cin>>n;
string s,c;
cin>>s>>c;
int f=0;
for(int i=0;i<n;i++)
    if(s[i]!=c[i]&&s[i]!='.')
        f++;
cout<<f;
return 0;
}

Even-odd Boxes

Lucy has an array of  boxes. The boxes are arranged in a straight line numbered  to  from left to right. Box  contains  chocolates.

Lucy thinks the arrangement looks beautiful if the boxes follow an even-odd repetitive pattern. That means the first box contains an even number of chocolates, the second box contains an odd number, the third box contains even, and so on. Here's a beautiful even-odd arrangement:

image

Lucy is asking you to make beautiful even-odd arrangements from her arrays of boxes. You are allowed to move some chocolates from one box to another. But you are not allowed to swap the boxes. In the final arrangement, every box must contain at least one chocolate.

Calculate the minimum number of chocolates you need to move to get an even-odd repetitive pattern. If it's not possible to get the desired pattern, print -1.

Input Format

The first line contains an integer  denoting the number of queries. 
The first line of each query contains an integer  denoting the number of boxes. 
The second line of each query contains  space-separated integers  describing the number of chocolates in each box.

Constraints

Subtask

  •  for  of the maximum score

Output Format

Print an integer describing the minimum number of chocolates you need to transfer to get the even-odd repetitive pattern. If it's not possible to get the desired pattern, print .

Sample Input 0

3
6
6 8 3 1 1 4
5
3 1 1 1 1
3
14 3 10

Sample Output 0

2
-1
0

Explanation 0

  • Query :

We have to transfer two chocolates to maintain the pattern. One possible way to transfer chocolates is shown below.

image

  • Query :

We can only transfer one chocolate from the first box. No matter what we do, we cannot get the even-odd pattern.

image

  • Query :

The boxes are already in the even-odd pattern so we don't need to transfer any chocolate.

 
最近也做了不少这样的题,都是奇偶分析,所以我想这个题可以我用模拟方法跑一下,大概也是O(n),但是是有些情况没有考虑到的
所以八一八大神的做法好了。找来一个选手的代码进行思路研究。
先找到1的个数one非1的个数remenan,然后求下容器的累加和。
1因为不允许0个所以sum有最小值
2sum要和n的奇偶性一致
这个求得最小的最小转换是很巧妙的,就是让他变成一个
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int T;
  cin>>T;
  while(T--){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    int one=0,rem=0;
    for(int i=0;i<n;i++)if(a[i]%2!=i%2){
      if(a[i]==1) ++one;
      else ++rem;
    }
    int ok=1;
    ll sum=accumulate(a.begin(),a.end(),0ll);
    if(sum%2!=n*(n-1ll)/2%2) ok=0;
    if(sum<n/2+(n+1)/2*2) ok=0;
    cout<<(ok?one+max(0,rem-one)/2:-1)<<endl;
  }
  return 0;
}

 

 
 

 

posted @ 2017-06-21 10:35  暴力都不会的蒟蒻  阅读(672)  评论(0编辑  收藏  举报