#include<bits/stdc++.h>usingnamespace std;template<typename t>voidread(t &x){char ch =getchar();x =0;t f =1;while(ch <'0'|| ch >'9')f =(ch =='-'?-1: f), ch =getchar();while(ch >='0'&& ch <='9')x = x *10+ ch -'0', ch =getchar();x *= f;}#define wi(n) printf("%d ", n)#define wl(n) printf("%lld ", n)#define P puts(" ")typedeflonglong ll;#define MOD 1000000007#define mp(a, b) make_pair(a, b)#define N 20005#define rep(i, j, n) for (int i = j; i <= n; i++)#define red(i, n, j) for (int i = n; i >= j; i--)#define fil(a, n) rep(i, 0, n, ) read(a[i])//---------------https://lunatic.blog.csdn.net/-------------------//vector<int> a[2];intmain(){int t, n, x;read(t);while(t--){a[0].clear();a[1].clear();read(n);rep(i,1, n +1){read(x);x %=2;a[x].push_back(i);}if(!a[0].empty()){puts("1");wi(a[0][0]),P;}elseif(a[1].size()>1)puts("2");wi(a[1][0]),wi(a[1][1]),P;elseputs("-1");}return0;}
B. Count Subrectangles 當(dāng)時沒做出來: 預(yù)處理: 首先統(tǒng)計 a序列和 b序列 的所有連續(xù) 1 區(qū)間長度, a 序列第 i 個連續(xù) 1 的區(qū)間length 為Ai ,b 序列第 i 個連續(xù) 1 的區(qū)間length 為Bi 考慮 k 的每一個約數(shù) d。 則有, d×kd 這個矩形的個數(shù)就是 (Ai?d+1) *(Bi?kd+1) ,對k的每一個因子d計算后求和即可。 解答: 然后排序后二分輸出。
#include<bits/stdc++.h>usingnamespace std;template<typename t>voidread(t &x){char ch =getchar();x =0;t f =1;while(ch <'0'|| ch >'9')f =(ch =='-'?-1: f), ch =getchar();while(ch >='0'&& ch <='9')x = x *10+ ch -'0', ch =getchar();x *= f;}#define wi(n) printf("%d ", n)#define wl(n) printf("%lld ", n)#define P puts(" ")typedeflonglong ll;#define MOD 1000000007#define mp(a, b) make_pair(a, b)#define N 20005#define rep(i, j, n) for (int i = j; i <= n; i++)#define red(i, n, j) for (int i = n; i >= j; i--)#define fil(a, n) rep(i, 0, n, ) read(a[i])//---------------https://lunatic.blog.csdn.net/-------------------//constint N =4e4+100;int a[N], b[N];
vector<pair<int,int>> node;voidinit(int k){for(int i =1; i * i <= k; i++){if(k % i ==0){node.push_back(make_pair(i, k / i));if(i != k / i)node.push_back(make_pair(k / i, i));}}}
unordered_map<int,int> A, B;voidsolve(int a[],int n, unordered_map<int,int>&mp){int pos =1, cnt =0;while(pos <= n){while(a[pos]){pos++;cnt++;}if(cnt){mp[cnt]++;cnt =0;}pos++;}}intmain(){int n, m, k;read(n),read(m),read(k);init(k);rep(i,1, n +1)read(a[i]);rep(i,1, m +1)read(b[i]);solve(a, n, A);solve(b, m, B);LL ans =0;rep(k,0, node.size()){int x = node[k].first, y = node[k].second;LL xx =0, yy =0;for(auto it : A)if(it.first >= x)xx += it.second *(it.first - x +1);for(auto it : B)if(it.first >= y)yy += it.second *(it.first - y +1);ans += xx * yy;}wi(ans), P;return0;}
C. Unusual Competitions 我覺得這個題應(yīng)該和B換一換 這個題把左括號看成-1,右括號看成1,如果前綴和小于0即存在單獨(dú)的右括號,需要重排。 然后找到所有前綴和為負(fù)的連續(xù)段包括最后一個0,計算長度和就行。
#include<bits/stdc++.h>usingnamespace std;template<typename t>voidread(t &x){char ch =getchar();x =0;t f =1;while(ch <'0'|| ch >'9')f =(ch =='-'?-1: f), ch =getchar();while(ch >='0'&& ch <='9')x = x *10+ ch -'0', ch =getchar();x *= f;}#define wi(n) printf("%d ", n)#define wl(n) printf("%lld ", n)#define P puts(" ")typedeflonglong ll;#define MOD 1000000007#define mp(a, b) make_pair(a, b)#define N 20000005#define rep(i, j, n) for (int i = j; i <= n; i++)#define red(i, n, j) for (int i = n; i >= j; i--)#define fil(a, n) rep(i, 0, n, ) read(a[i])//---------------https://lunatic.blog.csdn.net/-------------------//string s;int a[N], n, ans =0;intmain(){read(n);cin >> s;rep(i,0, n){a[i]= a[i -1]+(s[i]=='('?-1:1);}if(a[n -1]!=0)puts("-1");else{rep(i,0, n) ans +=(a[i]<0)+(a[i]<0&& a[i -1]>=0);wi(ans),P;}return0;}