solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
string a, b, c;
int a1, b1, c1;
bool check(int x, int y, int z) {
if (x == 8 && y == z && y == 5) return true;
return false;
}
bool check1(int x, int y, int z) {
if (x == 8 && y == z && y == 4) return true;
return false;
}
bool check2(int x, int y, int z) {
if (x == 5 && y == z && y == 4) return true;
return false;
}
bool check3(int x, int y, int z) {
if (x == 4 && y == z && y == 5) return true;
return false;
}
bool check4(int x, int y, int z) {
if (x == 4 && y == z && y == 8) return true;
return false;
}
bool check5(int x, int y, int z) {
if (x == 5 && y == z && y == 8) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> a >> b >> c;
a1 = a.size(), b1 = b.size(), c1 = c.size();
if (a1 == b1 && a1 == c1) {
cout << "?";
return 0;
}
if (a1 != b1 && b1 != c1 && a1 != c1) {
cout << "?";
return 0;
}
if (check(a1, b1, c1)) cout << "F";
if (check(b1, a1, c1)) cout << "M";
if (check(c1, b1, a1)) cout << "S";
if (check2(a1, b1, c1)) cout << "F";
if (check2(b1, a1, c1)) cout << "M";
if (check2(c1, a1, b1)) cout << "S";
if (check4(a1, c1, b1)) cout << "F";
if (check4(b1, a1, c1)) cout << "M";
if (check4(c1, a1, b1)) cout << "S";
if (check1(a1, b1, c1) || check1(b1, a1, c1) || check1(c1, a1, b1))
cout << "?";
if (check3(a1, b1, c1) || check3(b1, a1, c1) || check3(c1, a1, b1))
cout << "?";
if (check5(a1, b1, c1) || check5(b1, a1, c1) || check5(c1, a1, b1))
cout << "?";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, oo = inf;
inline long long read() {
register long long x = 0;
char f = 0;
register char c = getchar();
for (; !isdigit(c); c = getchar()) f |= (c == '-');
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return f ? -x : x;
}
inline void write(long long x) {
if (x < 0) x = -x, putchar('-');
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
inline void writeln(long long x) {
write(x);
puts("");
}
inline void writeln(long long x, char c, long long y) {
write(x);
putchar(c);
writeln(y);
}
inline void writeln(long long x, char c, long long y, char d, long long z) {
write(x);
putchar(c);
write(y);
putchar(d);
writeln(z);
}
const int maxn = 4e5 + 233;
int n, k, m;
long long a[maxn], b[maxn];
long long sl[maxn], sr[maxn];
int tl, tr;
int main() {
n = read(), k = read();
for (register int i = (1); i <= (n); ++i) a[i] = read();
tl = tr = 0;
for (int i = 1, j; i < n; i = j + 1) {
for (j = i; j <= n && a[j + 1] > a[j]; ++j)
;
if (i == j) continue;
assert(i < j);
while (tl && sl[tl] > a[i]) b[++m] = sr[tr--] - sl[tl--];
sl[++tl] = a[i];
while (tr && sr[tr] < a[j]) b[++m] = sr[tr--] - sl[tl--];
sr[++tr] = a[j];
}
while (tr) b[++m] = sr[tr--] - sl[tl--];
k = min(k, m);
nth_element(b + 1, b + k, b + 1 + m, greater<long long>());
long long ans = 0;
for (register int i = (1); i <= (k); ++i) ans += b[i];
writeln(ans);
return 0;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
int a, b;
cin >> a >> b;
for (int i = 1; i <= a; i++) cout << i << ' ';
for (int i = a + b + 1; i > a; i--) cout << i << ' ';
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
int n;
long double m;
int main() {
cin >> n >> m;
g.resize(n);
for (int i = 0; i < n - 1; ++i) {
int v, u;
cin >> v >> u;
--v, --u;
g[v].push_back(u);
g[u].push_back(v);
}
long long k = 0;
for (int i = 0; i < n; ++i) {
if (g[i].size() == 1) k += 1;
}
long double itog = 2 * m / k;
cout << fixed << setprecision(24) << itog << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000100;
int N, M;
int nlog;
string blog[MAXN];
int conv[MAXN];
int nmonth[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int cconv(char ch) { return (int)ch - '0'; }
int convert(string str) {
int month = 10 * cconv(str[5]) + cconv(str[6]);
int day = 10 * cconv(str[8]) + cconv(str[9]);
for (int i = 0; i < month - 1; i++) day += nmonth[i];
int hour = 10 * cconv(str[11]) + cconv(str[12]);
int minute = 10 * cconv(str[14]) + cconv(str[15]);
int second = 10 * cconv(str[17]) + cconv(str[18]);
return ((24 * day + hour) * 60 + minute) * 60 + second;
}
int main() {
cin >> N >> M;
getline(cin, blog[nlog]);
nlog = 0;
int nlast = 0;
while (true) {
getline(cin, blog[nlog]);
if (blog[nlog].length() < 5) break;
conv[nlog] = convert(blog[nlog]);
while (conv[nlast] <= conv[nlog] - N) nlast++;
if (nlog - nlast + 1 >= M) {
cout << blog[nlog].substr(0, 19) << "\n";
return 0;
}
nlog++;
}
cout << "-1\n";
return 0;
}
| 6 |
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
//#include <boost/foreach.hpp>
//#include <boost/range/algorithm.hpp>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1000000000
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define MP make_pair
#define MOD 1000000007LL
using namespace std;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int main (){
int n;cin>>n;vector<int> v(n);rep(i,0,n)cin>>v[i];
int q;
cin>>q;
rep(i,0,q){
int a,b;cin>>a>>b;
reverse(v.begin()+a,v.begin()+b);
}
rep(i,0,n){
if(i)cout<<" ";
cout<<v[i];
}
cout<<endl;
}
/*
1 2 3 4 5
6
*/
| 0 |
#include<bits/stdc++.h>
using namespace std;
int N, M, K;
map<int, long long> cnt[2];
int main(){
while(cin >> N >> M >> K, N){
cnt[0].clear();
cnt[0][0] = 1;
for(int i = 0; i < N; i++){
cnt[(i+1)%2].clear();
for(auto e : cnt[i % 2]){
for(int k = 1; k <= M; k++){
cnt[(i+1)%2][e.first+k] += e.second;
}
}
}
long long sum = 0;
for(auto e : cnt[N%2]) sum += e.second;
double ans = 0;
for(auto e : cnt[N%2]){
if(e.first <= K) ans += (double)e.second / sum;
else ans += (double)e.second * (e.first - K) / sum;
}
printf("%.10lf\n", ans);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, i, co = 0, odd = 0, in;
cin >> n;
int arr[n], dp[n];
for (i = 0; i < n; i++) {
cin >> arr[i];
dp[i] = 0;
}
for (i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
dp[i] = 1;
co++;
} else {
if (odd == 0) {
in = i;
odd = 1;
} else {
dp[i] = dp[in] = 1;
odd = 0;
co += 2;
}
}
}
if (co == 0)
cout << "-1" << endl;
else {
cout << co << endl;
for (i = 0; i < n; i++) {
if (dp[i] == 1) cout << i + 1 << " ";
}
cout << endl;
}
}
}
| 1 |
#include<bits/stdc++.h>
#define x 100000
using namespace std;
int main()
{
int n[x],a[x],i,b[x];
cin>>n[x];
for(i=1;i<=n[x];i++)
{
cin>>a[i];
b[a[i]]=i;
}
for(i=1;i<=n[x];i++){
cout<<b[i]<<" ";
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, a[100010], xx[100010];
vector<int> qv[100010];
ll ans[100010];
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", a + i);
xx[i] = a[i];
}
sort(xx + 1, xx + n + 1);
for(int i = 1; i <= n; i++) a[i] = (int)(lower_bound(xx + 1, xx + n + 1, a[i]) - xx);
for(int i = 1; i <= n; i++) qv[a[i]].push_back(i);
int mn = n + 1, cnt = 0;
for(int i = n; i >= 1; i--){
for(auto &j : qv[i]){
mn = min(mn, j);
cnt++;
}
ans[mn] += 1LL * cnt * (xx[i] - xx[i - 1]);
}
for(int i = 1; i <= n; i++) printf("%lld\n", ans[i]);
return 0;
}
| 0 |
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
int a[4][3][10]={0};
for(int i=0;i<n;i++){
int b,f,r,v;
cin>>b>>f>>r>>v;
a[b-1][f-1][r-1]+=v;
}
for(int i=0;i<4;i++){
for(int j=0;j<3;j++){
for(int k=0;k<10;k++){
cout<<" "<<a[i][j][k];
}
cout<<endl;
}
if(i!=3)
cout<<"####################"<<endl;
}
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
const int INF = 1e8;
using namespace std;
int main(){
string s;
while(cin >> s){
int mini[205][205];
int maxi[205][205];
rep(i,205) rep(j,205){
mini[i][j] = INF;
maxi[i][j] = -INF;
}
rep(i,s.size()){
if(isdigit(s[i])){
mini[i][i + 1] = maxi[i][i + 1] = s[i] - '0';
}
}
range(w,3,s.size() + 1){
rep(i,s.size() + 1 - w){
int j = i + w;
if(w > 3){
if(s[i] == '('){
maxi[i][j] = max(maxi[i][j], maxi[i + 1][j]);
mini[i][j] = min(mini[i][j], mini[i + 1][j]);
continue;
}else if(s[j - 1] == ')'){
maxi[i][j] = max(maxi[i][j], maxi[i][j - 1]);
mini[i][j] = min(mini[i][j], mini[i][j - 1]);
continue;
}
}
range(k,i + 1,j - 1){
if(s[k] == '-'){
maxi[i][j] = max(maxi[i][j], maxi[i][k] - mini[k + 1][j]);
mini[i][j] = min(mini[i][j], mini[i][k] - maxi[k + 1][j]);
}else if(s[k] == '+'){
//cout << i << ' ' << k << endl;
//cout << maxi[i][k] << ' ' << maxi[i][k + 1] << endl;
maxi[i][j] = max(maxi[i][j], maxi[i][k] + maxi[k + 1][j]);
mini[i][j] = min(mini[i][j], mini[i][k] + mini[k + 1][j]);
}
}
}
}
//rep(i,s.size() + 1){ rep(j,s.size() + 1){ cout << maxi[i][j] << ' '; } cout << endl; }
cout << maxi[0][s.size()] << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, z, orr[1111], now[1333], sz[3133], d[3133], nor[1111], dd[1111];
string na[1111], ty[1111], nam[1111], typ[1111], g;
vector<string> v[3133];
map<string, int> M;
set<string> B;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> na[i] >> ty[i] >> x >> y >> z >> sz[i];
M[na[i]] = i;
if (ty[i][0] == 'w') orr[i] = x;
if (ty[i][0] == 'a') orr[i] = y;
if (ty[i][0] == 'o') orr[i] = z;
nor[i] = orr[i];
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> nam[i] >> typ[i] >> d[i] >> g;
int o = M[g];
v[o].push_back(nam[i]);
if (ty[o][0] == 'w' && typ[i][0] == 'g' ||
ty[o][0] == 'a' && typ[i][0] == 's' ||
ty[o][0] == 'o' && typ[i][0] == 'p')
nor[o] += d[i];
}
int t = 0;
for (int i = 0; i < n; i++)
if (v[i].size() < sz[i]) t = 1;
if (!t) {
int oo = -1, l = 0;
for (int i = 0; i < n; i++)
if (ty[i][0] == 'w' && nor[i] > oo) oo = nor[i], l = i;
cout << na[l] << " " << v[l].size();
for (int i = 0; i < v[l].size(); i++) cout << " " << v[l][i];
puts("");
oo = -1, l = 0;
for (int i = 0; i < n; i++)
if (ty[i][0] == 'a' && nor[i] > oo) oo = nor[i], l = i;
cout << na[l] << " " << v[l].size();
for (int i = 0; i < v[l].size(); i++) cout << " " << v[l][i];
puts("");
oo = -1, l = 0;
for (int i = 0; i < n; i++)
if (ty[i][0] == 'o' && nor[i] > oo) oo = nor[i], l = i;
cout << na[l] << " " << v[l].size();
for (int i = 0; i < v[l].size(); i++) cout << " " << v[l][i];
puts("");
return 0;
}
int oo = -1, l1 = 0, l2 = 0, l3 = 0;
vector<pair<int, string> > uu1, uu2, uu3;
for (int i = 0; i < m; i++)
if (typ[i][0] == 'g') uu1.push_back(make_pair(d[i], nam[i]));
if (uu1.size()) {
sort(uu1.rbegin(), uu1.rend());
dd[0] = uu1[0].first;
for (int i = 1; i < uu1.size(); i++) dd[i] = dd[i - 1] + uu1[i].first;
for (int i = 0; i < n; i++)
if (ty[i][0] == 'w' && orr[i] + dd[min(sz[i], (int)uu1.size()) - 1] > oo)
oo = orr[i] + dd[min(sz[i], (int)uu1.size()) - 1], l1 = i;
} else
for (int i = 0; i < n; i++)
if (ty[i][0] == 'w' && orr[i] > oo) oo = orr[i], l1 = i;
for (int i = 0; i < m; i++)
if (typ[i][0] == 's') uu2.push_back(make_pair(d[i], nam[i]));
oo = -1;
if (uu2.size()) {
sort(uu2.rbegin(), uu2.rend());
dd[0] = uu2[0].first;
for (int i = 1; i < uu2.size(); i++) dd[i] = dd[i - 1] + uu2[i].first;
for (int i = 0; i < n; i++)
if (ty[i][0] == 'a' && orr[i] + dd[min(sz[i], (int)uu2.size()) - 1] > oo)
oo = orr[i] + dd[min(sz[i], (int)uu2.size()) - 1], l2 = i;
} else
for (int i = 0; i < n; i++)
if (ty[i][0] == 'a' && orr[i] > oo) oo = orr[i], l2 = i;
for (int i = 0; i < m; i++)
if (typ[i][0] == 'p') uu3.push_back(make_pair(d[i], nam[i]));
oo = -1;
if (uu3.size()) {
sort(uu3.rbegin(), uu3.rend());
dd[0] = uu3[0].first;
for (int i = 1; i < uu3.size(); i++) dd[i] = dd[i - 1] + uu3[i].first;
for (int i = 0; i < n; i++)
if (ty[i][0] == 'o' && orr[i] + dd[min(sz[i], (int)uu3.size()) - 1] > oo)
oo = orr[i] + dd[min(sz[i], (int)uu3.size()) - 1], l3 = i;
} else
for (int i = 0; i < n; i++)
if (ty[i][0] == 'o' && orr[i] > oo) oo = orr[i], l3 = i;
for (int i = min(sz[l1], (int)uu1.size()); i < uu1.size(); i++)
B.insert(uu1[i].second);
for (int i = min(sz[l2], (int)uu2.size()); i < uu2.size(); i++)
B.insert(uu2[i].second);
for (int i = min(sz[l3], (int)uu3.size()); i < uu3.size(); i++)
B.insert(uu3[i].second);
while (uu1.size() < sz[l1]) {
if (B.begin() == B.end()) break;
uu1.push_back(make_pair(0, *B.begin()));
B.erase(B.begin());
}
while (uu2.size() < sz[l2]) {
if (B.begin() == B.end()) break;
uu2.push_back(make_pair(0, *B.begin()));
B.erase(B.begin());
}
while (uu3.size() < sz[l3]) {
if (B.begin() == B.end()) break;
uu3.push_back(make_pair(0, *B.begin()));
B.erase(B.begin());
}
cout << na[l1] << " " << min(sz[l1], (int)uu1.size());
for (int i = 0; i < min(sz[l1], (int)uu1.size()); i++)
cout << " " << uu1[i].second;
puts("");
cout << na[l2] << " " << min(sz[l2], (int)uu2.size());
for (int i = 0; i < min(sz[l2], (int)uu2.size()); i++)
cout << " " << uu2[i].second;
puts("");
cout << na[l3] << " " << min(sz[l3], (int)uu3.size());
for (int i = 0; i < min(sz[l3], (int)uu3.size()); i++)
cout << " " << uu3[i].second;
puts("");
return 0;
}
| 3 |
#include<iostream>
#include<vector>
#include<string>
using namespace std;
bool hamming(int a) {
while (1) {
if (a == 1)return true;
if (a % 2 == 0) a /= 2;
else if (a % 3 == 0) a /= 3;
else if (a % 5 == 0) a /= 5;
else return false;
}
}
int main()
{
while (1) {
int a;
cin >> a;
if (a == 0)break;
int b;
cin >> b;
int s = 0;
for (int c = a; c < b + 1; c++) {
if (hamming(c))s++;
}
cout << s << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool equals(const char* a, const char* b, int length) {
if (strncmp(a, b, length) == 0)
return true;
else if (length % 2 == 0) {
int halfLength = length / 2;
bool resultLeft1 = equals(a + halfLength, b, halfLength);
bool resultLeft2 = equals(a, b + halfLength, halfLength);
if (resultLeft1 && resultLeft2) return true;
bool resultRight1 = equals(a, b, halfLength);
bool resultRight2 = equals(a + halfLength, b + halfLength, halfLength);
if (resultRight1 && resultRight2) return true;
}
return false;
}
bool DFS(const char* p1, const char* p2, int len) {
if (!strncmp(p1, p2, len)) return true;
if (len % 2) return false;
int n = len / 2;
if (DFS(p1, p2 + n, n) && DFS(p1 + n, p2, n)) return true;
if (DFS(p1, p2, n) && DFS(p1 + n, p2 + n, n)) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
string a, b;
getline(cin, a);
getline(cin, b);
if (DFS(a.c_str(), b.c_str(), a.length())) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, r = 0, ans = 1, a[4007], freq[1000007], dp[4007][4007];
unordered_map<int, int> um;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
std::cin >> n;
for (int i = 0; i < n; i++) std::cin >> a[i], freq[a[i]]++;
for (int i = 0; i < 1000007; i++)
if (freq[i]) r++, freq[i] = r;
for (int i = 0; i < n; i++) a[i] = freq[a[i]] - 1;
for (int i = 0; i < n; i++) {
int k = -1;
for (int j = 0; j < i; j++) {
if (k == -1)
dp[i][j] = 2;
else
dp[i][j] = dp[j][k] + 1;
if (a[i] == a[j]) k = j;
ans = (((ans) > (dp[i][j])) ? (ans) : (dp[i][j]));
}
}
printf("%d\n", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
long long l[2005], r[2005];
double rate[2005];
long long dp[25][5][5];
double ans[2005][2005];
int a[200];
int cnt;
long long numberDP(int pos, int limit, int zero, long long ok) {
long long ans = 0;
if (pos == -1) {
return ok;
}
if (dp[pos][limit][ok] != -1) {
return dp[pos][limit][ok];
}
int end = limit ? a[pos] : 9;
for (int i = 0; i <= end; i++) {
if (zero) {
if (i == 0) {
ans += numberDP(pos - 1, limit && i == end, zero, ok);
} else if (i == 1) {
ans += numberDP(pos - 1, limit && i == end, 0, 1);
}
} else {
if (ok) {
ans += numberDP(pos - 1, limit && i == end, 0, 1);
}
}
}
return dp[pos][limit][ok] = ans;
}
long long cal(long long x) {
for (int i = 0; i <= 22; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
dp[i][j][k] = -1;
}
}
}
cnt = 0;
if (x == 0) return 0;
while (x) {
a[cnt++] = x % 10;
x /= 10;
}
return numberDP(cnt - 1, 1, 1, 0);
}
int n;
int rt;
int main() {
for (int i = 0; i <= 1000; i++) {
for (int j = 0; j <= 1000; j++) ans[i][j] = 0.0;
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
rate[i] = 1.0 * (cal(r[i]) - cal(l[i] - 1)) / (r[i] - l[i] + 1);
}
cin >> rt;
double rr = 0;
rr = rt * 0.01;
ans[0][0] = 1.0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= i; j++) {
ans[i][j] += ans[i - 1][j] * (1 - rate[i]);
if (j >= 0) ans[i][j] += ans[i - 1][j - 1] * rate[i];
}
}
double tot = 0;
double x = 0;
for (int i = 0; i <= n; i++) {
x = 1.0 * i / n;
if (x + 1e-8 >= rr) {
tot += ans[n][i];
}
}
printf("%.10f\n", tot);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
int n, a[N];
int s, sl[N], sr[N];
long long pf[N], sf[N], ans, rt = 1e18;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
s += a[i];
}
if (s == 1) {
puts("-1");
return 0;
}
int mn;
for (int x = 2; x <= s; x++) {
if (s % x == 0) {
mn = x;
int cnt = 0, l = -1;
ans = 0;
for (int i = 1; i <= n; i++) {
cnt += a[i];
if (a[i] && cnt == 1) l = i;
if (cnt == mn) {
for (int j = l; j <= i; j++) {
pf[j] = pf[j - 1] + a[j] * j;
sl[j] = sl[j - 1] + a[j];
}
for (int j = i; j >= l; j--) {
sf[j] = sf[j + 1] + a[j] * j;
sr[j] = sr[j + 1] + a[j];
}
long long ret = 1e18;
for (int j = l; j <= i; j++) {
long long cur = sl[j] * 1ll * j - pf[j];
cur += sf[j] - sr[j] * 1ll * j;
ret = min(ret, cur);
}
ans += ret;
for (int j = l; j <= i; j++) pf[j] = sf[j] = sl[j] = sr[j] = 0;
cnt = 0;
}
}
rt = min(rt, ans);
}
}
cout << rt << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500 * 1000 + 20;
int n, m, ver_3k, root, par[N], h[N], d[N], cnt;
set<int> st, pap1;
bool vis[N], ok, seen;
vector<int> gr[N];
void find_3k_ver() {
for (int i = 1; i <= n; i++)
if (d[i] % 3 == 0)
if (st.empty()) st.insert(i);
}
void dfs_find_cycle(int v) {
vis[v] = true;
int mx = -1;
for (auto u : gr[v]) {
if (st.size()) return;
if (vis[u] && u != par[v]) {
if (mx == -1)
mx = u;
else if (h[u] > h[mx])
mx = u;
}
}
if (mx != -1) {
st.insert(v);
while (v != mx && v) v = par[v], st.insert(v);
return;
}
if (st.size()) return;
for (auto u : gr[v]) {
if (st.size()) return;
if (!vis[u] && d[u] % 3 == 2)
par[u] = v, h[u] = h[v] + 1, dfs_find_cycle(u);
}
}
void find_2_cycle() {
root = 0;
for (int i = 1; i <= n; i++) vis[i] = false, h[i] = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i] && d[i] % 3 == 2) dfs_find_cycle(i);
if (st.size()) return;
}
}
bool bfs(int v) {
queue<int> q;
q.push(v);
vis[v] = true;
while (!q.empty()) {
int f = q.front();
q.pop();
if (d[f] % 3 == 1 && f != v) {
st.insert(f);
while (f != v) f = par[f], st.insert(f);
return true;
}
for (auto u : gr[f])
if (!vis[u]) par[u] = f, vis[u] = true, q.push(u);
}
return false;
}
void find_1_1_path() {
root = 0;
for (int i = 1; i <= n; i++) vis[i] = false, h[i] = 0, par[i] = 0;
for (int i = 1; i <= n; i++)
if (d[i] % 3 == 1 && !vis[i])
if (bfs(i)) return;
}
void dfs(int v, int p) {
vis[v] = true;
for (auto u : gr[v]) {
if (root != p && u == root) {
seen = true;
st.insert(v);
while (v != root) v = par[v], st.insert(v);
return;
}
}
if (seen) return;
for (auto u : gr[v]) {
if (seen) return;
if (!vis[u]) par[u] = v, dfs(u, v);
}
if (seen) return;
}
void dfs_vis_cmp(int v) {
vis[v] = true;
for (auto u : gr[v])
if (u != root && !vis[u]) dfs_vis_cmp(u);
}
void find_papion() {
root = 0, cnt = 0;
for (int i = 1; i <= n; i++) {
vis[i] = false;
if (d[i] % 3 == 1) root = i;
}
if (!root) return;
seen = false;
dfs(root, -1);
for (auto u : st) {
pap1.insert(u);
if (u != root) dfs_vis_cmp(u);
}
st.clear();
seen = false;
dfs(root, -1);
if (!st.size()) return;
for (auto u : pap1) st.insert(u);
pap1.clear();
}
void print_ans() {
ok = true;
cout << "YES" << endl << n - st.size() << endl;
for (int i = 1; i <= n; i++)
if (!st.count(i)) cout << i << " ";
cout << endl;
}
void solve() {
find_3k_ver();
if (st.size() && st.size() < n) {
print_ans();
return;
}
find_2_cycle();
if (st.size() && st.size() < n) {
print_ans();
return;
}
find_1_1_path();
if (st.size() && st.size() < n) {
print_ans();
return;
}
find_papion();
if (st.size() && st.size() < n) {
print_ans();
return;
}
}
void reset() {
for (int i = 1; i <= n; i++)
d[i] = par[i] = h[i] = 0, vis[i] = false, gr[i].clear();
pap1.clear(), st.clear();
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
ok = false;
cin >> n >> m;
reset();
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
gr[u].push_back(v), gr[v].push_back(u);
d[u]++, d[v]++;
}
solve();
if (!ok) cout << "NO" << endl;
}
return 0;
}
| 6 |
#include<bits/stdc++.h>
#define int long long
#define print1(a) for(auto x:a)cout << x<<" ";cout <<endl;
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define print2(a,r,c) for(int i=0;i<r;i++){for(int j=0;j<c;j++)cout<<a[i][j]<<" ";cout << endl;}
using namespace std;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
void solve()
{
int n;
cin >> n;
string str;
cin >> str;
int ans[n+1];
for(int i=0;i<2;i++)
{
int left=i,right=i,total=0;
if(left>0)
{
int count=0;
for(int j=left-1;j>=0;j--)
{
if(count&1)
{
if(str[j]=='R')
count++;
else
break;
}
else if(count%2==0)
{
if(str[j]=='L')
count++;
else
break;
}
}
total+=count;
}
if(right<n)
{
int count=0;
for(int j=right;j<n;j++)
{
if(count&1)
{
if(str[j]=='L')
count++;
else
break;
}
else if(count%2==0)
{
if(str[j]=='R')
count++;
else
break;
}
}
total+=count;
}
ans[i]=total+1;
}
for(int i=2;i<=n;i++)
{
int right=i,total=0;
if(str[i-1]=='L' && str[i-2]=='R')
{
ans[i]=ans[i-2];
continue;
}
else
if(str[i-1]=='L')total++;
if(right<n)
{
int count=0;
for(int j=right;j<n;j++)
{
if(count&1)
{
if(str[j]=='L')
count++;
else
break;
}
else if(count%2==0)
{
if(str[j]=='R')
count++;
else
break;
}
}
total+=count;
}
ans[i]=total+1;
}
for(int x:ans)cout << x <<" ";
cout << endl;
}
int32_t main()
{
int t;
cin >> t;
while(t--)
{
solve();
}
} | 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin >> q;
while (q--) {
long long n, a, b;
cin >> n >> a >> b;
string str;
cin >> str;
long long first = 0;
for (int i = 0; i < n; i++) {
if (str[i] == '0')
first++;
else
break;
}
long long last = n - 1;
for (int i = n - 1; i >= 0; i--) {
if (str[i] == '0')
last--;
else
break;
}
if (first > last) {
cout << (n + 1) * b + (n)*a << endl;
} else {
long long ans = (first + 1) * a + (n - last) * a + (last - first + 1) * a;
ans += (first)*b + (n - last - 1) * b + (last - first + 2) * b * 2;
for (int i = first; i < last; i++) {
if (str[i] == '0') {
int num = 0;
while (str[i] == '0') {
num++;
i++;
}
if (ans > ans + (2 * a - (num - 1) * b)) {
ans += (2 * a - (num - 1) * b);
}
}
}
cout << ans << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<unsigned long long int> v[5004];
vector<unsigned long long int>::iterator it;
set<unsigned long long int> s;
set<unsigned long long int>::iterator iter;
bool check[100005];
int main() {
long long int n = 0, m = 0, x = 0, res = 0, i = 0, j = 0, flag = 0;
string s;
cin >> n;
for (unsigned long long int i = 1; i < n + 1; i++) {
cin >> s;
if (s[0] == 'X' && s[2] == '+' || s[0] == '+') {
res++;
} else {
res--;
}
}
cout << res << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
int ln(int n) {
int i = 1, s = 0, a;
while (n) {
a = n % 10;
if (a == 4 || a == 7) {
s += a * i;
i *= 10;
}
n /= 10;
}
return s;
}
int main() {
int a, b;
scanf("%d%d", &a, &b);
while (ln(++a) != b)
;
printf("%d\n", a);
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 60);
const int inf = (1 << 30);
const int nmax = 1e5 + 50;
const long long mod = 1e9 + 7;
using namespace std;
int ts, n, k, z, i, j, cur, rs, a[nmax], b[nmax];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cerr.tie(0);
cout.tie(0);
cin >> ts;
while (ts--) {
cin >> n >> k >> z;
for (i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i] + b[i - 1];
}
rs = b[k + 1];
for (i = 2; i <= k + 1; i++) {
for (j = 1; 2 * j - 1 + i - 1 <= k && j <= z; j++) {
if (k - (i - 1) - 2 * j >= 0)
cur = b[i + max(0, k - (i - 1) - 2 * j)] + j * a[i - 1] + j * a[i];
else
cur = b[i] + j * a[i - 1] + (j - 1) * a[i];
rs = max(rs, cur);
}
}
cout << rs << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int add(int a, int b) {
long long x = a + b;
if (x >= 1000000007) x -= 1000000007;
if (x < 0) x += 1000000007;
return x;
}
long long mul(long long a, long long b) { return (a * b) % 1000000007; }
long long pw(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % 1000000007;
a = (a * a) % 1000000007;
b >>= 1;
}
return ans;
}
int n, m, nr, start;
int val[200002];
vector<int> v[200002], sol[200002];
bool viz[200002];
int niv[200002], low[200002];
deque<int> d;
int mch[200002];
long long chainsum[200002];
vector<int> dfstree[200002];
void dfs(int dad, int nod) {
viz[nod] = 1;
low[nod] = niv[nod];
d.push_back(nod);
for (int i = 0; i < v[nod].size(); ++i) {
int vecin = v[nod][i];
if (vecin == dad) continue;
if (viz[vecin]) {
mch[nod]++;
low[nod] = min(low[nod], niv[vecin]);
continue;
}
niv[vecin] = niv[nod] + 1;
dfstree[nod].push_back(vecin);
dfstree[vecin].push_back(nod);
dfs(nod, vecin);
mch[nod] += mch[vecin];
chainsum[nod] = max(chainsum[nod], chainsum[vecin]);
low[nod] = min(low[nod], low[vecin]);
if (low[vecin] >= niv[nod]) {
nr++;
int lst;
do {
sol[nr].push_back(d.back());
lst = d.back();
d.pop_back();
} while (!d.empty() && lst != vecin);
sol[nr].push_back(nod);
}
}
chainsum[nod] += val[nod];
}
long long ans_max = 0, maxBFR;
bool gods[200002];
void bfr(int nod) {
deque<int> d;
d.push_back(nod);
while (!d.empty()) {
int nod = d[0];
gods[nod] = 1;
d.pop_front();
if (!mch[nod])
maxBFR = max(maxBFR, chainsum[nod]);
else {
ans_max += val[nod];
for (int i = 0; i < dfstree[nod].size(); ++i) {
int vecin = dfstree[nod][i];
if (gods[vecin]) continue;
d.push_back(vecin);
gods[vecin] = 1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> val[i];
for (int i = 1; i <= m; ++i) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
cin >> start;
dfs(0, start);
bfr(start);
cout << ans_max + maxBFR << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define rep(i,N) for(long long i = 0; i < (long long)(N); i++)
#define repr(i,N) for(long long i = (long long)(N) - 1; i >= 0; i--)
#define rep1(i,N) for(long long i = 1; i <= (long long)(N) ; i++)
#define repr1(i,N) for(long long i = (N) ; (long long)(i) > 0 ; i--)
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
using namespace std; void solve();
using ll = long long; template<class T = ll> using V = vector<T>;
using vi = V<int>; using vl = V<>; using vvi = V< V<int> >;
constexpr int inf = 1001001001; constexpr ll infLL = (1LL << 61) - 1;
struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7);} } iosetupnya;
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); rep(i,s) os << (i ? " " : "") << v[i]; return os; }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; }
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}
#ifdef NyaanDebug
#include "NyaanDebug.h"
#define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0)
#define trca(v,N) do { cerr << #v << " = "; array_out(v , N);cout << endl;} while(0)
#else
#define trc(...)
#define trca(...)
int main(){solve();}
#endif
using P = pair<ll,ll>; using vp = V<P>;
constexpr int MOD = /**/ 1000000007; //*/ 998244353;
//////////
void solve(){
ini(N , Q);
vi a(N); in(a);
sort(all(a));
rep(_ , Q){
ini(l , r);
auto il = lower_bound(all(a) , l);
auto ir = upper_bound(all(a) , r);
out((int)(ir - il));
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool PIIfs(const pair<int, int>& a, const pair<int, int>& b) {
return a.first < b.first || (a.first == b.first && a.second < b.second);
}
bool PIIsf(const pair<int, int>& a, const pair<int, int>& b) {
return a.second < b.second || (a.second == b.second && a.first < b.first);
}
bool PIIFS(const pair<int, int>& a, const pair<int, int>& b) {
return a.first > b.first || (a.first == b.first && a.second > b.second);
}
bool PIISF(const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second || (a.second == b.second && a.first > b.first);
}
bool PIIfS(const pair<int, int>& a, const pair<int, int>& b) {
return a.first < b.first || (a.first == b.first && a.second > b.second);
}
bool PIIsF(const pair<int, int>& a, const pair<int, int>& b) {
return a.second < b.second || (a.second == b.second && a.first > b.first);
}
bool PIIFs(const pair<int, int>& a, const pair<int, int>& b) {
return a.first > b.first || (a.first == b.first && a.second < b.second);
}
bool PIISf(const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second || (a.second == b.second && a.first < b.first);
}
template <typename T>
inline T max(T a, T b, T c) {
return max(max(a, b), c);
}
template <typename T>
inline T max(T a, T b, T c, T d) {
return max(max(a, b, c), d);
}
template <typename T>
inline T max(T a, T b, T c, T d, T e) {
return max(max(a, b, c, d), e);
}
template <typename T>
inline T min(T a, T b, T c) {
return min(min(a, b), c);
}
template <typename T>
inline T min(T a, T b, T c, T d) {
return min(min(a, b, c), d);
}
template <typename T>
inline T min(T a, T b, T c, T d, T e) {
return min(min(a, b, c, d), e);
}
template <typename T>
inline istream& RD(T& x) {
return cin >> x;
}
template <typename T, typename U>
inline istream& RD(T& a, U& b) {
return cin >> a >> b;
}
template <typename T, typename U, typename V>
inline istream& RD(T& a, U& b, V& c) {
return cin >> a >> b >> c;
}
template <typename T, typename U, typename V, typename W>
inline istream& RD(T& a, U& b, V& c, W& d) {
return cin >> a >> b >> c >> d;
}
template <typename T, typename U, typename V, typename W, typename X>
inline istream& RD(T& a, U& b, V& c, W& d, X& e) {
return cin >> a >> b >> c >> d >> e;
}
template <typename T>
void checkmin(T& a, const T& b) {
if (b < a) a = b;
}
template <typename T>
void checkmax(T& a, const T& b) {
if (b > a) a = b;
}
const int maxn = 1000100;
int VAL[2 * maxn];
int INPUT[maxn];
int L[2 * maxn];
int R[2 * maxn];
int MARK[2 * maxn];
inline void mergeMark(int& son, const int& father) { son += father; }
inline void setMark(int& where, const int& what) { where = what; }
inline int mergeValVal(const int& a, const int& b) { return a + b; }
inline const int withValue(const int& mark, const int& val, int l, int r) {
return val + (r - l) * mark;
}
inline void clearMark(int& m) { m = 0; }
const bool MERGABLE = true;
class SegmentTree {
public:
SegmentTree(size_t N) : query_head(indexof(0, N)) {
memset(MARK, 0, sizeof(MARK));
build(0, N);
}
int query(int l, int r) { return query_aid(query_head, l, r); }
void update(int l, int r, const int& mark) {
update_aid(query_head, l, r, mark);
}
private:
const int query_head;
inline int indexof(int l, int r) {
return (l - 1 + r) - ((r - l == 1) ? 0 : ((l + r) & 1));
}
void build(int l, int r) {
int ind = indexof(l, r);
if (l + 1 < r) {
build(l, (l + r) >> 1);
build((l + r) >> 1, r);
L[ind] = l;
R[ind] = r;
VAL[ind] = mergeValVal(VAL[indexof(l, (l + r) / 2)],
VAL[indexof((l + r) / 2, r)]);
} else {
L[ind] = l;
R[ind] = r;
VAL[ind] = INPUT[l];
}
}
void pushDown(int which_node) {
const int& mark = MARK[which_node];
if (mark) {
int l = L[which_node];
int r = R[which_node];
if (r - l > 1) {
if (MERGABLE) {
mergeMark(MARK[indexof(l, (l + r) / 2)], (mark));
mergeMark(MARK[indexof((l + r) / 2, r)], (mark));
} else {
pushDown(indexof(l, (l + r) / 2));
pushDown(indexof((l + r) / 2, r));
setMark(MARK[indexof(l, (l + r) / 2)], (mark));
setMark(MARK[indexof((l + r) / 2, r)], (mark));
}
}
VAL[which_node] = withValue(mark, VAL[which_node], l, r);
clearMark(MARK[which_node]);
}
}
int query_aid(int which_node, int l, int r) {
pushDown(which_node);
int lbound = L[which_node];
int rbound = R[which_node];
if (l <= lbound && rbound <= r) {
return VAL[which_node];
} else {
int ans;
int mid = (lbound + rbound) >> 1;
if (l < mid) {
ans = query_aid(indexof(lbound, mid), l, r);
if (r > mid) {
ans = mergeValVal(ans, query_aid(indexof(mid, rbound), l, r));
}
} else {
ans = query_aid(indexof(mid, rbound), l, r);
}
return ans;
}
}
void update_aid(int which_node, int l, int r, const int& mark) {
int lbound = L[which_node];
int rbound = R[which_node];
if (l <= lbound && rbound <= r) {
if (MERGABLE) {
mergeMark(MARK[which_node], (mark));
} else {
pushDown(which_node);
mergeMark(MARK[which_node], (mark));
}
} else {
pushDown(which_node);
int mid = (lbound + rbound) / 2;
if (l < mid) {
update_aid(indexof(lbound, mid), l, r, mark);
if (r > mid) {
update_aid(indexof(mid, rbound), l, r, mark);
}
} else {
update_aid(indexof(mid, rbound), l, r, mark);
}
pushDown(indexof(lbound, mid));
pushDown(indexof(mid, rbound));
VAL[which_node] =
mergeValVal(VAL[indexof(lbound, mid)], VAL[indexof(mid, rbound)]);
}
}
};
int dodo(int n) {
int ans = 0;
memset(INPUT, 0, sizeof(INPUT));
SegmentTree st(n + 1);
for (int i = 2; i <= n; i++) {
int bv = n / i;
int rm = n % i;
st.update(0, i, bv);
if (rm > 0) st.update(0, rm + 1, 1);
}
for (int i = 1; i <= (int)(n); ++i) {
int q = st.query(i, i + 1);
if (q % 2 == 1) {
ans ^= i;
}
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
int n;
int ans = 0;
RD(n);
for (int i = 0; i < (int)(n); ++i) {
int x;
RD(x);
ans ^= x;
}
ans ^= dodo(n);
cout << ans << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 2;
int n, x, y;
int a[N];
class SegmentTree {
int *tree;
public:
SegmentTree() { tree = new int[4 * N]; }
void build(int, int, int);
int query(int, int, int, int, int);
};
void SegmentTree ::build(int id, int start, int end) {
if (start > end) return;
if (start == end) {
tree[id] = a[start];
return;
}
int mid = (start + end) >> 1;
build(2 * id, start, mid);
build(2 * id + 1, mid + 1, end);
tree[id] = max(tree[2 * id + 1], tree[2 * id]);
}
int SegmentTree ::query(int id, int start, int end, int qs, int qe) {
if (start > end || start > qe || end < qs) return -N;
if (start >= qs && end <= qe) {
return tree[id];
}
int mid = (start + end) >> 1;
int L = query(2 * id, start, mid, qs, qe);
int R = query(2 * id + 1, mid + 1, end, qs, qe);
return max(L, R);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
unordered_map<int, int> pos;
for (int i = 0; i < n; i++) {
cin >> a[i];
pos[a[i]] = i;
}
int ans = 0;
SegmentTree ob;
ob.build(1, 0, n - 1);
for (int i = 0; i < n; i++) {
int low, high;
low = i, high = n - 1;
int L, R;
while (low <= high) {
int mid = (low + high) >> 1;
int k = ob.query(1, 0, n - 1, i, mid);
if (a[i] == k) {
low = mid + 1;
R = mid;
} else
high = mid - 1;
}
low = 0, high = i;
while (low <= high) {
int mid = (low + high) >> 1;
int k = ob.query(1, 0, n - 1, mid, i);
if (a[i] == k) {
high = mid - 1;
L = mid;
} else
low = mid + 1;
}
if (i - L <= R - i) {
for (int j = L; j < i; j++) {
assert(a[i] - a[j] > 0);
if (pos[a[i] - a[j]] > i and pos[a[i] - a[j]] <= R) {
ans++;
}
}
} else {
for (int j = i + 1; j <= R; j++) {
assert(a[i] - a[j] > 0);
if (pos[a[i] - a[j]] < i and pos[a[i] - a[j]] >= L) {
ans++;
}
}
}
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
char buf[100000], *p1 = buf, *p2 = buf;
inline int gi() {
int x = 0, f = 1;
char ch =
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++);
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++);
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++);
}
return (f == 1) ? x : -x;
}
const int maxn = 2.5e5 + 5, mod = 998244353;
int n;
vector<int> e[maxn];
namespace Poly {
const int G = 3;
vector<int> rev;
inline int qpow(int x, int k) {
int ret = 1;
while (k) {
if (k & 1) ret = 1ll * x * ret % mod;
k >>= 1, x = 1ll * x * x % mod;
}
return ret;
}
inline int getbit(int len) {
int bit = 0;
while (1 << bit < len) bit++;
return bit;
}
inline void calcrev(int len) {
rev.resize(len);
int bit = getbit(len);
for (int i = 0; i <= len - 1; ++i)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));
}
inline void dft(vector<int> &a) {
int n = (int)(a.size());
for (int i = 0; i <= n - 1; ++i)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int l = 1; l < n; l <<= 1) {
int wn = qpow(G, (mod - 1) / (l << 1));
for (int i = 0; i < n; i += l << 1) {
int w = 1;
for (int j = 0; j < l; ++j, w = 1ll * w * wn % mod) {
int v1 = a[i + j], v2 = 1ll * a[i + j + l] * w % mod;
a[i + j] = (v1 + v2) % mod;
a[i + j + l] = (v1 - v2 + mod) % mod;
}
}
}
}
inline void idft(vector<int> &a) {
int n = (int)(a.size());
reverse(a.begin() + 1, a.end());
dft(a);
int inv = qpow(n, mod - 2);
for (int i = 0; i <= n - 1; ++i) a[i] = 1ll * a[i] * inv % mod;
}
inline vector<int> operator*(vector<int> a, vector<int> b) {
int len = (int)(a.size()) + (int)(b.size()), l = len - 1;
if (!(int)(a.size()) || !(int)(b.size())) return vector<int>();
len = 1 << getbit(len);
calcrev(len);
a.resize(len), b.resize(len);
dft(a), dft(b);
for (int i = 0; i <= len - 1; ++i) a[i] = 1ll * a[i] * b[i] % mod;
idft(a);
a.resize(l);
return a;
}
inline vector<int> operator+(vector<int> a, vector<int> b) {
if ((int)(a.size()) > (int)(b.size())) swap(a, b);
for (int i = 0; i <= (int)(a.size()) - 1; ++i) b[i] = (b[i] + a[i]) % mod;
return b;
}
} // namespace Poly
using namespace Poly;
int fac[maxn], inv[maxn];
inline void input() {
n = gi();
inv[0] = inv[1] = fac[0] = fac[1] = 1;
for (int i = 2; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;
for (int i = 2; i <= n; ++i)
inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
for (int i = 2; i <= n; ++i) inv[i] = 1ll * inv[i - 1] * inv[i] % mod;
for (int i = 1; i <= n - 1; ++i) {
int u = gi(), v = gi();
e[u].push_back(v), e[v].push_back(u);
}
}
int son[maxn], fa[maxn], tp[maxn], siz[maxn], sons[maxn];
inline void dfs(int u) {
siz[u] = 1;
for (int v : e[u]) {
if (v == fa[u]) continue;
fa[v] = u;
dfs(v);
siz[u] += siz[v];
sons[u]++;
if (siz[v] > siz[son[u]]) son[u] = v;
}
}
vector<int> dp[maxn];
int f[maxn];
inline vector<int> cdq(vector<vector<int> > &seq, int l, int r) {
if (l == r) return seq[l];
int mid = (l + r) >> 1;
return cdq(seq, l, mid) * cdq(seq, mid + 1, r);
}
inline void dfs2(int u) {
vector<vector<int> > seq;
for (int x = u; x; x = son[x]) {
seq.clear();
seq.push_back(dp[x]);
for (int v : e[x]) {
if (v == fa[x] || v == son[x]) continue;
dfs2(v);
seq.push_back(dp[v]);
}
dp[x] = cdq(seq, 0, (int)(seq.size()) - 1);
}
seq.clear();
for (int x = u; x; x = son[x]) seq.push_back(dp[x]);
dp[u] = cdq(seq, 0, (int)(seq.size()) - 1);
}
inline void solve() {
dfs(1);
for (int i = 1; i <= n; ++i) dp[i] = {sons[i], 1};
dfs2(1);
for (int i = 0; i <= n; ++i) f[i] = 1ll * dp[1][n - i] * fac[n - i] % mod;
int ans = 0;
for (int i = 0; i <= n; ++i)
i & 1 ? ans = (ans + mod - f[i]) % mod : ans = (ans + f[i]) % mod;
printf("%d\n", ans);
}
int main() {
input();
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
char str[60];
int main() {
int len, i, j = 0;
scanf("%d %s", &len, &str[1]);
for (i = 1; j < len; i++) {
j += i;
printf("%c", str[j]);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
template <typename T>
void print(vector<T>& v) {
cout << "[";
for (int i = 0; i < v.size(); ++i) {
cout << v[i];
if (i != v.size() - 1) cout << ", ";
}
cout << "]\n";
}
template <typename T>
void print(set<T>& v) {
cout << "[";
for (auto it : v) {
cout << it;
if (it != *v.rbegin()) cout << ", ";
}
cout << "]\n";
}
template <typename T, typename S>
void print(map<T, S>& v) {
for (auto it : v) cout << it.first << " : " << it.second << "\n";
}
template <typename T, typename S>
void print(pair<T, S>& v) {
cout << "( " << v.first << ", " << v.second << " )" << '\n';
}
template <typename T>
void print(T x) {
cout << x << '\n';
}
int n, a, b, k;
string s;
vector<vector<int> > segments;
bool compare(const vector<int>& v1, const vector<int>& v2) {
return v1.size() < v2.size();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> a >> b >> k;
cin >> s;
s.push_back('1');
int pos = n + 1;
while (s.size()) {
s.pop_back();
pos--;
segments.push_back(vector<int>(0));
while (s.size() && s.back() == '0') {
s.pop_back();
segments.back().push_back(pos);
pos--;
}
}
sort(segments.begin(), segments.end(), compare);
int totalpossible = 0;
for (int i = 0; i < segments.size(); i++) {
totalpossible += segments[i].size() / b;
}
int tocheck = totalpossible - a + 1;
vector<int> display;
for (int i = 0; i < segments.size(); i++) {
if (tocheck == 0) break;
if (segments[i].size() < b) continue;
for (int j = b - 1; j < segments[i].size(); j += b) {
display.push_back(segments[i][j]);
tocheck--;
if (tocheck == 0) break;
}
}
cout << display.size() << '\n';
for (int i : display) {
cout << i << " ";
}
cout << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> A(n);
for (__typeof(0) i = (0); i < (n); i++) cin >> A[i];
sort(A.begin(), A.end());
int ans = 0, i = 0;
while (i < n) {
int c = A[i], mx = A[i];
while (i < n && c > 0) {
c--;
if (A[i] > mx) {
c += A[i] - mx;
mx = A[i];
}
i++;
}
if (c <= 0) ans++;
}
cout << ans << '\n';
}
return 0;
}
| 2 |
#include<iostream>
using namespace std;
char classify(int m, int e, int j){
if(m == 100 || e == 100 || j == 100) return 'A';
if((m+e)/2 >= 90) return 'A';
if((m+e+j)/3 >= 80) return 'A';
if((m+e+j)/3 >= 70) return 'B';
if((m+e+j)/3 >= 50 && max(m,e) >= 80) return 'B';
return 'C';
}
int main(){
int n;
while(cin >> n, n){
int m, e, j;
for(int i = 0; i < n; i++){
cin >> m >> e >> j;
cout << classify(m,e,j) << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int m[2015][2015];
bool vis[2015][2015];
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, -1, 0, 1};
int n;
void bfs(int row, int col) {
queue<int> q;
q.push(row);
q.push(col);
vis[row][col] = true;
while (!q.empty()) {
row = q.front();
q.pop();
col = q.front();
q.pop();
int i;
for (i = 0; i < 4; i++) {
int r0 = row + dr[i];
int c0 = col + dc[i];
if (r0 >= 1 && c0 >= 1 && r0 <= n && c0 <= n && !vis[r0][c0] &&
m[r0][c0]) {
vis[r0][c0] = true;
q.push(r0);
q.push(c0);
}
}
}
}
int main() {
scanf("%d", &n);
int i;
for (i = 1; i <= n; i++) {
int j;
for (j = 1; j <= n; j++) {
scanf("%d", &m[i][j]);
}
}
int s = 0;
int rect = 0;
for (i = 1; i <= n; i++) {
int j;
for (j = 1; j <= n; j++) {
if (!vis[i][j] && m[i][j]) {
bfs(i, j);
s++;
}
}
}
for (i = 1; i <= n; i++) {
int j;
for (j = 1; j <= n; j++) {
if (!m[i][j]) continue;
int k;
bool ok = true;
for (k = 0; k < 8; k++) {
if (!m[i + k][j] || m[i + k][j - 1] || !m[i][j + k] ||
m[i - 1][j + k]) {
ok = false;
break;
}
}
if (ok) {
rect++;
s--;
}
}
}
cout << s << ' ' << rect << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long xmax(long long& a, long long b) { return a = max(a, b); }
long long xmin(long long& a, long long b) { return a = min(a, b); }
vector<long long> res;
vector<vector<pair<long long, long long> > > adi;
void do_insert(multiset<long long>& sm, multiset<long long>& la, long long& sum,
long long ins) {
auto tap = *la.begin();
if (ins > tap) {
sum += ins - tap;
la.erase(la.find(tap));
sm.insert(tap);
la.insert(ins);
} else {
sm.insert(ins);
}
}
void do_erase(multiset<long long>& sm, multiset<long long>& la, long long& sum,
long long ins) {
if (sm.find(ins) != sm.end()) {
sm.erase(sm.find(ins));
return;
}
sum -= ins;
la.erase(la.find(ins));
}
void do_move(multiset<long long>& sm, multiset<long long>& la, long long& sum,
long long k) {
while (la.size() <= k) {
auto tap = sm.end();
tap--;
long long rem = *tap;
sum += rem;
sm.erase(tap);
la.insert(rem);
}
}
vector<long long> vis;
long long krat = 0;
long long done = 0;
pair<vector<long long>, vector<long long> > dfs(long long node, long long par) {
krat++;
vector<vector<long long> > child, child_;
for (auto x : adi[node]) {
if (x.first == par) {
vector<long long> e;
child.push_back(e);
child_.push_back(e);
continue;
}
auto retu = dfs(x.first, node);
child.push_back(retu.first);
child_.push_back(retu.second);
};
for (auto x : child) {
;
}
for (auto y : child_) {
;
}
long long sum = 0;
vector<long long> retk;
retk.push_back(0);
if (adi[node].size() == 1 && node != 0) {
return {retk, retk};
}
vector<long long> retk_;
long long k_large_sum = 0;
multiset<long long> vals_large;
vals_large.insert(1e9);
multiset<long long> vals_small;
for (long long i = 1; i < adi[node].size(); i++) {
for (long long j = 0; j < child.size(); j++) {
if (adi[node][j].first == par) continue;
if (i == child[j].size()) {
do_insert(vals_small, vals_large, k_large_sum, adi[node][j].second);
continue;
}
if (i > child[j].size()) break;
sum += child[j][i];
do_insert(vals_small, vals_large, k_large_sum,
max(adi[node][j].second -
(child[j][i] + child[j][i - 1] - child_[j][i - 1]),
(long long)0));
};
;
;
do_move(vals_small, vals_large, k_large_sum, i - 1);
retk_.push_back(sum + k_large_sum);
do_move(vals_small, vals_large, k_large_sum, i);
retk.push_back(sum + k_large_sum);
for (long long j = 0; j < adi[node].size(); j++) {
if (adi[node][j].first == par) continue;
if (i >= child[j].size()) break;
do_erase(vals_small, vals_large, k_large_sum,
max(adi[node][j].second -
(child[j][i] + child[j][i - 1] - child_[j][i - 1]),
(long long)0));
}
}
for (long long j = 0; j < adi[node].size(); j++) {
long long next = adi[node][j].first;
long long w = adi[node][j].second;
if (next == par || adi[node].size() > child[j].size()) continue;
child[j].push_back(w);
child_[j].push_back(0);
for (long long i = adi[node].size(); i < child[j].size(); i++) {
long long add =
child[j][i - 2] +
max(child[j][i - 1] + child[j][i], w + child_[j][i - 1]) -
max(child[j][i - 2] + child[j][i - 1], w + child_[j][i - 2]);
res[i] += add;
}
};
vector<long long> ret2k = retk;
vector<long long> ret2k_ = retk_;
;
;
for (long long i = 1; i < retk.size(); i++) {
ret2k[i] = retk[i] - retk[i - 1];
}
for (long long i = 1; i < retk_.size(); i++) {
ret2k_[i] = retk_[i] - retk[i - 1];
}
done++;
;
;
return {ret2k, ret2k_};
}
bool sorted(pair<long long, long long> a, pair<long long, long long> b) {
return adi[a.first].size() > adi[b.first].size();
}
void do_test() {
long long n;
cin >> n;
adi.resize(n + 1);
long long tot = 0;
vis.resize(n + 1);
for (long long i = 1; i < n; i++) {
long long u, v, w;
cin >> u >> v >> w;
adi[u].push_back({v, w});
adi[v].push_back({u, w});
tot += w;
}
adi[0].push_back({1, 0});
adi[0].push_back({-1, 0});
adi[1].push_back({0, 0});
res = vector<long long>(n + 2);
for (long long i = 1; i <= n; i++) {
sort(adi[i].begin(), adi[i].end(), sorted);
}
auto le = dfs(0, -1);
for (long long i = 0; i < le.first.size(); i++) {
res[i] += le.first[i];
}
long long sum = 0;
for (long long i = 0; i < n; i++) {
sum += res[i];
cout << tot - sum << " ";
}
cout << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
do_test();
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long long mod = (long long)1e9 + 7;
const long long MOD = (long long)998244353;
const long long INF = (long long)1e18;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
void solve() {
long long h[26] = {};
string s;
while (cin >> s) {
if (s[0] == '{' and s[1] != '}')
h[s[1] - 'a'] = 1;
else
h[s[0] - 'a'] = 1;
}
long long ans = 0;
for (__typeof(26) i = (0) - ((0) > (26)); i != (26) - ((0) > (26));
i += 1 - 2 * ((0) > (26)))
ans += h[i];
cout << ans;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
int IntMaxVal = (int)1e20;
long long LongMaxVal = (long long)1e20;
using namespace std;
vector<int> depth;
vector<vector<int> *> parents;
int lca(int a, int b) {
if (depth[a] > depth[b]) swap(a, b);
for (int i = parents[b]->size() - 1; i >= 0; --i) {
if (depth[b] - (1 << i) >= depth[a]) b = parents[b]->at(i);
}
if (a == b) return a;
for (int i = parents[b]->size() - 1; i >= 0; --i) {
if (parents[b]->size() > i)
if (parents[b]->at(i) != parents[a]->at(i)) {
a = parents[a]->at(i);
b = parents[b]->at(i);
}
}
return parents[a]->at(0);
}
int dist(int a, int b) {
int l = lca(a, b);
return depth[a] + depth[b] - 2 * depth[l];
}
int main() {
ios_base::sync_with_stdio(false);
int q;
cin >> q;
;
parents.resize(4);
depth.resize(4);
depth[0] = 0;
parents[0] = new vector<int>();
for (int i = 1; i < 3 + 1; ++i) {
depth[i] = 1;
parents[i] = new vector<int>();
parents[i]->reserve(10);
parents[i]->push_back(0);
}
auto cur = make_pair(2, make_pair(1, 2));
for (int i = 1; i < q + 1; ++i) {
int v;
cin >> v;
;
v--;
int newOne = depth.size();
for (int j = 0; j < 1 + 1; ++j) {
int x = depth.size();
depth.push_back(depth[v] + 1);
parents.push_back(new vector<int>());
auto lastP = parents.back();
lastP->push_back(v);
for (int k = 0; parents[parents[x]->at(k)]->size() > k; k++) {
parents[x]->push_back(parents[parents[x]->at(k)]->at(k));
}
}
int a = cur.second.first;
int b = cur.second.second;
{ cur = max(cur, make_pair(dist(newOne, a), make_pair(newOne, a))); };
{ cur = max(cur, make_pair(dist(newOne, b), make_pair(newOne, b))); };
cout << cur.first << endl;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct node {
long long y, next;
} a[200010];
long long len = 0, last[100010];
long long n, dfn[100010], z = 0;
struct trnode {
long long dep, fa[20];
} tr[100010];
void ins(long long x, long long y) {
a[++len].y = y;
a[len].next = last[x];
last[x] = len;
}
void dfs(long long x, long long fa) {
tr[x].dep = tr[fa].dep + 1;
tr[x].fa[0] = fa;
dfn[x] = ++z;
for (long long i = 1; (1 << i) <= tr[x].dep; i++)
tr[x].fa[i] = tr[tr[x].fa[i - 1]].fa[i - 1];
for (long long i = last[x]; i; i = a[i].next)
if (a[i].y != fa) dfs(a[i].y, x);
}
long long vis[100010], tim = 0;
long long h[100010], tot, sta[100010];
bool cmp(long long x, long long y) { return dfn[x] < dfn[y]; }
long long find_lca(long long x, long long y) {
if (tr[x].dep < tr[y].dep) swap(x, y);
for (long long i = 17; i >= 0; i--)
if ((1 << i) <= tr[x].dep - tr[y].dep) x = tr[x].fa[i];
if (x == y) return x;
for (long long i = 17; i >= 0; i--)
if ((1 << i) <= tr[x].dep && tr[x].fa[i] != tr[y].fa[i])
x = tr[x].fa[i], y = tr[y].fa[i];
return tr[x].fa[0];
}
void build() {
sort(h + 1, h + tot + 1, cmp);
long long top = 0;
sta[++top] = 1;
for (long long i = 1; i <= tot; i++) {
long long lca = find_lca(h[i], sta[top]);
if (lca == sta[top]) {
if (sta[top] != h[i]) sta[++top] = h[i];
continue;
}
while (top && tr[sta[top - 1]].dep >= tr[lca].dep)
ins(sta[top - 1], sta[top]), top--;
if (lca != sta[top]) ins(lca, sta[top]), sta[top] = lca;
sta[++top] = h[i];
}
while (top > 1) ins(sta[top - 1], sta[top]), top--;
len = 0;
}
const long long inf = 1 << 28;
long long f[100010][2];
void dp(long long x) {
f[x][0] = f[x][1] = 0;
for (long long i = last[x]; i; i = a[i].next) dp(a[i].y);
if (vis[x] == tim) {
f[x][0] = inf;
for (long long i = last[x]; i; i = a[i].next)
f[x][1] += min(f[a[i].y][0], f[a[i].y][1] + 1);
} else {
long long Min = 0;
for (long long i = last[x]; i; i = a[i].next) {
long long y = a[i].y;
f[x][0] += min(f[y][0], f[y][1]);
f[x][1] += f[y][0];
Min = min(Min, f[y][1] - f[y][0]);
}
f[x][0] = min(f[x][0] + 1, f[x][1]);
f[x][1] += Min;
}
last[x] = 0;
}
void solve() {
tot;
scanf("%lld", &tot);
tim++;
for (long long i = 1; i <= tot; i++) scanf("%lld", &h[i]), vis[h[i]] = tim;
for (long long i = 1; i <= tot; i++)
if (vis[tr[h[i]].fa[0]] == tim) {
puts("-1");
return;
}
build();
dp(1);
printf("%lld\n", min(f[1][1], f[1][0]));
}
int main() {
scanf("%lld", &n);
for (long long i = 1; i < n; i++) {
long long x, y;
scanf("%lld %lld", &x, &y);
ins(x, y);
ins(y, x);
}
tr[0].dep = -1;
dfs(1, 0);
long long q;
scanf("%lld", &q);
len = 0;
memset(last, 0, sizeof(last));
while (q--) solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
const int mod = 1e9 + 7;
int isprime[maxn];
int main() {
int n;
cin >> n;
int a, b;
a = b = 0;
for (int i = 0; i < n; i++) {
int g;
cin >> g;
if (g == 1)
a++;
else
b++;
}
if (a == 0) {
for (int i = 0; i < b; i++) {
cout << 2 << " ";
}
} else if (b == 0) {
for (int i = 0; i < a; i++) {
cout << 1 << " ";
}
} else {
cout << "2 1 ";
for (int i = 0; i < b - 1; i++) {
cout << 2 << " ";
}
for (int i = 0; i < a - 1; i++) {
cout << 1 << " ";
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[3];
int ans = 1000;
cin >> a[0] >> a[1] >> a[2];
sort(a, a + 3);
for (int i = 1; i <= a[2]; i++) {
int y = abs(i - a[0]) + abs(i - a[1]) + abs(i - a[2]);
if (y < ans) ans = y;
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long int n;
cin >> n;
long long int ar[n];
for (long long int i = 0; i < n; ++i) cin >> ar[i];
long long int xi, fi;
cin >> xi >> fi;
long long int cnt = 0;
for (long long int i = 0; i < n; ++i) {
if (ar[i] > xi) {
long long int re = ar[i] - xi, rr;
if (re % (fi + xi) != 0)
rr = re / (fi + xi) + 1;
else
rr = re / (fi + xi);
cnt += rr;
}
}
cout << cnt * fi << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int block_size = 320;
const long long mod = 1e9 + 7;
const long long inf = 1e9 + 7;
const long double eps = 1e-9;
const double PI = atan(1) * 4;
template <typename T>
inline int sign(const T &a) {
if (a < 0) return -1;
if (a > 0) return 1;
return 0;
}
template <typename T>
inline void in(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
long long twop(int x) { return 1LL << x; }
template <typename A, typename B>
inline void in(A &x, B &y) {
in(x);
in(y);
}
template <typename A, typename B, typename C>
inline void in(A &x, B &y, C &z) {
in(x);
in(y);
in(z);
}
template <typename A, typename B, typename C, typename D>
inline void in(A &x, B &y, C &z, D &d) {
in(x);
in(y);
in(z);
in(d);
}
template <class T>
void upd(T &a, T b) {
a = max(a, b);
}
namespace HLD {
struct edge {
int to;
edge(int x) : to(x) {}
};
const int root = 1;
const int maxn = 200010;
vector<edge> adj[maxn];
int dfnToID[maxn], dfn[maxn], head[maxn], fa[maxn], dep[maxn], size[maxn],
heavy[maxn], r[maxn], cnt = 1;
void firstDfs(int cur, int _fa) {
dep[cur] = dep[_fa] + 1;
size[cur] = 1;
fa[cur] = _fa;
for (auto e : adj[cur]) {
if (e.to != _fa) {
firstDfs(e.to, cur);
size[cur] += size[e.to];
}
}
int heavyChild = 0;
for (auto e : adj[cur]) {
if (e.to != _fa) {
if (size[e.to] > size[heavyChild]) {
heavyChild = e.to;
}
}
}
heavy[cur] = heavyChild;
}
void secondDfs(int cur, int _fa) {
if (cur != heavy[_fa]) {
head[cur] = cur;
} else {
head[cur] = head[_fa];
}
dfn[cur] = cnt++;
r[cur] = dfn[cur];
dfnToID[dfn[cur]] = cur;
if (!heavy[cur]) return;
secondDfs(heavy[cur], cur);
r[cur] = r[heavy[cur]];
for (auto e : adj[cur]) {
if (e.to == _fa || e.to == heavy[cur]) continue;
secondDfs(e.to, cur);
r[cur] = r[e.to];
}
}
void init() {
firstDfs(root, 0);
secondDfs(root, 0);
}
int kthFather(int k, int cur) {
while (k) {
if (head[cur] == cur) {
k--;
cur = fa[head[cur]];
} else {
if (dep[cur] - dep[head[cur]] <= k) {
k -= dep[cur] - dep[head[cur]];
cur = head[cur];
} else {
return dfnToID[dfn[cur] - k];
}
}
}
return cur;
}
int LCA(int u, int v) {
while (head[u] != head[v]) {
if (dep[head[u]] > dep[head[v]]) swap(u, v);
v = fa[head[v]];
}
if (dep[u] < dep[v]) return u;
return v;
}
int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[LCA(u, v)]; }
} // namespace HLD
vector<int> adj[200010];
int n;
const int maxn = 200010;
int len[maxn];
void dfs(int cur, int fa) {
for (auto i : adj[cur]) {
if (i != fa) {
len[i] = len[cur] + 1;
dfs(i, cur);
}
}
}
int getFarthest(int x) {
memset(len, -1, sizeof(len));
len[x] = 0;
dfs(x, 0);
pair<int, int> ans = make_pair(len[1], 1);
for (long long i = 1; i < n + 1; i++)
ans = max(ans, make_pair(len[i], (int)i));
return ans.second;
}
pair<int, int> getDiam() {
int a = getFarthest(1);
int b = getFarthest(a);
return make_pair(a, b);
}
int deg[maxn];
vector<pair<int, int> > op;
long long ans = 0;
bool rem[maxn];
void bfs(pair<int, int> diam) {
long long ans = 0;
for (long long i = 1; i < n + 1; i++) {
for (auto j : adj[i]) deg[j]++;
}
queue<int> q;
for (long long i = 1; i < n + 1; i++) {
if (deg[i] == 1 && i != diam.first && i != diam.second) {
q.push(i);
rem[i] = 1;
}
}
while (((int)q.size())) {
int f = q.front();
q.pop();
int dis[] = {HLD::dis(f, diam.first), HLD::dis(f, diam.second)};
if (dis[0] > dis[1]) {
op.push_back(make_pair(f, diam.first));
ans += dis[0];
} else {
op.push_back(make_pair(f, diam.second));
ans += dis[1];
}
for (auto to : adj[f]) {
deg[to]--;
if (rem[to]) continue;
if (to != diam.first && to != diam.second && deg[to] == 1) {
q.push(to);
}
}
}
q.push(diam.first);
while (((int)q.size())) {
int f = q.front();
q.pop();
int dis[] = {HLD::dis(f, diam.first), HLD::dis(f, diam.second)};
op.push_back(make_pair(f, diam.second));
ans += dis[1];
for (auto to : adj[f]) {
deg[to]--;
if (rem[to]) continue;
if (to != diam.first && to != diam.second && deg[to] == 1) {
q.push(to);
}
}
}
cout << ans << endl;
for (auto i : op) {
printf("%d %d %d\n", i.first, i.second, i.first);
}
}
int main() {
in(n);
for (long long i = 1; i < n; i++) {
int u, v;
in(u, v);
HLD::adj[u].push_back(HLD::edge(v));
HLD::adj[v].push_back(HLD::edge(u));
adj[u].push_back(v);
adj[v].push_back(u);
}
HLD::init();
auto diam = getDiam();
cerr << "diam.fi"
<< " = " << (diam.first) << endl;
;
cerr << "diam.se"
<< " = " << (diam.second) << endl;
;
bfs(diam);
return 0;
}
| 6 |
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 51
int mutton[MAXN];
int plate[MAXN];
int main()
{
int N,K,res=0;
cin>>N>>K;
for(int i=0;i<N;i++)
cin>>mutton[i];
//l+r+k<=K,l+r<=N
int bound=min(N,K);
for(int l=0;l<=bound;l++)
{
for(int r=0;r<=bound-l;r++)
{
int i,j;
for(i=0;i<l;i++)
plate[i]=mutton[i];
for(j=0;j<r;j++)
plate[i+j]=mutton[N-j-1];
sort(plate,plate+l+r);
int k=0,temp=0;
while(k+l+r<K&&plate[k]<0)
k++;
while(k<l+r)
{
temp+=plate[k];
k++;
}
res=max(res,temp);
}
}
cout<<res<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
set<long long> v;
for (long long i = 0; i <= 20; i++) {
for (long long j = 0; j <= 30; j++) {
long long num = (powl(2, j) * powl(3, i));
if (num <= 2000000000) v.insert(num);
}
}
for (long long i = 0; i <= 30; i++) {
for (long long j = 0; j <= 20; j++) {
long long num = (powl(3, j) * powl(2, i));
if (num <= 2000000000) v.insert(num);
}
}
long long l;
cin >> l;
long long r;
cin >> r;
;
long long res = 0;
for (auto i : v) {
if (i >= l && i <= r) {
res++;
}
}
cout << res << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > a, b;
int f[100005], g[100005];
int main() {
int n, s, l, i, x = 0;
scanf("%d%d%d", &n, &s, &l);
for (i = 1; i <= n; i++) scanf("%d", &g[i]);
a.clear();
b.clear();
f[0] = 0;
for (i = 1; i <= n; i++) {
a.insert(make_pair(g[i], i));
while (a.rbegin()->first - a.begin()->first > s) {
b.erase(make_pair(f[x], x));
x++;
a.erase(make_pair(g[x], x));
}
if (i >= l && a.size() >= l) {
if (f[i - l] >= 0) b.insert(make_pair(f[i - l], i - l));
}
if (b.size() == 0)
f[i] = -1;
else
f[i] = b.begin()->first + 1;
}
printf("%d\n", f[n]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500, inF = 1e9;
int n, m, u, v, w, cnt[N][N], dp[N][N], edge[N][N];
vector<int> ans, nei[N];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) dp[i][j] = edge[i][j] = inF;
for (int u = 0; u < n; u++) dp[u][u] = edge[u][u] = 0;
while (m--) {
cin >> u >> v >> w;
dp[--u][--v] = dp[v][u] = min(dp[v][u], w);
edge[u][v] = edge[v][u] = min(edge[u][v], w);
nei[u].push_back(v);
nei[v].push_back(u);
}
for (int i = 0; i < n; i++)
for (int v = 0; v < n; v++)
for (int u = 0; u < n; u++) dp[v][u] = min(dp[v][u], dp[v][i] + dp[i][u]);
for (int u = 0; u < n; u++)
for (int v = 0; v < n; v++)
for (int w : nei[v])
if (edge[w][v] + dp[w][u] == dp[v][u]) cnt[u][v]++;
for (int u = 0; u < n; u++)
for (int v = u + 1; v < n; v++) {
int res = 0;
for (int w = 0; w < n; w++)
res += cnt[u][w] * (dp[w][u] + dp[w][v] == dp[u][v]);
ans.push_back(res);
}
for (int num : ans) cout << num << ' ';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int N, T, A[100005], F[2][100005];
struct Nod {
int x, y;
} Ans[100005];
inline int Next(int st, int len) {
if (F[0][N] - F[0][st - 1] < len && F[1][N] - F[1][st - 1] < len) return -1;
int l = st - 1, r = N;
for (; l + 1 < r;) {
int Mid = l + r >> 1;
if (F[0][Mid] - F[0][st - 1] >= len || F[1][Mid] - F[1][st - 1] >= len)
r = Mid;
else
l = Mid;
}
return r;
}
inline void Work(int b) {
int x = 0, y = 0;
for (int i = 1, j; i <= N; i = j + 1) {
if ((j = Next(i, b)) == -1) return;
if (F[0][j] - F[0][i - 1] >= b) x++;
if (F[1][j] - F[1][i - 1] >= b) y++;
}
if (x == y) return;
if (x > y && A[N] == 2 || x < y && A[N] == 1) return;
T++;
Ans[T].x = max(x, y);
Ans[T].y = b;
}
inline bool Cmp(Nod a, Nod b) { return a.x < b.x || a.x == b.x && a.y < b.y; }
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
int x;
scanf("%d", &A[i]);
F[0][i] = F[0][i - 1] + (A[i] == 1);
F[1][i] = F[1][i - 1] + (A[i] == 2);
}
for (int i = 1; i <= N; i++) Work(i);
printf("%d\n", T);
sort(Ans + 1, Ans + T + 1, Cmp);
for (int i = 1; i <= T; i++) printf("%d %d\n", Ans[i].x, Ans[i].y);
}
| 2 |
#include<cstdio>
int x,y;
int main()
{
scanf("%d %d",&x,&y);
if(x%y)printf("%d",x);
else printf("-1");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7, K = 18, INF = 1e9 + 7;
pair<int, int> combine(const pair<int, int> &a, const pair<int, int> &b) {
return pair<int, int>(min(a.first, b.first), max(a.second, b.second));
}
struct SegTree {
int n;
vector<pair<int, int> > tr;
SegTree(){};
SegTree(int n, pair<int, int> *a) : n(n), tr(4 * n) { build(1, 0, n - 1, a); }
void build(int u, int st, int en, pair<int, int> *a) {
if (st == en) {
tr[u] = a[st];
} else {
int mid = (st + en) / 2;
build(2 * u, st, mid, a);
build(2 * u + 1, mid + 1, en, a);
tr[u] = combine(tr[2 * u], tr[2 * u + 1]);
}
}
pair<int, int> query(int u, int st, int en, int l, int r) {
if (r < st || en < l)
return {INF, -INF};
else if (l <= st && en <= r)
return tr[u];
else {
int mid = (st + en) / 2;
return combine(query(2 * u, st, mid, l, r),
query(2 * u + 1, mid + 1, en, l, r));
}
}
pair<int, int> query(int l, int r) { return query(1, 0, n - 1, l, r); }
};
int a[N];
pair<int, int> range[K][N];
SegTree tr[K];
int solve(int i, int d) {
if (d == 0) return 0;
int ans = 0;
int cl = i, cr = i;
for (int i = K - 1; i >= 0; i--) {
pair<int, int> pr = tr[i].query(cl, cr);
if (pr.second - pr.first >= d) continue;
cl = pr.first;
cr = pr.second;
ans += 1 << i;
}
return ans + 1;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = n; i < 3 * n; i++) a[i] = a[i - n];
for (int i = 0; i < 3 * n; i++) {
range[0][i].first = max(0, i - a[i]);
range[0][i].second = min(3 * n - 1, i + a[i]);
}
tr[0] = SegTree(3 * n, range[0]);
for (int k = 1; k < K; k++) {
for (int i = 0; i < 3 * n; i++) {
range[k][i] =
tr[k - 1].query(range[k - 1][i].first, range[k - 1][i].second);
}
tr[k] = SegTree(3 * n, range[k]);
}
for (int i = n; i < 2 * n; i++) cout << solve(i, n - 1) << " ";
}
| 4 |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
using namespace std;
#define all(c) (c).begin(), (c).end()
int main(){
int N, Q;
while(cin >> N >> Q, N|Q){
typedef tuple<int, int, string> Data;
vector<Data> era;
era.reserve(N);
while(N--){
string name;
int eyear, wyear;
cin >> name >> eyear >> wyear;
era.emplace_back(wyear - eyear + 1, wyear, name);
}
sort(all(era));
auto comp = [](Data& d, int x){return get<1>(d) < x;};
while(Q--){
int query, one = 0, known = 0;
string name;
cin >> query;
if(query <= get<1>(era.back()))tie(one, known, name) = *lower_bound(all(era), query, comp);
if(one <= query && query <= known)cout << name << " " << query - one + 1 << '\n';
else cout << "Unknown\n";
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int power(long long int x, unsigned long long y, int p) {
long long int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
string s;
cin >> s;
int n = s.length();
vector<int> sufs(n, 0), sufe(n, 0);
for (int i = 0; i < n; i++) {
for (int j = i, k = i; (k > -1) && (j < n); j++, k--) {
if (s[j] == s[k]) {
sufe[k]++;
sufs[j]++;
} else
break;
}
}
for (int i = 0; i < n; i++) {
for (int j = i, k = i + 1; (j > -1) && (k < n); j--, k++) {
if (s[j] == s[k]) {
sufs[k]++;
sufe[j]++;
} else
break;
}
}
for (int i = n - 2; i > -1; i--) {
sufe[i] += sufe[i + 1];
}
long long int ans = 0;
for (int i = 0; i < n - 1; i++) {
ans += ((long long int)sufs[i]) * ((long long int)sufe[i + 1]);
}
cout << ans << endl;
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,k;
LL t;
int main()
{
scanf("%d%d",&n,&k);
t=n/k;
LL ans=t*t*t;
if (!(k&1)&&n>=k/2)
{
t=(n-k/2)/k+1;
// cout<<t<<endl;
ans+=t*t*t;
}
printf("%lld\n",ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
int a, b, c, d, i, j, n, m, k;
vector<pair<int, int> > sm[100001];
bool used[100001];
int num[100001];
vector<int> bad, cand;
void dfs(int v) {
if (used[v]) return;
used[v] = 1;
for (int _n(((int)((sm[v]).size())) - 1), i(0); i <= _n; i++) {
int w = sm[v][i].first, add = sm[v][i].second;
if (used[w] && num[v] + add != num[w]) {
bad.push_back(abs(num[v] + add - num[w]));
} else if (!used[w]) {
num[w] = num[v] + add;
dfs(w);
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int _n((m)-1), i(0); i <= _n; i++) {
scanf("%d%d", &a, &b);
--a;
--b;
sm[a].push_back(make_pair(b, 1));
sm[b].push_back(make_pair(a, -1));
}
for (int _n((n)-1), i(0); i <= _n; i++)
if (!used[i]) dfs(i);
sort((bad).begin(), (bad).end());
bad.resize(unique((bad).begin(), (bad).end()) - bad.begin());
for (int _n((n)), i(1); i <= _n; i++) cand.push_back(i);
for (int _n(((int)((bad).size())) - 1), i(0); i <= _n; i++) {
if (!bad[i]) {
cerr << "HER" << endl;
printf("%d\n", 1);
exit(0);
}
a = 0;
while (a < (int)((cand).size())) {
if (bad[i] % cand[a]) {
swap(cand[a], cand.back());
cand.pop_back();
} else
++a;
}
}
printf("%d\n", *max_element((cand).begin(), (cand).end()));
}
| 3 |
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
double n, m, d;
cin >> n >> m >> d;
cout << setprecision(12) << fixed << (d == 0 ? 1 : 2) * (m - 1) * (n - d) / (n * n) << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long a, b, c;
int main() {
int yn = 1;
cout.sync_with_stdio(false);
cin.sync_with_stdio(false);
cin >> a >> b >> c;
long long c1 = c, c2 = c;
if (c % a == 0)
cout << "Yes";
else if (c % b == 0)
cout << "Yes";
else {
while (c1 % b != 0 && c1 > 0) {
c1 -= a;
}
if (c1 <= 0)
yn = 0;
else
cout << "Yes";
}
if (yn == 0) {
while (c2 % a != 0 && c2 > 0) {
c2 -= b;
}
if (c2 <= 0)
yn = 0;
else
cout << "Yes";
}
if (yn == 0) cout << "No";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e6 + 2;
const long long int mod = 1e9 + 7;
vector<int> sfr, msbt, mnf;
long long int ta, mx, f[N];
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (a == 0)
sfr.push_back(a);
else if (a > 0)
msbt.push_back(a);
else
mnf.push_back(a);
}
if (!msbt.size()) {
msbt.push_back(mnf.back());
mnf.pop_back();
msbt.push_back(mnf.back());
mnf.pop_back();
}
if (!(mnf.size() % 2)) sfr.push_back(mnf.back()), mnf.pop_back();
cout << mnf.size() << ' ';
for (int i = 0; i < mnf.size(); i++) cout << mnf[i] << " ";
cout << '\n' << msbt.size() << " ";
for (int i = 0; i < msbt.size(); i++) cout << msbt[i] << " ";
cout << '\n' << sfr.size() << " ";
for (int i = 0; i < sfr.size(); i++) cout << sfr[i] << " ";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
int v[200100];
struct IT {
int d[800100];
int s[800100];
IT() {
for (int i = 0; i <= 800000; i++) d[i] = 1;
}
void upd(int num, int l, int r, int x, int y, int z) {
if (l > y || r < x || l > r) return;
if (l >= x && y >= r) {
if (z > 0) {
d[num] = (1LL * d[num] * z) % mod;
s[num] = (1LL * s[num] * z) % mod;
} else
s[num] -= (1LL * z * d[num]) % mod;
return;
}
upd(num * 2, l, (l + r) / 2, x, y, z);
upd(num * 2 + 1, (l + r) / 2 + 1, r, x, y, z);
s[num] = (1LL * (s[num * 2] + s[num * 2 + 1]) * d[num]) % mod;
}
int get(int num, int l, int r, int x, int y) {
if (l > y || r < x || l > r) return 0;
if (l >= x && y >= r) {
return s[num];
}
return (1LL *
(get(num * 2, l, (l + r) / 2, x, y) +
get(num * 2 + 1, (l + r) / 2 + 1, r, x, y)) *
d[num]) %
mod;
}
} it, it2;
int s[200100];
int deg[200100];
int type[200100];
int x[200100];
int y[200100];
int inv[200100];
int z[200100];
int f[200100], cnt = 0;
vector<int> a[200100];
void dfs(int x, int last) {
s[x] = ++cnt;
for (int i = 0; i < a[x].size(); i++) {
if (a[x][i] == last) continue;
dfs(a[x][i], x);
}
f[x] = cnt;
}
int pow(int x, int y) {
int s = 1;
while (y) {
if (y & 1) s = (1LL * s * x) % mod;
x = (1LL * x * x) % mod;
y >>= 1;
}
return s;
}
int main() {
int q;
scanf("%d %d", &v[1], &q);
deg[1] = 1;
cnt = 1;
for (int i = 1; i <= 200000; i++) inv[i] = pow(i, mod - 2);
for (int i = 1; i <= q; i++) {
scanf("%d", &type[i]);
if (type[i] == 1) {
scanf("%d %d", &x[i], &y[i]);
z[i] = ++cnt;
a[x[i]].push_back(z[i]);
} else
scanf("%d", &x[i]);
}
cnt = 0;
dfs(1, 1);
for (int i = 1; i <= cnt; i++) it2.upd(1, 1, cnt, i, i, -1);
it.upd(1, 1, cnt, s[1], s[1], -v[1]);
for (int i = 1; i <= q; i++) {
if (type[i] == 1) {
deg[x[i]]++;
deg[z[i]] = 1;
v[z[i]] = y[i];
it.upd(1, 1, cnt, s[z[i]], s[z[i]], -y[i]);
it.upd(1, 1, cnt, s[x[i]], f[x[i]], inv[deg[x[i]] - 1]);
it2.upd(1, 1, cnt, s[x[i]], f[x[i]], inv[deg[x[i]]]);
it2.upd(1, 1, cnt, s[x[i]], f[x[i]], deg[x[i]] - 1);
it.upd(1, 1, cnt, s[x[i]], f[x[i]], deg[x[i]]);
} else {
cout << 1LL * it.get(1, 1, cnt, s[x[i]], f[x[i]]) *
it2.get(1, 1, cnt, s[x[i]], s[x[i]]) % mod * (deg[x[i]]) % mod
<< '\n';
}
}
}
| 4 |
#include <iostream>
using namespace std;
class Node{
public:
int depth;
int parent;
int degree;
int *children;
};
void Depth(Node *node,int cur, int dp){
node[cur].depth = dp;
for(int i=0;i<node[cur].degree;++i){
Depth(node,node[cur].children[i],dp+1);
}
}
int main() {
int n;
Node *node;
cin>>n;
node = new Node[n];
for(int i=0;i<n;++i)node[i].parent = -1;
for(int i=0;i<n;++i){
int id,k;
cin>>id;
cin>>k;
node[id].degree = k;
node[id].children = new int[k];
for(int j=0;j<k;++j){
int cid;
cin>>cid;
node[id].children[j] = cid;
node[cid].parent = id;
}
}
int root;
for(int i=0;i<n;++i)if(node[i].parent == -1)root = i;
Depth(node,root,0);
for(int i=0;i<n;++i){
cout<<"node "<<i<<": parent = "<<node[i].parent<<", depth = "<<node[i].depth<<
", "<<( i==root?"root": ( node[i].degree == 0 ? "leaf" : "internal node" ) )<<", [";
for(int j=0;j<node[i].degree;++j){
cout<<(j?", ":"")<<node[i].children[j];
}
cout<<"]"<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
int divs(int x) {
int res = 0;
for (int i = 1; i * i <= x; i++) {
if (x % i == 0) res++;
if (x % i == 0 && i * i != x) res++;
}
return res;
}
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int ans = 0;
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
for (int k = 1; k <= c; k++) {
if (cnt[i * j * k] == 0) cnt[i * j * k] = divs(i * j * k);
ans += cnt[i * j * k];
if (ans >= 1073741824) ans -= 1073741824;
}
}
}
printf("%d", ans);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
vector<int> arr;
for (int i = 0; i < 3; i++) {
int x;
cin >> x;
arr.push_back(x);
}
sort(arr.begin(), arr.end());
int a, b, c;
a = arr[2];
b = arr[1];
c = arr[0];
if (a >= 4 && b >= 4 && c >= 4)
cout << 7;
else if (a >= 3 && b >= 3 && c >= 3)
cout << 6;
else if (a >= 3 && b >= 2 && c >= 2)
cout << 5;
else if (a >= 2 && b >= 2 && c >= 1)
cout << 4;
else if (a >= 1 && b >= 1 && c >= 1 || a >= 2 && b >= 2 && c == 0)
cout << 3;
else if (a >= 1 && b >= 1 && c == 0)
cout << 2;
else if (a >= 1 && b == 0 && c == 0)
cout << 1;
else
cout << 0;
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
int main() {
char str1[1000005], str2[1000005];
int l1, l2, sum1, sum2, i;
scanf("%s", str1);
scanf("%s", str2);
l1 = strlen(str1);
l2 = strlen(str2);
sum1 = sum2 = 0;
if (l1 != l2) {
printf("NO\n");
} else {
for (i = 0; i < l1; i++) {
if (str1[i] == '1') sum1++;
}
for (i = 0; i < l2; i++) {
if (str2[i] == '1') sum2++;
}
if ((sum1 == 0 && sum2 == 0) || (sum1 != 0 && sum2 != 0))
printf("YES\n");
else
printf("NO\n");
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e6 + 7;
const long long INF = 1e18;
long long n, m, k, q, ans, a[N], b[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
for (long long i = 1; i <= k; i++) {
cin >> b[i];
}
long long m = (1 << 9);
for (long long mask = 0; mask < m; mask++) {
bool flag = 1;
for (long long i = 1; i <= n; i++) {
long long cur = 0;
for (long long j = 1; j <= k; j++) {
cur |= ((a[i] & b[j] & mask) == (a[i] & b[j]));
}
flag &= cur;
}
if (flag) {
cout << mask << endl;
return 0;
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
if (a < b) swap(a, b);
return gcd(a % b, b);
}
long long gcdextended(long long a, long long b, long long &x, long long &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
long long x1, y1, gcd = gcdextended(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
long long modinverse(long long a, long long m) {
long long x, y;
long long g = gcdextended(a, m, x, y);
if (g != 1) {
cout << "NOT POSSIBLE";
return -1;
} else
return (x % m + m) % m;
}
long long po(long long x, long long y) {
long long res = 1;
while (y > 0) {
if (y & 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
long long pom(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long root(long long x, vector<long long> &id) {
while (id[x] != x) {
id[x] = id[id[x]];
x = id[x];
}
return x;
}
void uni(long long x, long long y, vector<long long> &id) {
long long a = root(x, id);
long long b = root(y, id);
id[a] = id[b];
}
bool sec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
if (a.second == b.second) return a.first < b.first;
return a.second < b.second;
}
const long long N = 1e6 + 5;
long long p[N];
void solve() {
long long n, i, j;
cin >> n;
for (i = 0; i < N; i++) p[i] = i;
for (i = 2; i < N; i++) {
if (p[i] == i) {
for (j = 2 * i; j < N; j += i) p[j] = i;
}
}
long long x = p[n], y = (n / p[n] - 1) * p[n], an = n, z;
for (i = y + 1; i < n + 1; i++) {
if (p[i] != i) {
x = (i / p[i] - 1) * p[i] + 1;
an = min(an, x);
}
}
cout << an;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
srand(time(NULL));
;
long long t;
t = 1;
while (t--) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int i;
long long n, d, m, l;
cin >> n >> d >> m >> l;
long long ans = (n * m + d - 1) / d * d;
for ((i) = 0; (i) < (int)(m + 10); (i)++) {
long long x = d * i;
if (x % m > l) ans = min(ans, x);
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline long long read() {
long long x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(long long a) {
if (a < 0) {
a = -a;
putchar('-');
}
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(long long a) {
write(a);
puts("");
}
inline void wri(long long a) {
write(a);
putchar(' ');
}
const int N = 300005, inf = 1e9, M = 1 << 20 | 2;
vector<int> v[N << 1];
int n, tot, t1, t2, q1[N], q2[N], p[N];
int l[N << 1], r[N << 1], L[N], mx[N << 1], mn[N << 1], h[N << 1];
pair<int, int> tr[M];
int lzy[M];
void cb(int x, int dq) {
v[x].push_back(dq);
l[x] = min(l[x], l[dq]);
r[x] = max(r[x], r[dq]);
mx[x] = max(mx[x], mx[dq]);
mn[x] = min(mn[x], mn[dq]);
}
bool check(int x) { return mx[x] - mn[x] == r[x] - l[x]; }
void push_up(int nod) {
tr[nod] = min(tr[nod << 1], tr[nod << 1 | 1]);
tr[nod].first += lzy[nod];
}
void cao(int nod, int de) {
lzy[nod] += de;
tr[nod].first += de;
}
void ins(int l, int r, int i, int j, int de, int nod) {
if (l == i && r == j) {
cao(nod, de);
return;
}
int mid = (l + r) >> 1;
if (j <= mid)
ins(l, mid, i, j, de, nod << 1);
else if (i > mid)
ins(mid + 1, r, i, j, de, nod << 1 | 1);
else {
ins(l, mid, i, mid, de, nod << 1);
ins(mid + 1, r, mid + 1, j, de, nod << 1 | 1);
}
push_up(nod);
}
void build(int l, int r, int nod) {
if (l == r) {
tr[nod] = make_pair(l, l);
return;
}
int mid = (l + r) >> 1;
build(l, mid, nod << 1);
build(mid + 1, r, nod << 1 | 1);
push_up(nod);
}
int main() {
tot = n = read();
for (int i = (int)(1); i <= (int)(n); i++) p[read()] = read();
build(1, n, 1);
for (int i = (int)(1); i <= (int)(n); i++) {
while (t1 && p[q1[t1]] > p[i]) {
ins(1, n, q1[t1 - 1] + 1, q1[t1], p[q1[t1]] - p[i], 1);
t1--;
}
while (t2 && p[q2[t2]] < p[i]) {
ins(1, n, q2[t2 - 1] + 1, q2[t2], p[i] - p[q2[t2]], 1);
t2--;
}
q1[++t1] = i;
q2[++t2] = i;
L[i] = tr[1].second;
}
stack<int> st;
for (int i = (int)(1); i <= (int)(n); i++) {
mn[i] = mx[i] = p[i];
l[i] = r[i] = i;
h[i] = 0;
int dq = i;
while (st.size()) {
int x = st.top();
if (l[x] < L[i]) break;
if ((h[x] == 1 && mx[x] + 1 == mn[dq]) ||
(h[x] == -1 && mn[x] - 1 == mx[dq])) {
cb(x, dq);
dq = x;
st.pop();
} else {
int zs = ++tot, cnt = 0;
v[zs].push_back(dq);
mx[zs] = mx[dq];
mn[zs] = mn[dq];
l[zs] = l[dq];
r[zs] = r[dq];
h[zs] = h[dq];
while (1) {
int x = st.top();
st.pop();
cb(zs, x);
cnt++;
if (check(zs)) {
if (cnt == 1)
h[zs] = mx[x] == mx[zs] ? -1 : 1;
else
h[zs] = 0;
break;
}
}
dq = zs;
}
}
st.push(dq);
}
long long ans = 1;
for (int i = (int)(n + 1); i <= (int)(tot); i++) {
int ds = v[i].size();
if (h[i])
ans += (long long)ds * (ds + 1) / 2 - 1;
else
ans += ds;
}
cout << ans << endl;
}
| 6 |
#include <cstdio>
#include <algorithm>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
constexpr int MAX_N = 100000;
using ll = long long;
int N, K;
ll a[MAX_N];
ll S[MAX_N+1], zS[MAX_N+1];
int main()
{
scanf( "%d%d", &N, &K );
rep( i, N )
scanf( "%lld", a+i );
S[0] = 0;
zS[0] = 0;
rep( i, N )
{
S[i+1] = S[i]+a[i];
zS[i+1] = zS[i]+std::max( 0ll, a[i] );
}
ll ans = 0;
rep( i, N-K+1 )
ans = std::max( ans, std::max( 0ll, S[i+K]-S[i] )+zS[i]-zS[0]+zS[N]-zS[i+K] );
printf( "%lld\n", ans );
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, b, p, n, r = 0;
string s, alc[11] = {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM",
"SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"};
int main() {
cin >> n;
for (i = 1; i <= n; ++i) {
cin >> s;
if ((s[0] <= '9' && s[0] >= '0') &&
((s.length() == 1) || (s.length() == 2 && s[0] <= '1' && s[1] < '8')))
++r;
for (j = 0; j < 11; ++j)
if (s == alc[j]) ++r;
}
cout << r;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void func(void) {
freopen("input.c", "r", stdin);
freopen("output.c", "w", stdout);
}
const long long N = 1e5 + 10;
long long tree[N * 5][2], lazy[N * 5][2];
long long a[N], b[N], c[N];
void update(long long node, long long b, long long e, long long l, long long r,
long long val, long long i) {
if (lazy[node][i] != 0) {
tree[node][i] += lazy[node][i];
if (b != e) {
lazy[node * 2][i] += lazy[node][i];
lazy[node * 2 + 1][i] += lazy[node][i];
}
lazy[node][i] = 0;
}
if (b > e or b > r or e < l) return;
if (b >= l and e <= r) {
tree[node][i] += val;
if (b != e) {
lazy[node * 2][i] += val;
lazy[node * 2 + 1][i] += val;
}
return;
}
long long left = node * 2;
long long right = node * 2 + 1;
long long mid = (b + e) / 2;
update(left, b, mid, l, r, val, i), update(right, mid + 1, e, l, r, val, i);
tree[node][i] = tree[left][i] + tree[right][i];
}
long long query(long long node, long long b, long long e, long long l,
long long r, long long i) {
if (lazy[node][i] != 0) {
tree[node][i] += lazy[node][i];
if (b != e) {
lazy[node * 2][i] += lazy[node][i];
lazy[node * 2 + 1][i] += lazy[node][i];
}
lazy[node][i] = 0;
}
if (b > e or b > r or e < l) return 0;
if (b >= l and e <= r) {
return tree[node][i];
}
long long left = node * 2;
long long right = node * 2 + 1;
long long mid = (b + e) / 2;
return query(left, b, mid, l, r, i) + query(right, mid + 1, e, l, r, i);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, q, i, j = 0, temp, t, k, ans = 0, sum = 0, x, y, z, cnt = 0, m,
fg = 0, mx = 0, mx1 = 0, mn = 8000000000000000000,
mn1 = 8000000000000000000;
cin >> n >> m >> k;
for (long long i = 0; i < n; i++) {
cin >> x;
update(1, 1, n, i + 1, i + 1, x, 0);
}
for (long long i = 1; i <= m; i++) {
cin >> a[i] >> b[i] >> c[i];
}
for (long long i = 1; i <= k; i++) {
cin >> x >> y;
update(1, 1, m, x, y, 1, 1);
}
for (long long i = 1; i <= m; i++) {
long long val = query(1, 1, m, i, i, 1);
update(1, 1, n, a[i], b[i], c[i] * val, 0);
}
for (long long i = 1; i <= n; i++) {
cout << query(1, 1, n, i, i, 0) << " ";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int sher[11], mor[11], s[1001], m[1001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
char c;
cin >> c;
++sher[c - '0'];
s[i] = c - '0';
}
for (int j = 0; j < n; ++j) {
char c;
cin >> c;
++mor[c - '0'];
m[j] = c - '0';
}
sort(s, s + n);
sort(m, m + n);
int cnt = 0, j = 0;
for (int i = 0; i < n;) {
if (m[i] > s[j]) {
++cnt;
++j;
++i;
if (j == n) break;
} else {
++i;
}
}
int cnt2 = 0;
for (int i = 0; i < n; ++i) {
bool got = false;
for (int j = s[i]; j < 10; ++j) {
if (mor[j]) {
got = true;
--mor[j];
break;
}
}
if (!got) ++cnt2;
}
cout << cnt2 << '\n' << cnt << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int warriors, pairse;
vector<vector<int> > adjacent;
vector<int> connected;
vector<pair<int, int> > pairs;
int main() {
cin >> warriors >> pairse;
adjacent.resize(warriors + 1);
for (int i = 1; i < warriors + 1; i++) adjacent[i].resize(warriors + 1);
connected.resize(warriors + 1);
for (int i = 0; i < pairse; i++) {
int a, b;
cin >> a >> b;
adjacent[a][b] = 1;
adjacent[b][a] = 1;
connected[a]++;
connected[b]++;
pair<int, int> foo = make_pair(a, b);
pairs.push_back(foo);
}
int mini = 1600000;
for (pair<int, int> p : pairs) {
int soFar = connected[p.first] + connected[p.second] - 4;
for (int i = 1; i < warriors + 1; i++) {
if (adjacent[p.first][i] && adjacent[p.second][i])
if (mini > soFar + connected[i] - 2) mini = soFar + connected[i] - 2;
}
}
if (mini != 1600000)
cout << mini << endl;
else
cout << -1 << endl;
return 0;
}
| 2 |
#include <stdio.h>
int main()
{
int N,c=0; scanf ("%d", &N);
bool f[101][101] = { 0, };
for (int i = 1; i <= N; i++) for (int j = i + 1; j <= N; j++) f[i][j] = 1, c++;
if (N % 2) for (int i = 1, j = N - 1; i < j; i++, j--) f[i][j] = 0, c--;
else for (int i = 1, j = N; i < j; i++, j--) f[i][j] = 0, c--;
printf ("%d\n", c);
for (int i = 1; i <= N; i++) for (int j = i + 1; j <= N; j++) if (f[i][j]) printf ("%d %d\n", i, j);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int t;
long long n, k;
pair<pair<long long, long long>, long long> fnd(long long levels) {
long long res = 0, lst = 0;
for (int i = 0; i < levels; i++) {
lst += (1LL << i);
if (res > k - lst) {
return make_pair(make_pair(res, levels - i), lst);
}
res += lst;
}
lst += (1LL << levels);
return make_pair(make_pair(res, 0), lst);
}
inline long long log4(long long n) {
long long tot = 1, sum = 1;
for (int i = 0; i <= n; i++) {
if (sum >= n) return i + 1;
tot *= 4;
sum += tot;
}
assert(false);
}
inline long long total(long long levels) {
if (levels == 0) return 0;
long long tot = 1, sum = 1;
for (int i = 1; i < levels; i++) {
tot *= 4;
sum += tot;
}
return sum;
}
int main() {
scanf("%d", &t);
while (t--) {
scanf(" %lld %lld", &n, &k);
pair<pair<long long, long long>, long long> most = fnd(n);
k -= most.first.first;
long long req = log4(k);
if (n > req)
printf("YES %lld\n", most.first.second);
else if (n == req) {
long long side = most.second * total(most.first.second);
long long rem = total(n) - most.first.first - side;
if (rem >= k)
printf("YES %lld\n", most.first.second);
else
printf("NO\n");
} else
printf("NO\n");
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
set<int> S;
for (int i = 1; i <= n; i++) S.insert(i);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
auto it = S.find(x);
auto b = S.end();
b--;
printf("%d ", *b - (n - i - 1));
S.erase(it);
}
printf("1\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
template <class A>
void read(A& a) {
a = 0;
int c = getchar(), f = 0;
for (; !isdigit(c); c = getchar()) f |= c == '-';
for (; isdigit(c); c = getchar()) a = a * 10 + (c ^ 48);
if (f) a = -a;
}
template <class A, class... B>
void read(A& a, B&... b) {
read(a);
read(b...);
}
template <class T>
inline void print(T x) {
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
const int N = 1e5 + 10;
int n, m1, m2;
int f1[N], f2[N];
void init() {
for (int i = 1; i <= n; i++) f1[i] = f2[i] = i;
}
int finds(int* f, int x) {
if (x == f[x])
return x;
else
return f[x] = finds(f, f[x]);
}
void unio(int* f, int x, int y) {
int fx = finds(f, x), fy = finds(f, y);
if (fx < fy) f[fy] = fx;
if (fy < fx) f[fx] = fy;
}
int main() {
read(n, m1, m2);
init();
for (int i = 1, x, y; i <= m1; i++) {
read(x, y);
unio(f1, x, y);
}
for (int i = 1, x, y; i <= m2; i++) {
read(x, y);
unio(f2, x, y);
}
vector<pair<int, int> > ans;
vector<int> v1, v2;
for (int i = 1; i <= n; i++) {
if (finds(f2, i) != 1 && finds(f1, i) != 1) {
unio(f2, i, 1), unio(f1, i, 1);
ans.push_back({1, i});
}
if (finds(f2, i) != 1) v1.push_back(i);
if (finds(f1, i) != 1) v2.push_back(i);
}
for (auto i : v1) {
if (finds(f2, i) == 1) continue;
while (v2.size()) {
int u = v2.back();
v2.pop_back();
if (finds(f1, u) == 1) continue;
ans.push_back({i, u});
unio(f2, i, u), unio(f1, i, u);
break;
}
}
printf("%d\n", ans.size());
for (auto t : ans) printf("%d %d\n", t.first, t.second);
}
| 4 |
#include <valarray>
#include <cstdio>
using namespace std;
typedef valarray<int>V;
int main(){
int i,j,n,m,a,b,c,t;
for(;scanf("%d%d%d%d%d%d",&n,&m,&a,&b,&c,&t),n;){
V x(n*n);
V e(n*n);
V z(n*n);
V s(n);
x[0]=b;
x[1]=c;
e[0]=1;
for(i=1;i<n-1;i++){
x[i*n+i-1]=a;
x[i*n+i]=b;
x[i*n+i+1]=c;
e[i*n+i]=1;
}
x[i*n+i-1]=a;
x[i*n+i]=b;
e[i*n+i]=1;
for(;t;t>>=1){
const V &_x=e;
const V &_y=x;
if(t&1){
for(i=0;i<n;i++)for(j=0;j<n;j++)z[i*n+j]=(_x[slice(i*n,n,1)]*_y[slice(j,n,n)]).sum()%m;
e=z;
}
for(i=0;i<n;i++)for(j=0;j<n;j++)z[i*n+j]=(_y[slice(i*n,n,1)]*_y[slice(j,n,n)]).sum()%m;
x=z;
}
for(i=0;i<n;i++)scanf("%d",&s[i]);
const V &r=e;
for(i=0;i<n-1;i++)printf("%d ",(r[slice(i*n,n,1)]*s).sum()%m);
printf("%d\n",(r[slice(i*n,n,1)]*s).sum()%m);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c;
long long h, l;
while (~scanf("%lld %lld", &h, &l)) {
scanf("%lld %lld %lld", &a, &b, &c);
long long sa, sb;
sa = a * 2 + b;
sb = b + c * 3;
if (h <= sa)
sa = sa - h;
else
sa = 0;
if (l <= sb)
sb = sb - l;
else
sb = 0;
printf("%lld\n", sa + sb);
}
return 0;
}
| 1 |
#include<stdio.h>
int main()
{
int a;
int b;
int c;
scanf("%d %d %d",&a,&b,&c);
if(a<b&&b<c)
printf("Yes");
else
printf("No");
printf("\n");
}
| 0 |
#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;
//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
struct status {
int nx;
int ny;
int way;
};
bool operator<(const status&b, const status&a) {
return b.nx * 100 + b.ny * 10000 + b.way < a.nx * 100 + a.ny * 10000 + a.way;
}
const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,1,0,-1 };
int main() {
int N, X0, Y0, T; cin >> N >> X0 >> Y0 >> T;
vector<vector<vector<int>>>lines(60, vector < vector<int>>(60, vector<int>(4)));
for (int i = 0; i < N; ++i) {
int xs, ys, xe, ye; cin >> xs >> ys >> xe >> ye;
if (xs > xe)swap(xs, xe);
if (ys > ye)swap(ys, ye);
if (xs == xe) {
for (int y = ys; y < ye; ++y) {
lines[y][xs][1] = true;
lines[y + 1][xs][3] = true;
}
}
else {
for (int x = xs; x < xe; ++x) {
lines[ys][x][2] = true;
lines[ys][x + 1][0] = true;
}
}
}
vector<int>tways(1001,-1);
map<char, int>mp;
mp[N] = 3;
mp['W'] = 0;
mp['S'] = 3;
mp['E'] = 2;
mp['N'] = 1;
int nt = 0;
for (int i = 0; i < T; ++i) {
int a;char b; cin >> a >> b;
nt += a;
tways[nt-1] = mp[b];
}
set<status>aset;
for (int way = 0; way < 4; ++way) {
if (lines[Y0][X0][way]) {
aset.emplace(status{ X0,Y0,way });
}
}
for (int t = 0; t < nt; ++t) {
set<status>newset;
for (auto sta : aset) {
const int nowx = sta.nx;
const int nowy = sta.ny;
const int nowway = sta.way;
const int nexx = nowx + dx[nowway];
const int nexy = nowy + dy[nowway];
for (int way = 0; way < 4; ++way) {
if (tways[t] != -1) {
if (nowway != tways[t] && way != tways[t])continue;
}
if ((nowway + 4 - way) % 4 == 2)continue;
else if (lines[nexy][nexx][way]) {
newset.emplace(status{ nexx,nexy,way });
}
}
}
aset = newset;
}
set<pair<int, int>>ansset;
for (auto sta : aset) {
ansset.emplace(sta.nx, sta.ny);
}
for (auto sta : ansset) {
cout << sta.first << " " << sta.second << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T, typename TT>
inline ostream &operator<<(ostream &os, const pair<T, TT> &t) {
return os << t.first << " " << t.second;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &t) {
for (auto i : t) os << i << " ";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const set<T> &t) {
for (auto i : t) os << i << " ";
return os;
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &os, const map<T1, T2> &t) {
for (auto i : t) os << i.first << " : " << i.second << '\n';
return os;
}
template <typename T>
inline istream &operator>>(istream &is, vector<T> &v) {
for (T &t : v) is >> t;
return is;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
for (pair<T1, T2> &t : v) is >> t.first >> t.second;
return is;
}
const long long mod = 1e9 + 7;
void solve() {
long long n;
cin >> n;
vector<long long> v(n);
cin >> v;
vector<array<long long, 3>> smx, smn;
long long ans = 0;
for (int i = 0; i < n; i++) {
while (!smx.empty() and smx.back()[0] <= v[i]) smx.pop_back();
if (smx.empty())
smx.push_back({v[i], i, v[i] * (i + 1)});
else
smx.push_back({v[i], i, smx.back()[2] + v[i] * (i - smx.back()[1])});
while (!smn.empty() and smn.back()[0] >= v[i]) smn.pop_back();
if (smn.empty())
smn.push_back({v[i], i, v[i] * (i + 1)});
else
smn.push_back({v[i], i, smn.back()[2] + v[i] * (i - smn.back()[1])});
ans += smx.back()[2] - smn.back()[2];
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
;
long long T = 1;
while (T--) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct edge {
int v;
long long w;
};
const int MOD = 1000000007;
long long _MOD = 1000000009;
double EPS = 1e-10;
int INF = INT_MAX / 2;
vector<int> solve(vector<int> a, vector<int> b) {
int N = a.size();
vector<pair<int, int> > ai(N);
for (int i = 0; i < N; i++) ai[i] = pair<int, int>(a[i], i);
sort(ai.rbegin(), ai.rend());
priority_queue<pair<int, int> > pq;
vector<int> p;
for (int t = 0; t < N; t++) {
int i = ai[t].second;
pq.push(pair<int, int>(b[i], i));
if (t % 2 == 0) {
p.push_back(pq.top().second);
pq.pop();
}
}
sort(p.begin(), p.end());
return p;
}
int main() {
int N;
cin >> N;
vector<int> a(N), b(N);
for (int i = 0; i < N; i++) scanf("%d", &a[i]);
for (int i = 0; i < N; i++) scanf("%d", &b[i]);
if (N % 2 == 0) a.pop_back(), b.pop_back();
vector<int> p = solve(a, b);
if (N % 2 == 0) p.push_back(N - 1);
cout << p.size() << endl;
for (int i : p) printf("%d ", i + 1);
cout << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, i, x, y[111111], f, fin, a, b, c[111111];
string s, ss;
double ans = 1e9;
map<string, int> m;
int main() {
cin >> n >> x >> a >> b;
if (max(n * 3 / 10, n - n / 250 * a) < max(x * 3 / 10, x - x / 250 * b))
cout << "Vasya";
else if (max(n * 3 / 10, n - n / 250 * a) == max(x * 3 / 10, x - x / 250 * b))
cout << "Tie";
else
cout << "Misha";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100010;
struct Node {
int id;
int value;
Node* left;
Node* right;
Node* parent;
Node(const Node& node) {
id = node.id;
value = node.value;
left = node.left;
right = node.right;
parent = node.parent;
}
Node(int id, int value) : id(id), value(value) {
left = NULL;
right = NULL;
parent = NULL;
}
};
vector<Node> Nodes;
set<pair<int, int> > added;
int main() {
int n;
scanf("%d", &n);
for (int i = (0); i < (n); ++i) {
int val;
scanf("%d", &val);
Nodes.push_back(Node(i, val));
}
added.insert(make_pair(Nodes[0].value, 0));
added.insert(make_pair(Nodes[1].value, 1));
if (Nodes[0].value > Nodes[1].value)
Nodes[0].left = &Nodes[1];
else
Nodes[0].right = &Nodes[1];
Nodes[1].parent = &Nodes[0];
for (int i = (2); i < (n); ++i) {
auto r_it = added.lower_bound(make_pair(Nodes[i].value, i));
int r = -1;
int l = -1;
if (r_it != added.end()) {
r = r_it->second;
}
if (r_it != added.begin()) {
--r_it;
l = r_it->second;
}
if (r != -1 && Nodes[r].left == NULL) {
Nodes[r].left = &Nodes[i];
Nodes[i].parent = &Nodes[r];
}
if (l != -1 && Nodes[l].right == NULL) {
Nodes[l].right = &Nodes[i];
Nodes[i].parent = &Nodes[l];
}
added.insert(make_pair(Nodes[i].value, i));
}
for (int i = (1); i < (n); ++i)
if (Nodes[i].parent) printf("%d ", Nodes[i].parent->value);
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 82;
int dp[N][N][N][N];
int n, k, m, g[N][N];
int DP(int l, int r, int cur, int k) {
if (dp[l][r][cur][k] != -1) return dp[l][r][cur][k];
if (k == 1) {
return dp[l][r][cur][k] = 0;
}
for (int i = 1; i <= n; i++) {
if (g[cur][i] != 1e8 && i > l && i < r) {
int s = l, t = r;
if (cur < i)
s = cur;
else
t = cur;
int res = DP(s, t, i, k - 1);
if (dp[l][r][cur][k] > 0)
dp[l][r][cur][k] = min(dp[l][r][cur][k], res + g[cur][i]);
else
dp[l][r][cur][k] = res + g[cur][i];
}
}
if (dp[l][r][cur][k] > 0) return dp[l][r][cur][k];
return 0x3f3f3f3f;
}
int main() {
while (cin >> n >> k >> m) {
int u, v, w;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = 1e8;
memset(dp, -1, sizeof(dp));
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &u, &v, &w);
g[u][v] = min(g[u][v], w);
}
int ans = 1e8;
for (int i = 1; i <= n; i++) ans = min(ans, DP(0, n + 1, i, k));
if (ans >= 1e8) ans = -1;
cout << ans << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main() {
int a, b, c, i1, i2, r1, r2, i, r, j, k, sum = 0, n, m, tmp, t, ara[1];
double p, q, x, y;
char c1[1], c2[1];
scanf("%d", &n);
for (j = 0; j < n; j++) {
scanf("%d %d", &i, &r);
if (j == 0) {
i1 = i;
r1 = r;
} else {
if (i > i1) i1 = i;
if (r < r1) r1 = r;
}
}
scanf("%d", &m);
for (j = 0; j < m; j++) {
scanf("%d %d", &i, &r);
if (j == 0) {
i2 = i;
r2 = r;
} else {
if (i > i2) i2 = i;
if (r < r2) r2 = r;
}
}
a = i2 - r1;
b = i1 - r2;
if (a <= 0 && b <= 0)
printf("0\n");
else if (a > b)
printf("%d\n", a);
else if (b > a)
printf("%d\n", b);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
long long j;
int ax[N], ay[N];
int main() {
scanf("%d %lld", &n, &j);
int x, y;
scanf("%d %d", &x, &y);
for (int i = 0; i < n; ++i) scanf("%d %d", &ax[i], &ay[i]);
j %= (2 * n);
for (int k = 1; k <= j; ++k) {
x = 2 * ax[(k - 1) % n] - x;
y = 2 * ay[(k - 1) % n] - y;
}
printf("%d %d\n", x, y);
}
| 3 |
#include<bits/stdc++.h>
#define speed ios::sync_with_stdio(false), cin.tie(0);
using namespace std;
int main()
{
speed;
int n,i;
cin>>n;
long long int p,ans=0;
for (i=0;i<n;i++)
{
cin>>p;
ans^=p;
}
if (ans)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void solve();
mt19937 rng(time(0));
long long qpow(long long x, long long p, long long MOD) {
long long res = 1, v = x;
while (p) {
if (p & 1) {
res *= v;
res %= MOD;
}
v *= v;
v %= MOD;
p >>= 1;
}
return res;
}
long long inv(long long x, long long MOD) { return qpow(x, MOD - 2, MOD); }
void init() {}
template <class T>
T read() {
int ch = getchar();
if (ch == EOF) {
return INT_MIN;
}
while (!isdigit(ch) && ch != '-') {
ch = getchar();
if (ch == EOF) {
return INT_MIN;
}
}
T res = 0, sign = 1;
if (ch == '-') {
ch = getchar();
sign = -1;
}
while (isdigit(ch)) {
res = res * 10 + ch - '0';
ch = getchar();
}
return res * sign;
}
int main() {
init();
solve();
return 0;
}
const int MAXN = 55;
long long a[MAXN], b[MAXN];
void solve() {
int n = read<int>(), m = read<int>();
for (int i = 0; i < n; ++i) {
a[i] = read<int>();
}
for (int i = 0; i < m; ++i) {
b[i] = read<int>();
}
long long res = 2e18;
for (int hide = 0; hide < n; ++hide) {
long long cur = -2e18;
for (int c1 = 0; c1 < n; ++c1) {
if (c1 != hide) {
for (int c2 = 0; c2 < m; ++c2) {
cur = max(cur, a[c1] * b[c2]);
}
}
}
res = min(res, cur);
}
printf("%I64d\n", res);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + f * (c - '0'), c = getchar();
return x;
}
const long long MAXN = 2 * 1e5 + 10, inf = 1e18;
long long n, m, root;
long long u[MAXN * 2], v[MAXN * 2], first[MAXN], net[MAXN * 2], val[MAXN * 2];
long long uu[MAXN], vv[MAXN];
long long dep[MAXN], value[MAXN], son[MAXN], fa[MAXN], size[MAXN], top[MAXN];
long long dfn[MAXN], a[MAXN];
long long shu[MAXN * 4];
void ycl() {
for (long long i = 1; i <= n; i++) first[i] = 0;
}
long long cnt;
void add(long long x, long long y, long long z) {
cnt++;
u[cnt] = x;
v[cnt] = y;
val[cnt] = z;
net[cnt] = first[x];
first[x] = cnt;
}
void dfs1(long long x) {
son[x] = -1;
size[x] = 1;
for (long long i = first[x]; i; i = net[i]) {
if (dep[v[i]]) continue;
fa[v[i]] = x;
value[v[i]] = val[i];
dep[v[i]] = dep[x] + 1;
uu[(i - 1) / 2 + 1] = u[i];
vv[(i - 1) / 2 + 1] = v[i];
dfs1(v[i]);
size[x] += size[v[i]];
if (son[x] == -1 || size[son[x]] < size[v[i]]) son[x] = v[i];
}
}
long long tot = 0;
void dfs2(long long x, long long y) {
top[x] = y;
tot++;
dfn[x] = tot;
a[tot] = x;
if (son[x] == -1) return;
dfs2(son[x], y);
for (long long i = first[x]; i; i = net[i])
if (v[i] != son[x] && v[i] != fa[x]) dfs2(v[i], v[i]);
}
void update(long long x) {
if (shu[x * 2] < 0 || shu[x * 2 + 1] < 0 ||
(long double)shu[x * 2] * (long double)shu[x * 2 + 1] > inf) {
shu[x] = -1;
return;
}
shu[x] = shu[x * 2] * shu[x * 2 + 1];
}
void build(long long now, long long l, long long r) {
if (l == r) {
shu[now] = value[a[l]];
return;
}
long long mid = (l + r) >> 1;
build(now * 2, l, mid);
build(now * 2 + 1, mid + 1, r);
update(now);
}
void change(long long now, long long l, long long r, long long x, long long y) {
if (l == r) {
shu[now] = y;
return;
}
long long mid = (l + r) >> 1;
if (x <= mid)
change(now * 2, l, mid, x, y);
else
change(now * 2 + 1, mid + 1, r, x, y);
update(now);
}
long long query2(long long now, long long l, long long r, long long x,
long long y) {
if (l >= x && r <= y) {
return shu[now];
}
long long mid = (l + r) >> 1;
long long ans1 = 1, ans2 = 1;
if (mid >= x) ans1 *= query2(now * 2, l, mid, x, y);
if (ans1 < 0) return -1;
if (mid + 1 <= y) ans2 *= query2(now * 2 + 1, mid + 1, r, x, y);
if (ans2 < 0) return -1;
if ((long double)ans1 * (long double)ans2 > inf) return -1;
return ans1 * ans2;
}
long long query(long long x, long long y, long long c) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
long long z;
z = query2(1, 1, n, dfn[top[x]], dfn[x]);
if (z < 0 || c == 0) {
return 0;
} else
c /= z;
x = fa[top[x]];
}
if (dfn[x] > dfn[y]) swap(x, y);
long long z = query2(1, 1, n, dfn[x] + 1, dfn[y]);
if (z < 0)
return 0;
else
return c / z;
}
signed main() {
n = read();
m = read();
ycl();
for (long long i = 1; i <= n - 1; i++) {
long long x, y, z;
x = read();
y = read();
z = read();
add(x, y, z);
add(y, x, z);
}
root = 1;
dep[root] = 1;
dfs1(root);
dfs2(root, root);
build(1, 1, n);
for (long long i = 1; i <= m; i++) {
long long opt;
opt = read();
if (opt == 1) {
long long x, y, z;
x = read();
y = read();
z = read();
printf("%lld\n", query(x, y, z));
} else {
long long p, c;
p = read();
c = read();
change(1, 1, n, dfn[vv[p]], c);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool comp(string a, string b) {
if (a.compare(b) < 0) return true;
return false;
}
long long p1, p2, v1, v2, k;
long a[10000];
long long ans = 0;
long n = 1;
long maxlevel;
void res(long x1, long x2, long y1, long y2) {
long long o1, o2, t1, t2;
o1 = x1 > p1 ? x1 : p1;
o2 = x2 < p2 ? x2 : p2;
o1 = o1 <= o2 ? o2 - o1 + 1 : 0;
t1 = y1 > v1 ? y1 : v1;
t2 = y2 < v2 ? y2 : v2;
t1 = t1 <= t2 ? t2 - t1 + 1 : 0;
ans += t1 * o1;
o1 = x1 > v1 ? x1 : v1;
o2 = x2 < v2 ? x2 : v2;
o1 = o1 <= o2 ? o2 - o1 + 1 : 0;
t1 = y1 > p1 ? y1 : p1;
t2 = y2 < p2 ? y2 : p2;
t1 = t1 <= t2 ? t2 - t1 + 1 : 0;
ans += t1 * o1;
if ((x2 == y1) && (x2 >= p1) && (x2 <= p2)) ans -= 1;
}
void pr1(long t, long l) {
if (l == maxlevel) {
a[n] = t;
n++;
} else {
pr1(t * 10 + 4, l + 1);
pr1(t * 10 + 7, l + 1);
}
}
int main() {
a[0] = 0;
cin >> p1 >> p2 >> v1 >> v2 >> k;
for (maxlevel = 2; maxlevel <= 10; maxlevel++) pr1(0, 1);
a[n] = 1000000001;
n++;
for (long i = 1; i < n - k; i++) {
res(a[i - 1] + 1, a[i], a[i + k - 1], a[i + k] - 1);
}
cout.precision(20);
cout << double(ans) / (double((double(p2 - p1 + 1)) * (v2 - v1 + 1)));
getchar();
getchar();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin >> q;
int mx = 0, my = 0;
int x, y;
char op;
int menor, maior;
while (q--) {
cin >> op >> x >> y;
menor = min(x, y);
maior = max(x, y);
if (op == '+') {
mx = max(mx, menor);
my = max(my, maior);
} else {
if ((x >= mx && y >= my) || (y >= mx && x >= my))
cout << "YES\n";
else
cout << "NO\n";
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
const long long mod = 1000000007;
long long bp(long long x, long long y) {
long long ans = 1;
x %= mod;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long mod_inverse(long long x) { return bp(x, mod - 2); }
long long gcd(long long a, long long b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
long long max(long long a, long long b) {
if (a < b) swap(a, b);
return a;
}
long long min(long long a, long long b) {
if (a > b) swap(a, b);
return a;
}
long long countSetBits(long long n) {
long long count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}
pair<long long, long long> dp[1010][10010];
long long fact[10010], cnt = 0;
void solve() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
long long a[n + 1], b[n + 1], rem = k, mn = 1e18 + 1e9, mn_idx;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
b[i] = gcd(a[i], k);
rem /= gcd(rem, a[i]);
if (mn > a[i]) mn = a[i], mn_idx = i;
}
if (rem > 1) {
cout << "-1";
return;
}
if (k == 1) {
cout << "1" << '\n' << mn_idx;
return;
}
for (long long i = 1; i * i <= k; i++) {
if (k % i) continue;
fact[cnt++] = i;
if (i != k / i) fact[cnt++] = k / i;
}
sort(fact, fact + cnt);
map<long long, long long> mp;
for (long long i = 0; i < cnt; i++) mp[fact[i]] = i;
for (long long i = 1; i < cnt; i++) dp[0][i] = {n + 1, 0};
dp[0][0] = {0, 0};
for (long long i = 1; i <= n; i++) {
for (long long j = 0; j < cnt; j++) {
dp[i][j] = dp[i - 1][j];
long long t = mp[fact[j] / gcd(fact[j], b[i])];
if (dp[i][j] >
make_pair(dp[i - 1][t].first + 1, dp[i - 1][t].second + a[i]))
dp[i][j] = {dp[i - 1][t].first + 1, dp[i - 1][t].second + a[i]};
}
}
cout << dp[n][cnt - 1].first << '\n';
rem = k;
for (long long i = n; i > 0; i--) {
if (dp[i][mp[rem]] == dp[i - 1][mp[rem]]) continue;
cout << i << " ";
rem /= gcd(rem, b[i]);
}
}
signed main() { solve(); }
| 5 |
#include <bits/stdc++.h>
int main() {
char a[1005];
scanf("%s", &a);
if (a[0] >= 'a' && a[0] <= 'z') {
a[0] = a[0] - 32;
}
printf("%s\n", a);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str[3];
int win = -1;
int d[3], res = 0;
for (int i = 0; i < 3; i++) {
cin >> str[i];
if (str[i] == "rock")
d[i] = 1;
else if (str[i] == "paper")
d[i] = 2;
else
d[i] = 5;
res += d[i];
}
if (str[0] == str[1] && str[1] == str[2])
cout << "?" << endl;
else if (res == 8)
cout << "?" << endl;
else {
if (res == 11) {
for (int i = 0; i < 3; i++)
if (str[i] == "rock") win = i;
} else {
if (res == 9) {
for (int i = 0; i < 3; i++)
if (str[i] == "scissors") win = i;
} else {
if (res == 4) {
for (int i = 0; i < 3; i++)
if (str[i] == "paper") win = i;
}
}
}
switch (win) {
case 0:
cout << "F" << endl;
break;
case 1:
cout << "M" << endl;
break;
case 2:
cout << "S" << endl;
break;
default:
cout << "?" << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n, f;
cin >> n >> f;
vector<long long> loss;
long long sold = 0;
for (long long i = 0; i < n; ++i) {
long long k, l;
cin >> k >> l;
sold += min(k, l);
if (k >= l) continue;
long long tmp = min(k, l - k);
loss.push_back(tmp);
}
sort(loss.begin(), loss.end(), greater<long long>());
for (long long i = 0; i < f and i < loss.size(); ++i) sold += loss[i];
cout << sold << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char **argv) {
long long int hr, hb, wr, wb, c, result;
cin >> c >> hr >> hb >> wr >> wb;
if (wr > wb) {
swap(hr, hb);
swap(wr, wb);
}
result = c / wb * hb;
for (int i = 0; i <= min((long long int)1000000, c / wb); ++i) {
result = max(result, hb * i + (c - wb * i) / wr * hr);
}
swap(hr, hb);
swap(wr, wb);
for (int i = 0; i <= min((long long int)1000000, c / wb); ++i) {
result = max(result, hb * i + (c - wb * i) / wr * hr);
}
cout << result;
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main(void) {
int pos, neg, in, sum1, sum2, sum3, res1, res2;
scanf("%i", &pos);
scanf("%i", &neg);
scanf("%i", &in);
sum1 = pos + in;
sum2 = neg + in;
sum3 = pos + neg;
if (in == 0) {
if (pos == neg)
printf("0");
else if (pos > neg)
printf("+");
else
printf("-");
} else if (in < sum3) {
res1 = sum1 - neg;
res2 = sum2 - pos;
if (res1 == 0)
printf("?");
else if (res2 == 0)
printf("?");
else {
if (neg > sum1)
printf("-");
else if (pos > sum2)
printf("+");
else
printf("?");
}
} else
printf("?");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int num;
while (n--) {
scanf("%d", &num);
printf("%d\n", num > 2 ? num - 2 : 0);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int a[100010];
long long int tree[13][100006];
long long int dp[100005][13];
void update(long long int x, long long int len, long long int val) {
while (x <= 100000) {
tree[len][x] += val;
x += (x & -x);
}
}
long long int query(long long int x, long long int len) {
long long int sum = 0LL;
while (x > 0) {
sum += tree[len][x];
x -= (x & -x);
}
return sum;
}
int main() {
long long int n, m, ans = 0LL, i, j;
cin >> n >> m;
for (i = 1; i <= n; i++) cin >> a[i];
memset(tree, 0LL, sizeof tree);
if (m > n) {
cout << 0;
return 0;
}
for (i = 1; i <= n; ++i) {
dp[i][1] = 1LL;
}
for (i = 1; i <= n; ++i) {
update(a[i], 1, 1);
for (j = 2; j <= m + 1; ++j) {
long long int cur = query(a[i] - 1, j - 1);
dp[i][j] += cur;
update(a[i], j, cur);
}
}
for (i = 1; i <= n; ++i) {
ans += dp[i][m + 1];
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
int a[N];
int i, j;
for (i = 0; i < N; i++) {
cin >> a[i];
}
set<int> s(a, a + N);
vector<int> v(s.begin(), s.end());
j = s.size();
int b[j];
for (i = 0; i < j; i++) {
b[i] = count(a, a + N, v[i]);
}
sort(b, b + j);
cout << b[j - 1] << " " << j;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, m, mp[15] = {0};
cin >> n >> m;
for (long long i = (0); i < (long long)(n); ++i) {
int t;
cin >> t;
mp[t]++;
}
int res = 0;
for (int i = 1; i < 11; i++) {
for (int j = i + 1; j < 11; j++) {
res += (mp[i] * mp[j]);
}
}
printf("%d", res);
;
return 0;
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.