solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(uint64_t v) {
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(int v) : _m_int(int64_t(v)) {}
_m_int(unsigned v) : _m_int(uint64_t(v)) {}
explicit operator int() const { return val; }
explicit operator unsigned() const { return val; }
explicit operator int64_t() const { return val; }
explicit operator uint64_t() const { return val; }
explicit operator double() const { return val; }
explicit operator long double() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return unsigned(x % m);
unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod(uint64_t(val) * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
friend bool operator==(const _m_int &a, const _m_int &b) {
return a.val == b.val;
}
friend bool operator!=(const _m_int &a, const _m_int &b) {
return a.val != b.val;
}
friend bool operator<(const _m_int &a, const _m_int &b) {
return a.val < b.val;
}
friend bool operator>(const _m_int &a, const _m_int &b) {
return a.val > b.val;
}
friend bool operator<=(const _m_int &a, const _m_int &b) {
return a.val <= b.val;
}
friend bool operator>=(const _m_int &a, const _m_int &b) {
return a.val >= b.val;
}
static const int SAVE_INV = int(1e6) + 5;
static _m_int save_inv[SAVE_INV];
static void prepare_inv() {
for (int64_t p = 2; p * p <= MOD; p += p % 2 + 1) assert(MOD % p != 0);
save_inv[0] = 0;
save_inv[1] = 1;
for (int i = 2; i < SAVE_INV; i++)
save_inv[i] = save_inv[MOD % i] * (MOD - MOD / i);
}
_m_int inv() const {
if (save_inv[1] == 0) prepare_inv();
if (val < SAVE_INV) return save_inv[val];
_m_int product = 1;
int v = val;
while (v >= SAVE_INV) {
product *= MOD - MOD / v;
v = MOD % v;
}
return product * save_inv[v];
}
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
p >>= 1;
if (p > 0) a *= a;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
template <const int &MOD>
_m_int<MOD> _m_int<MOD>::save_inv[_m_int<MOD>::SAVE_INV];
extern const int MOD = int(1e9) + 7;
using mod_int = _m_int<MOD>;
vector<mod_int> fact(1, 1);
vector<mod_int> inv_fact(1, 1);
mod_int C(int n, int k) {
if (k < 0 || k > n) {
return 0;
}
while ((int)fact.size() < n + 1) {
fact.push_back(fact.back() * (int)fact.size());
inv_fact.push_back(fact.back().inv());
}
return fact[n] * inv_fact[k] * inv_fact[n - k];
}
void solve() {
int N, q;
cin >> N >> q;
vector<vector<mod_int>> dp(3 * N + 1, vector<mod_int>(3, 0));
dp[0][0] = dp[0][1] = dp[0][2] = N;
for (int i = 1; i < (3 * N + 1); ++i) {
dp[i][0] = (C(3 * N, i + 1) - 2 * (dp[i - 1][0]) - dp[i - 1][1]) / 3;
dp[i][1] = dp[i][0] + dp[i - 1][0];
dp[i][2] = dp[i][1] + dp[i - 1][1];
}
for (int i = 0; i < (q); ++i) {
int x;
cin >> x;
cout << dp[x][0] + C(3 * N, x) << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t = 1;
for (int i = 0; i < (t); ++i) solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 1000005;
inline int read() {
int res, ok = 1;
char ch;
for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') ok = -1;
res = ch - '0';
for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
res = res * 10 + ch - '0';
return res * ok;
}
long long dp[maxn * 2], sum[30], ans, cnt[maxn * 2], qaq[30];
int n, m, k;
char s[maxn];
int main() {
n = read();
k = read();
scanf("%s", s + 1);
m = strlen(s + 1);
cnt[0] = 1;
for (int i = 1; i <= m; i++) {
dp[i] = cnt[i - 1] - sum[s[i] - 'a' + 1];
if (dp[i] < 0) dp[i] += mod;
qaq[s[i] - 'a' + 1] = i;
cnt[i] = (cnt[i - 1] + dp[i]) % mod;
sum[s[i] - 'a' + 1] = (dp[i] + sum[s[i] - 'a' + 1]) % mod;
}
for (int i = 1; i <= n; i++) {
int q;
long long tot = 1LL << 60;
for (int j = 1; j <= k; j++) {
if (qaq[j] < tot) {
q = j;
tot = qaq[j];
}
}
dp[i + m] = cnt[i - 1 + m] - sum[q];
if (dp[i + m] < 0) dp[i + m] += mod;
qaq[q] = i + m;
cnt[i + m] = (cnt[i - 1 + m] + dp[i + m]) % mod;
sum[q] = (sum[q] + dp[i + m]) % mod;
}
for (int i = 1; i <= k; i++) ans = (ans + sum[i]) % mod;
ans = (ans + 1) % mod;
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char c = getchar();
long long x = 0;
bool f = 0;
for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
return x;
}
long long n, a[300005], l[300005], r[300005];
bool vis[300005];
void work() {
n = read();
for (register long long i = (1); i <= (n); ++i) a[i] = read(), vis[i] = 0;
long long all = 0, now = 0, mx = 0;
for (register long long i = (1); i <= (n); ++i) {
if (!vis[a[i]]) vis[a[i]] = 1, l[a[i]] = i, ++all;
r[a[i]] = i;
}
long long lst = -1;
for (register long long i = (1); i <= (n); ++i)
if (vis[i]) {
if (!now)
now = mx = 1, lst = i;
else {
if (l[i] > r[lst])
now++;
else
now = 1;
lst = i;
mx = max(mx, now);
}
}
cout << all - mx << endl;
}
signed main() {
long long T = read();
while (T--) work();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
char ch = getchar();
long long f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
}
template <typename T, typename... Args>
void read(T &first, Args &...args) {
read(first);
read(args...);
}
int mod = 1e9 + 7;
inline int mul(int x, int y) { return 1ll * x * y % mod; }
inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int sub(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline int sq(int x) { return 1ll * x * x % mod; }
int pow(int a, int b) {
return b == 0 ? 1 : (b & 1 ? mul(a, sq(pow(a, b / 2))) : sq(pow(a, b / 2)));
}
int n, m, s[5050];
vector<int> G[5050], V[5050];
int diff[5050];
int enl1[5050], enl2[5050], rhs[5050];
int cnt[5050] = {0}, el1[5050], el2[5050], pe[5050], way = 0, peway = 0;
int ans1 = 0, ans2 = 0;
int main() {
read(n, m);
for (int i = 1; i <= n; i++) {
read(s[i]);
V[s[i]].push_back(i);
}
for (int i = 1; i <= m; i++) {
int f, h;
read(f, h);
G[f].push_back(h);
}
for (int i = 1; i <= n; i++) {
for (auto ca : G[i]) {
if (ca > V[i].size()) continue;
for (auto cb : G[i]) {
if (cb > V[i].size()) continue;
if (ca == cb) continue;
if (ca + cb <= V[i].size()) {
diff[V[i][ca - 1]] += 1;
rhs[V[i][ca - 1]] += 1;
diff[V[i][V[i].size() - cb]] -= 1;
}
}
if (!el2[i]) way += 1;
el2[i] += 1;
enl1[V[i][ca - 1]] += 1;
enl2[V[i][V[i].size() - ca]] += 1;
}
}
ans1 = way, ans2 = 0;
int cans = 1;
for (int j = 1; j <= n; j++) {
if (add(el1[j], el2[j])) cans = mul(cans, add(el1[j], el2[j]));
}
ans2 = add(ans2, cans);
for (int i = 1; i <= n; i++) {
way -= (cnt[s[i]] != 0) * (1 + ((el1[s[i]] + el2[s[i]]) == 0));
cnt[s[i]] += diff[i];
way += (cnt[s[i]] != 0) * (1 + ((el1[s[i]] + el2[s[i]]) == 0));
if (cnt[s[i]] == 0) way -= ((el1[s[i]] + el2[s[i]]) != 0);
el1[s[i]] += enl1[i];
el2[s[i]] -= enl2[i];
if (cnt[s[i]] == 0) way += ((el1[s[i]] + el2[s[i]]) != 0);
if (way < ans1) continue;
if (way > ans1) ans1 = way, ans2 = 0;
int cans = 1;
for (int j = 1; j <= n; j++) {
if (j == s[i]) {
if (cnt[j])
cans = mul(cans, rhs[i]);
else
cans = mul(cans, enl1[i]);
} else {
if (cnt[j])
cans = mul(cans, cnt[j]);
else if (add(el1[j], el2[j]))
cans = mul(cans, add(el1[j], el2[j]));
}
}
ans2 = add(ans2, cans);
}
if (ans1 == 0) ans2 = 1;
cout << ans1 << ' ' << ans2 << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, *a, tmp;
cin >> n;
a = new int[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
for (int b = 0; b < n; b++) {
if (a[i] < a[b]) {
tmp = a[b];
a[b] = a[i];
a[i] = tmp;
}
}
}
for (int i = 0; i < n; i++) {
cout << a[i] << ' ';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int temp[n], a[n + 1];
for (int i = 0; i < n; i++) {
cin >> temp[i];
}
sort(temp, temp + n, greater<int>());
int ei = n / 2, j = 2;
for (int i = 0; i < ei; i++) {
a[j] = temp[i];
j += 2;
}
j = 1;
sort(temp, temp + n);
for (int i = 0; i < (n - ei); i++) {
a[j] = temp[i];
j += 2;
}
for (int i = 1; i <= n; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
| 2 |
#include <iostream>
#include <string>
using namespace std;
int main(void){
string str,tmp,command;
int q,a,b;
cin>>str>>q;
for(int i=1;i<=q;i++){
cin>>command>>a>>b;
if(command=="print"){
cout<<str.substr(a,b-a+1)<<endl;
}else if(command=="reverse"){
tmp=str.substr(a,b-a+1);
for(int i=a;i<=b;i++)str[i]=tmp[b-i];
}else{
cin>>tmp;
for(int i=a;i<=b;i++)str[i]=tmp[i-a];
}
}
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 666;
int n,m,a[N],b[N],d[N][N],f[N][N];
int e[N],c[N],s[N];
int solve(int x,int y){
int i,j,k,z=d[x][y],o,u,r;
memset(e,0,sizeof(e));
memset(c,0,sizeof(c));
memset(s,0,sizeof(s));
for(i=1;i<=n;i=i+1)
if(d[x][i]+d[y][i]==z)
e[d[x][i]]++;
for(i=0;i<=z;i=i+1)
if(e[i]!=1)
return 0;
for(i=1;i<=n;i=i+1){
o=d[x][i]-d[y][i];
if((o-z)&1)
return 0;
c[i]=o;
}
for(k=1;k<=m;k=k+1){
i=a[k],j=b[k];
o=d[x][i]+d[y][i];
u=d[x][j]+d[y][j];
if(o==z&&u==z)
continue;
if(c[i]!=c[j])
continue;
if(abs(o-u)==2){
if(o>u)
s[i]++;
else
s[j]++;
}
}
r=1;
for(i=1;i<=n;i=i+1){
o=d[x][i]+d[y][i];
if(o!=z)
r=(LL)r*s[i]%998244353;
}
return r;
}
int main(){
int i,j,k;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i=i+1){
for(j=1;j<=n;j=j+1)
d[i][j]=N;
d[i][i]=0;
}
for(i=1;i<=m;i=i+1){
scanf("%d%d",a+i,b+i);
d[a[i]][b[i]]=1;
d[b[i]][a[i]]=1;
}
for(k=1;k<=n;k=k+1)
for(i=1;i<=n;i=i+1)
for(j=1;j<=n;j=j+1)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
for(i=1;i<=n;i=i+1)
for(j=i;j<=n;j=j+1)
f[i][j]=solve(i,j);
for(i=1;i<=n;i=i+1)
for(j=1;j<i;j=j+1)
f[i][j]=f[j][i];
for(i=1;i<=n;i=i+1){
for(j=1;j<=n;j=j+1)
printf("%d ",f[i][j]);
printf("\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const long long mod = 998244353;
const long long INF = 1e9 + 7;
const int base = 131;
const double eps = 0.000001;
const double pi = 3.1415926;
int n, m, k;
int a[200010], b[2010];
int dp[200010], sum[2010];
struct node {
int x, y;
bool operator<(const node &a) const { return x < a.x; }
} e[200010];
int main() {
while (~scanf("%d%d%d", &n, &m, &k)) {
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 0; i < m; i++) {
scanf("%d%d", &e[i].x, &e[i].y);
}
e[m].x = 1, e[m].y = 0;
sort(e, e + m + 1);
sort(a + 1, a + n + 1);
sum[0] = 0;
for (int i = 1; i <= k; i++) {
b[i] = a[k - i + 1];
sum[i] = sum[i - 1] + b[i];
}
dp[0] = 0;
for (int i = 1; i <= k; i++) {
dp[i] = INF;
for (int j = 0; j <= m; j++) {
if (i - e[j].x < 0) break;
dp[i] = min(dp[i], dp[i - e[j].x] + sum[i - e[j].y] - sum[i - e[j].x]);
}
}
printf("%d\n", dp[k]);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long cnt, prime[100], pcnt;
long long flg;
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long search(long long a, long long b) {
if (!b) return 0;
long long d = gcd(a, b);
a /= d;
b /= d;
long long minn = 1ll << 60;
for (int i = 1; i <= cnt; i++)
if (a % prime[i] == 0) minn = min(minn, b % prime[i]);
if (minn == 1ll << 60) return b;
return minn + search(a, b - minn);
}
int main() {
long long a, b;
scanf("%I64d%I64d", &a, &b);
long long A = a;
for (long long i = 2; i * i <= A; i++) {
if (a % i == 0) {
while (a % i == 0) {
a /= i;
}
prime[++cnt] = i;
}
}
if (a > prime[cnt]) prime[++cnt] = a;
if (a == A) {
printf("%I64d", A > b ? b : b % A + (b - b % A) / A);
return 0;
}
long long ans = search(A, b);
printf("%I64d", ans);
}
| 5 |
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}
// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';
/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"
// イテレータ要素のインデックス
distance(A.begin(), itr);
*/
int main()
{
int q; cin >> q;
multiset<int> S;
rep(i,q){
int query; cin >> query;
if(query == 0){
int x; cin >> x;
S.insert(x);
cout << S.size() << endl;
}else if(query == 1){
int x; cin >> x;
cout << S.count(x) << endl;
}else if(query == 2){
int x; cin >> x;
S.erase(x);
}else if(query == 3){
int L,R; cin >> L >> R;
auto it = S.lower_bound(L);
while (it != S.end() && *it <= R) {
printf("%d\n", *it); ++it;
}
}
}
}
| 0 |
//ほぼstandard answer
#include<iostream>
using namespace std;
class Cube{
public:
int f[6];
Cube(){}
void roll_z(){ roll(1, 2, 4, 3);} //横向き
void roll_y(){ roll(0, 2, 5, 3);} //サイコロの番号は+1
void roll_x(){ roll(0, 1, 5, 4);}
void roll(int i, int j, int k, int l){
int t = f[i]; f[i] = f[j]; f[j] = f[k]; f[k] = f[l]; f[l] = t;
}
};
Cube c[100]; int n;
bool check();
bool eq( Cube c1, Cube c2 );
bool equal( Cube c1, Cube c2 );
int main(){
cin >> n;
for ( int j = 0; j < n; j++ )
for ( int i = 0; i < 6; i++ ) cin >> c[j].f[i];
if ( check() ) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
bool check(){
for ( int i = 0; i < n - 1; i++)
for ( int j = i + 1; j < n; j++ ) if ( equal(c[i], c[j]) ) return false;
return true;
}
bool equal( Cube c1, Cube c2 ){ //ほとんどDiceIIと同じ流れ
for ( int i = 0; i < 6; i++ ){
for ( int j = 0; j < 4; j++ ){
if ( eq(c1, c2) ) return true;
c1.roll_z();
}
if ( i % 2 == 0 ) c1.roll_x();
else c1.roll_y();
}
return false;
}
bool eq( Cube c1, Cube c2 ){
for ( int i = 0; i < 6; i++ ) {
if ( c1.f[i] != c2.f[i] ) return false;
}
return true;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
long N;cin>>N;
if(N%2==1){cout<<0;return 0;}
long ans=0,t=10;
for(int i=0;(N/t)>0;i++){ans+=N/t;t*=5;}
cout<<ans;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 501;
const int INF = 1e9;
int dp[MAX_N][MAX_N];
int main() {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
dp[i][i] = 1;
}
for (int i = 0; i + 1 < n; i++) {
if (a[i] == a[i + 1])
dp[i][i + 1] = 1;
else
dp[i][i + 1] = 2;
}
for (int ln = 2; ln <= n; ln++) {
for (int s = 0; s + ln - 1 < n; s++) {
dp[s][s + ln - 1] = INF;
if (a[s] == a[s + ln - 1] && ln != 2) {
dp[s][s + ln - 1] = min(INF, dp[s + 1][s + ln - 2]);
} else if (ln == 2 && a[s] == a[s + ln - 1]) {
dp[s][s + ln - 1] = 1 + (a[s] != a[s + 1]);
}
for (int div = s; div < s + ln - 1; div++) {
dp[s][s + ln - 1] =
min(dp[s][s + ln - 1], dp[s][div] + dp[div + 1][s + ln - 1]);
}
}
}
cout << dp[0][n - 1] << endl;
}
| 4 |
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
//#define mt make_tuple
//typedef tuple<double,int,double> tp;
//typedef vector<tp> vt;
const double PI=acos(-1);
const double EPS=1e-7;
const int inf=1e9;
const ll INF=2e18;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int main(){
int n,m;
while(cin>>n>>m,n){
int N=1<<n;
vi f(n),in(m);
rep(i,n)cin>>f[i];
rep(i,m)cin>>in[i];
sort(all(in));
vi dp(N,inf);
dp[0]=0;
rep(i,m){
vi ndp(N,inf);
rep(j,N){
rep(l,n)if((j&1<<l)==0)dp[j|1<<l]=min(dp[j|1<<l],dp[j]);
int sum=0;
rep(k,n)if(j&1<<k)sum+=f[k];
ndp[j]=dp[j]+abs(sum-in[i]);
}
dp=ndp;
}
int out=inf;
rep(i,N)out=min(dp[i],out);
cout<<out<<endl;
}
} | 0 |
#define _USE_MATH_DEFINES
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<climits>
using namespace std;
struct Team{
char name;
int win, lose;
bool operator < (const Team &a) const{
return (win == a.win) ? lose < a.lose : win > a.win;
}
};
int main(){
int N, R;
while(cin >> N, N){
Team t[10] = {};
for(int i = 0; i < N; i++){
cin >> t[i].name;
for(int j = 0; j < N-1; j++){
cin >> R;
if(R == 0) t[i].win++;
if(R == 1) t[i].lose++;
}
}
sort(t, t + N);
for(int i = 0; i < N; i++){
printf("%c\n", t[i].name);
}
}
return 0;
} | 0 |
#include<iostream>
using namespace std;
int main(){
long long int n,t;
cin>>n>>t;
string s;
cin>>s;
int ans = 0;
int b=1;
for(int i =0;i<s.size();i++){
b=1;
if(s[i]>='0'&&s[i]<='9'){
int a = s[i]-'0';
for(int j =0;j<a;j++){
b*=n;
if(b<0||b>1000000000){
cout<<"TLE"<<endl;
return 0;
}
}
ans+=b;
if(ans<0||ans>1000000000||ans*t<0||ans*t>1000000000){
cout<<"TLE"<<endl;
return 0;
}
}
}
cout<<ans*t<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
vector<int> a[N], b[N];
int da[N], db[N];
int id[N], q[N << 1], dph[N << 1], dfn = 0;
int dp[N << 1][20], lg2[N << 1];
int n, x, y;
void dfsb(int x, int fx) {
id[x] = ++dfn;
q[dfn] = x;
db[x] = db[fx] + 1;
dph[dfn] = db[x];
for (int y : b[x]) {
if (y == fx) continue;
dfsb(y, x);
q[++dfn] = x;
dph[dfn] = db[x];
}
}
void RMQ_init(int n) {
for (int i = 2; i <= n; i++) lg2[i] = lg2[i / 2] + 1;
for (int i = 1; i <= n; i++) dp[i][0] = i;
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
int a = dp[i][j - 1], b = dp[i + (1 << (j - 1))][j - 1];
dp[i][j] = dph[a] <= dph[b] ? a : b;
}
}
}
int RMQ_min(int L, int R) {
if (L > R) swap(L, R);
int k = lg2[R - L + 1];
int a = dp[L][k], b = dp[R - (1 << k) + 1][k];
return dph[a] <= dph[b] ? a : b;
}
int LCA(int u, int v) {
int x = id[u], y = id[v];
return q[RMQ_min(x, y)];
}
int ans = 0;
void dfsa(int x, int fx) {
da[x] = da[fx] + 1;
if (da[x] >= db[x]) return;
if (~ans) ans = max((db[x] - 1) << 1, ans);
for (int y : a[x]) {
if (y == fx) continue;
if (db[x] + db[y] - 2 * db[LCA(x, y)] > 2) ans = -1;
dfsa(y, x);
}
}
int main() {
scanf("%d%d%d", &n, &x, &y);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
a[u].push_back(v);
a[v].push_back(u);
}
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
b[u].push_back(v);
b[v].push_back(u);
}
dfsb(y, 0);
RMQ_init(dfn);
dfsa(x, 0);
printf("%d\n", ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, n;
int flag = 1;
scanf("%d%d%d", &a, &b, &n);
for (int i = 0; i < 10; i++) {
if ((a * 10 + i) % b == 0) {
printf("%d", a * 10 + i);
flag = 0;
for (int j = 1; j <= n - 1; j++) printf("0");
break;
}
}
if (flag == 1) printf("-1");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
string nap;
int k;
cin >> nap >> k;
nap += "1";
int n = nap.size();
int odp = 0;
for (int i = 0; i < k; ++i) {
char a, b;
cin >> a >> b;
int x = 0, y = 0;
for (int j = 0; j < n; ++j) {
if (nap[j] == a)
++x;
else {
if (nap[j] == b)
++y;
else {
odp += min(x, y);
x = 0, y = 0;
}
}
}
}
cout << odp;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
inline 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 << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
namespace Maxflow {
int head[1010], ecnt, S, T, dep[1010], pre[1010];
struct Edge {
int nxt, to, val;
} edge[1010 << 3];
void add(int a, int b, int c) {
edge[++ecnt] = {head[a], b, c};
head[a] = ecnt;
}
void adde(int a, int b, int c) { add(a, b, c), add(b, a, 0); }
void ST(int _S, int _T) { S = _S, T = _T; }
void init(int _S, int _T) {
ecnt = 1;
memset(head, 0, sizeof(head));
S = _S, T = _T;
}
queue<int> q;
bool bfs() {
memset(dep, 0, sizeof(dep));
q.push(S);
dep[S] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].to;
if (edge[i].val && !dep[v]) {
dep[v] = dep[u] + 1;
q.push(v);
}
}
}
return dep[T] > 0;
}
int dfs(int u, int limit) {
if (u == T) return limit;
int flow = 0;
for (int &i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].to;
if (dep[v] == dep[u] + 1 && edge[i].val) {
int k = dfs(v, min(limit, edge[i].val));
edge[i].val -= k;
edge[i ^ 1].val += k;
flow += k;
limit -= k;
}
if (!limit) break;
}
if (!flow) dep[u] = inf;
return flow;
}
void prepare() {
for (int i = 2; i <= ecnt; i += 2) {
edge[i].val += edge[i ^ 1].val;
edge[i ^ 1].val = 0;
}
}
int Dinic() {
prepare();
memcpy(pre, head, sizeof(pre));
int maxflow = 0;
while (bfs()) {
maxflow += dfs(S, inf);
memcpy(head, pre, sizeof(pre));
}
return maxflow;
}
} // namespace Maxflow
int n, m, vis[1010], ecnt, ans, rt, head[1010];
vector<int> all, qwq;
struct Edge {
int nxt, to, val;
} edge[1010 << 2];
void add(int a, int b, int c) {
edge[++ecnt] = {head[a], b, c};
head[a] = ecnt;
}
void build(vector<int> &vec) {
if (vec.size() == 1) return;
int u = vec[0], v = vec[1];
Maxflow::ST(u, v);
int flow = Maxflow::Dinic();
add(u, v, flow), add(v, u, flow);
ans += flow;
vector<int> A, B;
for (auto x : vec) {
if (Maxflow::dep[x])
B.push_back(x);
else
A.push_back(x);
}
build(A), build(B);
}
void Find(int u, int fa) {
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].to;
if (vis[i] || v == fa) continue;
if (!rt || edge[i].val < edge[rt].val) rt = i;
Find(v, u);
}
}
void Solve(int md) {
int x = edge[md].to;
int y = edge[md ^ 1].to;
vis[md] = vis[md ^ 1] = 1;
rt = 0;
Find(x, y);
if (!rt)
qwq.push_back(x);
else
Solve(rt);
rt = 0;
Find(y, x);
if (!rt)
qwq.push_back(y);
else
Solve(rt);
}
int main() {
n = read(), m = read();
Maxflow::init(0, 0);
for (int i = 1; i <= m; ++i) {
int u = read(), v = read(), w = read();
Maxflow::adde(u, v, w);
Maxflow::adde(v, u, w);
}
for (int i = 1; i <= n; ++i) all.push_back(i);
ecnt = 1;
build(all);
Find(1, 0);
Solve(rt);
printf("%d\n", ans);
for (auto x : qwq) printf("%d ", x);
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[101][101];
int main() {
int n;
int cur;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cur = abs(n / 2 + 1 - i) + abs(n / 2 + 1 - j);
if (cur <= n / 2)
cout << "D";
else
cout << "*";
}
cout << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
string s, t, z;
set<int> alph[26];
void init() {
z = "";
for (int i = 0; i < 26; i++) {
alph[i].clear();
}
}
int to_int(char c) { return int(c) - int('a'); }
void input() {
cin >> s;
cin >> t;
for (int i = 0; i < s.size(); i++) {
alph[to_int(s[i])].insert(i);
}
}
void solve() {
int i = s.size();
int ans = 0;
for (auto c : t) {
if (alph[to_int(c)].empty()) {
cout << -1 << endl;
return;
} else if (alph[to_int(c)].upper_bound(i) == alph[to_int(c)].end()) {
i = *alph[to_int(c)].begin();
ans++;
} else {
i = *alph[to_int(c)].upper_bound(i);
}
}
cout << ans << endl;
}
void output() {}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int number_of_testcases = 1;
cin >> number_of_testcases;
while (number_of_testcases--) {
init();
input();
solve();
output();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n,m;
char str[200050];
const int mod = 1000000007;
int dp[200050];
int main()
{
scanf("%d%d",&n,&m);
scanf("%s",str + 1);
bool c1 = 0,c2 = 0;
for(int i = 1;i <= m; ++ i)
{
c1 |= str[i] == 'R';
c2 |= str[i] == 'B';
}
int p = 1;
int lim;
if(c1 && c2)
{
if(n & 1)
{
printf("0\n");
return 0;
}
for(;str[p + 1] == str[1]; ++ p);
lim = p + (p % 2 == 0);
int len = 0;
for(int i = p + 1;i <= m; ++ i)
{
if(str[i] != str[1]) lim = min(lim,len % 2 ? len : 10000000),len = 0;
else len ++;
}
lim ++;
lim /= 2;
lim = min(lim,n / 2);
int pre = 1;
int cur = 0;
dp[0] = 1;
long long ans = 0;
for(int i = 0;i <= n / 2; ++ i)
{
if(i)
{
while(i - cur > lim) pre -= dp[cur],pre = (pre + mod) % mod,cur ++;
dp[i] = pre;
pre += dp[i];
pre %= mod;
}
if(n - i * 2 > 0 && (n - i * 2) / 2 <= lim)
ans = ans + 1LL * (n - i * 2) * dp[i] % mod,ans %= mod;
}
printf("%lld\n",ans);
}
else
{
int pre = 0;
int cur = 0;
dp[0] = 1;
long long ans = 0;
for(int i = 0;i <= n; ++ i)
{
if(i > 0)
{
dp[i] = pre;
pre += dp[i - 1];
pre %= mod;
}
if(n - i > 1)
ans = ans + 1LL * (n - i) * dp[i] % mod,ans %= mod;
}
printf("%lld\n",(ans + 1) % mod);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &P) {
in >> P.first >> P.second;
return in;
}
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &P) {
out << "(" << P.first << ", " << P.second << ")";
return out;
}
template <class T>
istream &operator>>(istream &in, vector<T> &arr) {
for (auto &x : arr) in >> x;
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<T> &arr) {
for (auto &x : arr) out << x << ' ';
cout << "\n";
return out;
}
template <class T>
istream &operator>>(istream &in, deque<T> &arr) {
for (auto &x : arr) in >> x;
return in;
}
template <class T>
ostream &operator<<(ostream &out, const deque<T> &arr) {
for (auto &x : arr) out << x << ' ';
cout << "\n";
return out;
}
int f(int x, int y) {
if (x % y == 0) {
return f(x / y, y);
}
return x;
}
int n, k;
int32_t main() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int u = 500000;
cin >> n >> k;
vector<int> v(n);
cin >> v;
vector<int> stv = v;
while (u--) {
vector<pair<int, int> > op;
stv = v;
while (v.size() > 1) {
shuffle(v.begin(), v.end(), rng);
pair<int, int> nv;
nv.first = v.back();
int second = v.back();
v.pop_back();
nv.second = v.back();
second += v.back();
v.pop_back();
op.push_back(nv);
v.push_back(f(second, k));
}
if (v[0] == 1) {
cout << "YES" << endl;
for (auto w : op) {
cout << w.first << " " << w.second << endl;
}
return 0;
}
v = stv;
}
cout << "NO" << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct data {
int arr[15];
int n;
int l;
int r;
int x;
int ans;
};
void print_bits(int a) { cout << a << " = " << bitset<32>(a) << endl; }
void is_good_problemset(unsigned int bitmask, data *dat) {
int minV = INT32_MAX, maxV = 0, sum = 0, count = 0;
for (int i = 0; i < dat->n; i++) {
if ((bitmask & 1 << i) != 0) {
minV = min(minV, dat->arr[i]);
maxV = max(maxV, dat->arr[i]);
sum += dat->arr[i];
count++;
}
}
if (count >= 2 && maxV - minV >= dat->x && sum >= dat->l && sum <= dat->r) {
dat->ans++;
}
}
void solve(unsigned int bitmask, int pos, int size,
void (*fn)(unsigned int, data *), data *dat) {
if (pos == size) {
fn(bitmask, dat);
return;
}
solve(bitmask, pos + 1, size, fn, dat);
bitmask |= 1 << pos;
solve(bitmask, pos + 1, size, fn, dat);
}
int main() {
data dat = {};
cin >> dat.n >> dat.l >> dat.r >> dat.x;
for (int i = 0; i < dat.n; i++) {
cin >> dat.arr[i];
}
dat.ans = 0;
solve(0, 0, dat.n, is_good_problemset, &dat);
cout << dat.ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long Mod = 1e9 + 7;
const double pi = 2 * acos(0.0);
int arr[3];
int main() {
int d;
for (int i = 0; i < 3; i++) {
cin >> arr[i];
}
sort(arr, arr + 3);
cin >> d;
int ans = max(d - (arr[1] - arr[0]), 0) + max(d - (arr[2] - arr[1]), 0);
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, x, achou;
scanf("%d", &n);
vector<int> v1;
vector<int> v2;
vector<int> v3;
for (i = 0; i < n; i++) {
scanf("%d", &x);
v1.push_back(x);
}
sort(v1.begin(), v1.end());
for (i = 0; i < n - 1; i++) {
scanf("%d", &x);
v2.push_back(x);
}
sort(v2.begin(), v2.end());
achou = 0;
for (i = 0; i < n && !achou; i++) {
if (i == n - 1 || v1.at(i) != v2.at(i)) {
printf("%d\n", v1.at(i));
achou = 1;
}
}
for (i = 0; i < n - 2; i++) {
scanf("%d", &x);
v3.push_back(x);
}
sort(v3.begin(), v3.end());
achou = 0;
for (i = 0; i < n - 1 && !achou; i++) {
if (i == n - 2 || v2.at(i) != v3.at(i)) {
printf("%d\n", v2.at(i));
achou = 1;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class T1>
void deb(T1 e1) {
cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
<< endl;
}
int n, nn;
int ans[2 * 100000 + 7];
pair<int, int> arr[100000 + 7];
vector<int> edges[2 * 100000 + 7];
void dfs(int u, int pre, int col) {
int v, sz, i, j, k;
sz = edges[u].size();
ans[u] = col;
for (i = 0; i < sz; i++) {
v = edges[u][i];
if (v == pre or ans[v] != -1) continue;
dfs(v, u, col ^ 1);
}
}
int main() {
int u, v, i, j, k;
scanf("%d", &n);
nn = n + n;
memset((ans), -1, sizeof(ans));
for (i = 1; i <= n; i++) {
scanf("%d %d", &u, &v);
arr[i] = make_pair(u, v);
edges[u].push_back(v);
edges[v].push_back(u);
if (i < n) {
edges[2 * i].push_back(2 * i + 1);
edges[2 * i + 1].push_back(2 * i);
}
}
for (i = 1; i <= nn; i++) {
if (ans[i] == -1) {
dfs(i, 0, 0);
}
}
for (i = 1; i <= n; i++) {
printf("%d %d\n", ans[arr[i].first] + 1, ans[arr[i].second] + 1);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int levels = 0;
int cur = 1;
int dif = 1;
while (N >= cur) {
N -= cur;
levels++;
dif++;
cur += dif;
}
cout << levels << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int L[1005], R[1005], A[1005];
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
int n, i, j, k, x, y, z, r, l;
cin >> n;
for (i = 1; i <= n; ++i) cin >> L[i];
for (i = 1; i <= n; ++i) cin >> R[i];
x = n;
z = n;
queue<int> q;
while (1) {
r = l = 0;
for (i = 1; i <= n; ++i) {
if (L[i] == 0 && R[i] == 0) {
A[i] = x;
r += 1;
L[i] = 1000000007;
R[i] = 1000000007;
}
}
if (r == 0) break;
z -= r;
for (i = 1; i <= n; ++i) {
if (A[i] == x) {
l += 1;
r -= 1;
} else {
R[i] -= r;
L[i] -= l;
}
}
x -= 1;
}
if (z) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
for (i = 1; i <= n; ++i) cout << A[i] << " ";
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool s1[10][10], s2[10][10];
bool can[10];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d%d", &x, &y);
s1[x][y] = 1;
s1[y][x] = 1;
}
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
s2[x][y] = 1;
s2[y][x] = 1;
}
for (int i = 1; i <= 9; i++)
for (int j = 1; j <= 9; j++)
if (s1[i][j]) {
bool ok1 = 0;
for (int k = 1; k <= 9; k++)
if (k != j && s2[i][k]) ok1 = 1;
bool ok2 = 0;
for (int k = 1; k <= 9; k++)
if (k != i && s2[j][k]) ok2 = 1;
if (ok1 && ok2) {
puts("-1");
return 0;
}
if (ok1) can[i] = 1;
if (ok2) can[j] = 1;
}
for (int i = 1; i <= 9; i++)
for (int j = 1; j <= 9; j++)
if (s2[i][j]) {
bool ok1 = 0;
for (int k = 1; k <= 9; k++)
if (k != j && s1[i][k]) ok1 = 1;
bool ok2 = 0;
for (int k = 1; k <= 9; k++)
if (k != i && s1[j][k]) ok2 = 1;
if (ok1 && ok2) {
puts("-1");
return 0;
}
if (ok1) can[i] = 1;
if (ok2) can[j] = 1;
}
int cnt = 0;
for (int i = 1; i <= 9; i++) cnt += can[i];
if (cnt > 1) {
puts("0");
return 0;
}
for (int i = 1; i <= 9; i++)
if (can[i]) {
printf("%d\n", i);
return 0;
}
return 0;
}
| 2 |
#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
using namespace std;
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))
typedef complex<double> P;
typedef long long ll;
const int MAX_SIZE = 10000;
// àÏ
double dot(P a, P b) {
return (a.real() * b.real() + a.imag() * b.imag());
}
// OÏ
double cross(P a, P b) {
return (a.real() * b.imag() - a.imag() * b.real());
}
// _cª¼üabãÉ é©È¢©
int is_point_on_line(P a, P b, P c) {
return EQ( cross(b-a, c-a), 0.0 );
}
// 2¼ü̼s»è
int is_orthogonal(P a1, P a2, P b1, P b2) {
return EQ( dot(a1-a2, b1-b2), 0.0 );
}
// 2¼ü̽s»è
int is_parallel(P a1, P a2, P b1, P b2) {
return EQ( cross(a1-a2, b1-b2), 0.0 );
}
// _a,bðÊé¼üÆ_cÌÔÌ£
double distance_l_p(P a, P b, P c) {
return abs(cross(b-a, c-a)) / abs(b-a);
}
// _a,bð[_Æ·éüªÆ_cÆÌ£
double distance_ls_p(P a, P b, P c) {
if ( dot(b-a, c-a) < EPS ) return abs(c-a);
if ( dot(a-b, c-b) < EPS ) return abs(c-b);
return abs(cross(b-a, c-a)) / abs(b-a);
}
// a1,a2ð[_Æ·éüªÆb1,b2ð[_Æ·éüªÌð·»è
int is_intersected_ls(P a1, P a2, P b1, P b2) {
return ( cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < EPS ) &&
( cross(b2-b1, a1-b1) * cross(b2-b1, a2-b1) < EPS );
}
// a1,a2ð[_Æ·éüªÆb1,b2ð[_Æ·éüªÌð_vZ
P intersection_ls(P a1, P a2, P b1, P b2) {
P b = b2-b1;
double d1 = abs(cross(b, a1-b1));
double d2 = abs(cross(b, a2-b1));
double t = d1 / (d1 + d2);
return a1 + (a2-a1) * t;
}
// a1,a2ðÊé¼üÆb1,b2ðÊé¼üÌð·»è
int is_intersected_l(P a1, P a2, P b1, P b2) {
return !EQ( cross(a1-a2, b1-b2), 0.0 );
}
// a1,a2ðÊé¼üÆb1,b2ðÊé¼üÌð_vZ
P intersection_l(P a1, P a2, P b1, P b2) {
P a = a2 - a1; P b = b2 - b1;
return a1 + a * cross(b, b1-a1) / cross(b, a);
}
/*
~üÆüªÌð·»è
*/
bool isCircleCrossLine(P a,P b,P c,double r){
double d1 = abs(a-c);
double d2 = abs(b-c);
if(d1<r&&d2<r){
return false;
}
double d = distance_ls_p(a,b,c);
if(EQ(d,r)||d<r){
return true;
}
else
return false;
}
// Op`ÌàÉ_ª é©Ç¤©
// OÏÌϪÈçàÉ_ è
bool isInTriangle(P p1,P p2,P p3,P s){
bool f=false;
//P tp=(p1+p2+p3);
//tp = P(tp.real()/3,tp.imag()/3);
//P ta=p1-tp;
//P tb=p2-tp;
//P tc=p3-tp;
//f = (cross(ta,tb)*cross(tb,tc)*cross(tc,ta))>0;
P a=p1-s;
P b=p2-s;
P c=p3-s;
return ((cross(a,b)>0&&cross(b,c)>0&&cross(c,a)>0)||(cross(a,b)<0&&cross(b,c)<0&&cross(c,a)<0));
}
int main(){
int n;
cin>>n;
for(int i = 0; i < n; i++){
int xp1,yp1,xp2,yp2,xp3,yp3,xk,yk,xs,ys;
cin>>xp1>>yp1>>xp2>>yp2>>xp3>>yp3>>xk>>yk>>xs>>ys;
P p1(xp1,yp1);
P p2(xp2,yp2);
P p3(xp3,yp3);
P k(xk,yk);
P s(xs,ys);
// ¼ûÆàOp`ÌàÉ_ª éê
if(isInTriangle(p1,p2,p3,k)&&isInTriangle(p1,p2,p3,s)){
cout<<"NG"<<endl;
}
// ¼ûÆàOp`ÌOÉ_ª éê
else if(!isInTriangle(p1,p2,p3,k)&&!isInTriangle(p1,p2,p3,s)){
cout<<"NG"<<endl;
}
else
cout<<"OK"<<endl;
}
return 0;
} | 0 |
#include<iostream>
#include<cstdio>
using namespace std;
int main(void){
int w,costs=4280;
while(scanf("%d",&w) && w+1){
costs=4280;
do{
if(w<=10){
w=0;
costs-=1150;
}else if(w<=20){
costs-=(w-10)*125;
w=10;
}else if(w<=30){
costs-=(w-20)*140;
w=20;
}else{
costs-=(w-30)*160;
w=30;
}
}while(w>0);
cout<<costs<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int main() {
int n;
while (scanf("%d", &n) + 1) {
if (n & 1)
printf("black\n");
else
printf("white\n1 2\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1, K = 500, mod = 998244353;
const char ans[2][5] = {"No\n", "Yes\n"};
int n, m, q, tot;
int a[N], pw[N], inv[N];
struct node {
int *s, len;
node() { s = NULL, len = 0; }
inline int val() { return s[len]; }
inline int calc(int x) {
return 1ll * (s[len] - s[len - x] + mod) * inv[len - x] % mod;
}
inline int del(int v, int x) { return calc(x) == v && (len -= x, 1); }
inline int ins(int x) {
if (x > 0)
return s[len + 1] = (1ll * x * pw[len] + s[len]) % mod, ++len;
else
return !len || calc(1) == -x && len--;
}
} sta[N];
inline int get(int l, int r, int d, node& out) {
static int p, tmp[4][K + 1];
if (out.s == NULL) out.s = tmp[p], p = p + 1 & 3;
for (out.len = 0; l != r + d; l += d)
if (!out.ins(a[l] * d)) return 0;
return 1;
}
inline int pop(node x) {
for (int len; tot && x.len; x.len -= len) {
len = min(x.len, sta[tot].len);
if (!sta[tot].del(x.calc(len), len)) return 0;
tot -= !sta[tot].len;
}
return !x.len;
}
inline void push(node x) {
if (x.len) sta[++tot] = x;
}
struct block {
int L, R, need, tag, f[K + 1], g[K + 1];
node pre, nxt;
inline void init(int l, int r) {
L = l, R = r, need = 1, pre.s = f, nxt.s = g;
}
inline void build() {
if (need) need = 0, tag = get(R, L, -1, pre) && get(L, R, 1, nxt);
}
inline int solve(int l, int r) {
if (R < l || L > r) return 1;
int out;
node x, y;
l = max(L, l), r = min(R, r);
if (L == l && r == R)
build(), x = pre, y = nxt, out = tag;
else
out = get(r, l, -1, x) && get(l, r, 1, y);
return out && pop(x) && (push(y), 1);
}
} t[N / K + 1];
inline void input() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
}
inline void init() {
pw[0] = inv[0] = 1, pw[1] = 19260817, inv[1] = 494863259, m = 0;
for (int i = 2; i <= n; i++)
pw[i] = 1ll * pw[i - 1] * pw[1] % mod,
inv[i] = 1ll * inv[i - 1] * inv[1] % mod;
for (int l = 1, r; l <= n; l = r + 1)
r = min(n, l + K - 1), t[++m].init(l, r);
}
inline int ask(int l, int r) {
if (tot = 0, r - l + 1 & 1) return 0;
for (int i = 1; i <= m; i++)
if (!t[i].solve(l, r)) return 0;
return !tot;
}
inline void work() {
scanf("%d", &q);
for (int opt, x, y; q--;) {
scanf("%d%d%d", &opt, &x, &y);
if (opt == 1)
a[x] = y, t[(x - 1) / K + 1].need = 1;
else
cout << ans[ask(x, y)];
}
}
int main() {
input();
init();
work();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename S>
struct SegmentTreeItLazy {
T neutro = 0;
int size, height;
vector<T> st, delUp;
vector<bool> upd;
SegmentTreeItLazy(int n, T val) {
st.resize(2 * n, val);
delUp.resize(n);
upd.resize(n);
size = n;
height = sizeof(int) * 8 - __builtin_clz(size);
build();
}
SegmentTreeItLazy(int n, vector<S>& v) {
st.resize(2 * n);
size = n;
delUp.resize(n);
upd.resize(n);
height = sizeof(int) * 8 - __builtin_clz(size);
for (int i = 0; i < n; i++) st[i + size] = set(v[i]);
build();
}
T merge(T a, T b) { return a + b; }
void apply(int i, T val, int k) {
st[i] = val * k;
if (i < size) delUp[i] = val, upd[i] = 1;
}
void calc(int i) {
if (!upd[i]) st[i] = merge(st[i << 1], st[i << 1 | 1]);
}
void build() {
for (int i = size - 1; i; i--) calc(i);
}
void build(int p) {
while (p > 1) p >>= 1, calc(p);
}
void push(int p) {
for (int s = height, k = 1 << (height - 1); s; s--, k >>= 1) {
int i = p >> s;
if (upd[i]) {
apply(i << 1, delUp[i], k);
apply(i << 1 | 1, delUp[i], k);
upd[i] = 0, delUp[i] = 0;
}
}
}
void update(int l, int r, T val) {
push(l += size), push(r += size);
int ll = l, rr = r, k = 1;
for (; l <= r; l >>= 1, r >>= 1, k <<= 1) {
if (l & 1) apply(l++, val, k);
if (~r & 1) apply(r--, val, k);
}
build(ll), build(rr);
}
T query(int l, int r) {
push(l += size), push(r += size);
T res = neutro;
for (; l <= r; l >>= 1, r >>= 1) {
if (l & 1) res = merge(res, st[l++]);
if (~r & 1) res = merge(res, st[r--]);
}
return res;
}
T& operator[](int i) { return st[i + size]; }
T set(S x) { return x; }
};
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) return a.second < b.second;
return a.first > b.first;
}
vector<int> solve(vector<pair<int, int> >& v) {
int n = v.size();
SegmentTreeItLazy<int, int> st(n + 1, -1);
int i;
i = 0;
while (i < n) {
int act = v[i].first;
int ant = 0;
int mx = 0;
while (i < n && v[i].first == act) {
mx = max(mx, v[i].second - ant - 1);
ant = v[i].second;
i++;
}
mx = max(mx, n - ant);
st.update(mx + 1, n, act);
}
vector<int> ans(n);
for (int j = 1; j <= n; j++) {
ans[j - 1] = st.query(j, j);
}
return ans;
}
int main() {
;
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<pair<int, int> > v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].first;
v[i].second = i + 1;
}
sort(v.begin(), v.end(), cmp);
vector<int> ans = solve(v);
for (int i = 0; i < n; i++) cout << ans[i] << ' ';
cout << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n, s, x[100001], y[100001], min = 99, dem0 = 0;
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> x[i] >> y[i];
if (n == 1 && x[i] < s) min = y[i];
if ((x[i] + (y[i] * 1.0 / 100)) <= s) {
if (y[i] < min && y[i] > 0)
min = y[i];
else if (y[i] == 0)
dem0++;
}
}
if (dem0 == n)
cout << 0;
else if (min > 0 && min != 99)
cout << 100 - min;
else if (dem0 != 0)
cout << 0;
else
cout << -1;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int n;
long long k, ans = 0, a[N];
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
sort(a + 1, a + n + 1);
int l = 1, r = n;
while (l < r) {
if (l < n - r + 1) {
if (k >= l * (a[l + 1] - a[l])) {
k -= l * (a[l + 1] - a[l]);
++l;
for (int i = l; i <= n; i++)
if (a[i] == a[l])
l = i;
else
break;
} else {
ans = k / l;
break;
}
} else {
if (k >= (n - r + 1) * (a[r] - a[r - 1])) {
k -= (n - r + 1) * (a[r] - a[r - 1]);
--r;
for (int i = r; i >= 1; i--)
if (a[i] == a[r])
r = i;
else
break;
} else {
ans = k / (n - r + 1);
break;
}
}
}
if (l >= r)
printf("0\n");
else
cout << a[r] - a[l] - ans << endl;
return 0;
}
| 5 |
#include <iostream>
using namespace std;
int main()
{
long long int n, h, w;
cin >> n >> h >> w;
cout << ((n-h)+1)*((n-w)+1) << '\n';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int MOD = 1000000007;
int main() {
long long int a, b, t;
cin >> a >> b >> t;
long long int s[] = {a, b, b - a, -a, -b, a - b};
cout << (s[(t - 1) % 6] % MOD + MOD) % MOD;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> types(n);
vector<int> parents(n);
vector<int> deg(n, 0);
for (int i = 0; i < n; i++) cin >> types[i];
for (int i = 0; i < n; i++) {
cin >> parents[i];
parents[i]--;
if (parents[i] != -1) {
deg[parents[i]]++;
}
}
int best = 0;
int bestInd = 0;
for (int i = 0; i < n; i++) {
if (types[i] == 1) {
int len = 1;
int cur = i;
while (parents[cur] != -1 && deg[parents[cur]] == 1 &&
types[parents[cur]] == 0) {
len++;
cur = parents[cur];
}
if (len > best) {
bestInd = i;
best = len;
}
}
}
vector<int> bestPath(best);
bestPath[best - 1] = bestInd;
for (int i = best - 2; i >= 0; i--) {
bestPath[i] = parents[bestPath[i + 1]];
}
cout << best << endl;
for (int i = 0; i < best; i++) cout << (bestPath[i] + 1) << " ";
cout << endl;
return 0;
}
| 2 |
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int n, s;
scanf("%d", &n);
for (int i = 0; i < n; i++){
scanf("%d", &s);
printf("Case %d:\n", i + 1);
for (int j = 0; j < 10; j++){
printf("%d\n", s = s * s / 100 % 10000);
}
}
return (0);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
string arr[2][1000 * 100 + 10];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[0][i] >> arr[1][i];
if (arr[1][i] < arr[0][i]) swap(arr[1][i], arr[0][i]);
}
int k, ans = 1;
cin >> k;
string prv = arr[0][k - 1];
for (int i = 0; i < n - 1; i++) {
cin >> k;
if (arr[0][k - 1] > prv)
prv = arr[0][k - 1];
else if (arr[1][k - 1] > prv)
prv = arr[1][k - 1];
else
ans = -1;
}
if (ans == -1)
cout << "NO";
else
cout << "YES";
return 0;
}
| 3 |
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
string ans[1000];
int n = -1;
int r,k = 0;
getline(cin,s);
for(int i=0;i<s.size();i++){
if(s[i] == ' ' || s[i] == ',' || s[i] == '.'){
if(i-1-n > 2 && i-1-n < 7){
for(int j=n+1;j<i;j++) ans[k] += s[j];
k++;
n = i;
}
else n = i;
}
}
cout << ans[0];
for(int i=1;i<k;i++) cout << " " << ans[i];
cout << endl;
} | 0 |
#include <bits/stdc++.h>
#define maxn 3030
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll dp[maxn][maxn],times=1,ans;
int a[maxn],n,T,x,y;
void qpow(int k){
ll base=2;
while(k){
if(k&1) times=(times*base)%mod;
base=base*base%mod;
k>>=1;
}
}
int main(){
scanf("%d%d",&n,&T);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dp[i][j]=a[i]>a[j];
qpow(T);
while(T--){
scanf("%d%d",&x,&y);
for(int i=1;i<=n;i++){
if(i==x||i==y) continue;
dp[i][x]=dp[i][y]=(dp[i][x]+dp[i][y])*((mod+1)/2)%mod;
dp[x][i]=dp[y][i]=(dp[x][i]+dp[y][i])*((mod+1)/2)%mod;
}
dp[x][y]=dp[y][x]=(dp[x][y]+dp[y][x])*((mod+1)/2)%mod;
}
for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) ans=(ans+dp[i][j])%mod;
printf("%lld",ans*times%mod);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
class Coord {
public:
int x;
int y;
Coord(int _x, int _y) : x(_x), y(_y) {}
Coord() {}
};
const int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, 0, -1, 0, 1};
const int n = 8;
char a[8][8];
bool mark[8][8];
queue<Coord> q;
queue<Coord> qt;
bool check(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < n && a[x][y] == '.';
}
void print_a() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << a[i][j];
}
cout << endl;
}
cout << endl;
}
int main(int argc, char** argv) {
ios_base::sync_with_stdio(false);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) cin >> a[i][j];
}
a[0][7] = '.';
int x;
int y;
q.push(Coord(7, 0));
for (int iter = 0; iter < 100; ++iter) {
while (!q.empty()) {
x = q.front().x;
y = q.front().y;
a[x][y] = '.';
for (int i = 0; i < 9; ++i) {
if (check(x + dx[i], y + dy[i])) {
qt.push(Coord(x + dx[i], y + dy[i]));
a[x + dx[i]][y + dy[i]] = 'M';
}
}
q.pop();
}
memset(mark, false, sizeof(mark));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) mark[i][j] = a[i][j] == 'S';
}
for (int i = n - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j)
if (mark[i][j]) {
a[i][j] = '.';
if (i + 1 < n) a[i + 1][j] = 'S';
}
}
while (!qt.empty()) {
x = qt.front().x;
y = qt.front().y;
if (a[x][y] == 'M') q.push(Coord(x, y));
qt.pop();
}
}
cout << (a[0][7] == 'M' ? "WIN" : "LOSE");
return EXIT_SUCCESS;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const double pi = acos(-1.0);
const int MAX = 1000000000;
class TheGridDivTwo {
public:
};
int arr[100001];
vector<int> cur, res;
int totalAnd, n;
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int mask = 0; mask < 30; mask++) {
totalAnd = (1 << 30) - 1;
cur.clear();
for (int i = 0; i < n; i++) {
if (arr[i] & (1 << mask)) {
cur.push_back(arr[i]);
totalAnd &= arr[i];
}
}
if (totalAnd % (1 << mask) == 0) res = cur;
}
cout << (int)res.size() << endl;
for (int i = 0; i < (int)res.size(); i++) cout << res[i] << ' ';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, fa[100005], to[100005], ont[100005], pv[100005], opt[100005],
ar[100005], cl[100005], fg[100005], sm[100005];
int vis[100005], tim;
vector<int> G[100005], E[100005];
void dfs(int u) {
for (int v : E[u]) dfs(v), sm[u] += sm[v];
if (!ont[u]) pv[u] += sm[u];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = (2), LIM = (n); i <= LIM; i++)
scanf("%d", &fa[i]), E[fa[i]].push_back(i);
for (int i = (1), LIM = (n); i <= LIM; i++) scanf("%d", &pv[i]);
for (int i = (1), LIM = (m); i <= LIM; i++) scanf("%d", &opt[i]);
int ans = 0;
for (int L = 1, R; L <= m; L += 355) {
R = min(L + 355 - 1, m);
memset(ont, 0, sizeof ont);
memset(sm, 0, sizeof sm);
ar[0] = 0;
vis[1] = ++tim, ont[1] = 1, ar[++ar[0]] = 1;
for (int j = (L), LIM = (R); j <= LIM; j++) {
int u = abs(opt[j]);
if (!ont[u]) ont[u] = 1, ar[++ar[0]] = u;
for (; vis[u] != tim; u = fa[u]) vis[u] = tim;
if (!ont[u]) ont[u] = 1, ar[++ar[0]] = u;
}
for (int j = (1), LIM = (ar[0]); j <= LIM; j++) {
int u = ar[j];
G[u].clear();
G[u].resize((355 + 5) << 1);
fg[u] = (355 + 5);
for (u = fa[u]; u && !ont[u]; u = fa[u])
if (cl[u] == 0 && abs(pv[u]) <= 355) G[ar[j]][(355 + 5) + pv[u]]++;
to[ar[j]] = u;
}
for (int j = (L), LIM = (R); j <= LIM; j++) {
int u = abs(opt[j]), tp = opt[j] / u;
sm[u] -= tp;
for (; u; u = to[u]) {
if (tp == 1)
ans += G[u][fg[u]++];
else
ans -= G[u][--fg[u]];
ans -= (cl[u] == 0 && pv[u] < 0);
if (u == abs(opt[j])) cl[u] ^= 1;
pv[u] -= tp;
ans += (cl[u] == 0 && pv[u] < 0);
}
printf("%d%c", ans, " \n"[j == m]);
}
dfs(1);
}
}
| 5 |
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3", "omit-frame-pointer","inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
/***********************************************/
/* Dear online judge:
* I've read the problem, and tried to solve it.
* Even if you don't accept my solution, you should respect my effort.
* I hope my code compiles and gets accepted.
* ___ __ _______ _______
* |\ \|\ \ |\ ___ \ |\ ___ \
* \ \ \/ /|_\ \ __/| \ \ __/|
* \ \ ___ \\ \ \_|/__\ \ \_|/__
* \ \ \\ \ \\ \ \_|\ \\ \ \_|\ \
* \ \__\\ \__\\ \_______\\ \_______\
* \|__| \|__| \|_______| \|_______|
*/
const long long mod = 1000000007;
//const long long mod = 998244353;
mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());
const int mxN = 1000010;
int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
srand(time(NULL));
cout << fixed << setprecision(9);
int t = 1;
// int Case = 1;
cin >> t;
// t = 100;
int rev[] = { 0, 1, 5, -1, -1, 2, -1, -1, 8, -1 };
auto ref = [rev](int x) {
int res = 0;
for(int f = 0;f < 2 && res >= 0;f++) {
int d = x%10;
x/=10;
res *= 10;
if(rev[d] == -1) res = -1;
else res += rev[d];
}
return res;
};
while (t--) {
int h, m, sh, sm;
char c;
cin >> h >> m;
cin >> sh >> c >> sm;
int oh = INT_MAX, om = INT_MAX;
for (int a = sh; a < h && oh == INT_MAX; a++) {
for (int b = 0; b < m && oh == INT_MAX; b++) {
if (a == sh && b < sm)
continue;
int ra = ref(a), rb = ref(b);
if (ra == -1 || ra >= m)
continue;
if (rb == -1 || rb >= h)
continue;
oh = a, om = b;
}
}
if (oh == INT_MAX)
oh = 0, om = 0;
cout << setfill('0') << setw(2) << oh << ":" << setfill('0') << setw(2) << om << '\n';
}
return 0;
}
/* stuff you should look for:
* overflow, array bounds
* special cases (n=1?)
* DON'T STICK TO ONE APPROACH
* solve simpler or different variation
* Solve DP using graph ( \_(-_-)_/ )
*/
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2005;
const int MOD = 1e9 + 7;
int n;
char s[2][10];
int main() {
while (scanf("%d %s %s", &n, s[0], s[1]) != EOF) {
if (s[1][0] == 'w') {
if (n <= 4 || n == 7)
cout << 52 << endl;
else
cout << 53 << endl;
} else {
if (n >= 1 && n <= 29) {
cout << 12 << endl;
} else if (n == 30) {
cout << 11 << endl;
} else
cout << 7 << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long v[100005][3];
long long a, b, c, sum, dif, x, ansx, ansy, ansz;
long long a1, a2, b1, b2, c1, c2, s1, s2;
long long l, r, mid;
int n;
const long long inf = 1ll << 62;
long long div2(long long x) {
if (x < 0)
return (x - 1) / 2;
else
return x / 2;
}
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%I64d%I64d%I64d", &v[i][0], &v[i][1], &v[i][2]);
l = 0, r = inf;
while (l <= r) {
mid = (l + r) / 2;
a1 = b1 = c1 = s1 = -inf;
a2 = b2 = c2 = s2 = inf;
for (int i = 1; i <= n; i++) {
a1 = max(v[i][1] + v[i][2] - v[i][0] - mid, a1);
a2 = min(v[i][1] + v[i][2] - v[i][0] + mid, a2);
b1 = max(v[i][0] + v[i][2] - v[i][1] - mid, b1);
b2 = min(v[i][0] + v[i][2] - v[i][1] + mid, b2);
c1 = max(v[i][0] + v[i][1] - v[i][2] - mid, c1);
c2 = min(v[i][0] + v[i][1] - v[i][2] + mid, c2);
s1 = max(v[i][0] + v[i][1] + v[i][2] - mid, s1);
s2 = min(v[i][0] + v[i][1] + v[i][2] + mid, s2);
}
if (a1 > a2 || b1 > b2 || c1 > c2 || s1 > s2) {
l = mid + 1;
continue;
}
long long xmn = max(div2(s1 - a2 + 1), div2(b1 + c1 + 1));
long long xmx = min(div2(s2 - a1), div2(b2 + c2));
for (x = xmn; x <= xmx; x++) {
for (sum = max(a1 + x, s1 - x); sum <= min(a2 + x, s2 - x); sum++) {
for (dif = max(x - b2, c1 - x); dif <= min(x - b1, c2 - x); dif++)
if ((sum + dif) % 2 == 0) break;
if (dif <= min(x - b1, c2 - x)) break;
}
if (sum <= min(a2 + x, s2 - x)) break;
}
if (x <= xmx) {
ansx = x;
ansy = div2(sum + dif);
ansz = div2(sum - dif);
r = mid - 1;
} else
l = mid + 1;
}
printf("%I64d %I64d %I64d\n", ansx, ansy, ansz);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m, x, n, ans = 0;
cin >> n >> m;
cin >> x;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (x == min(min(i, j), min(n - i + 1, m - j + 1)) && (i + j) % 2 == 0)
ans++;
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long t[200010];
set<long long> st;
int main() {
scanf("%lld", &n);
for (long long i = (long long)(0); i < (long long)(n); i++)
scanf("%lld", t + i);
long long res = 1;
st.insert(0);
for (long long i = (long long)(0); i < (long long)(n); i++) {
if (st.find(t[i]) == st.end()) {
res++;
st.insert(i + 1);
} else {
st.erase(t[i]);
st.insert(i + 1);
}
}
cout << res << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000009;
long long md(long long n) {
if (n >= mod) return n % mod;
return n;
}
long long power(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, a, b, k, inv;
cin >> n >> a >> b >> k;
string s;
cin >> s;
inv = power(a, mod - 2);
long long term = power(a, n), sum = 0;
for (long long i = 0; s[i]; i++) {
if (s[i] == '+')
sum = md(sum + term);
else
sum = md(sum - term + mod);
term = md(md(term * inv) * b);
}
long long ans;
long long A = sum, N = (n + 1) / k, R = power(md(b * inv), k);
if (R == 1) {
ans = sum * (n + 1) / k;
ans %= mod;
} else {
long long numerator = md(A * md((power(R, N) - 1 + mod)));
long long den = power(R - 1, mod - 2);
numerator = md(numerator * den);
ans = numerator;
}
cout << ans;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int K = 20;
int n, q, l;
int x[100000];
int d[K][100000];
void pre() {
int j = 0;
for (int i = 0; i < n; ++i) {
while (j < n && x[j] - x[i] <= l) {
++j;
}
d[0][i] = j - 1;
}
for (int k = 1; k < K; ++k) {
for (int i = 0; i < n; ++i) {
d[k][i] = d[k - 1][d[k - 1][i]];
}
}
}
int sub(int a, int b) {
int cnt = 0;
int i = a;
for (int k = K - 1; k >= 0; --k) {
int ni = d[k][i];
if (ni < b) {
cnt += 1 << k;
i = ni;
}
}
return cnt + 1;
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) cin >> x[i];
cin >> l >> q;
pre();
for (int j = 0; j < q; ++j) {
int a, b;
cin >> a >> b;
--a; --b;
if (a > b) swap(a, b);
cout << sub(a, b) << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, ans = 0;
cin >> n >> m;
string a[100];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < m - 1; j++) {
int k = 'f' * 'a' * 'c' * 'e';
int l = a[i][j] * a[i + 1][j] * a[i][j + 1] * a[i + 1][j + 1];
if (k == l) ans++;
}
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007, MX = (1 << 20);
long long fact[MX];
long long POW(long long x, long long y) {
if (y == 1) return x;
long long ret = POW(x, y / 2);
ret *= ret;
ret %= MOD;
if (y % 2) ret *= x;
return ret % MOD;
}
long long inverse(long long a, long long b) {
long long ret = a % MOD;
ret *= POW(b, MOD - 2);
return ret % MOD;
}
long long C(long long x, long long y) {
if (x < y) return 0;
long long ret = inverse(fact[x], fact[x - y]);
ret = inverse(ret, fact[y]);
return ret;
}
long long cum[MX], n, arr[MX], p[MX], k, ans;
char ch;
int main() {
cin >> n >> k;
p[0] = fact[0] = 1;
for (long long j = 1; j <= n; j++) p[j] = (10 * p[j - 1]) % MOD;
for (int j = 1; j <= n; j++) fact[j] = (j * fact[j - 1]) % MOD;
for (long long j = 1; j <= n; j++) {
cin >> ch;
arr[j] = ch - '0';
}
if (k == 0) {
for (int j = 1; j <= n; j++) {
ans *= 10;
ans += arr[j];
ans %= MOD;
}
cout << ans << endl;
return 0;
}
for (long long j = 0; j <= n; j++) {
cum[j] = (p[j] * C(n - j - 2, k - 1)) % MOD;
if (j) cum[j] += cum[j - 1];
cum[j] %= MOD;
}
long long theta;
for (long long j = 2; j < n; j++) {
ans += arr[j] * cum[n - j - 1];
ans %= MOD;
theta = (p[n - j] * arr[j]) % MOD;
theta *= C(j - 1, k);
theta %= MOD;
ans += theta;
ans %= MOD;
}
theta = (arr[n] * C(n - 1, k)) % MOD;
ans = (ans + theta) % MOD;
for (long long j = 1; j <= n; j++) {
theta = (arr[1] * p[j - 1]) % MOD;
theta = (theta * C(n - j - 1, k - 1)) % MOD;
ans = (ans + theta) % MOD;
}
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
cout << (s.size() + 1) * 25 + 1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long N,A,B;
cin >> N >> A >> B;
long long M,m,a;
a=0;
M=(N-2)*B;
m=(N-2)*A;
cout << max(M-m+1,a) << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 4;
long long n;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
long long temp = n;
int d = 0;
while (temp) {
d++;
temp /= 10;
}
int ans = (d - 1) * 9;
long long a = 0;
for (int i = 0; i < d - 1; ++i) {
a = a * 10 + 9;
}
long long b = n - a;
while (b) {
ans += (b % 10);
b /= 10;
}
cout << ans << '\n';
}
| 2 |
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int N = 2e5 + 5;
const int oo = 1e9;
int par[N];
int d[N];
int a[N];
int n,q,s,k;
int getpar (int u) {
return (par[u] < 0) ? u : (par[u] = getpar(par[u]));
}
void join(int u, int v) {
u = getpar(u);
v = getpar(v);
if (u == v)
return;
par[v] += par[u];
par[u] = v;
}
void update(int l, int r, int i, int k,
priority_queue <pair <int, int>> &heap) {
if (l > 1) {
int j = l - 1;
if (mini(d[j], a[i] + k - a[j]))
heap.push(mp(-d[j], j));
}
if (r < n) {
int j = r + 1;
if (mini(d[j], a[j] - (a[i] + k)))
heap.push(mp(-d[j], j));
}
l = getpar(l);
while (l <= r) {
d[l] = d[i];
heap.push(mp(-d[l], l));
join(l, l + 1);
l = getpar(l);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q >> s >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n + 1; i++)
par[i] = -1, d[i] = oo;
a[n + 1] = oo;
priority_queue <pair <int, int>> heap;
for (int i = 1; i <= n; i++) {
if (a[i] <= a[s] + k && a[s] + k <= a[i + 1]) {
d[i] = (a[s] + k - a[i]);
heap.push(mp(-d[i], i));
if (i < n) {
d[i + 1] = a[i + 1] - (a[s] + k);
heap.push(mp(-d[i + 1], i + 1));
}
break;
}
}
a[0] = -oo;
for (int i = 1; i <= n; i++) {
if (a[i - 1] <= a[s] - k && a[s] - k <= a[i]) {
if (mini(d[i], a[i] - (a[s] - k)))
heap.push(mp(-d[i], i));
if (i > 1) {
if (mini(d[i - 1], (a[s] - k) - a[i - 1]))
heap.push(mp(-d[i - 1], i - 1));
}
}
}
while (heap.size()) {
int i = heap.top().se, cur = -heap.top().fi;
heap.pop();
if (cur != d[i])
continue;
join(i, i + 1);
if (i < n && mini(d[i + 1], d[i] + a[i + 1] - a[i]))
heap.push(mp(-d[i + 1], i + 1));
if (i > 1 && mini(d[i - 1], d[i] + a[i] - a[i - 1]))
heap.push(mp(-d[i - 1], i - 1));
int l = upper_bound(a + 1, a + n + 1, a[i] + k - d[i]) - a;
int r = upper_bound(a + 1, a + n + 1, a[i] + k + d[i]) - a - 1;
update(l, r, i, k, heap);
l = upper_bound(a + 1, a + n + 1, a[i] - k - d[i]) - a;
r = upper_bound(a + 1, a + n + 1, a[i] - k + d[i]) - a - 1;
update(l, r, i, -k, heap);
}
// for (int i = 1; i <= n; i++)
// cerr << d[i] << " \n"[i == n];
while (q--) {
int i, k; cin >> i >> k;
if (i == s || d[i] <= k)
cout << "yes\n";
else
cout << "no\n";
}
return 0;
} | 6 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(static_cast<unsigned int>(
chrono::steady_clock::now().time_since_epoch().count()));
template <typename num_t>
inline void add_mod(num_t& a, const long long& b, const int& m) {
a = (a + b) % m;
if (a >= m) a -= m;
if (a < 0) a += m;
}
template <typename num_t>
inline bool update_max(num_t& a, const num_t& b) {
return a < b ? a = b, true : false;
}
template <typename num_t>
inline bool update_min(num_t& a, const num_t& b) {
return a > b ? a = b, true : false;
}
template <typename num_t>
num_t gcd(num_t lhs, num_t rhs) {
return !lhs ? rhs : gcd(rhs % lhs, lhs);
}
template <typename num_t>
num_t pw(num_t n, num_t k, const num_t& mod) {
if (k == -1) k = mod - 2;
num_t res = 1;
for (; k > 0; k >>= 1) {
if (k & 1) res = 1ll * res * n % mod;
n = 1ll * n * n % mod;
}
return res % mod;
}
const int inf = 1e9 + 7;
const int mod = 998244353;
const long long ll_inf = (long long)inf * inf + 7;
const int maxn = 2000 + 7;
int dp[maxn][maxn][2];
int n, m;
int row[maxn][maxn];
int col[maxn][maxn];
string s[maxn];
int row_calc[maxn][maxn];
int col_calc[maxn][maxn];
void solve() {
cin >> n >> m;
for (int i = (0), _b = (n); i < _b; ++i) cin >> s[i];
for (int i = 0; i < n; ++i) {
for (int j = m - 1; j >= 0; --j) {
row[i][j] = row[i][j + 1];
row[i][j] += (s[i][j] == 'R');
}
}
for (int j = 0; j < m; ++j) {
for (int i = n - 1; i >= 0; --i) {
col[i][j] = col[i + 1][j];
col[i][j] += (s[i][j] == 'R');
}
}
if (s[n - 1][m - 1] == 'R') {
cout << 0;
return;
}
if (n == 1 && m == 1) {
cout << 1;
return;
}
dp[0][0][0] = 1;
dp[0][0][1] = 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (i > 0) add_mod(col_calc[i][j], col_calc[i - 1][j], inf);
if (j > 0) add_mod(row_calc[i][j], row_calc[i][j - 1], inf);
add_mod(dp[i][j][0], row_calc[i][j], inf);
add_mod(dp[i][j][1], col_calc[i][j], inf);
if (dp[i][j][0] > 0) {
int temp = n - 1 - i - col[i + 1][j];
add_mod(col_calc[i + 1][j], dp[i][j][0], inf);
add_mod(col_calc[i + 1 + temp][j], -dp[i][j][0], inf);
}
if (dp[i][j][1] > 0) {
int temp = m - 1 - j - row[i][j + 1];
add_mod(row_calc[i][j + 1], dp[i][j][1], inf);
add_mod(row_calc[i][j + 1 + temp], -dp[i][j][1], inf);
}
}
}
cout << (dp[n - 1][m - 1][0] + dp[n - 1][m - 1][1]) % inf << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
const bool multiple_test = false;
int test = 1;
if (multiple_test) cin >> test;
for (int i = 0; i < test; ++i) {
solve();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
int main() {
std::ios::sync_with_stdio(false);
long long n, k;
int a, b, c, d;
cin >> n >> k;
cin >> a >> b >> c >> d;
vector<int> path;
if (n <= 4 || k <= n) {
cout << -1 << endl;
return 0;
}
cout << a << " " << c << " ";
for (int i = 1; i <= n; i++) {
if (!(i == a || i == b || i == c || i == d)) {
cout << i << " ";
path.push_back(i);
}
}
cout << d << " " << b << endl;
cout << c << " " << a << " ";
for (int i = 0; i < path.size(); i++) cout << path[i] << " ";
cout << b << " " << d << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void Yahia74() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 1e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;
int main() {
Yahia74();
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a != d) {
cout << 0;
} else {
if (c > 0 && a + d > 0 || c == 0) {
cout << 1;
} else {
cout << 0;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
int main() {
long long int n, m, k, l, a, b;
scanf("%lld%lld%lld%lld", &n, &m, &k, &l);
b = (l + k + m - 1) / m;
if (b * m > n)
printf("-1");
else
printf("%lld", b);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0, m = 0, k = 0;
double max1 = 0.0f;
long long sum = 0, sum2 = 0;
cin >> n >> k >> m;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
}
sort(v.begin(), v.end());
sum2 = sum;
int t = m;
double s = 0.0f;
for (int i = 0; i < v.size() && t >= 0 && v.size() - i != 0; i++) {
if (t < k)
s = ((double)sum2 + (double)t) / (double)(v.size() - i);
else
s = ((double)sum2 + (double)k) / (double)(v.size() - i);
max1 = max(s, max1);
sum2 -= v[i];
t--;
}
if (m == 2 && k == 1) {
double aux = ((double)sum + (double)m) / (double)(v.size());
max1 = max(aux, max1);
}
cout << setprecision(20) << fixed;
cout << max1 << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tt, nn, mm, i, j, k, l, m, n, a, b, c, d;
cin >> tt;
while (tt--) {
string a;
cin >> nn;
cin >> a;
long long maxi = nn;
for (i = 0; i < nn; i++) {
if (a[i] == '1') {
maxi = max(maxi, 2 * nn - 2 * i);
}
if (a[nn - i - 1] == '1') {
maxi = max(maxi, 2 * nn - 2 * i);
}
}
cout << maxi << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 111;
int k, n, A[N], res;
string s;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k;
cin.ignore();
getline(cin, s);
n = 0;
int a = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ' || s[i] == '-') {
n++;
A[n] = i - a + 1;
a = i + 1;
}
}
n++;
A[n] = s.size() - a;
int l = 1, r = s.size(), mid;
while (l <= r) {
mid = (l + r) >> 1;
int cnt = 1;
int tmp = 0;
bool ok = false;
for (int i = 1; i <= n; i++) {
if (A[i] > mid) {
l = mid + 1;
ok = true;
break;
}
if (tmp + A[i] > mid) {
cnt++;
tmp = 0;
}
tmp += A[i];
}
if (ok) continue;
if (cnt <= k) {
res = mid;
r = mid - 1;
} else
l = mid + 1;
}
cout << res;
return 0;
}
| 4 |
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define REP(i,n) for (int i=1;i<=(n);++i)
#define FOR(i,a,b) for (int i=(a);i<=(b);++i)
#define ROF(i,a,b) for (int i=(a);i>=(b);--i)
#define FEC(p,u) for (edge*p=head[u];p;p=p->nxt)
using namespace std;
typedef long long LL;
int n, L, Q, x[110000];
int a[110000][20];
int main(){
scanf("%d", &n);
REP(i,n) scanf("%d", x+i);
scanf("%d%d", &L, &Q);
REP(i,n) a[i][0] = upper_bound(x+i, x+n+1, x[i]+L)-x-1;
FOR(j,1,17) REP(i,n) a[i][j] = a[a[i][j-1]][j-1];
int u, v, ans;
while (Q--){
scanf("%d%d", &u, &v);
if (u > v) swap(u, v);
ans = 0;
ROF(j,17,0) {
if (a[u][j] < v) {
u = a[u][j];
ans += 1<<j;
}
}
printf("%d\n", ans+1);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string xx, yy;
cin >> xx;
bool dd = false;
int l = 0, r = 0;
for (int i = 0, _n = xx.length(); i < _n; i++) {
if (xx[i] == '|')
dd = true;
else if (dd)
r++;
else
l++;
}
cin >> yy;
int goal = (r + l + yy.length()), idx = 0;
if (goal % 2 || yy.length() < max(r, l) - min(r, l)) {
cout << "Impossible\n";
return 0;
}
goal /= 2;
dd = false;
for (int i = 0, _n = xx.length(); i < _n; i++) {
if (xx[i] == '|') {
dd = true;
while (l++ < goal) cout << yy[idx++];
cout << "|";
} else
cout << xx[i];
}
while (r++ < goal) cout << yy[idx++];
cout << "\n";
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main() {
int d; cin >> d;
int c[26];
for (int i = 0; i < 26; i++) cin >> c[i];
int s[d][26];
for (int i = 0; i < d; i++)
{
for (int j = 0; j < 26; j++)
{
cin >> s[i][j];
}
}
int t[d];
for (int i = 0; i < d; i++) {
cin >> t[i];
t[i]--;
}
int last[26] = {};
int v = 0;
for (int i = 0; i < d; i++)
{
v += s[i][t[i]];
last[t[i]] = i+1;
for (int j = 0; j < 26; j++)
{
v -= c[j] * (i + 1 - last[j]);
}
cout << v << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
scanf("%d", &k);
int maxn = 0;
for (int i = 1; i <= k; i++) {
int x;
scanf("%d", &x);
if (x >= 500001) {
maxn = max(maxn, 1000000 - x);
}
if (x <= 500000) {
maxn = max(maxn, x - 1);
}
}
printf("%d\n", maxn);
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int n,a[100000],b[100000];
int main()
{
cin>>n;
for (int i=0,k=1;i<n;i++,k*=-1)
cin>>b[i+1],a[1]+=k*b[i+1];
for (int i=2;i<=n;i++)
a[i]=2*b[i-1]-a[i-1];
for (int i=1;i<=n;i++)
cout<<a[i]<<" \n"[i==n];
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9;
int main() {
int n;
scanf("%d", &n);
int ans = 0;
while (n--) {
int x1, x2, gftxdtrtfhyjfctrxujkvbhyjice, y2;
scanf("%d%d%d%d", &x1, &gftxdtrtfhyjfctrxujkvbhyjice, &x2, &y2);
ans += (x2 - x1 + 1) * (y2 - gftxdtrtfhyjfctrxujkvbhyjice + 1);
}
cout << ans << endl;
return 0;
}
| 1 |
#include <iostream>
#include <algorithm>
#include <cmath>
#include <functional>
using namespace std;
int main() {
int n, a, b, c, d[10000];
cin >> n;
cin >> a >> b;
cin >> c;
for(int i = 0; i < n; i++)
cin >> d[i];
int pizza = c;
int best = c / a;
sort(d, d + n, greater<int>());
for(int i = 0; i < n; i++){
pizza += d[i];
best = max(best, pizza / (a + (i + 1) * b));
}
cout << best << endl;
return 0;
} | 0 |
// C - Base -2 Number
#include <bits/stdc++.h>
using namespace std;
int main(){
int N; cin>>N;
string ans = "";
int p = -2;
while(N != 0){
int r = N % p; N /= p;
if(r < 0){ r -= p; N++; }
ans = to_string(r) + ans;
}
if(ans == "") ans = "0";
cout<< ans <<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int cupe = 5;
int main() {
int n, a[cupe], sum = 0, ans = 0;
cin >> n;
for (int i = 1; i < 5; ++i) a[i] = 0;
for (int i = 0; i < n; ++i) {
int temp;
cin >> temp;
sum += temp;
++a[temp];
}
if (sum == 1 || sum == 2 || sum == 5) {
cout << -1;
return 0;
} else {
while (a[1] > 0 || a[2] > 0) {
if (a[2] > 0) {
if (a[1] > 0) {
int x = a[2];
int y = a[1];
if (y >= x) {
a[2] = 0;
a[3] += x;
a[1] -= x;
ans += x;
} else {
a[2] -= y;
a[1] = 0;
a[3] += y;
ans += y;
}
} else {
if (a[2] >= 3) {
int k = a[2] / 3;
a[3] += 2 * k;
a[2] -= 3 * k;
ans += 2 * k;
} else {
if (a[2] == 2) {
a[2] = 0;
ans += 2;
a[4]++;
}
if (a[2] == 1 && a[4] > 0) {
ans += 1;
a[2] = 0;
a[4]--;
}
if (a[2] == 1 && a[4] == 0) {
a[2] = 0;
ans += 2;
}
}
}
} else {
if (a[1] >= 3) {
int k = a[1] / 3;
a[3] += 2 * k;
a[1] -= 3 * k;
ans += 2 * k;
} else {
if (a[1] == 2) {
ans += a[1];
a[1] = 0;
} else {
if (a[3] > 0) {
a[1] = 0;
ans++;
} else {
a[1] = 0;
ans += 2;
}
}
}
}
}
}
cout << ans;
return 0;
}
| 5 |
#include <iostream>
using namespace std;
const int SIZE = 90;
int board[SIZE][SIZE];
int n,m;
int dfs(int y, int x){
int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
int nx, ny, max = 0, tmp;
board[y][x] = 0;
for(int i = 0;i < 4;++i){
nx = x + dx[i];
ny = y + dy[i];
if (0 <= nx && 0 <= ny && nx < m && ny < n && board[ny][nx]) {
tmp = dfs(ny, nx);
max = tmp > max ? tmp : max;
}
}
board[y][x] = 1;
return max + 1;
}
int main(){
int max, buf;
while(cin >> m >> n, m != 0, n != 0){
max = 0;
for(int i = 0;i < n;++i){
for(int j = 0;j < m;++j){
cin >> board[i][j];
}
}
for(int i = 0;i < n;++i){
for(int j = 0;j < m;++j){
if(board[i][j]) {
buf = dfs(i, j);
max = buf > max ? buf : max;
}
}
}
cout << max << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, -1, 1, 0};
int dy[4] = {1, 0, 0, -1};
char let[4] = {'D', 'L', 'R', 'U'};
int g[1010][1010];
int dist[1010][1010];
int used[1010][1010];
int pr[1010][1010];
vector<char> ans;
queue<pair<int, int> > q;
pair<int, int> start;
char second[1010];
int n, m, k;
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
cin >> second;
for (int j = 0; j < m; j++) {
pr[i][j] = 100;
dist[i][j] = 1e9;
if (second[j] == '*')
g[i][j] = -1;
else if (second[j] == '.')
g[i][j] = 0;
else {
start = make_pair(i, j);
g[i][j] = 1;
}
}
}
bool pos = false;
pair<int, int> curv = start;
for (int i = 0; i < 4; i++) {
if (curv.first + dy[i] >= 0 && curv.first + dy[i] < n &&
curv.second + dx[i] >= 0 && curv.second + dx[i] < m &&
g[curv.first + dy[i]][curv.second + dx[i]] != -1)
pos = true;
}
if (k % 2 != 0 || !pos) {
cout << "IMPOSSIBLE" << endl;
return 0;
}
q.push(start);
used[start.first][start.second] = true;
dist[start.first][start.second] = 0;
while (!q.empty()) {
curv = q.front();
for (int i = 0; i < 4; i++)
if (curv.first + dy[i] >= 0 && curv.first + dy[i] < n &&
curv.second + dx[i] >= 0 && curv.second + dx[i] < m &&
g[curv.first + dy[i]][curv.second + dx[i]] != -1) {
if (dist[curv.first][curv.second] + 1 <=
dist[curv.first + dy[i]][curv.second + dx[i]])
pr[curv.first + dy[i]][curv.second + dx[i]] =
min(pr[curv.first + dy[i]][curv.second + dx[i]], 3 - i);
if (!used[curv.first + dy[i]][curv.second + dx[i]]) {
q.push(make_pair(curv.first + dy[i], curv.second + dx[i]));
used[curv.first + dy[i]][curv.second + dx[i]] = true;
dist[curv.first + dy[i]][curv.second + dx[i]] =
dist[curv.first][curv.second] + 1;
}
}
q.pop();
}
curv = start;
while (dist[curv.first][curv.second] < k) {
for (int i = 0; i < 4; i++) {
if (curv.first + dy[i] >= 0 && curv.first + dy[i] < n &&
curv.second + dx[i] >= 0 && curv.second + dx[i] < m &&
g[curv.first + dy[i]][curv.second + dx[i]] != -1) {
ans.push_back(let[i]);
curv = make_pair(curv.first + dy[i], curv.second + dx[i]);
k--;
break;
}
}
}
while (k != 0) {
int dir = pr[curv.first][curv.second];
ans.push_back(let[dir]);
curv = make_pair(curv.first + dy[dir], curv.second + dx[dir]);
k--;
}
for (int i = 0; i < ans.size(); i++) cout << ans[i];
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 3000000;
int highest[MAXN];
int bids[MAXN];
vector<int> bidded[MAXN];
vector<int> V;
int bid[MAXN];
set<int> ppl;
set<pair<int, int> > active_ppl;
int calculate(int person, int lo, int hi) {
int q = upper_bound(bidded[person].begin(), bidded[person].end(), hi) -
bidded[person].begin();
int p = lower_bound(bidded[person].begin(), bidded[person].end(), lo) -
bidded[person].begin();
q--;
return (q - p + 1);
}
bool ok(int winner, int pos) {
int from = bidded[winner][pos] + 1;
int to = bidded[winner].back() - 1;
if (from > to) return true;
int sz = to - from + 1;
int amount = calculate(winner, from, to);
for (int i = 0; i < V.size(); i++) {
amount += calculate(V[i], from, to);
}
if (sz == amount) return true;
return false;
}
int bs(int winner) {
int lo = 0, hi = bidded[winner].size() - 2;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
if (mid >= bidded[winner].size()) return bidded[winner].back();
if (ok(winner, mid))
hi = mid - 1;
else
lo = mid;
}
if (!ok(winner, lo)) lo++;
if (lo >= bidded[winner].size()) return bidded[winner].back();
return bidded[winner][lo];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int person;
cin >> person >> bid[i];
ppl.insert(person);
highest[person] = bid[i];
bidded[person].push_back(i);
}
for (set<int>::iterator it = ppl.begin(); it != ppl.end(); it++) {
active_ppl.insert(make_pair(highest[*it], *it));
}
int q;
cin >> q;
int que = 0;
while (q--) {
int amount;
cin >> amount;
V.clear();
while (amount--) {
int v;
cin >> v;
V.push_back(v);
active_ppl.erase(make_pair(highest[v], v));
}
if (active_ppl.empty()) {
cout << 0 << " " << 0 << '\n';
} else {
pair<int, int> xwinner = *active_ppl.rbegin();
int winner = xwinner.second;
amount = bs(winner);
cout << winner << " " << bid[amount] << '\n';
}
for (int i = 0; i < V.size(); i++) {
if (highest[V[i]]) active_ppl.insert(make_pair(highest[V[i]], V[i]));
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long N, K;
const long long MD = 998244353;
class CombinatoricHelper {
private:
long long MD;
public:
int lim;
long long quickPower(long long base, long long pw) {
if (pw == 0) return 1;
if (pw == 1) return base;
long long ret = quickPower(base, pw / 2);
ret = (ret * ret) % MD;
if (pw % 2 == 0)
return ret;
else
return (ret * base) % MD;
}
long long getInverse(long long a) { return quickPower(a, MD - 2); }
private:
vector<long long> fact;
vector<long long> rfact;
public:
long long getSelection(int n, int k) {
if (k > n) return 0;
if (k == 0) return 1;
if (k == n) return 1;
long long ret = fact[n];
ret *= rfact[n - k];
ret %= MD;
ret *= rfact[k];
ret %= MD;
return ret;
}
public:
void preProcess(int desiredLimit, long long mod) {
MD = mod;
lim = desiredLimit;
fact = vector<long long>(lim + 7);
rfact = vector<long long>(lim + 7);
fact[0] = 1;
for (int i = 1; i <= lim; i++) fact[i] = (fact[i - 1] * i) % MD;
rfact[lim] = getInverse(fact[lim]);
for (int i = lim - 1; i >= 0; i--) rfact[i] = (rfact[i + 1] * (i + 1)) % MD;
}
void preProcess(int desiredLimit) { preProcess(desiredLimit, 998244353); }
long long getFact(int n) { return fact[n]; }
} chelper;
int testRun() {
int n = 4;
int cnt[n + 1];
for (int i = 1; i <= n; i++) cnt[i] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
for (int l = 0; l < n; l++) {
set<int> q;
q.insert(i);
q.insert(j);
q.insert(k);
q.insert(l);
cnt[q.size()]++;
}
for (int i = 1; i <= n; i++) printf("%d elements: %d ways\n", i, cnt[i]);
return 0;
}
long long solve(long long n, long long k) {
long long ret = 0;
for (int j = 0; j <= k; j++) {
long long thisRound = 1;
if ((k - j) % 2 == 1) thisRound = -1;
thisRound *= chelper.getSelection(k, j);
thisRound %= MD;
thisRound *= chelper.quickPower(j, n);
thisRound %= MD;
ret += thisRound;
ret %= MD;
}
ret *= chelper.getSelection(n, k);
ret %= MD;
return ret;
}
int main() {
chelper.preProcess(300007, MD);
scanf("%lld%lld", &N, &K);
if (K >= N) {
printf("0\n");
return 0;
}
if (K == 0) {
long long ans = chelper.getFact(N);
printf("%lld\n", ans % MD);
return 0;
}
long long ans = solve(N, N - K) % MD;
ans *= 2;
ans %= MD;
if (ans < 0) ans += MD;
printf("%lld\n", ans % MD);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, x, a[100010], m = 1e12, h, v, k;
int main() {
cin >> n >> x;
for (int i = 1; i <= n; i++) {
cin >> a[i];
m = min(m, a[i]);
}
for (int i = x; i >= 1; i--) {
if (a[i] == m) {
k = a[i];
a[i] = v;
h = i;
break;
} else
a[i]--;
v++;
if (i == 1) i = n + 1;
}
for (int i = 1; i <= n; i++) {
if (i == h)
cout << a[i] + k * n << " ";
else
cout << a[i] - k << " ";
}
return 0;
}
| 3 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long Int;
#define rep(i, n) for(int i=0; i<n; ++i)
#define srt(A) sort(A.begin(), A.end(), greater<Int>())
int main(){
Int X, Y, Z, K;
cin >> X >> Y >> Z >> K;
vector<Int> A(X), B(Y), C(Z), res;
rep(i, X) cin >> A[i]; srt(A);
rep(i, Y) cin >> B[i]; srt(B);
rep(i, Z) cin >> C[i]; srt(C);
rep(i, X){
rep(j, Y){
rep(k, Z){
if ((i+1)*(j+1)*(k+1) <= K) res.push_back(A[i]+B[j]+C[k]);
else break;
}
}
}
srt(res);
rep(i,K)
cout << res[i] << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int64_t N = 2e5 + 5;
const int64_t INF = 1e18 + 7;
const int64_t MOD = 1e9 + 7;
int64_t n, m;
int64_t dp[N];
bool good[N];
vector<pair<int64_t, int64_t>> v;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int64_t i = 0; i < n; i++) {
int64_t a, b;
cin >> a >> b;
for (int64_t j = max((int64_t)1, a - b); j <= min(m, a + b); j++)
good[j] = true;
v.push_back({a, b});
}
for (int64_t i = 1; i <= m; i++) dp[i] = dp[i - 1] + (!good[i]);
for (int64_t i = 0; i <= m; i++) {
if (good[i]) dp[i] = min(dp[i], dp[i - 1]);
for (auto x : v) {
int64_t len = x.first - i - 1;
int64_t cost = max((int64_t)0, len - x.second);
int64_t pos = min(m, (int64_t)len + x.first);
pos = max((int64_t)0, pos);
dp[pos] = min(dp[pos], dp[i] + cost);
}
}
int64_t ans = m;
for (int64_t i = 0; i <= m; i++) ans = min(ans, dp[i] + m - i);
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long test, ans[3][200005] = {0};
cin >> test;
while (test--) {
long long n, a, b, i, j, k, wan = 1, tw = 2;
string s;
cin >> n >> a >> b;
cin >> s;
ans[1][0] = b;
ans[2][0] = 1e18;
for (i = 1; i <= n; i++) {
if ((i == n) || ((s[i - 1] == '0') && (s[i] == '0'))) {
if (ans[1][i - 1] != (-1))
ans[1][i] = a + b + ans[1][i - 1];
else
ans[1][i] = 1e18;
ans[1][i] = min(ans[1][i], ans[2][i - 1] + b + (tw * a));
if (ans[1][i - 1] != (-1))
ans[2][i] = ans[1][i - 1] + (tw * a) + (tw * b);
else
ans[2][i] = 1e18;
ans[2][i] = min(ans[2][i], ans[2][i - 1] + a + (tw * b));
} else if ((s[i - 1] == '0') && (s[i] == '1')) {
ans[1][i] = -1;
if (ans[1][i - 1] != (-1))
ans[2][i] = ans[1][i - 1] + (tw * a) + (tw * b);
else
ans[2][i] = 1e18;
ans[2][i] = min(ans[2][i], ans[2][i - 1] + a + (tw * b));
} else if ((s[i - 1] == '1') && (s[i] == '0')) {
ans[1][i] = -1;
ans[2][i] = ans[2][i - 1] + a + (2 * b);
} else if ((s[i - 1] == '1') && (s[i] == '1')) {
ans[1][i] = -1;
ans[2][i] = ans[2][i - 1] + a + (tw * b);
}
}
cout << ans[1][n] << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int T, n;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
int resp = (n / 7) + (n % 7 == 0 ? 0 : 1);
printf("%d\n", resp);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<S<<endl
using namespace std;
bool isPrime(int x){
int d=5;
for(int i=6;i<x;i+=d){
d=7-d;
if(x%i==0){
return false;
}
}
return true;
}
int main(){
int N;
while(cin>>N&&N!=1){
int d=5;
cout<<N<<":";
for(int i=6;i<=N;i+=d){
d=7-d;
if(N%i==0){
if(isPrime(i)) cout<<" "<<i;
}
}
cout<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
const char dl = '\n';
const long double eps = 0.00000001;
const long long MOD = 1e9 + 7;
const double PI = 3.141592653589793238463;
using namespace std;
void debug() { cout << endl; }
template <typename H, typename... T>
void debug(H p, T... t) {
std::cout << p << " ";
debug(t...);
}
string second, t;
long long ans;
int n, m, k;
int sum[300005];
int main() {
fflush(stdin);
cout << fixed, cout.precision(18);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, j;
cin >> second;
int pos = second.find('^');
long long power1 = 0, power2 = 0;
for (i = 0; i < pos; ++i) {
if (second[i] >= '0' && second[i] <= '9') {
power1 += (pos - i) * 1ll * (second[i] - '0');
}
}
for (i = pos + 1; i < (int)second.size(); ++i) {
if (second[i] >= '0' && second[i] <= '9') {
power2 += (i - pos) * 1ll * (second[i] - '0');
}
}
if (power1 == power2)
cout << "balance";
else if (power1 > power2)
cout << "left";
else
cout << "right";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 21;
int tests, current_case;
int n, m, k, a[N];
pair<int, int> b[N];
int arr[N << 1];
void message(int current_case) {
fprintf(stderr, "Case %d solved!\n", current_case);
}
bool check(int cnt) {
int i, j, days = 0, sz = 0, last;
i = 1;
j = m - cnt + 1;
while (i <= n && j <= m) {
if (a[i] <= b[j].first)
arr[++sz] = a[i++];
else
arr[++sz] = b[j++].first;
}
while (i <= n) arr[++sz] = a[i++];
while (j <= m) arr[++sz] = b[j++].first;
sort(arr + 1, arr + 1 + sz);
for (i = 1; i <= sz; i += k) {
last = min(sz, i + k - 1);
if (arr[i] < days) return false;
++days;
}
return true;
}
int main() {
int i, left, right, middle;
tests = 1;
for (current_case = 1; current_case <= tests; current_case++) {
scanf("%d %d %d", &n, &m, &k);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (i = 1; i <= m; i++) {
scanf("%d", &b[i].first);
b[i].second = i;
}
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + m);
if (!check(0)) {
printf("-1\n");
goto MESSAGE;
}
left = 0;
right = m + 1;
while (right - left > 1) {
middle = (left + right) >> 1;
if (check(middle))
left = middle;
else
right = middle;
}
printf("%d\n", left);
if (left > 0) {
for (i = m; i >= m - left + 1; i--) {
if (i < m) printf(" ");
printf("%d", b[i].second);
}
printf("\n");
}
MESSAGE:
message(current_case);
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,x,n) for (int i = x;i < n;++i)
const int md = 1e9 + 7;
int p[200001];
int pw(int x, int p) {
if (!p)return 1;
int t = pw(x, p >> 1);
t = (ll)t*t%md;
if (p & 1)t = (ll)t*x%md;
return t;
}
int ch(int n, int r) { return (ll)p[n] * pw(p[n - r], md - 2) % md*pw(p[r], md - 2) % md; }
int main() {
p[0] = p[1] = 1;
f(i, 2, 200001)p[i] = (ll)p[i - 1] * i%md;
int h, w, a, b;
cin >> h >> w >> a >> b;
int an = 0;
int x = h - a, y = w - b;
f(i, 0, y) {
an += (ll)ch(b + i + x - 1, x - 1)*ch(a + y - i - 2, y - i - 1) % md;
if (an >= md)an -= md;
}
printf("%d\n", an);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, sm[5201][5201];
string s;
int to_digit(char ch) {
if (isdigit(ch)) {
return ch - '0';
}
return ch - 'A' + 10;
}
vector<long long> get_factor(long long x) {
vector<long long> ret;
for (long long i = 1; i * i <= x; i++) {
if (x % i == 0) {
ret.push_back(i);
ret.push_back(x / i);
}
}
return ret;
}
string hex_to_bin(string s) {
string out;
for (auto i : s) {
uint8_t n;
if (i <= '9' and i >= '0')
n = i - '0';
else
n = 10 + i - 'A';
for (int8_t j = 3; j >= 0; --j) out.push_back((n & (1 << j)) ? '1' : '0');
}
return out;
}
int get_sum(long long x, long long y, long long len) {
return sm[x][y] - sm[x - len][y] - sm[x][y - len] + sm[x - len][y - len];
}
void solve(long long x) {
long long cnt = 0;
for (long long i = x; i <= n; i += x) {
for (long long j = x; j <= n; j += x) {
long long sum = get_sum(i, j, x);
if (sum == 0 || sum == x * x) {
cnt++;
}
}
}
if (cnt == (n / x) * (n / x)) {
cout << x;
exit(0);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s;
s = hex_to_bin(s);
long long cnt = 0;
for (int j = 1; j <= n; j++) {
cnt += (s[j - 1] == '1');
sm[i][j] = sm[i - 1][j] + cnt;
}
}
vector<long long> v = get_factor(n);
sort(v.begin(), v.end(), greater<long long>());
for (long long x : v) {
solve(x);
}
cout << 1;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 10, C = 500;
int n, state[N], lion[N];
vector<int> adj[N], headj[N];
map<int, int> edgeIdx[N];
void add_edge(int u, int v) {
adj[u].push_back(v);
edgeIdx[u][v] = adj[u].size() - 1;
if (((int)(adj[u]).size()) < C)
lion[v] += state[u];
else if (((int)(adj[u]).size()) == C) {
for (int w : adj[u]) {
if (w > 0) {
if (w != v) lion[w] -= state[u];
headj[w].push_back(u);
} else
headj[-w].push_back(-u);
}
} else
headj[v].push_back(u);
}
void modify_edge(int u, int v) {
int idx = edgeIdx[u][v];
adj[u][idx] *= -1;
if (((int)(adj[u]).size()) < C && state[u])
lion[v] += (adj[u][idx] > 0 ? 1 : -1);
else {
for (int i = 0; i < ((int)(headj[v]).size()); i++)
if (headj[v][i] == u || headj[v][i] == -u) {
headj[v][i] *= -1;
return;
}
}
}
int main() {
int m, q, o, x;
scanf("%d%d%d", &n, &m, &q);
for (scanf("%d", &o); o--;) {
scanf("%d", &x);
state[x] = 1;
}
int u, v;
for (int i = (int)1; i <= m; i++) {
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
char cmd;
int ans = 0;
while (q--) {
scanf(" %c", &cmd);
if (cmd == 'O' || cmd == 'F') {
scanf("%d", &u);
state[u] = (cmd == 'O');
if (((int)(adj[u]).size()) < C)
for (int v : adj[u])
if (v > 0) lion[v] += (state[u] ? 1 : -1);
} else if (cmd == 'A') {
scanf("%d%d", &u, &v);
if (edgeIdx[u].count(v) == 0)
add_edge(u, v), add_edge(v, u);
else
modify_edge(u, v), modify_edge(v, u);
} else if (cmd == 'D') {
scanf("%d%d", &u, &v);
modify_edge(u, v);
modify_edge(v, u);
} else {
scanf("%d", &u);
ans = lion[u];
for (int w : headj[u])
if (w > 0) ans += state[w];
printf("%d\n", ans);
}
}
return 0;
}
| 4 |
#include <iostream>
#include <algorithm>
using namespace std;
int main(void){
int n;
cin>>n;
cout<<(n<1200?"ABC":"ARC");
}
| 0 |
#include <bits/stdc++.h>
int cmp(const void *a, const void *b) { return (*(int *)a) - (*(int *)b); }
int cmp1(const void *a, const void *b) {
return (((int *)a)[0]) - (((int *)b)[0]);
}
int main() {
double H, L;
scanf("%lf %lf", &H, &L);
double ans = (L * L - H * H) / (2.0 * H);
printf("%.8lf\n", ans);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
return a < b ? a : b;
}
namespace ae86 {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
if (s == t) {
t = (s = buf) + fread(buf, 1, bufl, stdin);
if (s == t) return EOF;
}
return *s++;
}
inline int ty() {
int a = 0, b = 1, c = fetch();
while (!isdigit(c)) b ^= c == '-', c = fetch();
while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
return b ? a : -a;
}
} // namespace ae86
using ae86::ty;
const int _ = 500007, inf = 0x3f3f3f3f;
struct sgt {
static const int _t = _ << 2;
int mival[_t], mxval[_t], tchan[_t];
long long len[_t], sval[_t];
inline void up(int x) {
sval[x] = sval[(x << 1)] + sval[(x << 1 | 1)];
mival[x] = min(mival[(x << 1)], mival[(x << 1 | 1)]);
mxval[x] = max(mxval[(x << 1)], mxval[(x << 1 | 1)]);
}
inline void putchan(int x, int dlt) {
sval[x] = 1ll * dlt * len[x];
mival[x] = mxval[x] = dlt;
tchan[x] = dlt;
}
inline void dwn(int x) {
if (tchan[x] >= 0)
putchan((x << 1), tchan[x]), putchan((x << 1 | 1), tchan[x]),
tchan[x] = -inf;
}
inline void plant(int x, int l, int r) {
len[x] = r - l + 1, tchan[x] = -inf;
if (l == r) {
sval[x] = mival[x] = mxval[x] = l;
return;
}
plant((x << 1), l, ((l + r) >> 1)),
plant((x << 1 | 1), ((l + r) >> 1) + 1, r), up(x);
}
inline void change(int x, int l, int r, int tl, int tr, int dlt) {
if (tl > tr || l > r || mival[x] >= dlt) return;
if (tl <= l && r <= tr && mxval[x] <= dlt) {
putchan(x, dlt);
return;
}
if (l == r) {
sval[x] = mival[x] = mxval[x] = dlt;
return;
}
dwn(x);
if (tl <= ((l + r) >> 1)) change((x << 1), l, ((l + r) >> 1), tl, tr, dlt);
if (tr > ((l + r) >> 1))
change((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr, dlt);
up(x);
}
inline int findval(int x, int l, int r, int tar) {
if (l == r) return sval[x];
dwn(x);
if (tar <= ((l + r) >> 1)) return findval((x << 1), l, ((l + r) >> 1), tar);
return findval((x << 1 | 1), ((l + r) >> 1) + 1, r, tar);
}
inline long long finder() { return sval[1]; }
sgt() {
memset(sval, 0, sizeof(sval));
memset(mival, 0, sizeof(mival));
memset(mxval, 0, sizeof(mxval));
memset(tchan, -64, sizeof(tchan));
}
} t;
int n, val[_], mxval = 0;
vector<int> loca[_], zsy[_];
long long ans[_];
int main() {
n = ty();
for (int i = 1; i <= n; i++)
val[i] = ty(), mxval = max(mxval, val[i]), loca[val[i]].emplace_back(i);
for (int i = 1; i <= mxval; i++) {
for (int j = i; j <= mxval; j += i) {
if (loca[j].size() <= 4)
for (auto k : loca[j]) zsy[i].emplace_back(k);
else {
zsy[i].emplace_back(loca[j][0]);
zsy[i].emplace_back(loca[j][1]);
zsy[i].emplace_back(loca[j][((loca[j]).size()) - 1]);
zsy[i].emplace_back(loca[j][((loca[j]).size()) - 2]);
}
}
sort(zsy[i].begin(), zsy[i].end());
}
t.plant(1, 1, n);
for (int i = mxval; i >= 0; i--) {
ans[i] = 1ll * n * (1ll + n) - t.finder();
int sz = ((zsy[i]).size());
if (sz <= 1) continue;
t.change(1, 1, n, 1, zsy[i][0], zsy[i][sz - 2]);
t.change(1, 1, n, zsy[i][0] + 1, zsy[i][1], zsy[i][sz - 1]);
t.change(1, 1, n, zsy[i][1] + 1, n, n + 1);
}
long long sans = 0;
for (int i = 1; i <= mxval; i++) sans += (ans[i] - ans[i - 1]) * i;
printf("%lld\n", sans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int max_digit(long long n) {
int large = 0, rem = 0;
while (n > 0) {
rem = n % 10;
n = n / 10;
if (rem > large) {
large = rem;
}
}
return large;
}
int main() {
long long n;
cin >> n;
long long i = 0;
while (n != 0) {
n = n - max_digit(n);
i++;
}
cout << i;
}
| 3 |
#include<cstdio>
#include<queue>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e5+5;
int n,nx,ny,nz,i,v[N];
ll sum,fx[N],fy[N],ans;
struct arr{int k,x,y;}a[N];
bool operator < (arr A,arr B){return A.k>B.k;}
int read(){
char c=getchar();int k=0;for (;c<48||c>57;c=getchar());
for (;c>47&&c<58;c=getchar()) k=(k<<3)+(k<<1)+c-48;return k;
}
int l[N],r[N];
struct drr{int x,k;}d[N];
bool operator < (drr A,drr B){return A.k>B.k;}
void solve(ll *f,int m){
for (i=1;i<=n;i++) d[i]=(drr){i,v[i]};
sort(d+1,d+n+1);ll res=0;int x=d[m].x;
for (i=1;i<=n;i++)
l[d[i].x]=d[i-1].x,r[d[i].x]=d[i+1].x;
for (i=1;i<=m;i++) res+=d[i].k;
for (f[n]=res,i=n;i>m;i--){
if (v[i]>=v[x])
x=r[x],res+=v[x]-v[i];
r[l[i]]=r[i];l[r[i]]=l[i];
f[i-1]=res;
}
}
int main(){
nx=read();ny=read();nz=read();n=nx+ny+nz;
for (i=1;i<=n;i++){
int x=read(),y=read(),z=read();
a[i]=(arr){x-y,x-z,y-z};sum+=z;
}
sort(a+1,a+n+1);
for (i=1;i<=n;i++) v[i]=a[i].x;
solve(fx,nx);
for (i=1;i<=n;i++) v[n-i+1]=a[i].y;
solve(fy,ny);
for (i=nx;i<=nx+nz;i++)
ans=max(ans,sum+fx[i]+fy[n-i]);
printf("%lld",ans);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ui64 = unsigned long long;
using i64 = long long;
using ui32 = unsigned int;
using Double = long double;
template <class T>
inline T Abs(const T& x) {
return x < 0 ? -x : x;
}
template <class T>
inline T Sqr(const T& x) {
return x * x;
}
template <class T>
void Print(const T& x);
template <>
void Print<ui64>(const ui64& x) {
printf("%" PRIu64, x);
}
template <>
void Print<i64>(const i64& x) {
printf("%" PRId64, x);
}
template <>
void Print<ui32>(const ui32& x) {
printf("%" PRIu32, x);
}
template <>
void Print<int>(const int& x) {
printf("%" PRId32, x);
}
template <>
void Print<float>(const float& x) {
printf("%.15f", x);
}
template <>
void Print<double>(const double& x) {
printf("%.15lf", x);
}
template <>
void Print<Double>(const Double& x) {
Print<double>(x);
}
template <>
void Print<string>(const string& s) {
printf("%s", s.c_str());
}
template <class T>
T Read();
template <>
ui64 Read<ui64>() {
ui64 x;
scanf("%" SCNu64, &x);
return x;
}
template <>
i64 Read<i64>() {
i64 x;
scanf("%" SCNd64, &x);
return x;
}
template <>
ui32 Read<ui32>() {
ui32 x;
scanf("%" SCNu32, &x);
return x;
}
template <>
int Read<int>() {
int x;
scanf("%" SCNd32, &x);
return x;
}
template <>
float Read<float>() {
float x;
scanf("%f", &x);
return x;
}
template <>
double Read<double>() {
double x;
scanf("%lf", &x);
return x;
}
template <>
Double Read<Double>() {
return Read<double>();
}
template <>
string Read<string>() {
static char buf[int(1e7 + 1)];
scanf("%s", buf);
return string(buf);
}
template <class T>
void ReadLine(T& x);
template <class T, class... Args>
void ReadLine(T& x, Args&... args);
template <class T>
void PrintLine(const T& x);
template <class T, class... Args>
void PrintLine(const T& x, Args... args);
template <class T>
void PrintVector(const vector<T>& x, char delimiter = ' ');
template <class T>
vector<T> ReadVector(size_t size);
vector<vector<int>> e, re;
vector<int> used;
vector<int> order;
vector<int> color;
vector<int> sccSize;
vector<int> minCycle;
void Dfs(int u) {
used[u] = 1;
for (int v : e[u]) {
if (!used[v]) {
Dfs(v);
}
}
order.push_back(u);
}
void DfsReverse(int u, int c) {
++sccSize[c];
color[u] = c;
for (int v : re[u]) {
if (!color[v]) {
DfsReverse(v, c);
}
}
}
int main() {
int n = Read<int>();
int m = Read<int>();
e.resize(n);
re.resize(n);
used.resize(n);
for (int i = 0; i < m; ++i) {
int u = Read<int>() - 1;
int v = Read<int>() - 1;
e[u].push_back(v);
re[v].push_back(u);
}
for (int i = 0; i < n; ++i) {
if (!used[i]) {
Dfs(i);
}
}
int colorCount = 0;
reverse(order.begin(), order.end());
color.resize(n + 1);
sccSize.resize(n + 1);
for (int u : order) {
if (!color[u]) {
++colorCount;
DfsReverse(u, colorCount);
}
}
minCycle.resize(n + 1, n + 1);
for (int from = 0; from < n; ++from) {
queue<int> q;
used.assign(n, 0);
q.push(from);
used[from] = 1;
vector<int> dist(n, 0);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : e[u]) {
if (!used[v]) {
used[v] = 1;
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
for (int to = 0; to < n; ++to) {
bool edgeToFrom = false;
for (int v : e[to]) {
if (v == from) {
edgeToFrom = true;
break;
}
}
if (edgeToFrom && color[to] == color[from] &&
minCycle[color[from]] > dist[to] + 1) {
minCycle[color[from]] = dist[to] + 1;
}
}
}
vector<int> outEdgeCount(n + 1);
for (int u = 0; u < n; ++u) {
for (int v : e[u]) {
if (color[u] != color[v]) {
++outEdgeCount[color[u]];
}
}
}
int ans = 0;
for (int i = 1; i <= colorCount; ++i) {
if (outEdgeCount[i]) {
ans += sccSize[i];
} else {
if (sccSize[i] == 1) {
ans += 1;
} else if (sccSize[i]) {
ans += sccSize[i] - minCycle[i];
ans += 999 * minCycle[i] + 1;
}
}
}
PrintLine(ans);
return 0;
}
template <class T>
void ReadLine(T& x) {
x = Read<T>();
}
template <class T, class... Args>
void ReadLine(T& x, Args&... args) {
x = Read<T>();
ReadLine(args...);
}
template <class T>
void PrintLine(const T& x) {
Print(x);
printf("\n");
}
template <class T, class... Args>
void PrintLine(const T& x, Args... args) {
Print(x);
printf(" ");
PrintLine(args...);
}
template <class T>
void PrintVector(const vector<T>& x, char delimiter) {
for (size_t i = 0; i < x.size(); ++i) {
Print(x[i]);
if (i + 1 < x.size()) {
printf("%c", delimiter);
}
}
printf("\n");
}
template <class T>
vector<T> ReadVector(size_t size) {
vector<T> res(size);
for (size_t i = 0; i < size; ++i) {
res[i] = Read<T>();
}
return res;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename X>
inline X sqr(const X& a) {
return a * a;
}
int nxt() {
int x;
cin >> x;
return x;
}
int main() {
int n = nxt(), m = nxt();
vector<int> a(n);
generate(a.begin(), a.end(), nxt);
while (m--) {
int l = nxt() - 1, r = nxt(), x = nxt() - 1;
int cnt = 0;
for (int i = int(l); i < int(r); ++i) {
cnt += a[i] < a[x];
}
cout << (x == l + cnt ? "Yes" : "No") << '\n';
}
return 0;
}
| 2 |
Subsets and Splits