Codeforces Round 1025 (Div. 2)
Problem - A - Codeforces
查有没有人说谎,有一个必错的情况:
两个人都说输了,必有人撒谎,还有就是所有人都赢了,也是撒谎
来看代码:
#include <iostream>
#include <vector>
using namespace std;int main()
{int t;cin >> t;while (t--){int n;cin >> n;vector<int>arr(n);int sum = 0;for (int i = 0; i < n; i++){cin >> arr[i];sum += arr[i];}if (sum == n){cout << "YES" << "\n";continue;}int flag = 1;//大于3个 滑动窗口解决if (n > 2){for (int i = 0; i < n - 2; i++){int one = arr[i];int two = arr[i + 1];int thr = arr[i + 2];//枚举过程if (one == 0 && two == 0){flag = 0;}if (two == 0 && thr == 0){flag = 0;}}}else{if (arr[0] == arr[1]){flag = 0;}}if (flag){cout << "NO" << "\n";}else{cout << "YES" << "\n";}}
}
Problem - B - Codeforces
如何躲得最好呢?
第一次切割:横切竖切有2种选择,我之前比较看谁切得多就切,但是会出错,所以我横竖都切一次,最后来比较
每次都躲中间,这个切就像二分,来看代码:
#include <iostream>
#include <vector>
using namespace std;int main()
{int t;cin >> t;int sum = 0;while (t--){sum++;int a, b, c, d;cin >> a >> b >> c >> d;//第一次判断 横切还是竖切int height = max(a - c, c - 1);int broad = max(b - d, d - 1);int left = 0;int right = a - 1-height;int count1 = 0;while (left < right) {int mid = (left + right) >> 1;right = mid;count1++;}right = b - 1;while (left < right) {int mid = (left + right) >> 1;right = mid;count1++;}left = 0;right = a - 1;int count = 0;while (left < right) {int mid = (left + right) >> 1;right = mid;count++;}right = b - 1 - broad;while (left < right) {int mid = (left + right) >> 1;right = mid;count++;}cout << min(count1,count)+1 << "\n";}
}
看看错误代码:
#include <iostream>
#include <vector>
using namespace std;int main()
{int t;cin >> t;while (t--){int a, b, c, d;cin >> a >> b >> c >> d;//第一次判断 横切还是竖切int height = max(a - c, c - 1);int broad = max(b - d, d - 1);//如果是横切if (height * b > broad * a){a -= height;}else{b -= broad;}int left = 0;int right = a - 1;int count = 0;while (left < right) {int mid = (left + right) >> 1; right = mid; count++;}right = b - 1;while (left < right) {int mid = (left + right) >> 1; right = mid; count++;}cout << count+1 << "\n";}
}