[解题纪录]:C. Manhattan Subarrays

题目:C. Manhattan Subarrays

心得:

这题我自己卡在题目的定义无法掌握(题目中bad的定义),突破点在于看到了题目中的範例测试资料,假如没有题目的範例测试资料了话,就需要想像更多的可能情况,才可以好好理解定义的意义为何

程式码:

// https://codeforces.com/contest/1550/problem/C#include <iostream>using namespace std;bool is_good(long long a, long long b, long long c){    return (b - a) * (b - c) > 0;}int main(){    int t;    cin >> t;    while (t--) {        int n;        cin >> n;        int a[n];        int count = 0;        for (int i = 0; i < n; i++)            cin >> a[i];        count += 2 * n - 1;        for (int i = 0; i < n-2; i++)            if (is_good(a[i], a[i+1], a[i+2]))                 count++;        for (int i = 0; i < n-3; i++)            if (is_good(a[i], a[i+1], a[i+2]) && is_good(a[i+1], a[i+2], a[i+3]) &&                        is_good(a[i], a[i+1], a[i+3]) && is_good(a[i], a[i+2], a[i+3]))                count++;        cout << count << endl;    }    return 0;}

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章