solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int n, m, arr[500005], l[500005], r[500005], ord[500005], ans[500005],
bit[500005];
map<int, int> mm;
bool cmp(int x, int y) { return r[x] < r[y]; }
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
for (int i = 0; i < 500005; i++) {
ans[i] = 1000000;
bit[i] = 1000000;
}
for (int i = 0; i < m; i++) {
ord[i] = i;
scanf("%d %d", &l[i], &r[i]);
}
sort(ord, ord + m, cmp);
for (int i = 0, j = 1; i < m; i++) {
int t = ord[i];
while (j <= r[t]) {
if (mm[arr[j]]) {
int pos = mm[arr[j]];
for (int k = pos; k; k -= (k & -k)) {
bit[k] = min(bit[k], j - pos);
}
}
mm[arr[j]] = j;
j++;
}
for (int k = l[t]; k <= n; k += (k & -k)) {
ans[t] = min(ans[t], bit[k]);
}
}
for (int i = 0; i < m; i++) printf("%d\n", ans[i] == 1000000 ? -1 : ans[i]);
}
| 4 |
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};
static const double EPS = 1e-8;
class Editor{
private:
string text;
int cursor;
public:
Editor(const string& _text) : text(_text),cursor(0) {}
void forward_char(){
if(text.size() > cursor) cursor++;
}
void forward_word(){
int tmp = text.size();
for(int pos=cursor;pos+1<text.size();pos++){
if(text[pos] != ' '
&& text[pos + 1] == ' '){
tmp = pos + 1;
break;
}
}
cursor = tmp;
}
void backward_char(){
if(0 < cursor) cursor--;
}
void backward_word(){
int tmp = 0;
for(int pos=cursor-1;pos-1>=0;pos--){
if(text[pos] != ' '
&& text[pos - 1] == ' '){
tmp = pos;
break;
}
}
cursor = tmp;
}
void insert(const string& str){
string middle = str;
string front = "";
string rear = "";
if(cursor > 0) front = text.substr(0,cursor);
if(cursor < text.size()) rear = text.substr(cursor,text.size()-cursor);
cursor = front.size() + middle.size();
text = front + middle + rear;
}
void delete_char(){
string front = "";
string rear = "";
if(cursor > 0) front = text.substr(0,cursor);
if(cursor+1 < text.size()) rear = text.substr(cursor+1,text.size()-(cursor+1));
text = front + rear;
}
void delete_word(){
int delete_first = cursor;
int delete_last = text.size();
if(text[cursor] == ' '){
bool update = false;
for(int pos=cursor;pos<text.size();pos++){
if(text[pos] != ' '){
update = true;
break;
}
}
if(!update) return;
for(int pos=cursor;pos<text.size();pos++){
if(text[pos] != ' '
&& text[pos+1] == ' '){
delete_last = pos;
break;
}
}
}
else{
for(int pos=cursor;pos<text.size();pos++){
if(text[pos] == ' '){
delete_last = pos - 1;
break;
}
}
}
string front = "";
string rear = "";
if(delete_first > 0){
front = text.substr(0,delete_first);
}
if(delete_last < text.size()){
rear = text.substr(delete_last+1,text.size()-delete_last);
}
text = front + rear;
// cout << front << "*****" << endl;
// cout << rear << "*****" << endl;
}
void print_text(){
string front = "";
string rear = "";
if(cursor > 0) front = text.substr(0,cursor);
if(cursor < text.size()) rear = text.substr(cursor,text.size()-cursor);
cout << front << "^" << rear << endl;
}
};
int main(){
string total_test_cases_str;
while(getline(cin,total_test_cases_str)){
stringstream ttc_ss;
ttc_ss << total_test_cases_str;
int total_test_cases;
ttc_ss >> total_test_cases;
for(int test_idx=0;test_idx<total_test_cases;test_idx++){
string text;
getline(cin,text);
Editor editor(text);
string total_commands_str;
getline(cin,total_commands_str);
stringstream total_commands_ss;
total_commands_ss << total_commands_str;
int total_commands;
total_commands_ss >> total_commands;
for(int command_idx=0;command_idx < total_commands; command_idx++){
string fr_str;
getline(cin,fr_str);
string front="";
string rear="";
int str_pos = 0;
for(int i=0;i<fr_str.size();i++){
if(fr_str[i] == ' '){
str_pos = i+1;
break;
}
front += fr_str[i];
}
for(int i=str_pos;i<fr_str.size();i++){
rear += fr_str[i];
if(i==str_pos && fr_str[i] == '"') {
//nothing todo
}
else if(fr_str[i] == '"'){
break;
}
}
if(front == "forward"){
if(rear == "char"){
editor.forward_char();
// editor.print_text();
}
else if(rear == "word"){
editor.forward_word();
// editor.print_text();
}
}
else if(front == "backward"){
if(rear == "char"){
editor.backward_char();
// editor.print_text();
}
else if(rear == "word"){
editor.backward_word();
// editor.print_text();
}
}
else if(front == "insert"){
editor.insert(rear.substr(1,rear.size()-2));
// editor.print_text();
}
else if(front == "delete"){
if(rear == "char"){
editor.delete_char();
// editor.print_text();
}
else if(rear == "word"){
editor.delete_word();
// editor.print_text();
}
}
}
editor.print_text();
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, arr[110][110], brr[110];
set<int> ans;
bool found = 0;
void solve() {
vector<pair<int, int>> temp;
vector<int> tempans;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < m; j++) {
int k = arr[j][n - 1] - arr[j][i];
temp.push_back({k, j});
}
sort(temp.begin(), temp.end());
long long tempsum = 0;
for (auto it : temp) {
if (tempsum + it.first <= 0) {
tempsum += it.first;
tempans.push_back(it.second);
} else
break;
}
if (tempans.size() > ans.size()) {
found = 1;
ans.clear();
for (auto it : tempans) ans.insert(it);
}
temp.clear();
tempans.clear();
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
brr[j] += arr[i][j];
}
}
sort(brr, brr + n - 1, greater<int>());
if (brr[0] >= brr[n - 1]) {
cout << 0 << '\n';
return 0;
}
solve();
if (found) {
cout << m - ans.size() << '\n';
for (int i = 0; i < m; i++)
if (ans.find(i) == ans.end()) cout << i + 1 << ' ';
} else {
cout << m << '\n';
for (int i = 1; i <= m; i++) cout << i << ' ';
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 1e5 + 4;
const long long MOD = 998244353;
stack<int> St;
bool visited[N];
vector<int> conc[N];
vector<long long> edge[N];
vector<long long> depth(N);
vector<long long> sub(N, 0);
pair<long long, pair<long long, long long>> wt[N];
int c = 0, f = 0;
long long POW(long long base, long long exponent) {
long long result = 1;
while (exponent > 0) {
if (exponent % 2 == 1) result = (result * base) % mod;
exponent = exponent >> 1;
base = (base * base) % mod;
}
return result;
}
long long Inv(long long x) { return POW(x, mod - 2); }
void DFS(int src) {
visited[src] = 1;
for (auto i : edge[src]) {
if (!visited[i]) {
depth[i] = depth[src] + 1;
DFS(i);
sub[src] += sub[i];
}
}
sub[src]++;
}
void BFS(int s, vector<int> &d) {
d[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto to : edge[v]) {
if (d[to] == (int)1e6) {
d[to] = d[v] + 1;
q.push(to);
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
long long x, y, z;
cin >> x >> y >> z;
wt[i + 1].first = z;
wt[i + 1].second.first = x;
wt[i + 1].second.second = y;
edge[x].push_back(y);
edge[y].push_back(x);
}
memset(visited, 0, sizeof(visited));
depth[1] = 0;
DFS(1);
double ans = 0;
for (int i = 1; i < n; i++) {
double a, b;
if (depth[wt[i].second.first] > depth[wt[i].second.second])
a = (double)sub[wt[i].second.first];
else
a = (double)sub[wt[i].second.second];
b = (double)n - a;
double add = a * b / (n * (n - 1) / 2);
add = add * wt[i].first;
ans = ans + 3 * add;
}
int q;
cin >> q;
while (q--) {
long long x, y;
cin >> x >> y;
double a, b;
if (depth[wt[x].second.first] > depth[wt[x].second.second])
a = (double)sub[wt[x].second.first];
else
a = (double)sub[wt[x].second.second];
b = (double)n - a;
double add = a * b / (n * (n - 1) / 2);
add = add * wt[x].first;
ans = ans - 3 * add;
wt[x].first = y;
add = a * b / (n * (n - 1) / 2);
add = add * wt[x].first;
ans = ans + 3 * add;
cout << fixed;
cout << setprecision(13) << ans << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
long long int n;
cin >> n;
vector<long long int> array(n);
for (long long int i = 0; i < n; i++) cin >> array[i];
long long int count = 0, last_index = 0, last_num = array[0];
for (long long int i = 0; i < n; i++) {
if (last_num != array[i]) {
last_index = i;
last_num = array[i];
}
count += 1 + (i - last_index);
}
cout << count << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
while (cin >> n) {
int num[n];
for (int i = 0; i < n; ++i) scanf("%d", &num[i]);
int vis[n];
int cnt = 0;
bool ans = false;
for (int i = 0; i < n; ++i) {
cnt = 0;
memset(vis, 0, sizeof(vis));
for (int j = 0; j < n; ++j) {
if ((j % 2) == 0)
num[j] = (num[j] + 1) % n;
else
num[j] = (num[j] + n - 1) % n;
if (j && num[j] == (num[j - 1] + 1)) {
++cnt;
}
}
if (cnt == (n - 1)) {
ans = true;
break;
}
}
if (ans)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, data[1000], orig[1000];
vector<pair<int, int> > ans[1000], stat;
bool up(int ind);
bool isUsed[1000];
void print(int ind);
bool increase(int &curVal, int i);
int lastlen;
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> orig[i];
data[i] = orig[i];
}
sort(data, data + n);
bool isAble = true;
for (int i = 0; i < n; i++) {
int j = i;
while (j < n && data[i] == data[j]) j++;
int cnt = j - i;
int curVal = -1;
for (int j = 0; j < cnt; j++) {
isAble = increase(curVal, i);
if (!isAble) break;
ans[i + j] = stat;
ans[i + j].push_back(make_pair(data[i] - lastlen, curVal));
}
if (!isAble) break;
i += cnt - 1;
if (i < n - 1) {
isAble = increase(curVal, i);
stat.push_back(make_pair(data[i] - lastlen, curVal));
if (!isAble) break;
}
lastlen = data[i];
}
if (!isAble)
cout << "NO" << endl;
else {
cout << "YES" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (orig[i] == data[j] && !isUsed[j]) {
isUsed[j] = true;
print(j);
break;
}
}
}
}
return 0;
}
void print(int ind) {
for (int i = 0; i < ans[ind].size(); i++) {
int len = ans[ind][i].first;
int val = ans[ind][i].second;
for (int j = len - 1; j >= 20; j--) printf("0");
for (int j = min(len - 1, 19); j >= 0; j--)
printf("%d", (val & (1 << j)) ? 1 : 0);
}
printf("\n");
}
bool increase(int &curVal, int i) {
curVal++;
int len = data[i] - lastlen;
if (len < 20 && curVal == (1 << len)) {
curVal = 0;
if (up((int)stat.size() - 1) == false) return false;
}
return true;
}
bool up(int ind) {
if (ind == -1) {
return false;
}
stat[ind].second++;
if (stat[ind].second == (1 << stat[ind].first)) {
stat[ind].second = 0;
return up(ind - 1);
}
return true;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d%d", &n, &m);
int mn = min(n, m);
printf("%d\n", mn + 1);
for (int i = 0; i <= mn; i++) {
printf("%d %d\n", i, mn - i);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool f1(pair<long long, long long> x, pair<long long, long long> y) {
return x.first > y.first;
}
bool f2(pair<long long, long long> x, pair<long long, long long> y) {
return x.second > y.second;
}
bool f3(long long x, long long y) { return x > y; }
long long gcd(long long x, long long y) {
if (x == 0) return y;
return gcd(y % x, x);
}
long long powmod(long long x, long long y, long long m) {
if (y == 0) return 1;
long long p = powmod(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
long long modif(long long x, long long m) { return (powmod(x, m - 2, m)); }
bool fa(vector<long long> x, vector<long long> y) { return x[0] < y[0]; }
bool fs(vector<long long> x, vector<long long> y) {
return x.size() > y.size();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<vector<long long> > b(26);
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
b[s[i] - 97].push_back(i);
}
string c;
cin >> c;
long long ans = 1;
long long l = -1;
for (int i = 0; i < c.size(); i++) {
long long x = c[i] - 97;
if (b[x].size() == 0) {
cout << -1;
exit(0);
}
long long y = upper_bound(b[x].begin(), b[x].end(), l) - b[x].begin();
if (y == b[x].size()) {
ans++;
l = b[x][0];
}
if (y != b[x].size()) {
l = b[x][y];
}
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int main() {
int i, j, n, g[25][25], k, find;
bool mark[1001];
int a[25];
a[0] = a[1] = 0;
k = 1;
memset(mark, 0, sizeof(mark));
mark[0] = 1;
for (i = 2; i <= 20; ++i) {
find = 0;
while (!find) {
for (j = 1; j < i; ++j) {
if (mark[a[j] + k]) break;
if (j == i - 1) find = 1;
}
if (find) {
a[i] = k;
for (j = 1; j < i; ++j) {
mark[a[j] + a[i]] = 1;
}
} else {
do {
k++;
} while (mark[k]);
}
}
}
scanf("%d", &n);
memset(g, 0, sizeof(g));
for (i = 1; i <= n; ++i) {
g[i][i] = 0;
for (j = i + 1; j <= n; ++j) {
g[j][i] = g[i][j] = a[i] + a[j];
}
}
for (i = 1; i <= n; ++i) {
for (j = 1; j < n; ++j) {
printf("%d ", g[i][j]);
}
printf("%d\n", g[i][n]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, x, y;
cin >> n >> x >> y;
string s;
cin >> s;
long long ans = 0, a = 0, b = 0;
reverse(s.begin(), s.end());
cerr << s << endl;
for (long long i = 0; i < y; i++) {
if (s[i] == '1') {
ans++;
}
}
cerr << ans;
if (s[y] == '0') ans++;
for (long long i = y + 1; i < x; i++) {
if (s[i] == '1') ans++;
}
cout << ans;
}
int main() {
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char ch;
set < char > s;
for(int i=0;i<4;i++)
{
cin >> ch;
s.insert(ch);
}
if(s.size()==2) cout << "Yes";
else cout << "No";
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 64;
const int inf = 1e9 + 5;
const long double eps = 1e-8;
const int Q = 1e9 + 7;
long long countInv = 0;
void mergeSort(vector<int> &a, long long l, long long r) {
if (l == r) return;
long long m = (l + r) / 2;
mergeSort(a, l, m);
mergeSort(a, m + 1, r);
vector<int> b(m - l + 1);
vector<int> c(r - m);
for (int i = l; i <= m; i++) b[i - l] = a[i];
for (int i = m + 1; i <= r; i++) c[i - m - 1] = a[i];
long long bc = 0, cc = 0;
for (int i = l; i <= r; i++) {
if (bc + l == m + 1) {
a[i] = c[cc];
cc++;
} else if (cc + m + 1 == r + 1) {
a[i] = b[bc];
bc++;
} else {
if (b[bc] < c[cc]) {
a[i] = b[bc];
bc++;
} else {
a[i] = c[cc];
countInv += m - l - bc + 1;
cc++;
}
}
}
}
int main() {
int n;
cin >> n;
set<int> s;
vector<pair<int, int> > p(n);
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
s.insert(p[i].first);
s.insert(p[i].second);
}
vector<int> a(s.size()), b(s.size());
map<int, int> m;
int id = 0;
for (auto it = s.begin(); it != s.end(); it++) {
a[id] = *it;
b[id] = *it;
m.insert(make_pair(*it, id));
id++;
}
for (int i = 0; i < p.size(); i++) swap(a[m[p[i].first]], a[m[p[i].second]]);
for (int i = 0; i < a.size(); i++) {
auto lit = lower_bound(b.begin(), b.end(), b[i]);
auto rit = lower_bound(b.begin(), b.end(), a[i]);
int l = *lit, r = *rit;
if (l > r) {
swap(l, r);
swap(lit, rit);
}
int diff = rit - lit;
if (l < r) countInv += (r - l - diff);
}
mergeSort(a, 0, a.size() - 1);
cout << countInv;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int cx[] = {-1, 0, 0, 1, -1, -1, 1, 1, 0},
cy[] = {0, -1, 1, 0, -1, 1, 1, -1, 0};
int ingrid(int ii, int jj, int R, int C) {
if (ii < 0 || jj < 0 || ii >= R || jj >= C) return 0;
return 1;
}
int in[10];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
in[a]++;
in[b]++;
}
int ch = 0;
for (int i = 1; i <= 5; i++) {
if (in[i] >= 3 || 4 - in[i] >= 3) {
ch = 1;
break;
}
}
if (ch)
printf("WIN\n");
else
printf("FAIL\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct __s {
__s() {
srand(time(NULL));
if (1) {
ios_base::Init i;
cin.sync_with_stdio(0);
cin.tie(0);
}
}
~__s() {
if (!1)
fprintf(stderr, "Execution time: %.3lf s.\n",
(double)clock() / CLOCKS_PER_SEC);
int n;
cin >> n;
}
} __S;
int L, n, p, t;
int l[111111];
int r[111111];
int dp[100000][500];
int maxDp[111111];
int get(int l, int r) {
int d = r - l;
return d / p;
}
int findPos(int x, set<pair<pair<int, int>, int> > &s) {
pair<pair<int, int>, int> p = make_pair(make_pair(x, 1e9 + 1), n);
s.insert(p);
set<pair<pair<int, int>, int> >::iterator i = s.find(p);
int idx = -1;
if (i != s.begin()) {
i--;
idx = (i->second);
}
s.erase(p);
return idx;
}
int solve1() {
set<pair<pair<int, int>, int> > s;
for (int i = 0; i < (int)(n); i++) {
int cnt = get(l[i], r[i]);
int pos = r[i] - p * cnt - t;
int j = findPos(pos, s);
for (int k = 0; k < p; k++) {
int R = r[i] - k;
int cnt = get(l[i], R);
dp[i][k] = cnt;
int pos = R - p * cnt - t;
if (j != -1 && pos < l[j]) j--;
int res = 0;
if (j != -1) {
if (pos >= r[j])
res = dp[j][0];
else {
int k = (r[j] - pos) % p;
res = dp[j][k];
res -= get(l[j], r[j] - k);
res += get(l[j], pos);
}
if (j) res = max(res, maxDp[j - 1]);
}
dp[i][k] += res;
maxDp[i] = max(maxDp[i], dp[i][k]);
if (i) maxDp[i] = max(maxDp[i], maxDp[i - 1]);
}
s.insert(make_pair(make_pair(l[i], r[i]), i));
}
return maxDp[n - 1];
}
int solve2() {
queue<pair<int, int> > q;
int ans = 0;
int CNT = 0;
for (int i = 0; i < (int)(n); i++) {
int cnt = CNT;
for (int j = l[i];
j + p <= r[i] || (q.size() && q.front().first + p <= r[i]); j += p) {
while (q.size() && j >= q.front().first) {
if (cnt <= q.front().second) {
j = max(l[i], q.front().first);
cnt = q.front().second;
}
CNT = max(CNT, q.front().second);
q.pop();
}
if (j + p > r[i]) break;
cnt++;
q.push(make_pair((j + p + t), cnt));
ans = max(ans, cnt);
}
}
return ans;
}
int main(void) {
cin >> L >> n >> p >> t;
for (int i = 0; i < (int)(n); i++) {
cin >> l[i] >> r[i];
}
if (p <= 120)
cout << solve1() << '\n';
else
cout << solve2() << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
int main() {
int n, d, flag = 0;
scanf("%d", &n);
int ara[n], i, j, k;
char str[n][101], stt[] = "aeiouy";
for (i = 0; i < n; i++) {
scanf("%d", &ara[i]);
}
getchar();
for (i = 0; i < n; i++) {
gets(str[i]);
}
for (i = 0; i < n; i++) {
d = 0;
for (j = 0; j < strlen(str[i]); j++) {
for (k = 0; k < 6; k++) {
if (str[i][j] == stt[k]) {
d++;
break;
}
}
}
if (d != ara[i]) {
flag = 1;
break;
}
}
if (flag == 0) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
vector<long long> dp(4, 0), a(4, 0);
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
dp[0] = 1;
for (int i = 1; i <= n; i++) {
a[0] = a[1] = a[2] = a[3] = 0;
a[0] = 1ll * (dp[1] + dp[2] + dp[3]) % M;
a[1] = 1ll * (dp[0] + dp[2] + dp[3]) % M;
a[2] = 1ll * (dp[0] + dp[1] + dp[3]) % M;
a[3] = 1ll * (dp[0] + dp[1] + dp[2]) % M;
for (int j = 0; j < 4; j++) {
dp[j] = a[j];
}
}
cout << dp[0];
return 0;
}
| 5 |
//
// 円の接線 (円外の 1 点から円へ)
//
// verified:
// AOJ Course CGL_7_F Circles - Tangent to a Circle
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F&lang=jp
//
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;
////////////////////////////
// 基本要素 (点, 線分, 円)
////////////////////////////
using DD = double;
const DD INF = 1LL<<60; // to be set appropriately
const DD EPS = 1e-10; // to be set appropriately
const DD PI = acos(-1.0);
DD torad(int deg) {return (DD)(deg) * PI / 180;}
DD todeg(DD ang) {return ang * 180 / PI;}
/* Point */
struct Point {
DD x, y;
Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}
friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << ", " << p.y << ')';}
};
inline Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}
inline Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}
inline Point operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);}
inline Point operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);}
inline Point operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);}
inline Point operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);}
inline Point conj(const Point &p) {return Point(p.x, -p.y);}
inline Point rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}
inline Point rot90(const Point &p) {return Point(-p.y, p.x);}
inline DD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;}
inline DD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;}
inline DD norm(const Point &p) {return dot(p, p);}
inline DD abs(const Point &p) {return sqrt(dot(p, p));}
inline DD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;}
inline bool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;}
inline bool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);}
inline bool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);}
inline Point operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);}
/* Line */
struct Line : vector<Point> {
Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {
this->push_back(a);
this->push_back(b);
}
friend ostream& operator << (ostream &s, const Line &l) {return s << '{' << l[0] << ", " << l[1] << '}';}
};
/* Circle */
struct Circle : Point {
DD r;
Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}
friend ostream& operator << (ostream &s, const Circle &c) {return s << '(' << c.x << ", " << c.y << ", " << c.r << ')';}
};
///////////////////////
// 接線
///////////////////////
// 点と円
vector<Point> tanline(const Point &p, const Circle &c) {
vector<Point> res;
DD d = norm(p - c);
DD l = d - c.r * c.r;
if (l < -EPS) return res;
if (l <= 0.0) l = 0.0;
Point cq = (p - c) * (c.r * c.r / d);
Point qs = rot90((p - c) * (c.r * sqrt(l) / d));
Point s1 = c + cq + qs, s2 = c + cq - qs;
res.push_back(s1);
res.push_back(s2);
return res;
}
// 円と円の共通接線
vector<Line> comtanline(Circle a, Circle b) {
vector<Line> res;
if (abs(a - b) > abs(a.r - b.r) + EPS) {
if (abs(a.r - b.r) < EPS) {
Point dir = b - a;
dir = rot90(dir * (a.r / abs(dir)));
res.push_back(Line(a + dir, b + dir));
res.push_back(Line(a - dir, b - dir));
}
else {
Point p = a * -b.r + b * a.r;
p = p * (1.0 / (a.r - b.r));
vector<Point> bs = tanline(p, a);
vector<Point> as = tanline(p, b);
for (int i = 0; i < min(as.size(), bs.size()); ++i) {
res.push_back(Line(bs[i], as[i]));
}
}
}
if (abs(a - b) > a.r + b.r + EPS) {
Point p = a * b.r + b * a.r;
p = p * (1.0 / (a.r + b.r));
vector<Point> bs = tanline(p, a);
vector<Point> as = tanline(p, b);
for (int i = 0; i < min(as.size(), bs.size()); ++i) {
res.push_back(Line(bs[i], as[i]));
}
}
return res;
}
int main() {
Point p; Circle c;
cin >> p.x >> p.y >> c.x >> c.y >> c.r;
auto res = tanline(p, c);
cout << fixed << setprecision(10);
if (make_pair(res[0].x, res[0].y) < make_pair(res[1].x, res[1].y)) {
cout << res[0].x << " " << res[0].y << " " << res[1].x << " " << res[1].y << endl;
}
else {
cout << res[1].x << " " << res[1].y << " " << res[0].x << " " << res[0].y << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int a[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = int(0); i < int(n); i++) cin >> a[i];
sort(a, a + n);
int h = 0, cnt = 0, mx = 0;
long long total = 0;
for (int i = int(0); i < int(n); i++) {
mx = max(mx, a[i]);
total += a[i];
cnt++;
if (a[i] > h) h++;
}
cnt += mx - h;
cout << total - cnt << '\n';
return 0;
}
| 2 |
#include<cstdio>
int main()
{
int a, b;
scanf("%d%d", &a, &b);
if ((a * b) & 1)
puts("Odd");
else
puts("Even");
return 0;
} | 0 |
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<bitset>
#include<cmath>
#include<ctime>
#include<queue>
#include<map>
#include<set>
#define int long long
#define lowbit(x) (x&(-x))
#define mp(x,y) make_pair(x,y)
#define lc (x<<1)
#define rc (x<<1|1)
#define fi first
#define se second
#define mid ((l+r)>>1)
#define fan(x) (((x-1)^1)+1)
#define max Max
#define min Min
#define abs Abs
using namespace std;
inline int read()
{
int ans=0,f=1;
char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){ans=(ans<<1)+(ans<<3)+c-'0';c=getchar();}
return ans*f;
}
inline void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x/10) write(x/10);
putchar((char)(x%10)+'0');
}
template<typename T>inline T Abs(T a){return a>0?a:-a;};
template<typename T,typename TT>inline T Min(T a,TT b){return a>b?b:a;}
template<typename T,typename TT> inline T Max(T a,TT b){return a>b?a:b;}
const int N=205;
int t,n;
char s[N];
signed main()
{
t=read();
while(t--)
{
n=read();
scanf("%s",s+1);
if(s[1]=='2'&&s[2]=='0'&&s[3]=='2'&&s[4]=='0')
printf("YES\n");
else if(s[n-3]=='2'&&s[n-2]=='0'&&s[n-1]=='2'&&s[n]=='0')
printf("YES\n");
else if(s[1]=='2'&&s[2]=='0'&&s[n-1]=='2'&&s[n]=='0')
printf("YES\n");
else if(s[1]=='2'&&s[2]=='0'&&s[3]=='2'&&s[n]=='0')
printf("YES\n");
else if(s[1]=='2'&&s[n-2]=='0'&&s[n-1]=='2'&&s[n]=='0')
printf("YES\n");
else printf("NO\n");
}
return 0;
} | 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long c1, c2, c3, c4;
cin >> c1 >> c2 >> c3 >> c4;
if (c3 == 0 && c1 == c4) {
cout << 1 << endl;
return 0;
}
c1 -= 1;
c4 -= 1;
if (c1 >= 0 && c1 == c4)
cout << 1 << endl;
else
cout << 0 << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long i, j, r[100], l[100], n, x, cnt = 1, ans;
int main() {
cin >> n >> x;
for (i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
ans += (l[i] - cnt) % x;
ans += (r[i] - l[i] + 1);
cnt = r[i] + 1;
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, d;
cin >> n >> m >> d;
int arr[n][m];
int arr1[n * m];
int flag = 1, median;
int last = -1;
int k = 0, ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
arr1[k] = arr[i][j];
k++;
if (last == -1)
last = arr[i][j] % d;
else {
if (arr[i][j] % d != last) flag = 0;
}
}
}
if (flag) {
sort(arr1, arr1 + n * m);
median = (n * m) / 2;
for (int i = 0; i < n * m; i++) {
ans += abs(arr1[i] - arr1[median]);
}
cout << ans / d;
} else
cout << -1;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string s;
long long N;
void change() {
for (int i = 0; i < s.size(); ++i) {
if (s[i] == 'L') {
s[i] = 'R';
} else {
s[i] = 'L';
}
}
}
bool good(int p) {
int curr = 0, step = 0, ma = 0, cnt = 1;
bool v = 0;
for (int i = 0; i < N; ++i) {
if (s[i] == 'R') {
step = 1;
} else {
step = -1;
}
if (curr + step != p) {
curr += step;
} else {
v = 1;
}
if (ma < curr) {
ma = curr;
cnt = 1;
} else if (ma == curr) {
++cnt;
}
}
if (ma == curr && cnt == 1 && v) {
return true;
}
return false;
}
long long caut() {
long long st = -N, dr = -1, ret = 0;
while (st <= dr) {
long long p = (st + dr) / 2;
if (good(p)) {
ret = p;
dr = p - 1;
} else {
st = p + 1;
}
}
return ret;
}
bool noObs() {
int curr = 0, step = 0, ma = 0, cnt = 1;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == 'R') {
step = 1;
} else {
step = -1;
}
curr += step;
if (ma < curr) {
ma = curr;
cnt = 1;
} else if (ma == curr) {
++cnt;
}
}
if (ma == curr && cnt == 1) {
return true;
}
return false;
}
int main() {
cin >> s;
N = s.size();
if (s[N - 1] == 'L') {
change();
}
if (noObs()) {
cout << 1;
} else {
cout << -caut();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int a[101010 * 3];
int b[101010 * 3];
int _rank[101010 * 3];
int n, m, k;
int main() {
memset(b, 0, sizeof(b));
scanf("%d", &n);
for (int i = 0; i < 3 * n; ++i) {
scanf("%d", &a[i]);
_rank[a[i]] = i;
}
int len = 0;
for (int i = 0; i < n; ++i) {
int p = len;
for (int j = 0; j < 3; ++j) {
scanf("%d", &a[len + j]);
if (_rank[a[len + j]] < _rank[a[p]]) p = len + j;
}
for (int j = 0; j < 3; ++j) {
if (_rank[a[len + j]] == _rank[a[p]])
b[a[len + j]] = 1;
else
b[a[len + j]] = 0;
}
len += 3;
}
scanf("%d", &k);
{}
int t;
len = 0;
for (int i = 0; i < n; ++i) {
if (a[len] == k || a[len + 1] == k || a[len + 2] == k) {
t = len;
}
len += 3;
}
for (int i = 0; i < 3 * n; ++i)
if (a[i] == k) a[i] = -1;
if (b[k] == 0) {
sort(a, a + 3 * n);
} else {
sort(a, a + t);
sort(a + t, a + t + 3);
sort(a + t + 3, a + 3 * n);
int f1 = 0, f2 = t, r1 = t, r2 = 3 * n, k = 0;
while (f1 < r1 || f2 < r2) {
if (f2 >= r2 || f1 < r1 && a[f1] < a[f2]) {
b[k++] = a[f1++];
} else
b[k++] = a[f2++];
}
for (int i = 0; i < 3 * n; ++i) a[i] = b[i];
}
int flag = 0;
for (int i = 0; i < 3 * n; ++i) {
if (a[i] != -1) {
if (flag++) printf(" ");
printf("%d", a[i]);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n;
string s[100005];
long long t[100005];
vector<pair<string, string> > ans;
set<string> S[2];
set<string> freeS[2];
set<string> wrong[2];
long long check(string &s) {
for (auto c : s) {
if (c < '0' || c > '9') return -1;
}
if (s.front() == '0') return -1;
return atoi(s.c_str());
}
string tostr(long long x) {
string ret;
for (; x; x /= 10) ret += x % 10 + '0';
reverse((ret).begin(), (ret).end());
return ret;
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long(i) = (1); (i) <= (n); (i)++) cin >> s[i] >> t[i];
long long e = 0;
for (long long(i) = (1); (i) <= (n); (i)++) e += t[i];
for (long long(i) = (1); (i) <= (e); (i)++) S[1].insert(tostr(i));
for (long long(i) = (e + 1); (i) <= (n); (i)++) S[0].insert(tostr(i));
for (long long(i) = (1); (i) <= (n); (i)++) {
long long res = check(s[i]);
if (res == -1 || res > n) {
freeS[t[i]].insert(s[i]);
continue;
}
if (res <= e)
S[1].erase(s[i]);
else
S[0].erase(s[i]);
if (res > e && t[i] == 1)
wrong[t[i]].insert(s[i]);
else if (res <= e && t[i] == 0)
wrong[t[i]].insert(s[i]);
}
if ((wrong[0].size() || wrong[1].size()) && S[0].size() == 0 &&
S[1].size() == 0) {
string str = *wrong[0].begin();
S[1].insert(str.c_str());
wrong[0].erase(str);
ans.push_back(pair<string, string>(str, "unko"));
str = "unko";
freeS[0].insert(str);
}
while (wrong[0].size() || wrong[1].size()) {
for (long long(i) = (0); (i) <= (1); (i)++) {
if (wrong[i].size() && S[i].size()) {
string str = *wrong[i].begin(), nstr = *S[i].begin();
S[1 - i].insert(str.c_str());
wrong[i].erase(str);
ans.push_back(pair<string, string>(str, nstr));
S[i].erase(nstr);
}
}
}
for (long long(i) = (0); (i) <= (1); (i)++) {
while (freeS[i].size()) {
string str = *freeS[i].begin(), nstr = *S[i].begin();
freeS[i].erase(str);
ans.push_back(pair<string, string>(str, nstr));
S[i].erase(nstr);
}
}
cout << ans.size() << endl;
for (auto p : ans) cout << "move " << p.first << " " << p.second << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
string queue_[6] = {"", "Sheldon", "Leonard", "Penny", "Rajesh", "Howard"};
int main() {
cin >> n;
double count = 1;
bool used = false;
while (count * 5 < n) {
n -= count * 5;
count *= 2;
used = true;
}
int x = ceil(n / count);
cout << queue_[x];
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
vector<long long> adj[1000005];
long long a[1000005];
long long val[1000005];
bool chan[1000005];
void dfs(long long n) {
if (a[n] < 3) {
dfs(adj[n][0]);
dfs(adj[n][1]);
long long v1 = val[adj[n][0]];
long long v2 = val[adj[n][1]];
if (a[n] == 0) {
val[n] = (v1 == 1 && v2 == 1);
} else if (a[n] == 2) {
val[n] = (v1 == 1 || v2 == 1);
} else {
val[n] = (v1 != v2);
}
} else if (a[n] == 3) {
dfs(adj[n][0]);
long long v1 = val[adj[n][0]];
val[n] = !v1;
}
}
void dfs2(long long n) {
if (a[n] == 4) {
chan[n] = true;
} else if (a[n] == 3) {
dfs2(adj[n][0]);
} else {
long long v1 = val[adj[n][0]];
long long v2 = val[adj[n][1]];
if (a[n] == 0) {
if (v1 == 0 && v2 == 1) {
dfs2(adj[n][0]);
} else if (v1 == 1 && v2 == 0) {
dfs2(adj[n][1]);
} else if (v1 == 1 && v2 == 1) {
dfs2(adj[n][0]);
dfs2(adj[n][1]);
}
} else if (a[n] == 2) {
if (v1 == 0 && v2 == 0) {
dfs2(adj[n][0]);
dfs2(adj[n][1]);
} else if (v1 == 1 && v2 == 0) {
dfs2(adj[n][0]);
} else if (v1 == 0 && v2 == 1) {
dfs2(adj[n][1]);
}
} else {
dfs2(adj[n][0]);
dfs2(adj[n][1]);
}
}
}
void solve() {
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
string s;
cin >> s;
if (s == "IN") {
a[i] = 4;
long long x;
cin >> x;
val[i] = x;
} else if (s == "NOT") {
a[i] = 3;
long long x;
cin >> x;
x--;
adj[i].push_back(x);
} else {
long long x, y;
cin >> x >> y;
x--;
y--;
adj[i].push_back(x);
adj[i].push_back(y);
if (s == "AND") {
a[i] = 0;
} else if (s == "XOR") {
a[i] = 1;
} else {
a[i] = 2;
}
}
}
dfs(0);
dfs2(0);
for (long long i = 0; i < n; i++) {
if (a[i] == 4) {
cout << (chan[i] ^ val[0]);
}
}
cout << endl;
}
void querySolve() {
long long t;
cin >> t;
for (long long i = 0; i < t; i++) {
solve();
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
| 4 |
#include <iostream>
using namespace std;
int main(){
unsigned h,w,x,y,t;
cin>>h>>w;
for(y=0; y<h; ++y){
for(x=0; x<w; ++x){
cin>>t;
if((x^y)&1)
cout<<"720720 ";
else cout<<720720+t*t*t*t<<' ';
}
cout<<'\n';
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, a[100005], it[100005][100], it2[100005][100], sum[100005],
it3[1000];
void buil() {
for (long long i = (1); i <= (n); ++i) it[i][0] = a[i];
for (long long k = 1; (1ll << k) <= n; k++)
for (long long i = 1; i + (1ll << k) - 1 <= n; i++)
it[i][k] = max(it[i][k - 1], it[i + (1 << (k - 1))][k - 1]);
}
long long get(long long l, long long r) {
long long tmp = log2(r - l + 1);
return max(it[l][tmp], it[r - (1ll << tmp) + 1][tmp]);
}
void buil2() {
for (long long i = (1); i <= (n); ++i) it2[i][0] = sum[i];
for (long long k = 1; (1ll << k) <= n; k++)
for (long long i = 1; i + (1ll << k) - 1 <= n; i++)
it2[i][k] = min(it2[i][k - 1], it2[i + (1 << (k - 1))][k - 1]);
}
long long get2(long long l, long long r) {
if (l > r) return 1000000000;
long long tmp = log2(r - l + 1);
return min(it2[l][tmp], it2[r - (1ll << tmp) + 1][tmp]);
}
void up(long long k, long long l, long long r, long long i, long long val) {
if (l > r) return;
if (i < l || i > r) return;
if (l == r && i == l) {
it3[k] = val;
return;
}
long long mid = (l + r) / 2;
up(k * 2, l, mid, i, val);
up(k * 2 + 1, mid + 1, r, i, val);
it3[k] = max(it3[k * 2], it3[k * 2 + 1]);
}
long long get3(long long k, long long l, long long r, long long u,
long long v) {
if (l > r || v < l || u > r) return 0;
if (l >= u && r <= v) return it3[k];
long long mid = (l + r) / 2;
return max(get3(k * 2, l, mid, u, v), get3(k * 2 + 1, mid + 1, r, u, v));
}
int main() {
cin >> n;
for (long long i = (1); i <= (n); ++i) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
buil();
buil2();
long long ans = 0;
for (long long i = (1); i <= (n); ++i) {
up(1, 1, 30 + 31, a[i] + 31, i);
for (long long k = (-30); k <= (30); ++k) {
long long l = get3(1, 1, 30 + 31, k + 1 + 31, 30 + 31);
if (l == 0)
l = 1;
else
l++;
if (l > i) continue;
ans = max(ans, sum[i] - k - get2(l, i - 1));
ans = max(ans, sum[i] - k - sum[l - 1]);
}
}
cout << ans;
return 0;
}
| 4 |
#include "stdio.h"
#include "math.h"
#include <algorithm>
using namespace std;
#define SUQARE_SIZE 100000
int block[SUQARE_SIZE];
int blockSize;
int n;
void updateArr(int arr[],int left,int num){
for(int i = left;i < left + blockSize;++i){
if(i >= n){
break;
}
arr[i] = num;
}
}
void update(int arr[],int l, int r,int value)
{
// 左端
if(l % blockSize != 0 && l != 0 && l < r){
int blockNumber = l / blockSize;
if(block[blockNumber] != __INT_MAX__){
updateArr(arr,blockNumber * blockSize,block[blockNumber]);
block[blockNumber] = __INT_MAX__;
}
do{
arr[l] = value;
l++;
}while(l % blockSize != 0 && l < r);
}
// blockに該当する部分
while ((l + blockSize) <= r)
{
block[l / blockSize] = value;
l += blockSize;
}
if(l <= r){
int blockNumber = r / blockSize;
if(block[blockNumber] != __INT_MAX__){
updateArr(arr,blockNumber * blockSize,block[blockNumber]);
block[blockNumber] = __INT_MAX__;
}
// 右端
do{
arr[l] = value;
l++;
}while(l <= r);
}
}
int query(int arr[],int index)
{
int blockNumber = index / blockSize;
if(block[blockNumber] != __INT_MAX__){
return block[blockNumber];
}
else{
return arr[index];
}
}
int main(){
int q,command;
scanf("%d %d",&n,&q);
int a[n];
blockSize = sqrt(n);
for(int i = 0;i < n;++i){
a[i] = block[i] = __INT_MAX__;
}
for(int i = 0;i < q;++i){
scanf("%d",&command);
if(command == 0){
int l,r,value;
scanf("%d %d %d",&l,&r,&value);
update(a,l,r,value);
}else{
int index;
scanf("%d",&index);
printf("%d\n",query(a, index));
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int a, b;
cin >> a >> b;
cout << (a ^ b) << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m, t, c[1001], first[500], last[500];
long long dp[1000][1000];
long long f(int l, int r) {
if (l > r) return 1;
if (dp[l][r] != -1) return dp[l][r];
int col = n;
for (int i = l; i <= r; i++) col = min(col, c[i]);
if (first[col] < l || r < last[col]) return 0;
long long s1 = 0, s2 = 0;
for (int a = l; a <= first[col]; a++)
s1 = (s1 + f(l, a - 1) * f(a, first[col] - 1)) % mod;
for (int b = last[col]; b <= r; b++)
s2 = (s2 + f(last[col] + 1, b) * f(b + 1, r)) % mod;
dp[l][r] = s1 * s2 % mod;
int x = first[col];
for (int i = first[col] + 1; i <= last[col]; i++) {
if (c[i] != col) continue;
dp[l][r] = (dp[l][r] * f(x + 1, i - 1)) % mod;
x = i;
}
return dp[l][r];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> t;
for (int i = 0; i < t; i++) {
cin >> c[m];
c[m]--;
if (m == 0 || c[m - 1] != c[m]) m++;
if (m > 2 * n) {
cout << "0\n";
return 0;
}
}
for (int i = 0; i < m; i++) last[c[i]] = i;
for (int i = m - 1; 0 <= i; i--) first[c[i]] = i;
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++) dp[i][j] = -1;
cout << f(0, m - 1) << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
int t, n;
std::cin >> t;
for (int i = 0; i < t; ++i) {
std::cin >> n;
std::vector<int> v(n);
for (int j = 0; j < n; ++j) {
std::cin >> v[j];
}
std::sort(v.begin(), v.end(), std::greater<int>());
for (int k : v) {
std::cout << k << " ";
}
std::cout << std::endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, maximum;
int v[200005];
stack<int> st;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> v[i];
maximum = max(maximum, v[i]);
}
for (int i = 1; i <= n; ++i) {
if (v[i] == maximum) continue;
int j = i;
while (j <= n && v[j] != maximum) {
if (st.size() && st.top() == v[j])
st.pop();
else {
if (!st.size() || st.top() > v[j])
st.push(v[j]);
else {
cout << "NO";
return 0;
}
}
++j;
}
if (st.size()) {
cout << "NO";
return 0;
}
i = j;
}
if (!st.size()) cout << "YES";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m, n;
int connect[30][30];
int a, b;
memset(connect, 0, sizeof connect);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
connect[a][b] = 1;
connect[b][a] = 1;
}
if (n <= 6)
printf("%d\n", m);
else {
int ans = 999999, nnn = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
int res = 0;
for (int k = 1; k <= n; k++) {
if (connect[i][k] && connect[j][k]) res++;
}
ans = min(res, ans);
}
}
printf("%d\n", m - ans);
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s,s1,s2;
cin>>s;
s1=s.substr(0,2),s2=s.substr(2,4);
bool b1="01"<=s1&&s1<="12",b2="01"<=s2&&s2<="12";
cout<<(b1?b2?"AMBIGUOUS":"MMYY":b2?"YYMM":"NA")<<endl;
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:32000000")
#pragma GCC optimize("O3")
using namespace std;
int n, m;
int arr[4][128];
int dp[128][16];
int mx(int id, int mask) {
int MX = 0;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
if (mask & (1 << j)) {
sum += (arr[(i + j) % n][id]);
}
}
MX = max(MX, sum);
}
return MX;
}
void solve() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < 16; j++) {
dp[i][j] = -1000000007;
}
}
for (int i = 0; i < (1 << n); i++) {
dp[0][i] = mx(0, i);
}
for (int cur = 0; cur < m; cur++) {
for (int prv = 0; prv < cur; prv++) {
for (int cmask = 0; cmask < (1 << n); cmask++) {
for (int pmask = 0; pmask <= cmask; pmask++) {
if ((pmask & cmask) != pmask) {
continue;
}
int add = (cmask ^ pmask);
dp[cur][cmask] = max(dp[cur][cmask], dp[prv][pmask] + mx(cur, add));
}
}
}
}
cout << dp[m - 1][(1 << n) - 1] << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(0);
cout.sync_with_stdio(0);
cout.precision(9);
srand(time(0));
int q;
cin >> q;
while (q--) {
solve();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, p, w, d;
cin >> n >> p >> w >> d;
if (p % w == 0 && n >= p / w) {
cout << p / w << " " << 0 << " " << n - p / w;
return 0;
}
if (p % d == 0 && n >= p / d) {
cout << 0 << " " << p / d << " " << n - p / d;
return 0;
}
if (p / w + p % w / d <= n && p / w * w + p % w / d * d == p) {
cout << p / w << " " << p % w / d << " " << n - p / w - p % w / d;
return 0;
}
for (long long i = 1; i <= min(p / d, n); i++) {
long long now = p - i * d;
if (now < 0) break;
if (now > 0 && now % w == 0 && n - now / w - i >= 0) {
cout << now / w << " " << i << " " << n - now / w - i;
return 0;
}
if (i == 10000000) break;
}
cout << -1;
}
| 3 |
#include <iostream>
#include <vector>
using namespace std;
int main(void){
while (1) {
int n; cin >> n;
if (!n) break;
vector<char> v1, v2;
for (int i = 0; i < n; i++) {
char c;
cin >> c; v1.push_back(c);
cin >> c; v2.push_back(c);
}
int m; cin >> m;
for (int i = 0; i < m; i++) {
char b; cin >> b;
for (int j = 0; j < n; j++) {
if (b == v1[j]) {
b = v2[j];
break;
}
}
cout << b;
}
cout << endl;
}
}
| 0 |
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <map>
#define all(x) (x).begin(), (x).end()
#define mkp make_pair
#define fr first
#define sc second
using namespace std;
int N;
void solve();
int main() {
while (cin >> N, N)
solve();
return 0;
}
void solve() {
istringstream iss;
string sdt, stm, stp, sid;
int hr, mn, tm, id;
map< int, int > tx, sum;
for (int i = 0; i < N; ++i) {
cin >> sdt >> stm >> stp >> sid;
stm[2] = ' ', iss.clear(), iss.str(stm);
iss >> hr >> mn, tm = 60 * hr + mn;
iss.clear(), iss.str(sid);
iss >> id;
if (stp == "I")
tx[id] = tm;
else {
if (id == 0) {
for (auto itr = tx.begin(); itr != tx.end(); ++itr)
if (itr->fr != 0 && itr->sc != 0)
sum[itr->fr] += tm - max(tx[0], itr->sc);
}
else {
if (tx[0] != 0)
sum[id] += tm - max(tx[0], tx[id]);
}
tx[id] = 0;
}
}
int res = 0;
for (auto itr = sum.begin(); itr != sum.end(); ++itr)
res = max(res, itr->sc);
cout << res << "\n";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
std::mt19937_64 rng(
std::chrono::steady_clock::now().time_since_epoch().count());
void solve() {
int t;
cin >> t;
while (t--) {
int a, b;
cin >> a >> b;
int c = abs(a - b);
int d = c / 10;
if (c % 10 == 0) {
cout << d << "\n";
} else {
cout << d + 1 << "\n";
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
cerr << "\nTime elapsed:" << setprecision(5)
<< 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10000, G = 1000, INF = 0x3f3f3f3f;
int xx[N], qu[G + 1][N], cnt[G + 1];
bool used[G + 1][N];
int main() {
int x_, n;
cin >> x_ >> n;
for (int i = 0; i < n; i++) cin >> xx[i];
int g, r;
cin >> g >> r;
sort(xx, xx + n);
used[0][0] = true, qu[0][cnt[0]++] = 0;
int k = 0;
while (cnt[0]) {
int ans = INF;
for (int h = 0; h < g; h++) {
for (int j = 0; j < cnt[h]; j++) {
int i = qu[h][j];
for (int di = -1; di <= 1; di += 2) {
int i_ = i + di;
if (i_ >= 0 && i_ < n) {
int x = abs(xx[i] - xx[i_]);
int h_ = h + x;
if (h_ <= g && !used[h_][i_]) {
if (i_ == n - 1) ans = min(ans, k * (g + r) + h_);
used[h_][i_] = true, qu[h_][cnt[h_]++] = i_;
}
}
}
}
cnt[h] = 0;
}
if (ans != INF) {
cout << ans << '\n';
return 0;
}
k++;
for (int j = 0; j < cnt[g]; j++) {
int i = qu[g][j];
used[0][i] = true, qu[0][cnt[0]++] = i;
}
cnt[g] = 0;
}
cout << "-1\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4005;
const int MAXK = 4005;
const int MAXT = 86400;
int n, k;
int dp[2][MAXK];
inline int getEndTime(int right, int a, int b) { return max(right, a) + b; }
int main() {
int a, b;
while (~scanf("%d%d", &n, &k)) {
if (n == k) {
printf("%d\n", MAXT);
return 0;
}
int ans;
int now, pre;
for (int i = 1; i <= n; ++i) {
now = i & 1;
pre = 1 - now;
scanf("%d%d", &a, &b);
if (i == 1) {
ans = a - 1;
dp[now][0] = a + b;
if (k) {
dp[now][1] = 1;
}
} else {
for (int j = 0; j <= min(i, k); ++j) {
ans = max(ans, a - dp[pre][j]);
if (j == 0) {
dp[now][j] = getEndTime(dp[pre][j], a, b);
} else {
dp[now][j] = min(getEndTime(dp[pre][j], a, b), dp[pre][j - 1]);
}
}
}
if (i == n) {
for (int j = 0; j <= min(i, k); ++j) {
ans = max(ans, MAXT - dp[now][j] + 1);
}
}
}
printf("%d\n", ans);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2002;
int n, l[MAX], r[MAX];
string a, b;
int solve(int d) {
int last = n-1;
while(!(b[last] - '0')) --last;
for(int i = 0; i < n; i++) {
if(b[i]-'0') last = i;
l[i] = min(d, -((i+n-last) % n));
}
last = 0;
while(!(b[last] - '0')) ++last;
for(int i = n-1; i >= 0; i--) {
if(b[i]-'0') last = i;
r[i] = max(d, (last+n-i) % n);
}
int ret = INT_MAX, add = 0;
vector<int> p;
for(int i = 0; i < n; i++) if(a[i] != b[(i+n+d) % n]) p.push_back(i);
if(d < 0) {
sort(p.begin(), p.end(), [] (int x, int y) { return l[x] < l[y]; });
int curr = 0;
for(int i = 0; i < p.size() && l[p[i]] <= d; i++) {
ret = min((curr - l[p[i]]) * 2 + d, ret);
curr = max(r[p[i]], curr);
++add;
}
if(p.empty() || l[p.size()-1] <= d) ret = min((curr - d) * 2 + d, ret);
} else {
sort(p.begin(), p.end(), [] (int x, int y) { return r[x] > r[y]; });
int curr = 0;
for(int i = 0; i < p.size() && r[p[i]] >= d; i++) {
ret = min((r[p[i]] - curr) * 2 - d, ret);
curr = min(l[p[i]], curr);
++add;
}
if(p.empty() || r[p.size()-1] >= d) ret = min((d - curr) * 2 - d, ret);
}
return ret + add;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b;
n = a.length();
bool a1 = 0, b1 = 0;
for(int i = 0; i < n; i++) a1 |= a[i]-'0', b1 |= b[i]-'0';
if(!b1) cout << (a1 ? -1 : 0), exit(0);
int ans = INT_MAX;
for(int d = -n+1; d < n; d++) ans = min(solve(d), ans);
cout << ans;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e8;
int main(){
int Q; cin >> Q;
for(int query = 0; query<Q; query++){
string S, T; cin >> S >> T;
//dp[i][j]: S[i],T[j]まででの最長共通部分列
vector<vector<int>> dp(1010, vector<int>(1010, 0));
for(int i=0; i<S.length(); i++){
for(int j=0; j<T.length(); j++){
if(S[i] == T[j]){
dp[i+1][j+1] = dp[i][j]+1;
} else {
dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
}
}
}
cout << dp[S.length()][T.length()] << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string s,ans;
int n,num,now;
int csize[10]={0,5,3,3,3,3,3,4,3,4};
char btn[10][5]={
{'-','-','-','-','-'},
{'.',',','!','?',' '},
{'a','b','c','-','-'},
{'d','e','f','-','-'},
{'g','h','i','-','-'},
{'j','k','l','-','-'},
{'m','n','o','-','-'},
{'p','q','r','s','-'},
{'t','u','v','-','-'},
{'w','x','y','z','-'}
};
int main(){
int i,a,j;
cin >> n;
for(a=0;a<n;a++){
ans="";
cin >> s;
for(i=0;i<s.size();i++){
now = s[i]-'0';
num=0;
if(now==0)continue;
while(s[++i]==now+'0')num++;
ans+=btn[now][num%csize[now]];
}
cout << ans << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool isprime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
int main() {
int n;
cin >> n;
if (isprime(n))
cout << "1" << endl;
else if (n % 2 == 0)
cout << "2" << endl;
else if (isprime(n - 2))
cout << "2" << endl;
else
cout << "3" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long p[1000100], h[1000100], g[1000100], ans;
int main() {
int m, n, x, y;
cin >> n >> m;
for (int i = 0; i <= n; i++) p[i] = (i ? p[i - 1] * 7 : 1);
while (m--) {
scanf("%d%d", &x, &y);
h[x] += p[y];
h[y] += p[x];
}
for (int i = 1; i <= n; i++) g[i] = h[i] + p[i];
sort(h + 1, h + n + 1);
for (int i = 1, cnt = 0; i <= n; i++)
if (h[i] == h[i - 1])
ans += cnt++;
else
cnt = 1;
sort(g + 1, g + n + 1);
for (int i = 1, cnt = 0; i <= n; i++)
if (g[i] == g[i - 1])
ans += cnt++;
else
cnt = 1;
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
string s;
cin >> s;
long long n = s.size();
vector<vector<long long> > p(4, vector<long long>(4, 0));
for (long long i = 0; i < n; ++i) {
if (s[i] == '0') {
if (p[2][0] == 0) {
cout << "3 1\n";
p[2][0] = 1;
} else {
p[2][0] = 0;
cout << "1 1\n";
}
} else {
if (p[0][2] == 0) {
cout << "1 3\n";
p[0][2] = 1;
} else {
p[0][2] = 0;
cout << "1 1\n";
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long T, n, ans, am, bm;
long long al[120], bl[120];
long long ad[120], bd[120];
int main() {
cin >> T;
while (T) {
T--;
cin >> n;
ans = 0;
am = 1e13 + 20, bm = 1e13 + 20;
for (int i = 1; i <= n; i++) {
cin >> al[i];
am = min(am, al[i]);
}
for (int i = 1; i <= n; i++) {
cin >> bl[i];
bm = min(bm, bl[i]);
}
for (int i = 1; i <= n; i++) {
ad[i] = al[i] - am;
bd[i] = bl[i] - bm;
}
for (int i = 1; i <= n; i++) ans += max(ad[i], bd[i]);
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long a1, a2, b1, b2, c1, c2;
long long flag = 0;
long long check(long long x, long long y) {
x -= b1, y -= b2;
long long dis = c1 * c1 + c2 * c2;
if (!dis) return !x && !y;
return !((c1 * x + c2 * y) % dis) && !((c1 * y - c2 * x) % dis);
}
signed main() {
cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2;
flag |= check(a1, a2);
flag |= check(-a1, -a2);
flag |= check(a2, -a1);
flag |= check(-a2, a1);
if (flag)
printf("YES");
else
printf("NO");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int k, n = s.length();
cin >> k;
string a[k];
for (int i = 0; i < k; i++) {
cin >> a[i];
}
int ans = 0;
for (int i = 0; i < k; i++) {
int temp = 0, temp1 = 0;
for (int j = 0; j < n; j++) {
if (s[j] == a[i][0]) {
temp++;
} else if (s[j] == a[i][1]) {
temp1++;
} else {
ans += min(temp, temp1);
temp = temp1 = 0;
}
}
ans += min(temp, temp1);
}
cout << ans << "\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
const int MIN = 2e5 + 20;
const int MAX = 1e9 + 20;
ll n, k, a, b, l, i, j, t, u[3], mn, r, v;
pair<ll, ll> p[1000];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
l = 1;
k = 0;
while (n > 1) {
if (n % 2 == 0)
n /= 2;
else if (n % 3 == 0)
n = (n / 3) * 2;
else if (n % 5 == 0)
n = (n / 5) * 4;
else {
l = 0;
break;
}
k++;
}
if (l)
cout << k << '\n';
else
cout << "-1\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long mod = 1e9 + 7;
const int N = 1e6 + 10;
bool present[2010];
bool vis[2010];
queue<pair<int, int> > q;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
memset(vis, 0, sizeof(vis));
int ans = 0;
bool flag1 = false, flag2 = false;
for (int i = 1; i <= k; ++i) {
int temp;
cin >> temp;
if (temp == n) ans = 1;
if (temp < n) flag1 = true;
if (temp > n) flag2 = true;
present[temp] = true;
}
if (ans) {
cout << 1;
return 0;
}
if (!flag1 or !flag2) {
cout << -1;
return 0;
}
q.push(make_pair(1000, 0));
flag1 = false;
while (!q.empty()) {
int val = q.front().first;
int len = q.front().second;
if (val == 1000 and flag1) {
cout << len;
return 0;
}
flag1 = true;
q.pop();
if (vis[val]) continue;
vis[val] = true;
for (int i = 0; i <= 1000; ++i) {
if (present[i]) {
if (0 <= val + i - n and 1000 >= val + i - n) {
q.push(make_pair(val + i - n, len + 1));
}
}
}
}
cout << ans << endl;
return 0;
}
| 3 |
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
#define NODE (128)
#define INF (1 << 30)
#define SHOP (6)
int cost[NODE][NODE];
int v[1 << (SHOP)][NODE];
int V;
int m, n, k, d;
int cake[SHOP];
typedef struct{
int pos, val, bit;
} State;
bool operator > (const State &a, const State &b)
{
return (a.val > b.val);
}
int conv(char *s)
{
if (s[0] == 'H') return (0);
if (s[0] == 'D') return (m + n + 1);
if (s[0] == 'C') return (atoi(s + 1));
return (atoi(s + 1) + m);
}
void dijkstra()
{
priority_queue<State, vector<State>, greater<State> > que;
State st;
st.pos = 0, st.val = 0, st.bit = 0;
que.push(st);
for (int i = 0; i < 1 << (SHOP); i++){
for (int j = 0; j < V; j++){
v[i][j] = INF;
}
}
v[0][0] = 0;
while (!que.empty()){
State temp = que.top(); que.pop();
for (int i = 0; i < V; i++){
if (i == temp.pos || cost[temp.pos][i] == INF){
continue;
}
State next;
next.val = temp.val + cost[temp.pos][i];
next.pos = i;
next.bit = temp.bit;
if (1 <= i && i <= m){
if (!((next.bit >> (i - 1)) & 1)){
next.bit |= (1 << (i - 1));
next.val -= cake[i - 1];
}
else continue;
}
if (v[next.bit][i] > next.val){
v[next.bit][i] = next.val;
que.push(next);
}
}
}
}
int main(void)
{
int i, j, l;
int from, to;
int cal;
char div[5];
while (1){
scanf("%d %d %d %d", &m, &n, &k, &d);
if (m + n + k + d == 0){
break;
}
V = m + n + 2;
for (i = 0; i < m; i++){
scanf("%d", &cake[i]);
}
for (i = 0; i < V; i++){
for (j = 0; j < V; j++){
cost[i][j] = INF;
}
}
for (i = 0; i < d; i++){
scanf("%s", div);
from = conv(div);
scanf("%s", div);
to = conv(div);
scanf("%d", &cal);
cost[from][to] = cost[to][from] = cal * k;
}
int ans = INF;
dijkstra();
for (i = 0; i < 1 << (SHOP); i++){
ans = min(ans, v[i][m + n + 1]);
}
printf("%d\n", ans);
}
return (0);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct range_t {
int rmin, rmax;
range_t() {}
range_t(int rmin, int rmax) : rmin(rmin), rmax(rmax) {}
inline int size() { return rmax - rmin + 1; }
};
struct edge_t {
int from, to;
range_t range;
edge_t() {}
edge_t(int from, int to, range_t range) : from(from), to(to), range(range) {}
};
const int MAXN = 1005;
int N;
int M;
vector<edge_t> adj[MAXN];
set<int> S;
int ub[MAXN];
inline range_t path(int lb) {
priority_queue<pair<int, int> > Q;
fill(ub, ub + MAXN, 0);
ub[1] = 1000000;
Q.push(pair<int, int>(ub[1], 1));
while (!Q.empty()) {
int v = Q.top().second;
if (Q.top().first != ub[v]) {
Q.pop();
continue;
} else
Q.pop();
for (vector<edge_t>::const_iterator e = adj[v].begin(); e != adj[v].end();
++e) {
int u = e->to;
int w = (e->range).rmax;
if ((e->range).rmin > lb || w < lb) continue;
if (min(ub[v], w) > ub[u]) {
ub[u] = min(ub[v], w);
Q.push(pair<int, int>(ub[u], u));
}
}
}
if (ub[N] < lb)
return range_t(0, 0);
else
return range_t(lb, ub[N]);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> N >> M;
for (int i = 0; i < M; ++i) {
int from, to, rmin, rmax;
cin >> from >> to >> rmin >> rmax;
adj[from].push_back(edge_t(from, to, range_t(rmin, rmax)));
adj[to].push_back(edge_t(to, from, range_t(rmin, rmax)));
S.insert(rmin);
}
range_t best(0, 0);
for (set<int>::const_iterator it = S.begin(); it != S.end(); ++it) {
range_t res = path(*it);
if (res.size() >= best.size()) best = res;
}
if (best.rmax != 0)
cout << best.size() << endl;
else
cout << "Nice work, Dima!" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<int> min_factor(1010);
void seive() {
for (int i = 1; i <= 1005; i++) min_factor[i] = i;
for (int i = 2; i * i <= 1005; i++) {
if (min_factor[i] != i) continue;
for (int j = i * i; j < 1005; j += i) {
min_factor[j] = i;
}
}
}
int main() {
long long t, n;
cin >> t;
seive();
while (t--) {
cin >> n;
vector<int> v(n);
vector<int> ans(n, -1);
unordered_map<int, vector<int>> mp;
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n; i++) mp[min_factor[v[i]]].push_back(i);
int cnt = 0;
for (auto it : mp) {
++cnt;
vector<int> x = it.second;
for (auto it : x) ans[it] = cnt;
}
cout << (int)mp.size() << "\n";
for (auto it : ans) cout << it << " ";
cout << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
long long h, w, i, j, p = 0;
cin >> h >> w;
char s[h][w];
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
cin >> s[i][j];
if (s[i][j] == '*') {
p++;
}
}
}
for (i = 1; i < h - 1; i++) {
for (j = 1; j < w - 1; j++) {
if (s[i][j] == '*' && s[i - 1][j] == '*' && s[i + 1][j] == '*' &&
s[i][j + 1] == '*' && s[i][j - 1] == '*' && j + 1 < w && i + 1 < h) {
long long x = i - 1, y = j, z = 1;
while (x >= 0) {
if (s[x][y] == '*') {
z++;
} else {
break;
}
x--;
}
x = i + 1;
y = j;
while (x < h) {
if (s[x][y] == '*') {
z++;
} else {
break;
}
x++;
}
x = i;
y = j - 1;
while (y >= 0) {
if (s[x][y] == '*') {
z++;
} else {
break;
}
y--;
}
x = i;
y = j + 1;
while (y <= w) {
if (s[x][y] == '*') {
z++;
} else {
break;
}
y++;
}
if (p == z) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
}
}
cout << "NO";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, r = 0;
cin >> n;
for (int i = 0; i < n; i++) {
char c;
cin >> c;
if (c == '(') {
r++;
cout << r % 2;
} else {
cout << r % 2;
r--;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int ans;
int main() {
int a[101010];
int b, c;
cin >> b >> c;
for (int i = 1; i <= b; ++i) {
cin >> a[i];
}
for (int i = 1; i <= b; ++i) {
if (a[i] > c)
ans += 2;
else if (a[i] <= c)
ans++;
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 200;
const int mod = 1e9 + 7;
int n, m, s, k, a[N];
int l[N], r[N];
int sum[N];
vector<int> g[1505];
int dp[1505][1505];
vector<pair<int, int> > f[1505];
inline int check(int x) {
for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + (a[i] <= x);
for (int r = 0; r <= n; ++r)
for (int j = 0; j <= s; ++j) dp[r][j] = -1e9;
dp[0][0] = 0;
for (int i = 0; i <= s; ++i) f[i].clear(), f[i].push_back({0, 0});
for (int r = 1; r <= n; ++r) {
int L = r + 1;
for (int l : g[r]) L = min(L, l);
for (int j = 0; j <= s; ++j) {
dp[r][j] = max(dp[r][j], dp[r - 1][j]);
if (j + 1 <= s && L <= r) {
int le = -1, ri = int(f[j].size()) - 1;
while (le + 1 < ri) {
int mi = le + ri >> 1;
if (f[j][mi].second >= L - 1)
ri = mi;
else
le = mi;
}
dp[r][j + 1] = max(dp[r][j + 1], f[j][ri].first + sum[r]);
}
}
for (int j = 0; j <= s; ++j) {
int val = dp[r][j] - sum[r];
while (f[j].size() && f[j].back().first <= val) f[j].pop_back();
f[j].push_back({val, r});
}
}
int ans = 0;
for (int i = 0; i <= m; ++i) ans = max(ans, dp[n][i]);
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s >> m >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= s; ++i) {
cin >> l[i] >> r[i];
int fail = 0;
for (int j = 1; j < i; ++j)
if (l[j] <= l[i] && r[i] <= r[j]) fail = 1;
if (!fail) {
g[r[i]].push_back(l[i]);
} else
--s, --i;
}
vector<int> all;
;
for (int i = 1; i <= n; ++i) all.push_back(a[i]);
sort(all.begin(), all.end());
all.resize(unique(all.begin(), all.end()) - all.begin());
int le = -1, ri = int(all.size());
while (le + 1 < ri) {
int mi = le + ri >> 1;
if (check(all[mi]) >= k)
ri = mi;
else
le = mi;
}
if (ri == all.size()) return cout << -1, 0;
cout << all[ri];
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long arr[n];
for (long long i = 0; i < n; ++i) cin >> arr[i];
long long max_index = 0, digi_add = arr[0] - 1;
for (long long i = 1; i < n; ++i) {
if (arr[i] > arr[max_index] && (arr[i] - (i + 1)) > digi_add) {
max_index = i;
digi_add = arr[i] - (i + 1);
}
}
cout << digi_add << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
long long read() {
long long f = 1, x = 0;
char ss = getchar();
while (ss < '0' || ss > '9') {
if (ss == '-') f = -1;
ss = getchar();
}
while (ss >= '0' && ss <= '9') {
x = x * 10 + ss - '0';
ss = getchar();
}
return f * x;
}
long long seed, vmax;
long long rnd() {
long long res = seed;
seed = (seed * 7 + 13) % 1000000007;
return res;
}
const int maxn = 100010;
int n, m;
long long a[maxn];
struct node {
int ll, rr;
mutable long long val;
node(int L, int R = -1, long long V = 0) : ll(L), rr(R), val(V) {}
bool operator<(const node &tt) const { return ll < tt.ll; }
};
set<node> st;
long long qpow(long long a, long long k, long long p) {
long long res = 1;
a %= p;
while (k > 0) {
if (k & 1) res = (res * a) % p;
a = (a * a) % p;
k >>= 1;
}
return res;
}
set<node>::iterator split(int pos) {
set<node>::iterator it = st.lower_bound(node(pos));
if (it != st.end() && it->ll == pos) return it;
--it;
int ll = it->ll, rr = it->rr;
long long val = it->val;
st.erase(it);
st.insert(node(ll, pos - 1, val));
return st.insert(node(pos, rr, val)).first;
}
void assign(int ll, int rr, long long val) {
set<node>::iterator itr = split(rr + 1), itl = split(ll);
st.erase(itl, itr);
st.insert(node(ll, rr, val));
}
void add(int ll, int rr, long long val) {
set<node>::iterator itr = split(rr + 1), itl = split(ll);
for (; itl != itr; ++itl) itl->val += val;
}
long long kth(int ll, int rr, int k) {
vector<pair<long long, int> > vec;
set<node>::iterator itr = split(rr + 1), itl = split(ll);
for (; itl != itr; ++itl)
vec.push_back(pair<long long, int>(itl->val, itl->rr - itl->ll + 1));
sort(vec.begin(), vec.end());
for (vector<pair<long long, int> >::iterator it = vec.begin();
it != vec.end(); ++it) {
k -= it->second;
if (k <= 0) return it->first;
}
return -1;
}
long long qsum(int ll, int rr, long long x, long long y) {
long long res = 0;
set<node>::iterator itr = split(rr + 1), itl = split(ll);
for (; itl != itr; ++itl)
res += (qpow(itl->val, x, y) * ((itl->rr - itl->ll + 1) % y)) % y, res %= y;
return res;
}
int main() {
n = read();
m = read();
seed = read();
vmax = read();
for (int i = 1; i <= n; ++i) {
a[i] = (rnd() % vmax) + 1;
st.insert(node(i, i, a[i]));
}
for (int i = 1; i <= m; ++i) {
int op = (rnd() % 4) + 1;
int ll = (rnd() % n) + 1, rr = (rnd() % n) + 1;
long long x, y;
if (ll > rr) swap(ll, rr);
if (op == 3)
x = (rnd() % (rr - ll + 1)) + 1;
else
x = (rnd() % vmax) + 1;
if (op == 4) y = (rnd() % vmax) + 1;
if (op == 1)
add(ll, rr, x);
else if (op == 2)
assign(ll, rr, x);
else if (op == 3)
printf("%lld\n", kth(ll, rr, x)), ++cnt;
else if (op == 4)
printf("%lld\n", qsum(ll, rr, x, y)), ++cnt;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string a;
cin >> a;
bool flag;
string poke[8] = {"vaporeon", "jolteon", "flareon", "espeon",
"umbreon", "leafeon", "glaceon", "sylveon"};
for (int j = 0; j < 8; j++) {
if (poke[j].size() == n) {
flag = true;
for (int i = 0; i < a.size(); i++) {
if (a[i] != '.') {
if (a[i] != poke[j][i]) {
flag = false;
break;
}
}
}
if (flag == true) {
cout << poke[j] << endl;
return 0;
}
}
}
return 0;
}
| 1 |
#include<stdio.h>
#include <map>
int main(){
int n,a,s,t,w,i;
for(;scanf("%d",&n),n;puts(!a?"Yes":"No")){
w=a=0;
std::multimap<int,int> m;
for(i=n;i--;m.insert(std::pair<int,int>(s,t)))
scanf("%d%d",&t,&s);
for(std::multimap<int,int>::iterator i=m.begin();i!=m.end();++i){
w+=i->second;
i->first<w?a=1:0;
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin >> q;
for (int i = 0; i < q; i++) {
long long l, r, sum;
cin >> l >> r;
if (l == r)
sum = l * pow(-1, l);
else {
if (l == 1) {
sum = (r * pow(-1, r + 2) - (r + 1) * pow(-1, r + 1) + -1) / 4;
} else {
int dl = 0, dc = 0;
long long k1 = 0, k2 = 0;
if (l % 2 == 0 && r % 2 == 0) {
dc += (r - l) / 2 + 1;
dl = (r - l) + 1 - dc;
k1 = (dc * (l + r)) / 2;
k2 = (dl * (-(l + 1) + -(r - 1))) / 2;
sum = k1 + k2;
} else if (l % 2 != 0 && r % 2 != 0) {
dl += (r - l) / 2 + 1;
dc = (r - l) + 1 - dl;
k1 = (dl * (-l - r)) / 2;
k2 = (dc * (l + 1 + r - 1)) / 2;
sum = k1 + k2;
} else if (l % 2 == 0 && r % 2 != 0) {
dc += (r - 1 - l) / 2 + 1;
dl = (r - l) + 1 - dc;
k1 = (dc * (l + r - 1)) / 2;
k2 = (dl * (-(l + 1) - r)) / 2;
sum = k1 + k2;
} else {
dl += (r - 1 - l) / 2 + 1;
dc = (r - l) + 1 - dl;
k1 = (dl * (-l - (r - 1))) / 2;
k2 = (dc * (l + 1 + r)) / 2;
sum = k1 + k2;
}
}
}
cout << sum << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 20;
int H, W;
char board[MAXN][MAXN + 1];
void init() {
scanf("%d%d", &H, &W);
for (int i = 0; i < H; ++i) {
scanf(" %s ", board[i]);
}
}
int grundy[2 * MAXN + 1][2 * MAXN + 1][2 * MAXN + 1][2 * MAXN + 1][2];
bool visited[2 * MAXN + 1][2 * MAXN + 1][2 * MAXN + 1][2 * MAXN + 1][2];
int calcGrundy(int plow, int pupper, int mlow, int mupper, int odd) {
if (visited[plow][pupper][mlow][mupper][odd]) {
return grundy[plow][pupper][mlow][mupper][odd];
}
visited[plow][pupper][mlow][mupper][odd] = true;
int& ret = grundy[plow][pupper][mlow][mupper][odd] = 0;
set<int> visibleState;
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x)
if ((x + y) % 2 == odd) {
const int plus = y + x, minus = y - x + W;
if (plow <= plus && plus < pupper && mlow <= minus && minus < mupper) {
const char cell = board[y][x];
int xo = 0;
if (cell == 'L') {
xo = calcGrundy(plow, plus, mlow, mupper, odd) ^
calcGrundy(plus + 1, pupper, mlow, mupper, odd);
}
if (cell == 'R') {
xo = calcGrundy(plow, pupper, mlow, minus, odd) ^
calcGrundy(plow, pupper, minus + 1, mupper, odd);
}
if (cell == 'X') {
xo = calcGrundy(plow, plus, mlow, minus, odd) ^
calcGrundy(plow, plus, minus + 1, mupper, odd) ^
calcGrundy(plus + 1, pupper, mlow, minus, odd) ^
calcGrundy(plus + 1, pupper, minus + 1, mupper, odd);
}
visibleState.insert(xo);
}
}
}
for (; visibleState.find(ret) != visibleState.end(); ++ret)
;
return ret;
}
bool solve() {
int ans =
calcGrundy(0, H + W, 0, H + W, 0) ^ calcGrundy(0, H + W, 0, H + W, 1);
return ans != 0;
}
int main() {
init();
puts(solve() ? "WIN" : "LOSE");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n;
cin >> k;
int l, r;
int x = 0;
for (int i = 1; i <= n; i++) {
cin >> l >> r;
x = x + (r - l + 1);
}
int m;
m = x % k;
int moves;
if (m == 0)
moves = 0;
else
moves = k - m;
cout << moves << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int S = 1500;
struct bigint {
char d[S];
bigint() { memset(d, 0, sizeof(d)); }
void normalize() {
int r = 0;
for (int i = 0; i < S; ++i) {
d[i] += r;
r = d[i] / 10;
d[i] %= 10;
}
}
void output() {
int first = 0;
for (int i = S - 1; i >= 0; --i)
if (d[i] > 0) {
first = i;
break;
}
for (int i = first; i >= 0; --i) printf("%d", d[i]);
}
};
bigint operator+(bigint &a, bigint &b) {
bigint c;
for (int i = 0; i < int(S); ++i) c.d[i] = a.d[i] + b.d[i];
c.normalize();
return c;
}
bigint operator*(const bigint &a, int x) {
bigint c;
for (int i = 0; i < int(S); ++i) c.d[i] = a.d[i] * x;
c.normalize();
return c;
}
bool operator<(const bigint &a, const bigint &b) {
for (int i = S - 1; i >= 0; --i)
if (a.d[i] != b.d[i]) return a.d[i] < b.d[i];
return false;
}
const int N = 5000 + 5;
bigint dp[N], p2[2005];
int n, pos[2005];
bool read() {
if (!(cin >> n)) return false;
return true;
}
void solve() {
dp[0] = bigint();
memset(pos, -1, sizeof(pos));
p2[0].d[0] = 1;
for (int i = 0; i < int(2002); ++i) p2[i + 1] = p2[i] * 2;
for (int i = 0; i < int(n); ++i) {
string s;
int t;
cin >> s >> t;
dp[i + 1] = dp[i];
if (s == "win") {
pos[t] = i + 1;
} else {
if (pos[t] != -1) {
bigint ndp = dp[pos[t]] + p2[t];
if (dp[i + 1] < ndp) dp[i + 1] = ndp;
}
}
}
dp[n].output();
}
int main() {
while (read()) solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read() {
int ret = 0, sgn = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * sgn;
}
inline void Out(int a) {
if (a > 9) Out(a / 10);
putchar(a % 10 + '0');
}
long long __gcd(long long a, long long b) {
return b == 0 ? a : __gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / __gcd(a, b); }
long long qpow(long long x, long long n, long long mod) {
long long res = 1;
while (n) {
if (n & 1) res = (res * x) % mod;
x = x * x % mod, n >>= 1;
}
return res;
}
const int N = 1e6 + 50;
int n, m;
int a[N];
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
a[0] = 1;
for (int i = 1; i <= n + 1; i++) a[i] = 0;
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
a[x]++;
}
int ans = 1;
for (int i = 1; i <= n; i++) {
a[i] += a[i - 1];
if ((a[i] - 1) >= i) ans = a[i];
}
printf("%d\n", ans);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, MOD = 1000000007, a[100001], q1[100001], q2[100001], fail[100001],
found[100001];
string s, t;
void kmp() {
int i, j, k;
j = fail[0] = -1;
for (i = 1; i <= m; i++) {
while (j >= 0 and t[j] != t[i - 1]) j = fail[j];
fail[i] = j + 1;
j++;
}
i = 0, j = 0, k = 0;
for (; i < n; i++) {
while (k >= 0 and t[k] != s[i]) k = fail[k];
if (++k >= m) {
k = fail[k];
found[i] = 1;
}
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> s;
cin >> t;
n = s.length(), m = t.length();
kmp();
int i;
for (i = 0; i < m - 1; i++) {
a[i] = q1[i] = q2[i] = 0;
}
if (m == 1 and s[0] == t[0]) {
a[0] = q1[0] = q2[1] = 1;
}
for (; i <= n; i++) {
if (found[i - 1] == 0) {
a[i] = a[i - 1];
} else {
a[i] = (q2[i - m] + i - m + 1) % MOD;
}
q1[i] = (a[i] + q1[i - 1]) % MOD;
q2[i] = (q2[i - 1] + q1[i]) % MOD;
}
cout << q1[n] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
string d;
cin >> d;
bool f = true;
for (int i = 0; i < d.size(); i++) {
if (d[i] == '0') {
d.erase(i, 1);
f = false;
break;
}
}
if (f) {
d.erase(0, 1);
}
cout << d << endl;
return EXIT_SUCCESS;
}
| 3 |
// 基本テンプレート
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
typedef pair<int, int> pii;
typedef long long ll;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
int N;
map< vector<int>, int > dp;
int solve(vector<int>& vec, int sum = 0, int id = 1) {
int res = 0;
if(sum == 10) {
res++;
sum = 0; id = 1;
}
if(sum == 0 and dp.count(vec)) return dp[vec];
if(id > 9 or sum + id > 10) return 0;
int add = 0;
if(vec[id] > 0) {
vec[id]--;
chmax(add, solve(vec, sum + id, id));
vec[id]++;
}
chmax(add, solve(vec, sum, id + 1));
res += add;
if(sum == 0) dp[vec] = res;
return res;
}
signed main() {
while(cin >> N, N) {
dp.clear();
vector<int> countManju(11);
for(int i=0; i<N; i++) {
int val; cin >> val;
countManju[val]++;
}
int ans = 0;
for(int i=1; i<5; i++) {
int add = min(countManju[i], countManju[10 - i]);
ans += add;
countManju[i ] -= add;
countManju[10 - i] -= add;
}
ans += countManju[5] / 2;
countManju[5] %= 2;
ans += solve(countManju);
cout << ans << endl;
}
return 0;
}
| 0 |
#include<iostream>
using namespace std;
int main()
{
int h, r;
cin >> h >> r;
if( h + r == 0 )
cout << 0 << endl;
else if( h + r > 0 )
cout << 1 << endl;
else
cout << -1 << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 150005;
string str;
int solve(int l, int r, char t) {
if (l >= r) return str[l] != t;
int mid = l + r >> 1;
int res = 0;
int res1 = solve(l, mid, (char)(t + 1));
int res2 = solve(mid + 1, r, (char)(t + 1));
int cnt = 0;
for (int i = mid + 1; i <= r; i++)
if (str[i] != t) cnt++;
res = cnt + res1;
cnt = 0;
for (int i = l; i <= mid; i++)
if (str[i] != t) cnt++;
res = min(res, cnt + res2);
return res;
}
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
cin >> str;
str = ' ' + str;
cout << solve(1, n, 'a') << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int N;
struct Node {
int a, len, l, r;
Node(int a = 0, int len = 0, int l = 0, int r = 0)
: a(a), len(len), l(l), r(r) {}
bool operator<(const Node& n1) const { return a < n1.a; }
} n[305];
int c[305];
int dp[305][305];
int main() {
cin >> N;
int top = 0;
for (int i = 1; i <= N; i++) {
cin >> n[i].a >> n[i].len;
n[i].l = n[i].a - n[i].len;
n[i].r = n[i].a + n[i].len;
c[++top] = n[i].a;
c[++top] = n[i].l;
c[++top] = n[i].r;
}
sort(c + 1, c + 1 + top);
top = unique(c + 1, c + 1 + top) - c - 1;
for (int i = 1; i <= top; i++) {
}
for (int i = 1; i <= N; i++) {
n[i].a = lower_bound(c + 1, c + 1 + top, n[i].a) - c;
n[i].l = lower_bound(c + 1, c + 1 + top, n[i].l) - c;
n[i].r = lower_bound(c + 1, c + 1 + top, n[i].r) - c;
}
sort(n + 1, n + 1 + N);
int a, l, r;
for (int i = 1; i <= N; i++) {
a = n[i].a;
l = n[i].l;
r = n[i].r;
for (int j = a; j <= r; j++) {
dp[i][j] = max(dp[i][j], dp[i - 1][a] + (c[j] - c[a]));
}
int rmax = a;
for (int k = i - 1; k >= 1; k--) {
rmax = max(rmax, n[k].r);
for (int j = a; j <= rmax; j++) {
dp[i][j] = max(dp[i][j], dp[k - 1][l] + (c[j] - c[l]));
}
}
for (int j = a; j > l; j--) {
dp[i][j] = max(dp[i][j], dp[i - 1][l] + (c[j] - c[l]));
}
for (int j = 1; j <= top; j++) {
dp[i][j] = max(dp[i][j], dp[i][j - 1]);
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
}
}
cout << dp[N][top] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main(){
string S;
cin >> S;
for(int i = 0; i < S.size(); i++){
if((i % 2 == 0 && S[i] == 'L') || (i % 2 == 1 && S[i] == 'R')){
printf("No\n");
return 0;
}
}
printf("Yes\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int parition(vector<pair<int, int>> &song_size, int low, int high) {
int pivot = song_size[high].first - song_size[high].second;
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (song_size[j].first - song_size[j].second >
song_size[high].first - song_size[high].second) {
i++;
swap(song_size[i], song_size[j]);
}
}
swap(song_size[i + 1], song_size[high]);
return i + 1;
}
void quicksort(vector<pair<int, int>> &song_size, int low, int high) {
if (low < high) {
int pi = parition(song_size, low, high);
quicksort(song_size, low, pi - 1);
quicksort(song_size, pi + 1, high);
}
}
void process() {
int n;
long long m;
cin >> n >> m;
vector<pair<int, int>> song_size(n);
long long max_sum = 0;
long long min_sum = 0;
for (int i = 0; i < n; i++) {
cin >> song_size[i].first >> song_size[i].second;
max_sum += song_size[i].first;
min_sum += song_size[i].second;
}
if (max_sum <= m) {
cout << 0;
return;
}
if (min_sum == m) {
cout << n;
return;
} else if (min_sum > m) {
cout << -1;
return;
}
quicksort(song_size, 0, n - 1);
int i = 0;
long long current_sum = max_sum;
while (i < n) {
current_sum = current_sum - (song_size[i].first - song_size[i].second);
if (current_sum <= m) {
cout << i + 1;
return;
}
i++;
}
}
int main() {
process();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
int A, cnt = 0;
while (cin >> A) if (A % 2) cnt++;
cout << (!(cnt % 2) ? "YES" : "NO") << "\n";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[n];
for(int i=0;i<n;i++){
cin >> a[i];
}
sort(a,a+n);
int ls=a[n-1];
int temp,count=0,maxcnt=0;
if(a[0]>=1){
cout << 1 << endl;
}
else{
int kt=n-1;
int f1=1;
while(a[kt]>0){
kt--;
f1*=0;
}
if(f1==0){
kt++;
}
for(int k=kt;k>=0 && k>=maxcnt;k--){
ls=a[k];
if(ls>0){
count=0;
for(int i=k;i>=0;i--){
// cout << "<" << ls << ">";
// cout << "==" << a[i];
temp=a[i];
count++;
int j=i-1;
while(abs(temp-a[j])<ls && j>=0){
j--;
}
j++;
if(j==-1 && abs(temp-a[j+1])>=ls){
count++;
}
i=j;
}
// cout << endl;
}
else{
count=k+1;
}
// cout << a[k] << " " << count << endl;
maxcnt=max(count,maxcnt);
if(ls<=0){
break;
}
int flag=1;
while(a[k-1]==ls && k>=0){
k--;
flag*=0;
}
if(flag==0){
k++;
}
}
cout << maxcnt << endl;
}
}
return 0;
} | 2 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const vector<int> vec(3, -1);
vector<vector<int>> trie(1, vec);
vector<int> cnt(1, 0);
void add() {
string s;
cin >> s;
int v = 0, n = s.size();
for (int i = 0; i < n; i++) {
int c = s[i] - 'a';
if (trie[v][c] == -1) {
trie.push_back(vec);
cnt.push_back(0);
trie[v][c] = trie.size() - 1;
}
v = trie[v][c];
}
cnt[v]++;
}
bool dfs(int v, int ind, string& s, bool used) {
if (ind == s.size()) return used && (cnt[v] > 0);
bool ans = 0;
for (long long i = (long long)0; i != (long long)3; i = i + 1) {
if (trie[v][i] == -1) continue;
if (s[ind] - 'a' == i)
ans = ans || dfs(trie[v][i], ind + 1, s, used);
else if (!used)
ans = ans || dfs(trie[v][i], ind + 1, s, true);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
for (long long i = (long long)0; i != (long long)n; i = i + 1) {
add();
}
for (long long i = (long long)0; i != (long long)m; i = i + 1) {
string s;
cin >> s;
if (dfs(0, 0, s, false)) {
cout << "YES" << '\n';
} else {
cout << "NO" << '\n';
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, count_of_0 = 0, idx1 = 1, idx2 = 1;
cin >> n;
vector<int> bal(n + 1, 0);
vector<int> balance(n + 1);
vector<int> prefix_count_1(n + 1, 0);
vector<int> prefix_count_2(n + 1, 0);
vector<int> vector_of_1;
vector<int> vector_of_0 = {0};
int now = 0, mn = 0, idx = 0;
for (int i = 0; i < n; ++i) {
char t;
cin >> t;
if (t == '(')
++now;
else
--now;
bal[i + 1] = now;
if (now < mn) mn = now, idx = i + 1;
}
if (bal[n] != 0) {
cout << 0 << "\n" << 1 << " " << 1;
return 0;
}
for (int i = idx; i <= idx + n; ++i) balance[i - idx] = bal[i % n] - mn;
for (int i = 1; i <= n; ++i) {
prefix_count_1[i] = prefix_count_1[i - 1];
prefix_count_2[i] = prefix_count_2[i - 1];
if (balance[i] == 2) prefix_count_2[i]++;
if (balance[i] == 1) {
vector_of_1.push_back(i);
prefix_count_1[i]++;
}
if (balance[i] == 0) {
vector_of_0.push_back(i);
count_of_0++;
}
}
int ans = count_of_0;
for (int i = 0; i < vector_of_0.size() - 1; ++i) {
int i1 = vector_of_0[i];
int i2 = vector_of_0[i + 1];
if (ans < prefix_count_1[i2] - prefix_count_1[i1]) {
ans = prefix_count_1[i2] - prefix_count_1[i1];
idx1 = i1;
idx2 = i2 - 1;
}
}
for (int i = 0; i < vector_of_1.size() - 1; ++i) {
int i1 = vector_of_1[i];
int i2 = vector_of_1[i + 1];
if (ans < count_of_0 + prefix_count_2[i2] - prefix_count_2[i1]) {
ans = prefix_count_2[i2] - prefix_count_2[i1] + count_of_0;
idx1 = i1;
idx2 = i2 - 1;
}
}
cout << ans << "\n" << (idx1 + idx) % n + 1 << " " << (idx2 + idx) % n + 1;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
const long long N = 3e3 + 7;
vector<long long> ad[N];
long long n, m;
long long a[N];
long long dp[N][N];
long long f[N][N];
long long g[N], t[N], sz[N];
void get(long long v, long long p) {
sz[v] = 1;
f[v][1] = a[v];
for (auto u : ad[v])
if (u != p) {
get(u, v);
for (long long i = 0; i < N; i++) t[i] = -1e18;
memset(g, 0, sizeof g);
for (long long i = 1; i <= sz[u]; i++)
for (long long j = 1; j <= sz[v]; j++) {
if (g[i + j] < dp[u][i] + dp[v][j] + (f[u][i] > 0) ||
(g[i + j] == dp[u][i] + dp[v][j] + (f[u][i] > 0) &&
t[i + j] < f[v][j])) {
g[i + j] = dp[u][i] + dp[v][j] + (f[u][i] > 0);
t[i + j] = f[v][j];
}
if (g[i + j - 1] < dp[u][i] + dp[v][j] ||
(g[i + j - 1] == dp[u][i] + dp[v][j] &&
t[i + j - 1] < f[u][i] + f[v][j])) {
g[i + j - 1] = dp[u][i] + dp[v][j];
t[i + j - 1] = f[u][i] + f[v][j];
}
}
sz[v] += sz[u];
for (long long i = 0; i < N; i++) {
dp[v][i] = g[i];
f[v][i] = t[i];
}
}
}
void solve() {
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
ad[i].clear();
a[i] = 0;
memset(dp[i], 0, sizeof dp[i]);
}
for (long long i = 1; i <= n; i++) {
long long t;
cin >> t;
a[i] -= t;
}
for (long long i = 1; i <= n; i++) {
long long t;
cin >> t;
a[i] += t;
}
for (long long i = 1; i < n; i++) {
long long v, u;
cin >> v >> u;
ad[v].push_back(u);
ad[u].push_back(v);
}
get(1, 0);
cout << dp[1][m] + (f[1][m] > 0) << '\n';
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long q;
cin >> q;
while (q--) {
solve();
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
bool parse_string(string cur){
int L = 0 ,R = cur.length()-1;
int ptr = cur.length()-1;
while(L<R){
char alphabet = ('a'+(ptr--));
//bacout<<alphabet<<endl;
if(alphabet==cur[R])R--;
else if(alphabet==cur[L])L++;
else
return false;
}
return cur[L]=='a';
}
int main(){
int testcase;
cin>>testcase;
while(testcase--){
string tmp;
cin>>tmp;
cout<<((parse_string(tmp))?"Yes":"No")<<endl;
}
return 0;
} | 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,x,t;
cin>>n>>x>>t;
n = ceil(n/double(x));
cout << n*t;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class t>
inline t read(t &x) {
char c = getchar();
bool f = 0;
x = 0;
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
if (f) x = -x;
return x;
}
template <class t>
inline void write(t x) {
if (x < 0)
putchar('-'), write(-x);
else {
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
}
const int MOD = 998244353;
int N;
int D[300005][3];
vector<int> con[300005];
void dp(int n, int p) {
D[n][0] = D[n][2] = 1;
D[n][1] = 0;
int m = ((int)(con[n]).size());
if (!m) return;
vector<int> a1(m), a2(m);
for (int i = 0; i < m; i++) {
int t = con[n][i];
a1[i] = a2[i] = 1;
if (t == p) continue;
dp(t, n);
a1[i] = a2[i] = (D[t][0] + D[t][1]) % MOD;
D[n][0] = (long long)D[n][0] * D[t][0] % MOD;
}
for (int i = 1; i < m; i++) a1[i] = (long long)a1[i] * a1[i - 1] % MOD;
for (int i = m - 1; i--;) a2[i] = (long long)a2[i] * a2[i + 1] % MOD;
for (int i = 0; i < m; i++) {
int t = con[n][i];
if (t == p) continue;
D[n][1] = (D[n][1] + (long long)D[t][2] * (i > 0 ? a1[i - 1] : 1) % MOD *
(i + 1 < m ? a2[i + 1] : 1)) %
MOD;
}
D[n][2] = a2[0];
D[n][0] = (D[n][0] + D[n][1]) % MOD;
}
int main() {
read(N);
for (int i = 1; i < N; i++) {
int a, b;
read(a);
read(b);
con[a].push_back(b);
con[b].push_back(a);
}
dp(1, 0);
printf("%d\n", D[1][0]);
}
| 6 |
#include<bits/stdc++.h>
#define N 100
#define M 26
using namespace std;
string s;
int p,len;
string bnf(bool rev){
string res;
int cnt=0;
while(p!=len){
if(s[p]=='+')p++,cnt++;
if(s[p]=='-')p++,cnt--;
if('A'<=s[p]&&s[p]<='Z')
res+=((s[p++]+cnt+M*N)-'A')%M+'A',cnt=0;
if(s[p]=='?')res+=s[p++],cnt=0;
if(s[p]=='[')p++,res+=bnf(true);
if(s[p]==']'){
p++;
break;
}
}
if(rev)reverse(res.begin(),res.end());
return res;
}
int main(){
while(1){
cin>>s;
if(s==".")break;
len=s.size();
p=0;
string ans=bnf(false);
for(int i=0;i<(int)ans.size();i++)
if(ans[i]=='?')ans[i]='A';
cout<<ans<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool isprime(long long n) {
if (n == 2) return true;
if (n < 2 || n % 2 == 0) return false;
for (int i = 3; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long gcd(long long a, long long b) {
if (a < b) swap(a, b);
while (a != 0 && b != 0) {
long long r = a % b;
a = b;
b = r;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
fast();
int T, N, K, dump;
cin >> T;
while (T--) {
int sum = 0;
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> dump;
sum += dump;
}
if (sum == K)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
return 0;
}
| 1 |
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
long long n;
int ans;
int main()
{
scanf("%lld",&n);
n++;
while(n>=10)
{
ans+=9;
n/=10;
}
ans+=n-1;
printf("%d",ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void smax(T &x, T y) {
x = max((x), (y));
}
template <class T>
inline void smin(T &x, T y) {
x = min((x), (y));
}
inline void sc(long long &x) { cin >> x; }
void sc(char &x) { scanf("%c", &x); }
void sc(long long &x, long long &y) {
sc(x);
return sc(y);
}
void sc(long long &x, long long &y, long long &z) {
sc(x);
sc(y);
return sc(z);
}
const double eps = 1e-7;
long long n, k, m, x, s;
pair<long long, long long> A[2000005], B[2000005];
char str[2000005];
int main() {
std::ios::sync_with_stdio(false);
sc(n, m, k);
sc(x, s);
for (long long i = 0; i < m; i++) sc(A[i].second);
for (long long i = 0; i < m; i++) sc(A[i].first);
A[m++] = {0, x};
for (long long i = 0; i < k; i++) sc(B[i].second);
for (long long i = 0; i < k; i++) sc(B[i].first);
long long ans = x * n;
for (int i = 0; i < m; i++) {
if (s - A[i].first < 0) continue;
long long less = 0;
auto it = lower_bound(B, B + k, make_pair(s - A[i].first, LLONG_MAX)) - B;
if (it > 0) less = B[it - 1].second;
;
;
smin(ans, A[i].second * (n - less));
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
char a[N][N];
int T, n, m, id[N][N];
int head[N * N], ver[N * N << 1], Next[N * N << 1], tot;
int deg[N * N], dp[N * N];
pair<int, int> sta[N * N];
queue<int> q;
inline void add(int x, int y) {
deg[y]++;
ver[++tot] = y;
Next[tot] = head[x];
head[x] = tot;
}
inline void topsort() {
while (!q.empty()) q.pop();
for (int i = 1; i <= n * m; i++)
if (!deg[i]) q.push(i), dp[i] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = head[x], y; i; i = Next[i]) {
y = ver[i];
if (dp[y] < dp[x] + 1) dp[y] = dp[x] + 1, sta[y] = sta[x];
if (!--deg[y]) q.push(y);
}
}
}
inline pair<int, pair<int, int>> bfs(int a, int b) {
int s = id[a][b];
int mx = 0, siz = 0;
pair<int, int> ss;
while (!q.empty()) q.pop();
q.push(s);
while (!q.empty()) {
int x = q.front();
q.pop(), deg[x] = 0;
siz++;
if (dp[x] > mx) mx = dp[x], ss = sta[x];
for (int i = head[x], y; i; i = Next[i]) {
if (!deg[y = ver[i]]) continue;
q.push(y);
}
}
if (!mx) return {siz, {a, b}};
return {mx + siz - 1, ss};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> T;
while (T--) {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> (a[i] + 1);
for (int i = 1, k = 0; i <= n; i++)
for (int j = 1; j <= m; j++) id[i][j] = ++k, sta[k] = {i, j};
memset(head + 1, 0, sizeof(int) * n * m), tot = 0;
memset(deg + 1, 0, sizeof(int) * n * m);
memset(dp + 1, 0, sizeof(int) * n * m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (a[i][j] == 'U' && i > 1) add(id[i][j], id[i - 1][j]);
if (a[i][j] == 'D' && i < n) add(id[i][j], id[i + 1][j]);
if (a[i][j] == 'L' && j > 1) add(id[i][j], id[i][j - 1]);
if (a[i][j] == 'R' && j < m) add(id[i][j], id[i][j + 1]);
}
topsort();
int x = 0, y = 0, ans = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (deg[id[i][j]]) {
auto res = bfs(i, j);
if (res.first > ans) {
ans = res.first;
x = res.second.first, y = res.second.second;
}
} else {
if (dp[id[i][j]] > ans) {
ans = dp[id[i][j]];
x = sta[id[i][j]].first, y = sta[id[i][j]].second;
}
}
}
cout << x << " " << y << " " << ans << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
long long modPow(long long a, long long b) {
if (b == 0) return 1LL;
if (b == 1) return a % M;
long long res = 1;
while (b) {
if (b % 2 == 1) res = mul(res, a);
a = mul(a, a);
b = b / 2;
}
return res;
}
int a[105][105];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cin >> a[i][j];
}
long long total = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int other1 = a[i][m - j - 1];
int other2 = a[n - i - 1][j];
vector<int> b;
b.push_back(a[i][j]);
b.push_back(other1);
b.push_back(other2);
sort(b.begin(), b.end());
a[i][j] = a[i][m - j - 1] = a[n - i - 1][j] = b[1];
total += (long long)(b[2] - b[1]) + (b[1] - b[0]);
}
}
cout << (total) << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(10);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long mcm(long long a, long long b) { return (a * b) / gcd(a, b); }
bool comp(long long a, long long b) { return a > b; }
void solve() {
int n;
cin >> n;
vector<int> a(n);
vector<int> b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
if (a[0] != b[0]) {
cout << "NO\n";
} else {
vector<pair<bool, bool>> posneg(n + 10, pair<bool, bool>(0, 0));
for (int i = 1; i < n; i++) {
if (a[i - 1] == -1 || posneg[i - 1].second) {
posneg[i].second = true;
}
if (a[i - 1] == 1 || posneg[i - 1].first) {
posneg[i].first = true;
}
}
for (int i = 1; i < n; i++) {
if (a[i] != b[i]) {
if (a[i] > b[i]) {
if (!posneg[i].second) {
cout << "NO\n";
return;
}
} else {
if (a[i] < b[i]) {
if (!posneg[i].first) {
cout << "NO\n";
return;
}
}
}
}
}
cout << "YES\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct Matrix {
int a[6][6];
int* operator[](int x) { return a[x]; }
} m[800005];
int n, q, a[200005];
Matrix init(int x) {
Matrix ans;
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++) ans[i][j] = 1000000000;
for (int i = 1; i <= 5; i++) ans[i][i] = 0;
if (x == 0) ans[2][2] = 1, ans[3][2] = 0;
if (x == 1) ans[3][3] = 1, ans[4][3] = 0;
if (x == 2) ans[1][1] = 1, ans[2][1] = 0;
if (x == 6) ans[5][5] = 1, ans[4][4] = 1;
if (x == 7) ans[4][4] = 1, ans[5][4] = 0;
return ans;
}
Matrix operator*(Matrix x, Matrix y) {
Matrix ans;
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++) ans[i][j] = 1000000000;
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)
for (int k = 1; k <= 5; k++)
ans[i][j] = min(ans[i][j], x[i][k] + y[k][j]);
return ans;
}
void build(int k, int l, int r) {
if (l == r) {
m[k] = init(a[l]);
return;
}
int mid = (l + r) / 2;
build(2 * k, l, mid);
build(2 * k + 1, mid + 1, r);
m[k] = m[2 * k + 1] * m[2 * k];
return;
}
Matrix query(int k, int l, int r, int ql, int qr) {
if (ql <= l && qr >= r) {
return m[k];
}
int mid = (l + r) / 2;
if (ql <= mid && qr > mid)
return query(2 * k + 1, mid + 1, r, ql, qr) * query(2 * k, l, mid, ql, qr);
if (ql <= mid) return query(2 * k, l, mid, ql, qr);
if (qr > mid) return query(2 * k + 1, mid + 1, r, ql, qr);
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
char tmp;
scanf(" %c", &tmp);
a[i] = tmp - '0';
}
build(1, 1, n);
for (int i = 1; i <= q; i++) {
int l, r;
scanf("%d%d", &l, &r);
Matrix tmp = query(1, 1, n, l, r);
if (tmp[5][1] > r - l + 1)
printf("-1\n");
else
printf("%d\n", tmp[5][1]);
}
}
| 5 |
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
using namespace std;
int w,h;
char inpMap[51][51]={'0'},ctemp;
const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
void dfs(int y,int x,char moji){
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(0<=nx && nx<w && 0<=ny && ny<h){
if(inpMap[ny][nx]=='.'){
inpMap[ny][nx]=ctemp;//(moji=='W')? 'w' : 'b';
dfs(ny,nx,moji);
}
else if((moji=='B'&&inpMap[ny][nx]=='w')||(moji=='W'&&inpMap[ny][nx]=='b')){
inpMap[ny][nx]='?';
dfs(ny,nx,moji);
}
}
}
}
void solve(int y, int x,char moji){
if(moji=='W'){
ctemp='w';
}
else if(moji=='B'){
ctemp='b';
}
dfs(y,x,moji);
}
int main(){
while(cin>>w>>h,w||h){
int white=0,black=0;
fill((int *)inpMap,(int *)inpMap+lengthof(inpMap),'0');
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>inpMap[i][j];
}
}
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(inpMap[i][j]=='W' || inpMap[i][j]=='B'){
solve(i,j,inpMap[i][j]);
}
}
}
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(inpMap[i][j]=='b'){
black++;
}
else if(inpMap[i][j]=='w'){
white++;
}
}
}
cout<<black<<' '<<white<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int n = 0;
bool b = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') b = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
n = (n << 1) + (n << 3) + (c ^ 48);
c = getchar();
}
return b ? -n : n;
}
const int N = 3e6 + 7;
struct node {
int to, next;
} e[N << 1];
int cnt, head[N];
int fa[N], res, t, dis[N];
bool vis[N], vis2[N];
inline void add(int u, int v) {
e[++cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) {
int fx = find(x), fy = find(y);
fa[fx] = fy;
}
inline void dfs(int x, int val) {
if (res < val) res = val, t = x;
for (int i = head[x]; i; i = e[i].next) {
int v = e[i].to;
if (!vis[v]) vis[v] = 1, dfs(v, val + 1);
}
vis[x] = 0;
}
inline void dfs2(int x, int val) {
if (res < val) res = val;
for (int i = head[x]; i; i = e[i].next) {
int v = e[i].to;
if (!vis[v]) {
vis[v] = 1;
dfs2(v, val + 1);
}
}
vis[x] = 0;
}
int main() {
int n = read(), m = read(), Q = read();
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
add(x, y);
add(y, x);
merge(x, y);
}
for (int i = 1; i <= n; i++) {
int x = find(i);
if (vis2[x]) continue;
res = -1;
vis[x] = 1;
dfs(x, 0);
res = -1;
vis[t] = 1;
dfs2(t, 0);
vis2[x] = 1;
dis[x] = res;
}
for (int i = 1; i <= Q; i++) {
int opt = read(), x = read();
if (opt == 1)
printf("%d\n", dis[find(x)]);
else {
int y = read();
int fx = find(x), fy = find(y);
if (fx == fy) continue;
dis[fy] =
max(max((dis[fx] + 1) / 2 + (dis[fy] + 1) / 2 + 1, dis[fx]), dis[fy]);
merge(x, y);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using std::cin;
using std::cout;
long long MOD = 998244353;
int main() {
long long x;
cin >> x;
long long m = 1000000;
vector<pair<long long, long long> > sets;
for (long long i = 1; i < 2000005; i++) {
long long s = 6 * x + i * i * i - i;
m = (3 * i + 3) * i;
if (!(s % m) && i <= s / m) {
sets.push_back({i, s / m});
if (s / m != i) sets.push_back({s / m, i});
}
}
sort(sets.begin(), sets.end());
cout << sets.size() << endl;
for (int i = 0; i < sets.size(); i++) {
cout << sets[i].first << " " << sets[i].second << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &item) {
out << '(' << item.first << ", " << item.second << ')';
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
for (const auto &item : v) out << item << ' ';
return out;
}
const int N = 100010;
const int B = 666013;
const int SZ = 300;
const int NRB = N / SZ + 5;
struct Block {
int i, l, r;
Block(int i, int l, int r) : i(i), l(l), r(r) {}
};
int bid[N], s[N], aux[SZ + 5];
int nOpen[NRB], nClosed[NRB];
ull pw[N], open[NRB][SZ + 5], closed[NRB][SZ + 5];
bool ok[N];
ull getHash(ull h[], int l, int r) { return h[r] - h[l] * pw[r - l]; }
int genStack(int l, int r) {
if (l >= N) return -2;
int top = -1;
for (int p = l; p < r && s[p]; ++p) {
if (s[p] > 0)
aux[++top] = s[p];
else {
if (top == -1 || aux[top] < 0)
aux[++top] = s[p];
else if (aux[top] != -s[p])
return -2;
else
--top;
}
}
return top;
}
void makeHashes(int i, int top) {
int p;
for (p = 0; p <= top && aux[p] < 0; ++p)
closed[i][p + 1] = closed[i][p] * B - aux[p];
nClosed[i] = p;
nOpen[i] = top + 1 - p;
reverse(aux + p, aux + p + nOpen[i]);
for (int j = 0; p + j <= top; ++j)
open[i][j + 1] = open[i][j] * B + aux[p + j];
}
void buildBlock(int i) {
int top = genStack(i * SZ, (i + 1) * SZ);
if (top == -2) return void(ok[i] = false);
ok[i] = true;
makeHashes(i, top);
}
void update(int p, int t) {
s[p] = t;
buildBlock(bid[p]);
}
bool process(int i, vector<Block> &v) {
int a = 0, b = nClosed[i];
while (a < b && !v.empty()) {
auto &lst = v.back();
int d = min(b - a, lst.r - lst.l);
if (lst.r - lst.l <= b - a) {
if (getHash(open[lst.i], lst.l, lst.r) == getHash(closed[i], a, a + d))
a += d, v.pop_back();
else
return false;
} else {
if (getHash(open[lst.i], lst.l, lst.l + d) == getHash(closed[i], a, b))
lst.l += d, a = b;
else
return false;
}
}
if (a < b) return false;
if (nOpen[i]) v.emplace_back(i, 0, nOpen[i]);
return true;
}
bool query(int l, int r) {
if (bid[l] == bid[r - 1]) return genStack(l, r) == -1;
int lid = NRB - 2, rid = NRB - 1;
int top = genStack(bid[l] * SZ + l % SZ, (bid[l] + 1) * SZ);
if (top == -2) return false;
makeHashes(lid, top);
top = genStack(bid[r] * SZ, bid[r] * SZ + r % SZ);
if (top == -2) return false;
makeHashes(rid, top);
vector<Block> v;
if (!process(lid, v)) return false;
for (int i = bid[l] + 1; i < bid[r]; ++i) {
if (!ok[i]) return false;
if (!process(i, v)) return false;
}
if (!process(rid, v)) return false;
return v.empty();
}
int main() {
ios_base::sync_with_stdio(false);
pw[0] = 1;
for (int i = 1; i < N; ++i) pw[i] = pw[i - 1] * B;
for (int i = 0; i < N; ++i) bid[i] = i / SZ;
int n, k, q, type, p, t, l, r;
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> s[i];
for (int i = 0; i < NRB; ++i) buildBlock(i);
for (cin >> q; q; --q) {
cin >> type;
if (type == 1) {
cin >> p >> t;
update(p - 1, t);
} else {
cin >> l >> r;
cout << (query(l - 1, r) ? "Yes" : "No") << '\n';
}
}
return 0;
}
| 6 |