题目
题意
Derek 和 Alfia 是两个好朋友,他们回答三个问题,然后 Derek 会将他的成绩 X 和 Alfia 的成绩 Y 告诉 Alfia, 但是 Derek 可能会说谎。现在将这两个人的答案告诉你,请你判断 Derek 是否说谎。
题解
推公式,签到题。
我们假设 Alfia 的得分是正确的,根据他们俩的答案推出 Derek 得分的区间,然后判断 Derek 是否在此区间内。
首先定义两个变量 same 和 different 分别代表他们答案一样和不一样的数量。
然后就可以推出 左区间 l = max(y-different,0);
右区间 r = min(same,y) + (different - max(0,y-same));
代码
#include <algorithm>
#include <bitset>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n,x,y;
cin >> n >> x >> y;
string a,b;
cin >> a >> b;
int same = 0;
for(int i=0;i<n;i++){
if(a[i] == b[i])
same++;
}
int different = n - same;
int l = max(y-different,0);
int r = min(same,y) + (different - max(0,y-same));
if(x >= l && x <= r)
cout << "Not lying" << endl;
else
cout << "Lying" << endl;
}
return 0;
}