solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1000005;
const long long M = 28;
long long n, a[N], b[N], ans, tans;
signed main() {
ios::sync_with_stdio(false);
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long k = 0; k < M; k++) {
for (long long i = 1; i <= n; i++) {
b[i] = a[i] % (1 << (k + 1));
}
tans = 0;
sort(b + 1, b + n + 1);
for (long long i = 1; i <= n; i++) {
tans += lower_bound(b + 1, b + n + 1, (1 << (k + 1)) - b[i]) -
lower_bound(b + 1, b + n + 1, (1 << (k + 0)) - b[i]);
tans +=
lower_bound(b + 1, b + n + 1, (1 << (k + 2)) - b[i]) -
lower_bound(b + 1, b + n + 1, (1 << (k + 1)) + (1 << (k + 0)) - b[i]);
}
for (long long i = 1; i <= n; i++)
if ((a[i] * 2) & (1 << (k))) --tans;
tans /= 2;
if (tans & 1) ans += (1 << (k));
}
cout << ans;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
const int M=998244353;
const long long inf=101010101010101ll;
const int N=1000005;
int n,i,j,k,f[N],m;
int g[1005][1005],cnt[N],siz[N];
struct str{
int u,v,w;
}e[N];
vector<int> t[N],p;
bool cmp(str a,str b)
{
return a.w<b.w;
}
int Find(int x)
{
return f[x]?f[x]=Find(f[x]):x;
}
void uniot(int x,int y)
{
int u=Find(x),v=Find(y);
if(u!=v)
{
f[u]=v;
siz[v]+=siz[u];
}
}
long long ans;
void cal3(int u,int v,int w)
{
int i,j,x;
long long s=0;
for(i=1;i<=m;++i)
if(w<e[i].w)
break;
x=i;
for(i=m+1;i>x;--i)
e[i]=e[i-1];
e[i]={u,v,w};
for(i=1;i<=n;++i)
f[i]=0;
for(i=1;i<=n;++i)
for(j=i+1;j<=n;++j)
if(!g[i][j]&&(i!=u||j!=v))
uniot(i,j);
for(i=1;i<=m+1;++i)
{
int x=Find(e[i].u),y=Find(e[i].v);
if(x!=y)
{
f[x]=y;
s+=e[i].w;
}
}
for(i=x;i<=m;++i)
e[i]=e[i+1];
ans=min(ans,s);
}
void cal()
{
int mn=0;
for(i=1;i<=m;++i)
mn^=e[i].w;
ans=1111111111111111ll;
sort(e+1,e+1+m,cmp);
for(i=1;i<=n;++i)
for(j=i+1;j<=n;++j)
if(g[i][j]==0)
cal3(i,j,mn);
cout<<ans;
}
void cal2()
{
memset(f,0,sizeof(f));
for(i=1;i<=n;++i)
siz[i]=1;
for(i=1;i<=m;++i)
t[max(e[i].u,e[i].v)].push_back(min(e[i].u,e[i].v));
for(i=1;i<=n;++i)
{
for(auto it:t[i])
++cnt[Find(it)];
for(auto it:p)
if(Find(it)==it)
if(cnt[it]!=siz[it])
uniot(i,it);
if(Find(i)==i)
p.push_back(i);
for(auto it:p)
cnt[it]=0;
}
sort(e+1,e+1+m,cmp);
for(i=1;i<=m;++i)
if(Find(e[i].u)!=Find(e[i].v))
{
uniot(e[i].u,e[i].v);
ans+=e[i].w;
}
cout<<ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
scanf("%d %d",&n,&m);
for(i=1;i<=m;++i)
scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);
if(m>=1ll*n*(n-1)/2-n)
{
for(i=1;i<=m;++i)
g[e[i].u][e[i].v]=g[e[i].v][e[i].u]=1;
bool flag=true;
for(i=1;i<=n;++i)
for(j=i+1;j<=n;++j)
if(!g[i][j])
{
int x=Find(i),y=Find(j);
if(x==y)
flag=false;
else
uniot(x,y);
}
if(!flag)
cal2();
else
cal();
}
else
cal2();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double EPS = (1e-7);
const ll INF =(1e9);
const double PI = (acos(-1));
const ll MOD = ll(1e9) + 7;
#define REP(i, n) for(ll i = 0; i < (ll)(n); i++)
#define REPR(i, n) for(ll i = n; i > -1; i--)
#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(), (x).end())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define SZ(x) ((ll)(x).size())
#define pb push_back
#define mp make_pair
//chmax(a, b): a>bならaをbで更新 更新したときにtrueを返す
//chmin(a, b): a<bならaをbで更新 更新したときにtrueを返す
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define dump(x) cerr<< #x << "= " << (x) << endl;
int gcd(int a,int b){return b?gcd(b,a%b):a;};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main(){
//input
ll n; cin >> n;
vector<ll> a(n);
REP(i, n){
cin >> a[i];
}
map<ll, ll> p;
map<ll, ll> q;
ll diff = 0;
REP(i, n){
if (a[i] != a[n - 1 - i]){
diff += 2 * (p[a[i]] - q[a[i]]) - 2 * (p[a[n - 1 - i]] - q[a[n - 1 - i]]) + 2;
}
if (diff == 0){
cout << i + 1;
if (i != n - 1){
cout << ' ';
}
}
p[a[i]] += 1;
q[a[n - 1 - i]] += 1;
}
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, num[200005];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
if (num[i] != 0) num[i] = 1e9 + 5;
}
for (int i = 0; i < n; i++) {
if (num[i] == 0) {
for (int j = i - 1, k = 1; j >= 0; j--, k++) {
if (num[j] == 0) break;
if (num[j] > k) num[j] = k;
}
for (int j = i + 1, k = 1; j < n; j++, k++) {
if (num[j] == 0) break;
num[j] = k;
}
}
}
for (int i = 0; i < n; i++) printf("%d ", num[i]);
printf("\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main() {
int n, i;
float m;
scanf("%d %f", &n, &m);
float mi, c[10000], a, b;
for (i = 0; i < n; i++) {
scanf("%f %f", &a, &b);
c[i] = a / b;
}
mi = c[0];
for (i = 1; i < n; i++) {
if (c[i] < mi) mi = c[i];
}
printf("%.8f", mi * m);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, a[5005], ans[5005], id[5005], tot, book[5005];
signed main() {
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long i = 1; i <= n; i++) {
for (register long long j = 1; j < i; j++) {
long long x = a[i] * a[j];
if (x <= 0) continue;
long long sq = (long long)(sqrt(x));
long long fg = 0;
for (register long long k = max(0ll, sq - 3); k <= sq + 3; k++)
if (k * k == x) fg = 1;
if (fg == 1) {
id[i] = id[j];
break;
}
}
if (id[i] == 0) id[i] = ++tot;
}
for (register long long i = 1; i <= n; i++) {
memset(book, 0, sizeof(book));
register long long num = 0;
for (register long long j = i; j <= n; j++) {
if (a[j] != 0) book[id[j]]++;
if (book[id[j]] == 1) num++;
if (num != 0)
ans[num]++;
else
ans[1]++;
}
}
for (register long long i = 1; i <= n; i++) cout << ans[i] << ' ';
cout << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0;
int ch = getchar(), f = 1;
while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
if (ch == '-') {
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
const int N = 1e5 + 10;
int n, l, r, mid, k, cnt[N];
long long S, tot;
inline long long L(int x) {
long long lim = x, sum = 1, Log = 2, ret = 1;
while (sum + lim < n) sum += lim, ret += Log * lim, Log++, lim *= x;
return ret + Log * (n - sum);
}
inline long long power(long long a, int b) {
long long ret = 1;
for (; b; b >>= 1, a = a * a)
if (b & 1) ret = ret * a;
return ret;
}
int Cnt, fa[N], dep[N];
inline void dfs(int u) {
dep[u] = dep[fa[u]] + 1, cnt[dep[u]]--;
for (register int i = (1); i <= (k); i++)
if (cnt[dep[u] + 1] > 0)
fa[++Cnt] = u, dfs(Cnt);
else
break;
}
int main() {
n = read(), S = read();
if (S < 2 * n - 1 || S > 1ll * n * (n + 1) / 2) return puts("No"), 0;
l = 1, r = n;
while (l <= r) {
mid = l + r >> 1;
if (L(mid) <= S)
k = mid, r = mid - 1;
else
l = mid + 1;
}
if (k == 1 && S != 1ll * (n + 1) * n / 2) return puts("No"), 0;
long long s = 1ll * (n + 1) * n / 2;
r = n, l = 2, tot = k - 1;
for (register int i = (1); i <= (n); i++) cnt[i] = 1;
while (s - (r - l) > S) {
s -= r - l, cnt[r]--, cnt[l]++;
if (cnt[r] == 0) r--;
if (--tot == 0) l++, tot = power(k, l - 1) - 1;
}
int x = s - S;
cnt[r]--, cnt[r - x]++;
Cnt = 1, dfs(1);
puts("Yes");
for (register int i = (2); i <= (n); i++) printf("%d ", fa[i]);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, dx, dy, cnt[1000100];
int main() {
scanf("%d%d%d%d", &n, &m, &dx, &dy);
map<int, int> tocnt;
int tmpx = 0, tmpy = 0;
for (int i = 0; i < n; ++i) {
tocnt[tmpx] = tmpy;
tmpx += dx, tmpy += dy;
tmpx %= n;
tmpy %= n;
}
for (int i = 0; i < m; ++i) {
int appx, appy;
scanf("%d%d", &appx, &appy);
++cnt[(appy + n - tocnt[appx]) % n];
}
int ans = 0, res = 0;
for (int i = 0; i < n; ++i) {
if (cnt[i] > res) {
res = cnt[i];
ans = i;
}
}
printf("%d %d\n", 0, ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void PV(T a, T b) {
for (T i = a; i < b; ++i) cout << *i << " ";
cout << endl;
}
template <class T>
void chmin(T &t, T f) {
if (t > f) t = f;
}
template <class T>
void chmax(T &t, T f) {
if (t < f) t = f;
}
const int inff = 0x3f3f3f3f;
const int infc = 0x3f3f3f3f;
struct MCMF {
int nv, ne, to[100000], next[100000];
int vis[10000], Q[10000], head[10000], prev[10000], pe[10000];
int mflow, flow[100000], cap[100000];
int mcost, cost[100000], d[10000];
int src, sink;
MCMF() {}
MCMF(int n, int s, int t) { init(n, s, t); }
void init(int n, int s, int t) {
nv = n, src = s, sink = t, ne = 0;
mflow = 0, mcost = 0;
memset(head, -1, sizeof(head));
}
void add(int u, int v, int c, int w) {
to[ne] = v, cap[ne] = c, cost[ne] = +w, flow[ne] = 0, next[ne] = head[u],
head[u] = ne++;
to[ne] = u, cap[ne] = 0, cost[ne] = -w, flow[ne] = 0, next[ne] = head[v],
head[v] = ne++;
}
bool spfa() {
for (int i = 0; i < nv; ++i) {
prev[i] = -1, vis[i] = 0, d[i] = infc;
}
d[src] = 0, vis[src] = 1, Q[0] = src;
int f = 0, r = 1;
while (r != f) {
int x = Q[f++];
if (f == 10000) f = 0;
vis[x] = 0;
for (int k = head[x]; k != -1; k = next[k]) {
int y = to[k];
if (flow[k] < cap[k] && d[y] > cost[k] + d[x]) {
d[y] = cost[k] + d[x];
if (!vis[y]) {
vis[y] = 1;
Q[r++] = y;
if (r == 10000)
r = 0;
else if (Q[r - 1] < Q[f])
swap(Q[r - 1], Q[f]);
}
prev[y] = x;
pe[y] = k;
}
}
}
return (-1 != prev[sink]);
}
int minCostmaxFlow() {
while (spfa()) {
int expand = inff;
for (int k = sink; k != src; k = prev[k])
if (cap[pe[k]] < expand) expand = cap[pe[k]];
for (int k = sink; k != src; k = prev[k])
cap[pe[k]] -= expand, cap[pe[k] ^ 1] += expand;
mflow += expand;
mcost += d[sink] * expand;
}
return mcost;
}
} g;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int n, m;
int a[110][110];
int place(int x, int y) { return (x - 1) * m + y; }
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cin >> a[i][j];
int S = n * m + 1;
int T = S + 1;
g.init(T + 1, S, T);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if ((i + j) & 1) {
g.add(S, place(i, j), 1, 0);
for (int k = 0; k < 4; ++k) {
int x = i + dx[k], y = j + dy[k];
if (x >= 1 && x <= n && y >= 1 && y <= m)
g.add(place(i, j), place(x, y), 1, a[i][j] != a[x][y]);
}
} else {
g.add(place(i, j), T, 1, 0);
}
g.minCostmaxFlow();
int flow = g.mflow;
int cost = g.mcost;
cout << cost << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
template <typename T, typename U>
std::istream& operator>>(std::istream& i, pair<T, U>& p) {
i >> p.first >> p.second;
return i;
}
template <typename T>
std::istream& operator>>(std::istream& i, vector<T>& t) {
for (auto& v : t) {
i >> v;
}
return i;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {
o << p.first << ' ' << p.second;
return o;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
if (t.empty()) o << '\n';
for (size_t i = 0; i < t.size(); ++i) {
o << t[i] << " \n"[i == t.size() - 1];
}
return o;
}
template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxheap = priority_queue<T, vector<T>, less<T>>;
template <typename T>
bool in(T a, T b, T c) {
return a <= b && b < c;
}
unsigned int logceil(int x) { return 8 * sizeof(int) - __builtin_clz(x); }
namespace std {
template <typename T, typename U>
struct hash<pair<T, U>> {
hash<T> t;
hash<U> u;
size_t operator()(const pair<T, U>& p) const {
return t(p.x) ^ (u(p.y) << 7);
}
};
} // namespace std
template <typename T, typename F>
T bsh(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
l = m + 1;
r = m;
} else {
h = m - 1;
}
}
return r;
}
template <typename F>
double bshd(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
l = m;
} else {
h = m;
}
}
return (l + h) / 2;
}
template <typename T, typename F>
T bsl(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
h = m - 1;
r = m;
} else {
l = m + 1;
}
}
return r;
}
template <typename F>
double bsld(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
h = m;
} else {
l = m;
}
}
return (l + h) / 2;
}
template <typename T>
T gcd(T a, T b) {
if (a < b) swap(a, b);
return b ? gcd(b, a % b) : a;
}
template <typename T>
class vector2 : public vector<vector<T>> {
public:
vector2() {}
vector2(size_t a, size_t b, T t = T())
: vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T>
class vector3 : public vector<vector2<T>> {
public:
vector3() {}
vector3(size_t a, size_t b, size_t c, T t = T())
: vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
};
template <typename T>
class vector4 : public vector<vector3<T>> {
public:
vector4() {}
vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
: vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
};
template <typename T>
class vector5 : public vector<vector4<T>> {
public:
vector5() {}
vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
: vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
};
void fastIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
inline long long proc(int n, int k) {
long long num = n / k;
long long s = n - k;
return s * num / 2 + num;
}
class CNoviiGodISfericheskayaPeredacha {
public:
void solve(std::istream& in, std::ostream& out) {
fastIO();
int n;
in >> n;
set<long long> ans;
for (int i = 1; i * i <= n; ++i) {
if (n % i != 0) continue;
long long cans = proc(n, i);
ans.insert(cans);
cans = proc(n, n / i);
ans.insert(cans);
}
for (auto x : ans) {
out << x << " ";
}
out << endl;
}
};
int main() {
CNoviiGodISfericheskayaPeredacha solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int size = 2e6 + 7;
int arr[size];
void fill() {
long long a = 1, b = 0, c = 0;
for (int i = 2; i < size; i++) {
b = a;
a += c * 2 + (i % 3 == 1);
c = b;
a %= 1000000007;
arr[i + 1] = (b * 4) % 1000000007;
}
}
int solve() {
int n;
cin >> n;
if (n < 3) return 0;
return arr[n];
}
int main() {
ios_base::sync_with_stdio(0);
fill();
short t;
cin >> t;
while (t--) cout << solve() << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double PI = 2.0 * acos(0.0);
const double eps = 1e-11;
const int _mod = 1000000;
const int save[10][4] = {{0}, {0}, {6, 2, 4, 8}, {1, 3, 9, 7}, {6, 4},
{0}, {0}, {1, 7, 9, 3}, {6, 8, 4, 2}, {1, 9}};
int todigit(char c) { return (isdigit(c)) ? (c - 48) : c; }
int toint(string s) {
int r = 0;
istringstream sin(s);
sin >> r;
return r;
}
long long tolong(string s) {
long long r = 0;
istringstream sin(s);
sin >> r;
return r;
}
double todouble(string s) {
double r = 0.0;
istringstream sin(s);
sin >> r;
return r;
}
template <class T>
string toString(T n) {
ostringstream ost;
ost << n;
ost.flush();
return ost.str();
}
template <class T>
T Abs(T x) {
return x > 0 ? x : -x;
}
template <class T>
T power(T N, T P) {
return (P == 0) ? 1 : N * power(N, P - 1);
}
template <class T>
void max(T &a, T b) {
if (a < b) a = b;
}
template <class T>
void min(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline T gcd(T a, T b) {
if (a < 0) return gcd(-a, b);
if (b < 0) return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
if (a < 0) return lcm(-a, b);
if (b < 0) return lcm(a, -b);
return a * (b / gcd(a, b));
}
int main() {
string str;
int t, sum = 0;
scanf("%d", &t);
while (t--) {
cin >> str;
if (str == "++X" || str == "X++")
++sum;
else
--sum;
}
printf("%d", sum);
return 0;
}
| 1 |
#include<bits/stdc++.h>
typedef long long LL;
#define pb push_back
const LL maxn=1e5+9;
LL Read(){
LL 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<<3ll)+(x<<1ll)+c-'0'; c=getchar();
}return x*f;
}
void Chkmax(LL &x,LL y){
if(y>x) x=y;
}
LL n;
LL a[maxn];
char s[maxn];
namespace sub1{
void Solve(){
for(LL i=1;i<n;++i){
printf("%lld",a[i]); printf("%c",s[1]);
}
printf("%lld\n",a[n]);
return;
}
}
namespace sub2{
void Solve(){
LL flag(0),nw(0);
for(LL i=1;i<=n;++i){
if(!a[i]){
nw=i; flag=1;
break;
}
}
if(!flag){
for(LL i=1;i<n;++i){
printf("%lld*",a[i]);
}
printf("%lld\n",a[n]);
return;
}
if(nw==1){
for(LL i=1;i<n;++i){
printf("%lld*",a[i]);
}
printf("%lld\n",a[n]);
return;
}
for(LL i=1;i<nw-1;++i){
printf("%lld*",a[i]);
}
printf("%lld-",a[nw-1]);
for(LL i=nw;i<n;++i){
printf("%lld*",a[i]);
}
printf("%lld\n",a[n]);
return;
}
}
namespace sub3{
LL pre[maxn],f[maxn];
std::vector<char> Ans;
void Dfs(LL x,LL l){
if(pre[x]!=l-1){
Dfs(pre[x],l);
Ans.pb('+');
for(LL i=pre[x]+1;i<x;++i){
Ans.pb(a[i]+'0');
Ans.pb('*');
}
Ans.pb(a[x]+'0');
}else{
for(LL i=pre[x]+1;i<x;++i){
Ans.pb(a[i]+'0');
Ans.pb('*');
}
Ans.pb(a[x]+'0');
}
}
void Dp(LL l,LL r){
{
LL ret(1);
for(LL i=l;i<=r;++i){
ret=ret*a[i];
f[i]=ret; pre[i]=l-1;
}
}
//for(LL i=l;i<=r;++i)
for(LL i=l;i<r;++i){
if(a[i+1]==1){
Chkmax(f[i+1],f[i]+1);
if(f[i]+1==f[i+1]){
pre[i+1]=i;
}
}else{
//printf("(%lld:%lld)\n",i,f[i]);
LL ret(1);
for(LL j=i+1;j<=r;++j){
ret=ret*a[j];
Chkmax(f[j],f[i]+ret);
if(f[j]==f[i]+ret){
pre[j]=i;
}
}
}
}
//for(LL i=l;i<=r;++i) printf("%lld ",pre[i]); puts("");
//printf("%lld\n",f[r]);
Dfs(r,l);
}
void Get(LL l,LL r){
LL ret(1);
LL lt(r+1),rt(r);
while(a[lt-1]==1) --lt;
r=lt-1;
LL flag(0);
for(LL i=l;i<=r;++i){
ret*=a[i];
if(ret>=10000000000000000){
flag=1; break;
}
}
//printf("(%d)(%lld,%lld)%lld\n",Ans.size(),lt,rt,flag);
if(flag){
for(LL i=l;i<r;++i){
Ans.pb(a[i]+'0'); Ans.pb('*');
}
Ans.pb(a[r]+'0');
}else{
Dp(l,r);
}
for(LL i=lt;i<=rt;++i){
Ans.pb('+');
Ans.pb(a[i]+'0');
}
}
void Solve(){
//puts("033");
for(LL l=1,r;l<=n;l=r+1){
r=l;
if(l!=1) Ans.pb('+');
if(!a[l] || a[l]==1){
Ans.pb(a[l]+'0');
continue;
}
//printf("(%lld,%lld)\n",l,r);
while(r<n && a[r+1]) ++r;
//printf("(%lld,%lld)\n",l,r);
Get(l,r);
}
for(LL i=0;i<Ans.size();++i){
printf("%c",Ans[i]);
}
puts("");
}
}
int main(){
n=Read();
for(LL i=1;i<=n;++i) a[i]=Read();
scanf(" %s",s+1);
LL len=strlen(s+1);
if(len==1) sub1::Solve();
else if(len==2){
LL flag(0);
for(LL i=1;i<=len;++i) flag|=(s[i]=='*');
if(!flag){
s[1]='+';
sub1::Solve();
return 0;
}
flag=0;
for(LL i=1;i<=len;++i) flag|=(s[i]=='+');
if(!flag) sub2::Solve();
else sub3::Solve();
}else{
LL flag(0);
for(LL i=1;i<=len;++i) flag|=(s[i]=='+');
if(!flag) sub2::Solve();
else sub3::Solve();
}
return 0;
} | 6 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct modint {
int n;
modint() : n(0) {}
modint(int n) : n(n) {}
};
modint operator+(modint a, modint b) {
return modint((a.n += b.n) >= mod ? a.n - mod : a.n);
}
modint operator*(modint a, modint b) { return modint(1LL * a.n * b.n % mod); }
bool operator<(modint a, modint b) { return a.n < b.n; }
bool operator!=(modint a, modint b) { return a.n != b.n; }
const int S = 4;
using modints = array<modint, S>;
modints operator+(modints a, modints b) {
modints ret;
for (int i = 0; i < S; i++) {
ret[i] = a[i] + b[i];
}
return ret;
}
modints operator*(modints a, modints b) {
modints ret;
for (int i = 0; i < S; i++) {
ret[i] = a[i] * b[i];
}
return ret;
}
bool operator<(modints a, modints b) {
for (int i = 0; i < S; i++) {
if (a[i] != b[i]) return a[i] < b[i];
}
return false;
}
const int N = 1e5;
int n;
vector<int> g[N];
vector<int> gg[N];
modints rnd[N];
int height[N];
vector<int> h;
modints val[N];
modints one;
map<modints, int> mp;
pair<int, int> ans;
int cnt = 0;
void add(modints a) {
if (mp[a]++ == 0) {
cnt++;
}
}
void del(modints a) {
if (mp[a]-- == 1) {
cnt--;
}
assert(mp[a] >= 0);
}
void dfs(int u, int p) {
for (int v : g[u])
if (v != p) {
dfs(v, u);
height[u] = max(height[u], height[v] + 1);
}
val[u] = one;
for (int v : g[u])
if (v != p) {
val[u] = val[u] * (rnd[height[u]] + val[v]);
}
add(val[u]);
}
void dfs2(int u, int p) {
auto tmp = val[u];
del(tmp);
int n = g[u].size();
h.clear();
h.push_back(0);
for (int v : g[u]) {
h.push_back(height[v] + 1);
}
sort(h.rbegin(), h.rend());
int h0 = h[0];
int h1 = h[1];
modints L0 = one;
modints L1 = one;
vector<modints> R0(n);
vector<modints> R1(n);
R0[n - 1] = one;
R1[n - 1] = one;
for (int i = n - 2; i >= 0; i--) {
R0[i] = R0[i + 1] * (rnd[h0] + val[g[u][i + 1]]);
R1[i] = R1[i + 1] * (rnd[h1] + val[g[u][i + 1]]);
}
ans = max(ans, make_pair(cnt, u));
for (int i = 0; i < n; i++) {
int v = g[u][i];
if (height[v] + 1 != h0) {
height[u] = h0;
val[u] = L0 * R0[i];
} else {
height[u] = h1;
val[u] = L1 * R1[i];
}
L0 = L0 * (rnd[h0] + val[v]);
L1 = L1 * (rnd[h1] + val[v]);
if (v == p) continue;
add(val[u]);
dfs2(v, u);
del(val[u]);
}
add(tmp);
}
int main() {
mt19937 mt(time(NULL) ^ 1234567);
uniform_int_distribution<int> uni(0, mod - 1);
for (int i = 0; i < N; i++) {
for (int j = 0; j < S; j++) {
rnd[i][j] = uni(mt);
}
}
for (int i = 0; i < S; i++) {
one[i] = 1;
}
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
if (n == 1) {
cout << 1 << endl;
return 0;
}
dfs(0, -1);
dfs2(0, -1);
cout << ans.second + 1 << endl;
}
| 4 |
using namespace std;
#include <iostream>
#include <string>
#include <set>
#include <functional>
#include <vector>
#include <algorithm>
#include <stdio.h>
int main()
{
int n;
while(cin>>n&&n){
while(n--){
int a,b,c,d,e,f,g,h;
cin>>a>>b>>c>>d>>e>>f>>g>>h;
printf("%d %d %d %d\n",a*e-b*f-c*g-d*h,a*f+b*e+c*h-d*g,a*g-b*h+c*e+d*f,a*h+b*g-c*f+d*e);
}
}
return 0;
} | 0 |
#include <iostream>
int main(){
int N,H,W;
std::cin>> N >> H >> W;
std::cout << (N-H+1)*(N-W+1);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1507;
int n;
long long a[maxn];
struct node {
int l, r;
long long sum;
node() {}
node(int a, int b, long long c) { l = a, r = b, sum = c; }
bool operator<(const node &a) const { return r < a.r; }
};
map<long long, vector<node> > mp, ans;
map<long long, vector<node> >::iterator it;
long long ss[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
ss[i] = a[i] + ss[i - 1];
}
for (int i = 1; i <= n; i++) {
for (int l = 0, r = l + i; r <= n; l++, r++) {
long long tmp = ss[r] - ss[l];
mp[tmp].push_back(node(l + 1, r, tmp));
}
}
long long ansval = 0, cnt = 0;
for (it = mp.begin(); it != mp.end(); it++) {
sort(it->second.begin(), it->second.end());
int tmp = 0, tim = 0;
for (int i = 0; i < it->second.size(); i++) {
if (it->second[i].l > tim) {
ans[it->first].push_back(it->second[i]);
tmp++, tim = it->second[i].r;
}
}
if (tmp > cnt) cnt = tmp, ansval = it->first;
}
printf("%d\n", cnt);
for (int i = 0; i < cnt; i++)
printf("%d %d\n", ans[ansval][i].l, ans[ansval][i].r);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a[3];
bool ok(int last) {
if (a[0] == a[1]) return false;
if (a[0] == a[2]) return false;
if (a[1] == a[2]) return false;
int mx = 0;
for (int i = 0; i < 3; i++) {
if (a[i] > a[mx]) mx = i;
}
if (mx != last) return true;
ll v1 = -1, v2 = -1;
for (int i = 0; i < 3; i++)
if (i != last) {
if (v1 == -1)
v1 = a[last] - a[i];
else
v2 = a[last] - a[i];
}
if (v1 > v2) swap(v1, v2);
assert(v1 > 0);
printf(">> %lld %lld\n", v1, v2);
if (v1 * 2 == v2) return false;
return true;
}
int main() {
scanf("%lld %lld %lld", a, a + 1, a + 2);
printf("First\n");
fflush(stdout);
ll big = 1e9 + 7;
printf("%lld\n", big);
fflush(stdout);
int pos;
scanf("%d", &pos);
a[pos - 1] += big;
big = 2 * a[pos - 1];
for (int i = 0; i < 3; i++)
if (i != pos - 1) big -= a[i];
printf("%lld\n", big);
fflush(stdout);
scanf("%d", &pos);
a[pos - 1] += big;
ll mx = -1;
for (int i = 0; i < 3; i++)
if (i != pos - 1) {
if (mx == -1 or mx > a[pos - 1] - a[i]) mx = a[pos - 1] - a[i];
}
printf("%lld\n", mx);
fflush(stdout);
scanf("%d", &pos);
assert(pos == 0);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
const int INF = 1e9;
int head[maxn + 5], sz[maxn + 5], dis[maxn + 5], cnt[maxn + 5], tp[5];
bool vis[maxn + 5];
long long dp1[maxn + 5], dp2[maxn + 5], ans[5];
int root, mn, tot;
int n, k, u, v;
long long sum;
int num = 0;
struct edge {
int to, _next;
} G[2 * maxn + 5];
void add(int u, int v) {
G[++num].to = v;
G[num]._next = head[u];
head[u] = num;
}
void findrt(int u, int fa) {
int num = 0;
sz[u] = 1;
for (int i = head[u]; i; i = G[i]._next) {
int v = G[i].to;
if (v == fa || vis[v]) continue;
findrt(v, u);
sz[u] += sz[v];
if (num < sz[v]) num = sz[v];
}
if (tot - sz[u] > num) num = tot - sz[u];
if (num < mn) {
mn = num;
root = u;
}
}
void getDis(int u, int fa) {
dis[u] = dis[fa] + 1;
for (int i = 0; i < k; i++) ans[(dis[u] + i) % k] += tp[i];
for (int i = head[u]; i; i = G[i]._next) {
int v = G[i].to;
if (v == fa || vis[v]) continue;
getDis(v, u);
}
}
void update(int u, int fa, bool op) {
if (op)
tp[dis[u] % k]++;
else
tp[dis[u] % k] = 0;
for (int i = head[u]; i; i = G[i]._next) {
int v = G[i].to;
if (v == fa || vis[v]) continue;
update(v, u, op);
}
}
void calc(int u) {
dis[u] = 0;
tp[0] = 1;
for (int i = head[u]; i; i = G[i]._next) {
int v = G[i].to;
if (vis[v]) continue;
getDis(v, u);
update(v, u, 1);
}
tp[0] = 0;
for (int i = head[u]; i; i = G[i]._next) {
int v = G[i].to;
if (vis[v]) continue;
update(v, u, 0);
}
}
void divide(int u) {
vis[u] = true;
calc(u);
for (int i = head[u]; i; i = G[i]._next) {
int v = G[i].to;
if (vis[v]) continue;
tot = sz[v];
mn = INF;
findrt(v, 0);
divide(root);
}
}
void dfs1(int u, int fa) {
cnt[u] = 1;
dp1[u] = 0;
for (int i = head[u]; i; i = G[i]._next) {
int v = G[i].to;
if (v == fa) continue;
dfs1(v, u);
cnt[u] += cnt[v];
dp1[u] += dp1[v] + cnt[v];
}
}
void dfs2(int u, int fa) {
for (int i = head[u]; i; i = G[i]._next) {
int v = G[i].to;
if (v == fa) continue;
dp2[v] = dp2[u] + (n - cnt[u]);
dp2[v] = dp2[v] + dp1[u] - (dp1[v] + cnt[v]) + (cnt[u] - cnt[v]);
dfs2(v, u);
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
tot = n;
mn = INF;
findrt(1, 0);
divide(root);
dfs1(1, 0);
dfs2(1, 0);
for (int i = 1; i <= n; i++) sum = sum + dp1[i] + dp2[i];
sum /= 2;
for (int i = 1; i < k; i++) sum = sum + ans[i] * (k - i);
printf("%I64d\n", sum / k);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, m, r;
cin >> n >> m >> r;
int si = 1001, bi = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
si = min(x, si);
}
for (int i = 0; i < m; i++) {
int x;
cin >> x;
bi = max(x, bi);
}
int ans = (r / si) * bi, res = r % si;
if (ans + res > r)
cout << ans + res << '\n';
else
cout << r << '\n';
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void IO() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
IO();
long long int n;
cin >> n;
vector<long long int> arr(n);
for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
i += 1 - 2 * ((0) > (n))) {
cin >> arr[i];
}
vector<long long int> quantity(n, 1);
long long int cp = 0;
vector<vector<int> > ans;
do {
vector<long long int> brr;
vector<long long int> nquant;
n = (int)(arr.size());
long long int prev = arr[0];
set<int> put;
put.insert(prev);
long long int sum = quantity[0];
for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
i += 1 - 2 * ((1) > (n))) {
long long int diff = arr[i] - prev;
if (diff == 1) {
put.insert(arr[i]);
sum += quantity[i];
} else {
auto it = put.begin();
brr.push_back(*it);
nquant.push_back(sum);
sum = quantity[i];
put.clear();
put.insert(arr[i]);
}
prev = arr[i];
}
auto it = put.begin();
brr.push_back(*it);
nquant.push_back(sum);
vector<long long int> temp = brr;
sort(temp.begin(), temp.end());
map<long long int, long long int> seq;
long long int cnt = 1;
for (auto it : temp) {
seq[it] = cnt;
cnt++;
}
for (auto &it : brr) {
it = seq[it];
}
arr = brr;
quantity = nquant;
if ((int)(arr.size()) > 1) {
n = (int)(arr.size());
long long int l = 0, r = 0;
for (__typeof(n - 1) i = (0) - ((0) > (n - 1));
i != (n - 1) - ((0) > (n - 1)); i += 1 - 2 * ((0) > (n - 1))) {
for (__typeof(n) j = (i + 1) - ((i + 1) > (n));
j != (n) - ((i + 1) > (n)); j += 1 - 2 * ((i + 1) > (n))) {
long long int diff = arr[i] - arr[j];
if (diff == 1) {
l = i, r = j;
break;
}
}
if (r > l) break;
}
vector<int> res;
vector<long long int> temp;
vector<long long int> newQ;
int cnt = 0;
for (int i = r + 1; i <= n - 1; i++) {
temp.push_back(arr[i]);
newQ.push_back(quantity[i]);
cnt += quantity[i];
}
if (cnt) res.push_back(cnt);
temp.push_back(arr[r]);
newQ.push_back(quantity[r]);
res.push_back(quantity[r]);
cnt = 0;
for (int i = l; i <= r - 1; i++) {
temp.push_back(arr[i]);
newQ.push_back(quantity[i]);
cnt += quantity[i];
}
if (cnt) res.push_back(cnt);
cnt = 0;
for (int i = 0; i <= l - 1; i++) {
temp.push_back(arr[i]);
newQ.push_back(quantity[i]);
cnt += quantity[i];
}
if (cnt) res.push_back(cnt);
reverse(res.begin(), res.end());
arr = temp;
quantity = newQ;
ans.push_back(res);
}
} while ((int)(arr.size()) > 1);
cout << (int)(ans.size()) << "\n";
for (auto it : ans) {
cout << (int)(it.size()) << " ";
for (auto ti : it) cout << ti << " ";
cout << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
bool found = false;
for (int i = 0; i < n and !found; i++) {
for (int j = i + 1; j < n and !found; j++) {
int a = 0;
int b = 0;
for (int k = i; k <= j; k++) {
if (s[k] == 'a')
a++;
else
b++;
}
if (a == b) {
cout << i + 1 << ' ' << j + 1 << '\n';
found = true;
}
}
}
if (!found) cout << -1 << ' ' << -1 << '\n';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
map<int,int> mp;
vector<long long int>count(n+1,0);
for(int i=0;i<m;i++){
int a;
cin >> a;
mp[a]=1;
}
count[0]=1;
count[1]=mp[1]==0;
for(int i=2;i<=n;i++){
if(mp[i]==1)count[i]=0;
else count[i]=(count[i-1]+count[i-2])%1000000007;
}
cout << count[n];
} | 0 |
#include <bits/stdc++.h>
using namespace std;
pair<long long, double> dp[2000][2000];
struct XD {
int scs, scl, ts, tl;
double prf;
long long prll;
bool operator<(const XD& b) const {
if (prll == 1000000) return 0;
if (b.prll == 1000000) return 1;
return prll * tl * (1000000 - b.prll) < b.prll * b.tl * (1000000 - prll);
}
} in[1010];
inline void update(pair<long long, double>& a,
const pair<long long, double>& b) {
a = max(a, b);
}
int main() {
int n, t;
scanf("%d%d", &n, &t);
for (int i = 0; i < (n); i++) {
scanf("%d%d", &in[i].scs, &in[i].scl);
scanf("%d%d", &in[i].ts, &in[i].tl);
scanf("%lf", &in[i].prf);
in[i].prll = (long long)(in[i].prf * 1000000 + 0.5);
}
sort(in, in + n);
for (int i = (0); i <= (n); i++)
for (int j = (0); j <= (t); j++) dp[i][j].first = -1;
dp[0][0] = make_pair(0, 0.0);
for (int i = 0; i < (n); i++) {
for (int st = (0); st <= (t); st++) {
if (dp[i][st].first < 0) continue;
{
pair<long long, double>& d = dp[i + 1][st];
update(d, dp[i][st]);
}
if (st + in[i].ts <= t) {
pair<long long, double>& d = dp[i + 1][st + in[i].ts];
pair<long long, double> p = dp[i][st];
p.first += in[i].scs * 1000000LL;
update(d, p);
}
if (st + in[i].ts + in[i].tl <= t) {
pair<long long, double>& d = dp[i + 1][st + in[i].ts + in[i].tl];
pair<long long, double> p = dp[i][st];
p.first += in[i].scs * 1000000LL + (1000000 - in[i].prll) * in[i].scl;
p.second += in[i].tl;
p.second *= in[i].prf;
update(d, p);
}
}
}
long long a1 = 0;
double b1 = 0;
for (int st = (0); st <= (t); st++) {
long long aa = dp[n][st].first;
double bb = st - dp[n][st].second;
if (aa < 0) continue;
if (a1 < aa || (a1 == aa && b1 > bb)) {
a1 = aa;
b1 = bb;
}
}
printf("%.15lf %.15lf\n", a1 / 1000000.0, b1);
}
| 4 |
#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define REPP(i,j,n) for(int i=(j);i<(int)(n);i++)
using namespace std;
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-5, pi = acos(-1.0);
ld dot(Point a, Point b){return real(conj(a)*b);}
ld cross(Point a, Point b){ return imag(conj(a)*b);}
Point p[310];
int n;
void solve(){
int result=1;
REP(i,n){
REPP(j,i+1,n){
Point ab=p[j]-p[i];
if(norm(ab)>4)continue;
Point q=(p[i]+p[j])/2.L;
Point aq=q-p[j];
Point normal=Point{imag(aq),-real(aq)}*sqrt(1-norm(aq))/abs(aq);
Point c=q+normal;
Point d=q-normal;
int cntc=0,cntd=0;
REP(k,n){
if(norm(p[k]-c)<=1.0001L)cntc++;
if(norm(p[k]-d)<=1.0001L)cntd++;
}
result=max({result,2,cntc,cntd});
}
}
cout<<result<<endl;
}
int main(){ _;
ld r,i;
while(cin>>n,n!=0){
REP(j,n){
cin>>r>>i;
p[j]=Point{r,i};
}
solve();
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 405;
const int oo = 1e9;
int n, k;
int dp[N][N], pr[N], eg[N][N];
bool kt[N];
map<pair<int, int>, int> mask[N];
vector<int> vi[N];
vector<pair<int, int> > egtmp;
void solve(int pre, int u) {
pr[u] = pre;
dp[u][1] = 0;
for (int v : vi[u])
if (v != pre) {
solve(u, v);
for (int i = k; i >= 1; --i) {
dp[u][i]++;
for (int j = 1; j < i; ++j) {
if (dp[u][i] > dp[u][i - j] + dp[v][j]) {
mask[i][pair<int, int>(u, v)] = j;
dp[u][i] = dp[u][i - j] + dp[v][j];
}
}
}
}
}
void trace(int u, int rem) {
kt[u] = true;
for (int i = vi[u].size() - 1; i >= 0; --i) {
auto v = vi[u][i];
int l = mask[rem][pair<int, int>(u, v)];
if (l) {
trace(v, l);
}
rem -= l;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= k; ++j) dp[i][j] = oo;
}
for (int i = 1; i <= n - 1; ++i) {
int u, v;
cin >> u >> v;
egtmp.push_back(pair<int, int>(u, v));
eg[u][v] = eg[v][u] = i;
vi[u].push_back(v);
vi[v].push_back(u);
}
solve(1, 1);
int ans = oo, pos = 0;
for (int i = 1; i <= n; ++i) {
int nex = dp[i][k];
if (i > 1) nex++;
if (ans > nex) {
ans = nex;
pos = i;
}
}
cout << (pos == 1 ? dp[pos][k] : dp[pos][k] + 1) << '\n';
trace(pos, k);
for (int i = 0; i < egtmp.size(); ++i) {
if (kt[egtmp[i].first] + kt[egtmp[i].second] == 1) cout << i + 1 << ' ';
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int arr[30];
int calen[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
for (int i = 0; i < 144 - n + 1; ++i) {
bool yes = true;
for (int j = 0; j < n; ++j) {
if (arr[j] != calen[j + i]) {
yes = false;
break;
}
}
if (yes == true) {
cout << "YES";
return 0;
}
}
cout << "NO";
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int a[30],b[30];
int ans;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=n;i++){
if(b[i]<a[i])
ans+=(a[i]-b[i]);
}
cout<<ans<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
const long long int null = 3000;
long long int si[10000], sj[10000], m[2050][2050], s[5000][5000], max[2][2],
maxij[2][2][2], n;
long long int maxst(long long int a, long long int b) {
if (a > b)
return a;
else
return b;
}
void sol() {
long long int i, j, x;
for (i = 0; i < 10000; i++) {
si[i] = 0;
sj[i] = 0;
}
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
scanf("%I64d", &x);
m[i][j] = x;
si[i + j + null] += x;
sj[j - i + null] += x;
}
for (i = null; i < null + 2 * n - 1; i++) {
for (j = null - n + 1; j <= i; j++) {
if (i % 2 == j % 2) {
if ((i + j) / 2 - null >= 0) {
s[(i - j) / 2][(i + j) / 2 - null] =
si[i] + sj[j] - m[(i - j) / 2][(i + j) / 2 - null];
}
}
}
}
max[0][0] = -1;
max[1][0] = -1;
max[0][1] = -1;
max[1][1] = -1;
for (i = 0; i < n; i += 2)
for (j = 0; j < n; j += 2) {
if (s[i][j] > max[0][0]) {
max[0][0] = s[i][j];
maxij[0][0][0] = i;
maxij[0][0][1] = j;
}
}
for (i = 1; i < n; i += 2)
for (j = 0; j < n; j += 2) {
if (s[i][j] > max[1][0]) {
max[1][0] = s[i][j];
maxij[1][0][0] = i;
maxij[1][0][1] = j;
}
}
for (i = 0; i < n; i += 2)
for (j = 1; j < n; j += 2) {
if (s[i][j] > max[0][1]) {
max[0][1] = s[i][j];
maxij[0][1][0] = i;
maxij[0][1][1] = j;
}
}
for (i = 1; i < n; i += 2)
for (j = 1; j < n; j += 2) {
if (s[i][j] > max[1][1]) {
max[1][1] = s[i][j];
maxij[1][1][0] = i;
maxij[1][1][1] = j;
}
}
printf("%I64d\n", maxst(max[0][0], max[1][1]) + maxst(max[0][1], max[1][0]));
if (max[0][0] > max[1][1])
printf("%I64d %I64d ", maxij[0][0][0] + 1, maxij[0][0][1] + 1);
else
printf("%I64d %I64d ", maxij[1][1][0] + 1, maxij[1][1][1] + 1);
if (max[0][1] > max[1][0])
printf("%I64d %I64d ", maxij[0][1][0] + 1, maxij[0][1][1] + 1);
else
printf("%I64d %I64d ", maxij[1][0][0] + 1, maxij[1][0][1] + 1);
printf("\n");
}
int main() {
#pragma comment(linker, "/STACK:16777216")
while (scanf("%I64d", &n) != EOF) sol();
}
| 3 |
#include<stdio.h>
#include<iostream>
#include<stack>
#include<algorithm>
#define MAX 1400
using namespace std;
struct Rectangle { int height; int pos; };
int getLargestRectangle(int size, int buffer[]) {
stack<Rectangle> S;
int maxv = 0;
buffer[size] = 0;
for ( int i = 0; i <= size; i++ ) {
Rectangle rect;
rect.height = buffer[i];
rect.pos = i;
if ( S.empty() ) {
S.push(rect);
} else {
if ( S.top().height < rect.height ) {
S.push(rect);
} else if ( S.top().height > rect.height ) {
int target = i;
while ( !S.empty() && S.top().height >= rect.height ) {
Rectangle pre = S.top(); S.pop();
int area = pre.height * (i - pre.pos);
maxv = max(maxv, area);
target = pre.pos;
}
rect.pos = target;
S.push(rect);
}
}
}
return maxv;
}
int H, W;
int buffer[MAX][MAX];
int T[MAX][MAX];
int getLargestRectangle() {
for ( int j = 0; j < W; j++ )
for ( int i = 0; i < H; i++ ) {
if ( buffer[i][j] )
T[i][j] = 0;
else
T[i][j] = (i > 0) ? T[i - 1][j] + 1 : 1;
}
int maxv = 0;
for ( int i = 0; i < H; i++ )
maxv = max(maxv, getLargestRectangle(W, T[i]));
return maxv;
}
int main() {
scanf("%d %d", &H, &W);
for ( int i = 0; i < H; i++ )
for ( int j = 0; j < W; j++ )
scanf("%d", &buffer[i][j]);
cout << getLargestRectangle() << endl;
return 0;
}
| 0 |
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const int mod=1000000007;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-4;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int main(void){
llint i,n,mae=mod,ans=0;
cin>>n;
vector<llint>a(n);
for(i=0;i<n;i++){cin>>a[i];}
SO(a);REV(a);
for(i=0;i<n;i++){ans+=a[i]-i-1;}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s = "";
cin >> n;
while (true) {
for (char ch = 'a'; ch <= 'a' + 3; ch++) {
s += ch;
if (s.length() == n) {
cout << s << endl;
return 0;
}
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long int a[100001];
int N, M, c, v, x, j, add;
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
for (int i = 0; i < M; i++) {
cin >> c;
switch (c) {
case 1:
cin >> v >> x;
a[v] = x - add;
break;
case 2:
cin >> x;
add += x;
break;
case 3:
cin >> v;
cout << (a[v] + add) << endl;
break;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int T, n, k;
long long ans;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &k);
ans = 0;
int now = ceil((double)n / 2);
for (int i = 1, x; i <= n * k; i++) {
scanf("%d", &x);
if (i <= (now - 1) * k) continue;
if ((i - 1 - (now - 1) * k) % (n - now + 1) == 0) ans += x;
}
printf("%lld\n", ans);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 9, inf = 1e18, mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
long long n, k1, k2;
cin >> n >> k1 >> k2;
bool f = false;
while (k1--) {
long long x;
cin >> x;
if (x == n) f = true;
}
while (k2--) {
long long x;
cin >> x;
}
if (f)
cout << "YES\n";
else
cout << "NO\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int m, c[50];
long long t, a[1048576 + 5], b[1048576 + 5], p;
long long mul(long long x, long long y) {
return ((x * y - (long long)((long double)x / p * y + 1e-3) * p) % p + p) % p;
}
long long pow(long long x, long long k) {
long long sum = 1;
for (; k; k >>= 1, x = mul(x, x))
if (k & 1) sum = mul(sum, x);
return sum;
}
void FWT(long long* a, int l, int r) {
if (l == r) return;
int mid = l + r >> 1;
FWT(a, l, mid);
FWT(a, mid + 1, r);
for (int i = 0; i <= mid - l; ++i) {
long long x = a[l + i], y = a[mid + 1 + i];
a[l + i] = (x + y) % p, a[mid + 1 + i] = (x - y + p) % p;
}
}
void IFWT(long long* a, int l, int r) {
if (l == r) return;
int mid = l + r >> 1;
for (int i = 0; i <= mid - l; ++i) {
long long x = a[l + i], y = a[mid + 1 + i];
a[l + i] = (x + y) % p, a[mid + 1 + i] = (x - y + p) % p;
}
IFWT(a, l, mid);
IFWT(a, mid + 1, r);
}
int main() {
m = read();
scanf("%lld", &t);
p = 1LL * read() * (1 << m);
for (int i = 0; i < 1 << m; ++i) a[i] = read() % p;
for (int i = 0; i <= m; ++i) c[i] = read() % p;
for (int i = 0; i < 1 << m; ++i) {
int num = 0;
for (int j = 0; j < m; ++j)
if (i & (1 << j)) ++num;
b[i] = c[num];
}
FWT(a, 0, (1 << m) - 1);
FWT(b, 0, (1 << m) - 1);
for (int i = 0; i < 1 << m; ++i) a[i] = mul(a[i], pow(b[i], t));
IFWT(a, 0, (1 << m) - 1);
for (int i = 0; i < 1 << m; ++i) printf("%lld\n", a[i] >> m);
return 0;
}
| 4 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);
using namespace std;
namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;
struct flowGraph{
struct edge{ int to, cap, rev; };
int n, *level, *iter;
vector<vector<edge> > G;
flowGraph(int sz) : n(sz){
G.resize(n);
iter = new int[n]; level = new int[n];
}
~flowGraph(){
delete [] iter; delete [] level;
}
void add(int s, int t, int cap){
G[s].pb((edge){t, cap, (int)G[t].size()});
G[t].pb((edge){s, 0, (int)G[s].size() - 1});
}
void bfs(int s){
rep(i, n) level[i] = -1;
queue<int> q;
level[s] = 0;
q.push(s);
while(!q.empty()){
int v = q.front();
q.pop();
rep(i, G[v].size()){
edge &e = G[v][i];
if(e.cap > 0 && level[e.to] < 0){
level[e.to] = level[v] + 1;
q.push(e.to);
}
}
}
}
int dfs(int v, int t, int f){
if(v == t) return f;
for(int &i = iter[v]; i < (int)G[v].size(); i++){
edge &e = G[v][i];
if(e.cap > 0 && level[v] < level[e.to]){
int d = dfs(e.to, t, min(f, e.cap));
if(d > 0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t){
int flow = 0;
while(1){
bfs(s);
if(level[t] < 0) return flow;
rep(i, n) iter[i] = 0;
int f;
while((f = dfs(s, t, inf)) > 0) flow += f;
}
}
};
bool can(int n, const vector<pi> &es, int low, int diff){
int m = es.size();
int s = n + m, t = s + 1;
int S = t + 1, T = S + 1;
auto check = [&](bool pre_check){
flowGraph g(T + 1);
int id = 0;
for(auto p : es){
int a = p.first, b = p.second;
g.add(s, id, 1);
g.add(id, a + m, 1);
g.add(id, b + m, 1);
id++;
}
rep(i, n){
g.add(i + m, t, diff);
g.add(S, t, low);
g.add(i + m, T, low);
}
if(pre_check){
g.add(t, s, inf);
}
else{
g.add(S, s, inf);
g.add(t, T, inf);
}
return g.max_flow(S, T);
};
if(check(1) < n * low) return 0;
if(check(0) - n * low < m) return 0;
return 1;
}
int main(){
cin.tie(0); cin.sync_with_stdio(0);
int n, m;
while(cin >> n >> m, n){
vector<pi> es;
rep(i, m){
int a, b; cin >> a >> b; a--; b--;
es.emplace_back(a, b);
}
int ans = n, ansl;
for(int low = 0; low * n <= m; low++){
if(!can(n, es, low, ans)) continue;
int lo = -1, hi = ans, mid;
while(lo + 1 < hi){
mid = (lo + hi) / 2;
if(can(n, es, low, mid)) hi = mid;
else lo = mid;
}
//dbg(low, lo, hi);
ans = hi;
ansl = low;
assert(can(n, es, ansl, ans));
}
cout << ansl << " " << ansl + ans << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long mod = 1000000007;
long long r(pair<int, int> a) {
long long x = a.first;
long long y = a.second;
return x * x + y * y;
}
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
return make_pair(a.first + b.first, a.second + b.second);
}
pair<int, int> operator-(pair<int, int> a, pair<int, int> b) {
return make_pair(a.first - b.first, a.second - b.second);
}
int main() {
ios_base::sync_with_stdio(false);
int N;
cin >> N;
vector<pair<pair<int, int>, vector<int>>> a;
for (int i = 1; i <= N; i++) {
int x, y;
cin >> x >> y;
a.push_back({{x, y}, {i}});
}
shuffle(a.begin(), a.end(), rng);
while (a.size() >= 3) {
int i = a.size() - 1;
int j = a.size() - 2;
int wj = 1;
pair<int, int> mnn = {1e8, 1e8};
for (int x = a.size() - 3; x < (int)a.size(); x++) {
for (int y = x + 1; y < (int)a.size(); y++) {
if (x == y) continue;
pair<int, int> v1 = a[x].first + a[y].first;
pair<int, int> v2 = a[x].first - a[y].first;
if (r(v1) < r(mnn)) {
mnn = v1;
i = x;
j = y;
wj = 1;
}
if (r(v2) < r(mnn)) {
mnn = v2;
i = x;
j = y;
wj = -1;
}
}
}
a[i].first = a[j].first = mnn;
if (a[j].second.size() < a[i].second.size()) {
for (int c : a[j].second) {
a[i].second.push_back(c * wj);
}
a.erase(a.begin() + j);
} else {
if (wj == 1) {
for (int c : a[i].second) {
a[j].second.push_back(c * wj);
}
a.erase(a.begin() + i);
} else {
a[j].first = make_pair(0, 0) - a[j].first;
for (int c : a[i].second) {
a[j].second.push_back(-c);
}
a.erase(a.begin() + i);
}
}
}
if (a.size() == 2) {
if (r(a[0].first - a[1].first) < r(a[0].first + a[1].first)) {
for (int &i : a[1].second) i *= -1;
}
}
int w[N + 1];
for (auto ii : a) {
for (int i : ii.second) {
if (i > 0)
w[i] = 1;
else
w[-i] = -1;
}
}
for (int i = 1; i <= N; i++) cout << w[i] << " ";
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+10;
int gi() {
int x=0,o=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-') ch=getchar();
if(ch=='-') o=-1,ch=getchar();
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
return x*o;
}
struct line {
int k;ll b;
line(int k=0,ll b=0):k(k),b(b) {}
ll cal(int x) {
return 1ll*k*x+b;
}
} q[N];
long double sec(const line &x,const line &y) {
return 1.0*(y.b-x.b)/(x.k-y.k);
}
int n,a[N],h,t;
ll f[N],g[N],s[N],v[N],tmp[N];
void push(line l) {
while(h<t&&sec(q[t],l)>=sec(q[t],q[t-1])) --t;
q[++t]=l;
}
ll query(int x) {
while(h<t&&q[t].cal(x)<=q[t-1].cal(x)) --t;
return q[t].cal(x);
}
ll cal1(int x) {
return (1ll*x*x+x)/2-s[x];
}
ll cal2(int x) {
return (1ll*x*x-x)/2+s[x];
}
void dp(ll *f) {
for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i];
h=1,t=0;
for(int i=1;i<=n;i++) {
push((line){-i+1,f[i-1]+cal2(i-1)});
f[i]=max(f[i-1],query(i)+cal1(i));
}
}
void solve(int l,int r) {
if(l==r) {
v[l]=max(v[l],f[l-1]+g[l+1]+1-a[l]);
return;
}
int mid=(l+r)>>1;
h=1,t=0;
for(int i=l-1;i<mid;i++) push((line){-i,f[i]+cal2(i)});
for(int i=mid+1;i<=r;i++) tmp[i]=query(i)+g[i+1]+cal1(i);
for(int i=r-1;i>mid;i--) tmp[i]=max(tmp[i],tmp[i+1]);
for(int i=mid+1;i<=r;i++) v[i]=max(v[i],tmp[i]);
h=1,t=0;
for(int i=mid+1;i<=r;i++) push((line){-i,g[i+1]+cal1(i)});
for(int i=l-1;i<mid;i++) tmp[i+1]=query(i)+f[i]+cal2(i);
for(int i=l+1;i<=mid;i++) tmp[i]=max(tmp[i],tmp[i-1]);
for(int i=l;i<=mid;i++) v[i]=max(v[i],tmp[i]);
solve(l,mid),solve(mid+1,r);
}
int main() {
cin>>n;
for(int i=1;i<=n;i++) a[i]=gi();
reverse(a+1,a+n+1);dp(g);reverse(g+1,g+n+1);
reverse(a+1,a+n+1);dp(f);
memset(v,0xc0,sizeof(v));solve(1,n);
int m=gi();
while(m--) {
int p=gi(),x=gi();
cout<<max(f[p-1]+g[p+1],v[p]-x+a[p])<<'\n';
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], b[maxn], c[maxn], lson[maxn], rson[maxn], stk[maxn];
int vis[maxn];
int dep[maxn];
int rt, n;
void build() {
int top = 0;
for (int i = 1; i <= n; i++) {
lson[i] = rson[i] = vis[i] = 0;
}
for (int i = 1; i <= n; i++) {
int k = top;
while (k > 0 && a[stk[k - 1]] > a[i]) k--;
if (k) rson[stk[k - 1]] = i;
if (k < top) lson[i] = stk[k];
stk[k++] = i;
top = k;
}
for (int i = 1; i <= n; i++) {
vis[lson[i]] = vis[rson[i]] = 1;
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) rt = i;
}
}
void dfs(int u) {
dep[u] = 1;
if (lson[u]) dfs(lson[u]), dep[u] = max(dep[u], dep[lson[u]] + 1);
if (rson[u]) dfs(rson[u]), dep[u] = max(dep[u], dep[rson[u]] + 1);
}
pair<int, int> solve(int p) {
build();
dfs(rt);
if (dep[lson[rt]] <= dep[rson[rt]]) return {dep[rt], 0};
int l = 0, r = p - 1, shift, depth;
while (l <= r) {
int mid = l + r >> 1;
for (int i = mid % n + 1, j = 1; j <= n; j++, i = i % n + 1) {
b[j] = a[i];
c[j] = a[j];
}
for (int i = 1; i <= n; i++) {
a[i] = b[i];
}
build();
dfs(rt);
if (dep[lson[rt]] >= dep[rson[rt]]) {
l = mid + 1;
shift = mid;
depth = dep[rt];
} else
r = mid - 1;
for (int i = 1; i <= n; i++) a[i] = c[i];
}
l = 0, r = shift;
while (l <= r) {
int mid = l + r >> 1;
for (int i = mid % n + 1, j = 1; j <= n; j++, i = i % n + 1) {
b[j] = a[i];
c[j] = a[j];
}
for (int i = 1; i <= n; i++) {
a[i] = b[i];
}
build();
dfs(rt);
if (dep[rt] == depth) {
r = mid - 1;
shift = mid;
} else
l = mid + 1;
for (int i = 1; i <= n; i++) a[i] = c[i];
}
return {depth, shift};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
int p;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 1) {
p = i;
}
}
pair<int, int> ans1 = solve(p);
for (int i = p % n + 1, j = 1; j <= n; j++, i = i % n + 1) {
b[j] = a[i];
}
for (int i = 1; i <= n; i++) a[i] = b[i];
pair<int, int> ans2 = solve(n);
ans2.second += p;
ans1 = min(ans1, ans2);
cout << ans1.first << " " << ans1.second << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long w = 1, q = 0;
char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') q = q * 10 + ch - '0', ch = getchar();
return w * q;
}
const int maxn = 9 + 2e5;
const long long mod1 = 998244353;
const long long mod2 = 1000000007;
const long long mod3 = 1000000009;
int _;
long long qpow(long long m, long long n, long long mod) {
long long temp;
if (n == 0) return 1;
temp = qpow(m, n >> 1, mod);
if (n % 2 == 0)
return temp * temp % mod;
else
return m * temp % mod * temp % mod;
}
int main() {
int n = read();
long long a[maxn];
for (int i = 0; i < n; i++) a[i] = read();
long long ans = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (i == j || j == k || k == i) continue;
ans = max(ans, a[i] | a[j] | a[k]);
}
}
}
if (n == 2) ans = a[0] | a[1];
if (n == 1) ans = a[0];
printf("%lld\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<long long int, string> sn;
pair<long long int, pair<long long int, string> > rsn;
pair<long long int, pair<string, string> > rn1n2;
pair<string, string> n1n2;
set<pair<long long int, pair<string, string> > > ans;
set<pair<long long int, pair<long long int, string> > > k;
set<pair<long long int, pair<long long int, string> > >::iterator it;
set<pair<long long int, pair<long long int, string> > >::iterator tit;
set<pair<long long int, pair<long long int, string> > >::iterator lit;
set<pair<long long int, pair<string, string> > >::iterator ansit;
long long int n, m;
cin >> n >> m;
long long int i, re, score, temp, max1, max2;
bool flag = 0;
string name;
sn = make_pair(-1, "s");
rsn = make_pair(0, sn);
k.insert(rsn);
for (i = 0; i < n; i++) {
cin >> name >> re >> score;
sn = make_pair(score, name);
rsn = make_pair(re, sn);
k.insert(rsn);
}
it = k.begin();
it++;
while (it != k.end()) {
lit = it;
while (it->first == lit->first && lit != k.end()) lit++;
lit--;
flag = 0;
tit = lit;
max1 = lit->second.first;
tit--;
n1n2 = make_pair(lit->second.second, tit->second.second);
rn1n2 = make_pair(it->first, n1n2);
max2 = tit->second.first;
tit--;
while (it->first == tit->first && tit != k.begin() && flag == 0) {
temp = tit->second.first;
if (temp == max1 || temp == max2) {
n1n2 = make_pair("?", "0");
rn1n2 = make_pair(it->first, n1n2);
}
tit--;
}
ans.insert(rn1n2);
it = lit;
it++;
}
ansit = ans.begin();
while (ansit != ans.end()) {
if (ansit->second.first != "?") {
cout << ansit->second.first;
cout << ' ' << ansit->second.second;
} else
cout << ansit->second.first;
cout << endl;
ansit++;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int a, b, x, y;
cin >> a >> b >> x >> y;
x++;
y++;
int a1 = (x - 1) * b;
int a2 = (a - x) * b;
int a3 = (y - 1) * a;
int a4 = (b - y) * a;
cout << max(max(max(a1, a2), a3), a4) << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long x, y = 1e18;
vector<long long> v;
map<long long, long long> m;
bool h = false;
for (long long i = 0; i < n; i++) {
cin >> x;
m[x]++;
if (m[x] >= 4 and y == 1e18) {
h = true;
y = x;
}
}
if (h) {
cout << y << " " << y << " " << y << " " << y << '\n';
continue;
}
for (auto it = m.begin(); it != m.end(); it++) {
if (m[it->first] >= 2) v.push_back(it->first);
}
sort((v).begin(), (v).end());
long long a, b;
double mx = 1e18;
for (long long i = 0; i < v.size() - 1; i++) {
if (abs(1 - (double)v[i + 1] / (double)v[i]) < mx) {
mx = abs(1 - (double)v[i + 1] / (double)v[i]);
a = v[i];
b = v[i + 1];
}
}
cout << a << " " << a << " " << b << " " << b << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const string nl = "\n";
static struct FastInput {
static constexpr long long BUF_SIZE = 1 << 20;
char buf[BUF_SIZE];
size_t chars_read = 0;
size_t buf_pos = 0;
FILE* in = stdin;
char cur = 0;
inline char get_char() {
if (buf_pos >= chars_read) {
chars_read = fread(buf, 1, BUF_SIZE, in);
buf_pos = 0;
buf[0] = (chars_read == 0 ? -1 : buf[0]);
}
return cur = buf[buf_pos++];
}
inline void tie(long long) {}
inline explicit operator bool() { return cur != -1; }
inline static bool is_blank(char c) { return c <= ' '; }
inline bool skip_blanks() {
while (is_blank(cur) && cur != -1) {
get_char();
}
return cur != -1;
}
inline FastInput& operator>>(char& c) {
skip_blanks();
c = cur;
return *this;
}
inline FastInput& operator>>(string& second) {
if (skip_blanks()) {
second.clear();
do {
second += cur;
} while (!is_blank(get_char()));
}
return *this;
}
template <typename T>
inline FastInput& read_integer(T& n) {
n = 0;
if (skip_blanks()) {
long long sign = +1;
if (cur == '-') {
sign = -1;
get_char();
}
do {
n += n + (n << 3) + cur - '0';
} while (!is_blank(get_char()));
n *= sign;
}
return *this;
}
template <typename T>
inline typename enable_if<is_integral<T>::value, FastInput&>::type operator>>(
T& n) {
return read_integer(n);
}
template <typename T>
inline typename enable_if<is_floating_point<T>::value, FastInput&>::type
operator>>(T& n) {
n = 0;
if (skip_blanks()) {
string second;
(*this) >> second;
sscanf(second.c_str(), "%lf", &n);
}
return *this;
}
} fast_input;
static struct FastOutput {
static constexpr long long BUF_SIZE = 1 << 20;
char buf[BUF_SIZE];
size_t buf_pos = 0;
static constexpr long long TMP_SIZE = 1 << 20;
char tmp[TMP_SIZE];
FILE* out = stdout;
inline void put_char(char c) {
buf[buf_pos++] = c;
if (buf_pos == BUF_SIZE) {
fwrite(buf, 1, buf_pos, out);
buf_pos = 0;
}
}
~FastOutput() { fwrite(buf, 1, buf_pos, out); }
inline FastOutput& operator<<(char c) {
put_char(c);
return *this;
}
inline FastOutput& operator<<(const char* second) {
while (*second) {
put_char(*second++);
}
return *this;
}
inline FastOutput& operator<<(const string& second) {
for (long long i = 0; i < (long long)second.size(); i++) {
put_char(second[i]);
}
return *this;
}
template <typename T>
inline char* integer_to_string(T n) {
char* p = tmp + TMP_SIZE - 1;
if (n == 0) {
*--p = '0';
} else {
bool is_negative = false;
if (n < 0) {
is_negative = true;
n = -n;
}
while (n > 0) {
*--p = (char)('0' + n % 10);
n /= 10;
}
if (is_negative) {
*--p = '-';
}
}
return p;
}
template <typename T>
inline typename enable_if<is_integral<T>::value, char*>::type stringify(T n) {
return integer_to_string(n);
}
template <typename T>
inline typename enable_if<is_floating_point<T>::value, char*>::type stringify(
T n) {
sprintf(tmp, "%.17f", n);
return tmp;
}
template <typename T>
inline FastOutput& operator<<(const T& n) {
auto p = stringify(n);
for (; *p != 0; p++) {
put_char(*p);
}
return *this;
}
} fast_output;
template <class T>
struct fenwick {
long long n;
vector<T> tree;
fenwick(const long long _n) {
n = _n;
tree.assign(n + 2, 0LL);
}
void add(long long pos, T dif) {
++pos;
for (long long i = pos; i <= n; i += (i & -i)) {
tree[i] += dif;
}
}
void set(long long pos, T val) { add(pos, val - query(pos, pos)); }
T query(long long l) {
++l;
T sum = 0;
for (long long i = l; i > 0; i -= (i & -i)) {
sum += tree[i];
}
return sum;
}
T query(long long l, long long r) { return query(r) - query(l - 1); }
};
int32_t main() {
ios::sync_with_stdio(0);
fast_input.tie(0);
long long n;
fast_input >> n;
vector<long long> a(n);
for (auto& i : a) {
fast_input >> i;
}
vector<long long> b = a;
sort(b.begin(), b.end());
b.resize(unique(b.begin(), b.end()) - b.begin());
for (long long i = 0; i < n; ++i) {
a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
}
vector<long long> cnt((long long)1e6 + 100);
vector<long long> hld, hld1;
for (long long i = 0; i < n; ++i) {
hld.push_back(++cnt[a[i]]);
}
reverse(a.begin(), a.end());
fill(cnt.begin(), cnt.end(), 0LL);
for (long long i = 0; i < n; ++i) {
hld1.push_back(++cnt[a[i]]);
}
reverse(a.begin(), a.end());
fenwick<long long> ft((long long)1e6 + 100);
long long ans = 0;
for (long long i = n - 2; i >= 0; --i) {
ft.add(hld1[n - i - 2], 1);
ans += ft.query(hld[i] - 1);
}
fast_output << ans << nl;
}
| 4 |
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
int dp[1 << 15][110];
int solve_testcase() {
int N, M; scanf("%d%d", &N, &M);
if(N == 0 and M == 0) return 1;
vector<int> S(N), D(M);
for(int i=0; i<N; i++) scanf("%d", &S[i]);
for(int i=0; i<M; i++) scanf("%d", &D[i]);
sort(D.begin(), D.end());
vector<int> S_sum(1 << N);
for(int bit=0; bit<(1<<N); bit++) {
for(int i=0; i<N; i++) {
if(bit >> i & 1) S_sum[bit] += S[i];
}
}
fill(dp[0], dp[1<<N], INF);
dp[0][0] = 0;
for(int i=0; i<M; i++) {
for(int bit=0; bit<(1<<N); bit++) {
// そのまま使う
chmin(dp[bit][i+1], dp[bit][i] + abs(S_sum[bit] - D[i]));
// 1 個増やす
for(int j=0; j<N; j++) {
if(bit >> j & 1) continue;
int nbit = bit | (1 << j);
chmin(dp[nbit][i], dp[bit][i]);
}
}
}
int ans = INF;
for(int bit=0; bit<(1<<N); bit++) chmin(ans, dp[bit][M]);
printf("%d\n", ans);
return 0;
}
int main() {
while(!solve_testcase());
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, b[100005], g[100005], bmx, gmi, sum[100005], sumg;
int main() {
scanf("%lld%lld", &n, &m);
bmx = 0, gmi = 100000000;
for (int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
bmx = max(bmx, b[i]);
}
for (int i = 1; i <= m; i++) {
scanf("%lld", &g[i]);
gmi = min(gmi, g[i]);
sumg += g[i];
}
if (gmi < bmx)
printf("-1\n");
else {
sort(b + 1, b + 1 + n);
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + b[i];
}
long long ans = sum[n] * m + sumg;
if (gmi == bmx)
ans -= bmx * m;
else
ans -= b[n] * (m - 1) + b[n - 1];
printf("%lld\n", ans);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int const N = 5e5, oo = 1e9;
int mod = oo + 7;
double const eps = 5e-6;
int n, s, an, aa[N];
map<int, int> cnt;
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> aa[i];
if (i == s - 1) {
if (aa[i]) an++;
cnt[0]++;
} else
cnt[aa[i]]++;
}
if (cnt[0] > 1) {
cnt[n] += cnt[0] - 1;
cnt[0] = 1;
}
cnt.erase(0);
for (int i = 1; i < n && !cnt.empty(); i++) {
if (cnt.find(i) == cnt.end()) {
auto e = cnt.end();
e--;
an++;
pair<int, int> r = *e;
cnt.erase(e);
r.second--;
if (r.second) cnt[r.first] = r.second;
} else
cnt.erase(i);
}
cout << an;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long res_even[n / 2];
long long res_odd[n / 2];
int quotient = n / 2;
if (quotient % 2 != 0) {
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
for (int i = 0; i < n / 2; i++) {
res_even[i] = 2 * (i + 1);
}
for (int i = 0; i < (n / 4); i++) {
res_odd[i] = res_even[i] - 1;
}
for (int i = (n / 4); i < n / 2; i++) {
res_odd[i] = res_even[i] + 1;
}
for (int i = 0; i < n / 2; i++) {
cout << res_even[i] << " ";
}
for (int i = 0; i < n / 2; i++) {
cout << res_odd[i] << " ";
}
cout << endl;
}
return 0;
}
| 2 |
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int h, w;
while (cin >> h >> w, h) {
vector<vector<string>> map(h, vector<string>(w));
for (auto &seq : map) {
for (auto &v : seq) {
char c;
cin >> c;
v = c;
}
}
int cx = 0;
int cy = 0;
while (true) {
string str = map[cy][cx];
map[cy][cx] = "0";
if (str == ">") {
++cx;
} else if (str == "<") {
--cx;
} else if (str == "^") {
--cy;
} else if (str == "v") {
++cy;
} else if (str == ".") {
cout << cx << " " << cy << endl;
break;
} else if (str == "0") {
cout << "LOOP" << endl;
break;
}
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long maxs = 1e6 + 321;
long long mpow(long long a, long long b, long long p) {
if (b == 0) return 1;
long long t = mpow(a * a % p, b / 2, p);
if (b % 2) t = t * a % p;
return t;
}
long long fact[maxs], invf[maxs];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, m, a, q;
cin >> n >> m >> a >> q;
a %= q;
long long ord = 1;
long long ac = a;
while (ac != 1) {
ac = ac * a;
ac %= q;
++ord;
}
invf[0] = fact[0] = 1;
for (int i = 1; i < maxs; ++i) {
fact[i] = fact[i - 1] * i % ord;
invf[i] = mpow(fact[i], ord - 2, ord);
}
vector<long long> sol;
long long acum = 0;
for (int i = 0; i < min(n, m + 1); ++i) {
acum = (acum + fact[m] * invf[m - i] % ord * invf[i]) % ord;
sol.push_back(acum);
}
while (sol.size() < n) {
sol.push_back(sol.back());
}
reverse(sol.begin(), sol.end());
for (int i = 0; i < n; ++i) {
cout << (i ? " " : "") << mpow(a, sol[i], q);
}
cout << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char a[] = "23456789TJQKA";
char b[] = "SDHC";
int Get() {
char s[3];
scanf("%s", s);
int i = 0;
while (a[i] != s[0]) ++i;
int j = 0;
while (b[j] != s[1]) ++j;
return i * 4 + j;
}
int Good(int first, int second) {
return (first / 4 == second / 4) || (first % 4 == second % 4);
}
int arr[100];
int n;
bool dp[55][55][55][55];
bool Solve(int n, int first, int second, int z) {
if (n == 1) return 1;
if (n == 2) return Good(first, second);
if (n == 3) return Good(z, second) && Good(z, first);
}
void Out(int first) {
putchar(a[first / 4]);
putchar(b[first % 4]);
}
int main() {
scanf("%d", &n);
for (int i = (0); i < (int)(n); i++) arr[i] = Get();
memset(dp, 0, sizeof(dp));
bool ans = false;
if (n <= 3) {
ans = Solve(n, arr[0], arr[1], arr[2]);
} else {
dp[n - 4][arr[n - 3]][arr[n - 2]][arr[n - 1]] = true;
for (int k = n - 4; k >= 0; k--) {
for (int first = (0); first < (int)(52); first++)
for (int second = (0); second < (int)(52); second++)
for (int z = (0); z < (int)(52); z++) {
if (dp[k][first][second][z]) {
if (k == 0) {
if (Good(second, z)) ans |= Solve(3, arr[k], first, z);
if (Good(arr[k], z)) ans |= Solve(3, z, first, second);
} else {
if (Good(second, z)) dp[k - 1][arr[k]][first][z] = true;
if (Good(arr[k], z)) dp[k - 1][z][first][second] = true;
}
}
}
}
}
puts(ans ? "YES" : "NO");
return 0;
}
| 2 |
//56
#include<iostream>
#include<algorithm>
using namespace std;
#define INF (1<<29)
int main(){
for(int n,m;cin>>n>>m,n|m;){
int cm[101][101];
int tm[101][101];
fill_n(cm[0],101*101,INF);
fill_n(tm[0],101*101,INF);
for(int i=1;i<=100;i++){
cm[i][i]=tm[i][i]=0;
}
for(int i=0;i<n;i++){
int a,b,c,t;
cin>>a>>b>>c>>t;
tm[a][b]=tm[b][a]=t;
cm[a][b]=cm[b][a]=c;
}
for(int k=1;k<=100;k++){
for(int j=1;j<=100;j++){
for(int i=1;i<=100;i++){
tm[i][j]=min(tm[i][j],tm[i][k]+tm[k][j]);
cm[i][j]=min(cm[i][j],cm[i][k]+cm[k][j]);
}
}
}
int k;
cin>>k;
while(k--){
int p,q,r;
cin>>p>>q>>r;
cout<<((r)?tm:cm)[p][q]<<endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool used[200001];
int diff[200001];
int n;
vector<int> picked;
void bf() {
if (picked.size() == n) {
for (auto it : picked) cout << it << ' ';
exit(0);
}
int find = picked.back() + diff[picked.size() - 1];
if (find <= 0 || find > n || used[find]) {
return;
} else {
picked.push_back(find);
used[find] = true;
bf();
picked.pop_back();
used[find] = false;
return;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int dsum = 0;
int d_min = 10000001, d_max = -1;
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> diff[i];
dsum += diff[i];
d_min = min(d_min, dsum);
d_max = max(d_max, dsum);
if (diff[i] == 0) {
cout << -1 << '\n';
exit(0);
}
}
if (dsum == 0) {
cout << -1 << '\n';
exit(0);
}
for (int i = 1; i <= n; i++) {
if (dsum > 0 && i + dsum > n) continue;
if (dsum < 0 && i - dsum <= 0) continue;
if (i + d_min <= 0 || i + d_max > n) continue;
picked.push_back(i);
used[i] = true;
bf();
picked.pop_back();
used[i] = false;
}
cout << -1 << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long a[205], b[205];
long long dp[205][10005];
long long n, m;
long long ans;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
memset(dp, -1, sizeof(dp));
dp[0][0] = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
long long t;
cin >> t;
while (t % 2 == 0) t /= 2, a[i]++;
while (t % 5 == 0) t /= 5, b[i]++;
}
for (int i = 1; i <= n; i++) {
for (int j = m; j >= 0; j--) {
for (int k = 10000; k >= 0; k--) {
if (j >= 1 && k >= b[i] && dp[j - 1][k - b[i]] != -1)
dp[j][k] = max(dp[j][k], dp[j - 1][k - b[i]] + a[i]);
}
}
}
for (int i = 0; i <= 10000; i++) {
ans = max(ans, min((long long)i, dp[m][i]));
}
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n, i;
cin >> n;
vector<long long int> a(n);
for (i = 0; i < (n); i++) cin >> a[i];
long long int l, r;
l = 0;
r = n - 1;
long long int cnt1 = 0;
long long int cnt2 = 0;
long long int f = 0;
while (l <= r) {
if (a[l] > a[r]) {
if (f == 0) {
cnt1 += a[l];
f = 1;
} else {
cnt2 += a[l];
f = 0;
}
l++;
} else {
if (f == 0) {
cnt1 += a[r];
f = 1;
} else {
cnt2 += a[r];
f = 0;
}
r--;
}
}
cout << cnt1 << " " << cnt2 << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct work {
int l, r, t, id;
};
int inf = (int)2e9;
struct node {
int to_min, to_sub;
node() { to_min = to_sub = 0; }
};
struct MTree {
vector<node> tree;
int st, d;
MTree(int n) {
st = 1;
d = 0;
while (st < n) {
st *= 2;
d++;
}
tree.resize(st * 2);
for (int i = 0; i < (int)tree.size(); i++) {
tree[i].to_min = inf;
}
}
void flush(int v) {
if (tree[v].to_min != inf) {
tree[v].to_min -= tree[v].to_sub;
}
tree[v].to_sub = 0;
}
inline void fix(int v) {
if (tree[v].to_sub) {
if (v < st) {
int l = v * 2, r = l + 1;
tree[l].to_sub += tree[v].to_sub;
tree[r].to_sub += tree[v].to_sub;
}
flush(v);
}
}
void recalc(int v) {
int l = v * 2, r = l + 1;
tree[v].to_min = min(tree[l].to_min, tree[r].to_min);
}
void fix_down(int i) {
int v = 1, x = d - 1;
while (v != i) {
int f = (i >> x) & 1;
x--;
fix(v);
v = v * 2 + f;
}
fix(v);
}
void go(int v, int vl, int vr, int l, int r, int x) {
if (r < vl || vr < l) {
fix(v);
return;
}
if (l <= vl && vr <= r) {
tree[v].to_sub += x;
fix(v);
return;
}
int nl = v * 2, nr = nl + 1, vc = (vl + vr) / 2;
go(nl, vl, vc, l, r, x);
go(nr, vc + 1, vr, l, r, x);
recalc(v);
}
void sub(int l, int r, int x) { go(1, 0, st - 1, l, r, x); }
int get(int v, int vl, int vr, int l, int r) {
if (r < vl || vr < l) {
return inf;
}
fix(v);
if (l <= vl && vr <= r) {
return tree[v].to_min;
}
int nl = v * 2, nr = nl + 1, vc = (vl + vr) / 2;
int a = get(nl, vl, vc, l, r), b = get(nr, vc + 1, vr, l, r);
return min(a, b);
}
int get(int l, int r) { return get(1, 0, st - 1, l, r); }
int &operator[](int i) {
i += st;
fix_down(i);
return tree[i].to_min;
}
void fix_up(int v) {
v += st;
while (v != 1) {
int u = v ^ 1;
fix(u);
v /= 2;
recalc(v);
}
}
};
int reset(MTree &D, MTree &T, int e, int j, const work &work_j) {
int ne = max(e, work_j.l) + work_j.t;
D[j] = ne - work_j.t - work_j.l;
D.fix_up(j);
T[j + 1] = -work_j.t;
T.fix_up(j + 1);
return ne;
}
int main(void) {
int work_n;
scanf("%d", &work_n);
vector<work> works(work_n + 1);
for (int i = 1; i <= work_n; i++) {
scanf("%d %d %d", &works[i].l, &works[i].r, &works[i].t);
works[i].r++;
works[i].id = i;
}
vector<int> ids(work_n + 1);
int res_i = 0;
MTree T(work_n + 2), D(work_n + 2);
works[0].l = 0;
works[0].r = inf;
works[0].t = 0;
D[0] = 0;
D.fix_up(0);
T[1] = 0;
T.fix_up(1);
for (int i = 1; i <= work_n; i++) {
ids[res_i + 1] = works[i].id;
int old_ans = D[res_i] + works[ids[res_i]].l + works[ids[res_i]].t;
int x_ans = reset(D, T, old_ans, res_i + 1, works[i]);
if (x_ans <= works[i].r) {
res_i++;
printf("0 ");
continue;
}
int best_j = -1, best_sub = 0;
{
int v = 1;
int cur_min = inf, cur_max = 0;
D.fix(v);
while (v < T.st) {
int r = v * 2 + 1;
D.fix(r);
int new_min = min(D.tree[r].to_min, cur_min),
new_max = max(-T.tree[r].to_min, cur_max);
if (new_min <= new_max) {
v = r;
} else {
cur_min = new_min;
cur_max = new_max;
v = r - 1;
D.fix(v);
}
}
cur_min = min(D.tree[v].to_min, cur_min);
cur_max = max(-T.tree[v].to_min, cur_max);
v -= T.st;
int cur = min(cur_min, cur_max);
if (v > 1) {
best_sub = cur;
best_j = v - 1;
}
v++;
if (v <= res_i + 1) {
int need_x = T.get(v, res_i + 1) + 1;
int v = 1;
while (v < T.st) {
v *= 2;
if (T.tree[v + 1].to_min < need_x) {
v++;
}
}
v -= T.st;
cur = min(D.get(v, res_i + 1), -need_x + 1);
if (cur >= best_sub) {
best_sub = cur;
best_j = v - 1;
}
}
}
if (x_ans - best_sub >= old_ans) {
printf("-1 ");
} else {
res_i++;
printf("%d ", ids[best_j]);
int prev_e =
D[best_j - 1] + works[ids[best_j - 1]].l + works[ids[best_j - 1]].t;
reset(D, T, prev_e, best_j, works[0]);
D.sub(best_j + 1, res_i, best_sub);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, a, b;
cin >> n >> m >> a >> b;
cout << min(n * a, (n / m * b + min((n % m) * a, b)));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e6 + 9;
const int Maxv = 3015;
pair<int, int> Que[Maxn * 10];
int bg, ed;
bool arr_mark[Maxv][Maxv];
int arr_num[Maxv][Maxv];
inline int num(pair<int, int> p) {
int x = p.first, y = p.second;
if (abs(x) >= Maxv / 2 - 1 || abs(y) >= Maxv / 2 - 1) return 0;
return arr_num[x + Maxv / 2][y + Maxv / 2];
}
inline void ins(const int &x, const int &y) {
if (!arr_mark[x + Maxv / 2][y + Maxv / 2] && num(make_pair(x, y)) >= 4) {
arr_mark[x + Maxv / 2][y + Maxv / 2] = 1;
Que[ed++] = make_pair(x, y);
ed %= Maxn;
}
}
int main() {
int n;
cin >> n;
arr_num[Maxv / 2][Maxv / 2] = n;
ins(0, 0);
while (bg != ed) {
int x = Que[bg].first, y = Que[bg++].second;
bg %= Maxn;
int cnt = arr_num[x + Maxv / 2][y + Maxv / 2] / 4;
arr_num[x + Maxv / 2][y + Maxv / 2] %= 4;
arr_mark[x + Maxv / 2][y + Maxv / 2] = 0;
for (int r = -1; r < 2; r++) {
for (int l = -1; l < 2; l++) {
if (abs(r + l) == 1) {
int nx = r + x, ny = l + y;
arr_num[nx + Maxv / 2][ny + Maxv / 2] += cnt;
ins(nx, ny);
}
}
}
}
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", num(make_pair(x, y)));
}
return 0;
}
| 2 |
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
using namespace std;
int n ,m;
int main()
{
int cas;
cin >> cas;
for(int i = 0 ;i < cas;i++) {
scanf("%d%d",&m,&n);
if(m *m + n * n <= 1) {
puts("C");
continue;
}
int div = 0;
int sq = sqrt((double) m*m + n*n) + 0.1;
for(int f = -sq; f <= sq;f ++)
for(int g = -sq;g <= sq;g++) {
int tt = f*f + g*g;
if(0 == tt)
continue;
int a = m*f + n*g;
int b = m*g - n*f;
if(0 == a%tt && 0 == b%tt) {
div ++;
}
}
if(8 == div)
puts("P");
else
puts("C");
}
return 0;
} | 0 |
#include<bits/stdc++.h>
#define rep(i,n) for(ll i=0; i<n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
int main(){
ll n, k; cin >> n >> k;
vector<ll> a(n);
rep(i,n) cin >> a[i];
ll ans1=0;
rep(j,n)rep(i,j)
if(a[i]>a[j]) ans1++;
ll ans2=0;
rep(i,n)rep(j,n)
if(a[i]>a[j]) ans2++;
ll ans=(((ans1%mod)*(k%mod))%mod + (ans2%mod)*((k*(k-1)/2l)%mod)%mod)%mod;
cout << ans<< endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(time(0));
long long n;
const long long maxn = 2e5 + 50;
vector<long long> r[maxn], dp(maxn), second(maxn);
void dfs(long long v, long long p = -1) {
dp[v] = 0;
second[v] = 1;
for (auto &u : r[v]) {
if (u == p) continue;
dfs(u, v);
second[v] += second[u];
dp[v] += dp[u];
}
dp[v] += second[v];
}
long long ans = 0;
void recalc(long long v, long long p = -1) {
ans = max(dp[v], ans);
for (auto &u : r[v]) {
if (u == p) continue;
dp[v] -= dp[u] + second[u];
second[v] -= second[u];
dp[u] += dp[v] + second[v];
second[u] += second[v];
recalc(u, v);
dp[u] -= dp[v] + second[v];
second[u] -= second[v];
dp[v] += dp[u] + second[u];
second[v] += second[u];
}
}
void solve() {
cin >> n;
for (long long i = 0; i < n - 1; ++i) {
long long a, b;
cin >> a >> b;
a--;
b--;
r[a].push_back(b);
r[b].push_back(a);
}
dfs(0);
recalc(0);
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug& operator<<(const c&) {
return *this;
}
};
long long n, m, i, j, a, b;
long long solve(long long fi, long long fj) {
long long r1 = abs(i - fi) % a, r2 = abs(j - fj) % b;
if (r1 || r2) return 1e18 + 5;
r1 = abs(i - fi) / a, r2 = abs(j - fj) / b;
if ((r1 + r2) & 1) return 1e18 + 5;
if (r1 < r2) {
if (i - a < 1 && i + a > n) return 1e18 + 5;
}
if (r1 > r2) {
if (j - b < 1 && j + b > m) return 1e18 + 5;
}
return max(r1, r2);
}
bool check() { return i - a > 0 && j - b > 0 && i + a <= n && j + b <= m; }
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> i >> j >> a >> b;
long long c1[] = {1, 1, n, n};
long long c2[] = {1, m, 1, m};
long long ans = 1e18 + 5;
for (long long ii = 0; ii < 4; ii++) {
long long val = solve(c1[ii], c2[ii]);
ans = min(val, ans);
}
if (ans == 1e18 + 5)
cout << "Poor Inna and pony!\n";
else
cout << ans << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d = 0, e = 0;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i > 0) {
e += a[i - 1] - a[i];
if (e < 0) {
d -= e;
e = 0;
}
} else {
d += a[0];
}
}
cout << d;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
char s[MAXN];
int len;
long long a[MAXN];
int n;
long long pre1[MAXN], pre2[MAXN];
long long cnt[5][5], ans[5];
int main() {
pre1[0] = pre2[0] = 0;
for (int i = 1; i <= 50000; i++) {
pre1[i] = pre1[i - 1];
pre2[i] = pre2[i - 1];
if (i & 1)
pre1[i] += i;
else
pre2[i] += i;
}
while (scanf("%s", s) != EOF) {
len = strlen(s);
ans[0] = ans[1] = 0;
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i <= len - 1; i++) {
int c = s[i] - 'a';
if (i & 1) {
ans[0] += cnt[c][0];
ans[1] += cnt[c][1] + 1;
cnt[c][1]++;
} else {
ans[0] += cnt[c][1];
ans[1] += cnt[c][0] + 1;
cnt[c][0]++;
}
}
printf("%I64d %I64d\n", ans[0], ans[1]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 100;
map<int, int> m;
map<int, int> m_;
int d[MAXN];
vector<int> N[MAXN];
bool mark[MAXN];
void dfs(int v) {
mark[v] = 1;
cout << m_[v] << " ";
for (int i = 0; i < N[v].size(); i++)
if (mark[N[v][i]] == 0) dfs(N[v][i]);
}
int main() {
int n;
cin >> n;
int cnt = 0;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
if (m[x] == 0) {
cnt++;
m[x] = cnt;
m_[cnt] = x;
}
if (m[y] == 0) {
cnt++;
m[y] = cnt;
m_[cnt] = y;
}
x = m[x];
y = m[y];
d[x]++;
d[y]++;
N[x].push_back(y);
N[y].push_back(x);
}
for (int i = 1; i <= n; i++)
if (d[i] == 1) {
dfs(i);
cout << endl;
return 0;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct seg {
public:
long long x, y, x1, x2, y1, y2;
};
bool pl(seg a, seg b) {
if (a.y <= b.y2 && a.y >= b.y1 && b.x <= a.x2 && b.x >= a.x1)
return true;
else
return false;
}
long long f(seg a, seg b) {
long long x = b.x;
long long y = a.y;
long long ans = x - a.x1;
ans = min(ans, a.x2 - x);
ans = min(ans, y - b.y1);
ans = min(ans, b.y2 - y);
return ans;
}
seg v[1003];
seg h[1003];
int main() {
int n, m;
cin >> n >> m;
long long x, y, l;
for (int i = 0; i < n; i++) {
cin >> x >> y >> l;
h[i].x = h[i].x1 = h[i].x2 = x;
h[i].y = y;
h[i].y1 = y;
h[i].y2 = y + l;
}
for (int i = 0; i < m; i++) {
cin >> x >> y >> l;
v[i].y = v[i].y1 = v[i].y2 = y;
v[i].x = v[i].x1 = x;
v[i].x2 = x + l;
}
long long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (pl(v[j], h[i])) {
ans = max(ans, f(v[j], h[i]));
}
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
const int maxn = 1e5 + 100, mod = 1e9 + 7;
using namespace std;
long long fib[51];
bool swap(int* arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int binarySearch(int p1, int p2, long long target) {
if (p1 > p2) {
return p2;
}
int pivot = (p1 + p2) / 2;
if (fib[pivot] == target) {
return pivot;
} else if (fib[pivot] > target) {
return binarySearch(p1, pivot - 1, target);
} else {
if ((pivot < p2 && fib[pivot + 1] > target)) {
return pivot;
}
return binarySearch(pivot + 1, p2, target);
}
}
int main() {
int n;
long long k;
cin >> n >> k;
int arr[51];
fib[0] = 1;
fib[1] = 1;
for (int(i) = (1); (i) < (51); ++(i)) {
arr[i] = i;
if (i >= 2) fib[i] = fib[i - 1] + fib[i - 2];
}
int first = 1, last = n, tot = n, index;
long long cur = 1;
while (cur < k && tot) {
if ((k - cur + 1) > fib[tot - 1]) {
swap(arr, first, first + 1);
cur += (fib[tot - 1]);
tot -= 2;
first += 2;
} else {
index = binarySearch(1, tot - 1, k - cur + 1);
if (fib[index] == k - cur + 1) {
index--;
}
if (index < 0) break;
tot = index + 1;
first = last - tot + 1;
}
}
for (int(i) = (1); (i) < (n + 1); ++(i)) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int MinDist(int n, int k, const string& s) {
int first_cow_pos = 0;
while (first_cow_pos < n && s[first_cow_pos] == '1') {
++first_cow_pos;
}
int last_cow_pos = first_cow_pos;
int rooms = 1;
while (rooms < k + 1) {
++last_cow_pos;
if (s[last_cow_pos] == '0') {
++rooms;
}
}
int farmer_pos = first_cow_pos;
int delta = last_cow_pos - first_cow_pos;
for (int i = first_cow_pos + 1; i <= last_cow_pos; ++i) {
if (s[i] == '0' && i - first_cow_pos < delta && last_cow_pos - i < delta) {
delta = max(i - first_cow_pos, last_cow_pos - i);
farmer_pos = i;
}
}
--rooms;
for (++first_cow_pos; first_cow_pos < n; ++first_cow_pos) {
if (s[first_cow_pos] == '1') {
continue;
}
while (rooms < k + 1 && last_cow_pos < n) {
++last_cow_pos;
if (last_cow_pos == n) {
break;
}
if (s[last_cow_pos] == '0') {
++rooms;
}
}
if (last_cow_pos == n) {
break;
}
int npos = max(farmer_pos, first_cow_pos);
int cur_delta = max(farmer_pos - first_cow_pos, last_cow_pos - farmer_pos);
while (npos < last_cow_pos) {
++npos;
if (s[npos] == '1') {
continue;
}
if (npos - first_cow_pos < cur_delta && last_cow_pos - npos < cur_delta) {
cur_delta = max(npos - first_cow_pos, last_cow_pos - npos);
farmer_pos = npos;
} else if (npos - first_cow_pos >= cur_delta) {
break;
}
}
delta = min(delta, cur_delta);
--rooms;
}
return delta;
}
int MinDistSlow(int n, int k, const string& s) {
int result = n;
for (int fcp = 0; fcp < n; ++fcp) {
if (s[fcp] == '1') {
continue;
}
int lcp = fcp;
int rooms = 1;
while (rooms < k + 1 && lcp < n) {
++lcp;
if (lcp == n) {
break;
}
if (s[lcp] == '0') {
++rooms;
}
}
if (lcp == n) {
break;
}
for (int fp = fcp; fp <= lcp; ++fp) {
if (s[fp] == '1') {
continue;
}
int delta = max(fp - fcp, lcp - fp);
if (delta < result) {
result = delta;
}
}
}
return result;
}
int main() {
int n, k;
cin >> n >> k;
string s;
char buffer[1000000];
scanf("%s", buffer);
s = buffer;
int delta = MinDist(n, k, s);
cout << delta << endl;
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b, c; cin >> a >> b >> c;
for (int i = 1; i <= (int)1e7; i++){
if (a & 1 || b & 1 || c & 1){
cout << i-1 << "\n";
return 0;
}
int aa = a/2, bb = b/2, cc = c/2;
a = bb+cc;
b = cc+aa;
c = aa+bb;
}
cout << "-1\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
int main() {
int a, b, x, y;
scanf("%d%d%d%d", &a, &b, &x, &y);
int k = gcd(x, y);
x /= k;
y /= k;
k = min(a / x, b / y);
printf("%d %d\n", k * x, k * y);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long mod(long long a, long long n) {
if (n < 0) n *= -1;
if (a >= 0) return a % n;
a = (-a) % n;
if (a < 0) a += n;
return a;
}
long long gcd(long long a, long long b) {
if (a < 0) a *= -1;
if (b < 0) b *= -1;
if (a < b) return gcd(b, a);
if (a % b == 0) return b;
return gcd(b, a % b);
}
pair<long long, long long> sol(long long a, long long b) {
pair<long long, long long> p1, p2, p3;
long long m, n, q;
if (a < b) {
pair<long long, long long> p = sol(b, a);
return pair<long long, long long>(p.second, p.first);
}
p1 = pair<long long, long long>(1, 0);
p2 = pair<long long, long long>(0, 1);
m = a;
n = b;
while (n != 1) {
q = m / n;
p2.first = p1.first - q * p2.first;
p2.second = p1.second - q * p2.second;
p1.first = (p1.first - p2.first) / q;
p1.second = (p1.second - p2.second) / q;
n = m % n;
m = (m - n) / q;
}
return p2;
}
long long pro(long long a, long long b, long long c) {
long long q1, q2, r1, r2;
r1 = mod(a, c);
if (a > 0 || r1 == 0)
q1 = a / c;
else
q1 = -(a / c + 1);
r2 = mod(b, c);
if (b > 0 || r2 == 0)
q2 = b / c;
else
q2 = -(b / c + 1);
long long res = q1 + q2;
if (r1 + r2 >= c) res++;
return res;
}
int main() {
long long A, B, C;
cin >> A >> B >> C;
if (C == 0)
cout << "0 0\n";
else if (A == 0) {
if (mod(C, B) == 0)
cout << "0 " << -C / B << endl;
else
cout << "-1\n";
} else if (B == 0) {
if (mod(C, A) == 0)
cout << -C / A << " 0\n";
else
cout << "-1\n";
} else {
if (A < 0) {
A *= -1;
B *= -1;
C *= -1;
}
long long D = gcd(A, B);
if (mod(C, D) != 0)
cout << "-1\n";
else {
A /= D;
B /= D;
C /= D;
long long l1, r1, l2, r2;
pair<long long, long long> p;
if (B > 0) {
p = sol(A, B);
p.first *= -C;
p.second *= -C;
} else {
p = sol(A, -B);
p.second *= -1;
p.first *= -C;
p.second *= -C;
}
cout << p.first << " " << p.second << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename __T>
inline void read(__T &x) {
x = 0;
int f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
x *= f;
}
pair<int, int> mp(int a, int b) { return make_pair(a, b); }
const int mod = 1000000007;
long long qpow(long long a, long long b = mod - 2) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans;
}
int popc(int n) { return __builtin_popcount(n); }
int n, k;
bool check(int n, int k) {
if ((n & 1) == 0) return 0;
if (n == 1 && k == 0) return 1;
if (n == 9 && k == 2) return 0;
int sb = (n - 3) / 2;
if (k > sb) return 0;
if (k < 0) return 0;
if (n < 0) return 0;
if (popc(n + 1) == 1) return k != 1;
return k != 0;
}
int tot = 0;
int faa[100005];
bool ckv(int a, int b) { return a >= 2 * b || b >= 2 * a; }
void dfs(int n, int k, int fa) {
faa[++tot] = fa;
int now = tot;
if (n == 1) return;
if (k == 0) {
dfs(n / 2, 0, now);
dfs(n / 2, 0, now);
return;
}
bool jb = 0;
for (int a = 1; a <= n && jb == 0; a += 2) {
bool fg = ckv(a, n - a - 1);
int mv = min(a - 1, k);
for (int x = 0; x <= mv; x++) {
if (check(a, x) == 0) continue;
if (check(n - a - 1, k - x - fg) == 0) continue;
jb = 1;
dfs(a, x, now);
dfs(n - a - 1, k - x - fg, now);
break;
}
}
if (jb == 0) cout << "SMJB" << n << ' ' << k << endl;
}
int main() {
int T;
T = 1;
while (T--) {
read(n);
read(k);
if (check(n, k) == 0) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
dfs(n, k, 0);
for (int i = 1; i <= n; i++) cout << faa[i] << ' ';
cout << endl;
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
string s, t;
cin >> s >> t;
long long sa = 0, sb = 0, ta = 0, tb = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == 'a')
sa++;
else
sb++;
if (t[i] == 'a')
ta++;
else
tb++;
}
if ((ta + sa) % 2 == 1 || (tb + sb) % 2 == 1) return cout << -1, 0;
vector<pair<long long, long long> > ans;
vector<long long> va, vb;
for (long long i = 0; i < n; i++) {
if (s[i] != t[i]) {
if (s[i] == 'a')
va.push_back(i);
else
vb.push_back(i);
}
}
{
for (long long i = 0;
i < (long long)va.size() && (long long)va.size() > i + 1; i += 2)
ans.push_back(make_pair(va[i], va[i + 1]));
for (long long i = 0;
i < (long long)vb.size() && (long long)vb.size() > i + 1; i += 2)
ans.push_back(make_pair(vb[i], vb[i + 1]));
}
if ((long long)va.size() % 2 == 1) {
ans.push_back(
make_pair(va[(long long)va.size() - 1], va[(long long)va.size() - 1]));
ans.push_back(
make_pair(va[(long long)va.size() - 1], vb[(long long)vb.size() - 1]));
}
cout << ans.size() << "\n";
for (long long i = 0; i < (long long)ans.size(); i++)
cout << ans[i].first + 1 << " " << ans[i].second + 1 << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> m;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--) {
m.clear();
int n;
cin >> n;
int a[n + 1], mx = 0, cnt = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
m[a[i]]++;
mx = max(mx, m[a[i]]);
}
for (int i = 1; i <= n; i++)
if (m[i] == mx) cnt++;
int ans = 0;
if (cnt == 1)
ans = (n - mx) / (mx - 1);
else {
n -= mx * (cnt - 1);
ans = (n - mx) / (mx - 1);
ans += (cnt - 1);
}
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
int a, b, temp;
scanf("%d %d", &a, &b);
if (a > b) {
temp = a;
a = b;
b = temp;
}
printf("%d %d\n", a, (b - a) / 2);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long l[10003], s[10003];
int main() {
long i, j, k, m, n;
char c[10003];
cin >> n >> m;
for (i = 1; i <= n; ++i) {
cin >> (c + 1);
l[0] = 0;
for (j = 1; j <= m; ++j)
if (c[j] == '1') l[++l[0]] = j;
if (l[0] == 0) {
cout << -1 << endl;
return 0;
}
l[++l[0]] = l[1] + m;
for (j = 1; j < l[0]; ++j) {
for (k = (l[j] + l[j + 1]) >> 1; k >= l[j]; --k)
s[(k - 1) % m + 1] += k - l[j];
for (k = ((l[j] + l[j + 1]) >> 1) + 1; k <= l[j + 1]; ++k)
s[(k - 1) % m + 1] += l[j + 1] - k;
}
}
k = 100000000;
for (i = 1; i <= m; ++i) k = k < s[i] ? k : s[i];
cout << k << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int a, b, y = 0;
cin >> a >> b;
int arr1[a], arr2[a];
for (int i = 0; i < a; i++) {
cin >> arr1[i];
}
for (int i = 0; i < a; i++) {
cin >> arr2[i];
}
sort(arr1, arr1 + a);
sort(arr2, arr2 + a, greater<int>());
if (b == 0) {
cout << accumulate(arr1, arr1 + a, y) << endl;
continue;
}
for (int i = 0; i < a; i++) {
if (arr1[i] < arr2[i]) {
arr1[i] = arr2[i];
b -= 1;
}
if (b <= 0) break;
}
cout << accumulate(arr1, arr1 + a, y) << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int mat[105][105];
int col[105];
int row[105];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> mat[i][j];
}
}
bool flag = 0;
while (!flag) {
flag = 1;
for (int i = 0; i < n && flag; i++) {
int sum = 0;
for (int j = 0; j < m; j++) {
sum += mat[i][j];
}
if (sum < 0) {
for (int j = 0; j < m; j++) {
mat[i][j] *= -1;
}
row[i]++;
flag = 0;
}
}
for (int j = 0; j < m && flag; j++) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += mat[i][j];
}
if (sum < 0) {
for (int i = 0; i < n; i++) {
mat[i][j] *= -1;
}
col[j]++;
flag = 0;
}
}
}
vector<int> rans, cans;
for (int i = 0; i < n; i++) {
if (row[i] & 1) rans.push_back(i + 1);
}
for (int i = 0; i < m; i++) {
if (col[i] & 1) cans.push_back(i + 1);
}
cout << rans.size();
for (auto it : rans) {
cout << ' ' << it;
}
cout << '\n';
cout << cans.size();
for (auto it : cans) {
cout << ' ' << it;
}
cout << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
int H[100050], parent[100050], sz[100050], L[100050 << 1], E[100050 << 1],
M[100050][20], SEG[(100050 << 3) + 15];
int n, timer;
int dfs(int node, int depth, int perr) {
sz[node] = 1;
H[node] = timer;
E[timer] = node;
L[timer++] = depth;
parent[node] = perr;
for (int i = 0; i < adj[node].size(); ++i) {
if (adj[node][i] != perr) {
sz[node] += dfs(adj[node][i], depth + 1, node);
E[timer] = node;
L[timer++] = depth;
}
}
return sz[node];
}
void buildKthParent() {
memset(M, -1, sizeof(M));
for (int i = 0; i < n; ++i) M[i][0] = parent[i];
for (int j = 1; (1 << j) < n; ++j)
for (int i = 1; i < n; ++i)
if (M[i][j - 1] != -1) M[i][j] = M[M[i][j - 1]][j - 1];
return;
}
void construct(int index, int st, int en) {
if (st == en) {
SEG[index] = st;
return;
}
if ((index << 1) + 2 < (100050 << 3)) {
int mid = st + ((en - st) >> 1);
construct((index << 1) + 1, st, mid);
construct((index << 1) + 2, mid + 1, en);
if (L[SEG[(index << 1) + 1]] < L[SEG[(index << 1) + 2]])
SEG[index] = SEG[(index << 1) + 1];
else
SEG[index] = SEG[(index << 1) + 2];
}
return;
}
int Query(int index, int Rs, int Re, int first, int last) {
if (Rs >= first and Re <= last) return SEG[index];
if (Re < first || Rs > last) return timer;
if ((index << 1) + 2 < (100050 << 3)) {
int mid = Rs + ((Re - Rs) >> 1);
int x = Query((index << 1) + 1, Rs, mid, first, last);
int y = Query((index << 1) + 2, mid + 1, Re, first, last);
if (L[x] < L[y]) return x;
return y;
}
return timer;
}
int LCA(int u, int v) {
int idx = Query(0, 0, timer - 1, H[u], H[v]);
return E[idx];
}
int getKthParent(int node, int kth) {
int lg = log2(L[H[node]]);
for (int i = lg; i > -1; --i)
while (L[H[node]] - (1 << i) >= kth) node = M[node][i];
return node;
}
int main() {
scanf("%d", &n);
adj.resize(n + 5);
for (int i = 1; i < n; ++i) {
int a, b;
scanf("%d%d", &a, &b);
adj[a - 1].push_back(b - 1);
adj[b - 1].push_back(a - 1);
}
dfs(0, 0, -1);
L[timer] = (1 << 30);
buildKthParent();
construct(0, 0, timer - 1);
int m;
scanf("%d", &m);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
--a;
--b;
if (H[a] > H[b]) swap(a, b);
if (a == b) {
printf("%d\n", n);
continue;
}
int node = LCA(a, b);
int len = L[H[a]] + L[H[b]] - (L[H[node]] << 1);
if (len & 1)
printf("0\n");
else {
int ans;
if (L[H[a]] < L[H[b]]) swap(a, b);
if (L[H[node]] < L[H[a]] and L[H[node]] < L[H[b]]) {
len >>= 1;
--len;
int node1 = getKthParent(a, L[H[a]] - len);
int node2 = parent[node1];
if (node2 == node) {
int node3 = getKthParent(b, L[H[b]] - len);
ans = n - sz[node3] - sz[node1];
} else
ans = sz[node2] - sz[node1];
} else {
len >>= 1;
--len;
int node1 = getKthParent(a, L[H[a]] - len);
int node2 = parent[node1];
ans = sz[node2] - sz[node1];
}
printf("%d\n", ans);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
char st[10000];
int len, f[30];
int main() {
scanf("%s", st);
len = strlen(st);
memset(f, 0, sizeof(f));
int o = 0;
for (int i = 0; i < len; i++)
if (st[i] == '*') f[++o] = i;
long long ans = 0;
long long fac = 1;
long long sum;
for (int i = 0; i < len; i++) {
if (st[i] >= '0' && st[i] <= '9') {
fac *= st[i] - '0';
if (st[i + 1] != '*') {
ans += fac;
fac = 1;
}
}
}
for (int i = 1; i <= o; i++) {
st[f[i]] = '.';
sum = 0;
fac = 1;
for (int j = 0; j < f[i]; j++) {
if (st[j] >= '0' && st[j] <= '9') {
fac *= st[j] - '0';
if (st[j + 1] != '*') {
sum += fac;
fac = 1;
}
}
}
st[f[i]] = '*';
fac = sum;
sum = 0;
for (int j = f[i] + 1; j < len; j++) {
if (st[j] >= '0' && st[j] <= '9') {
fac *= st[j] - '0';
if (st[j + 1] != '*') {
sum += fac;
fac = 1;
}
}
}
if (sum > ans) ans = sum;
sum = 0;
fac = 1;
for (int j = f[i] + 1; j < len; j++) {
if (st[j] >= '0' && st[j] <= '9') {
fac *= st[j] - '0';
if (st[j + 1] != '*') {
sum += fac;
fac = 1;
}
}
}
long long last = sum;
sum = 0;
fac = 1;
for (int j = 0; j < f[i]; j++) {
if (st[j] >= '0' && st[j] <= '9') {
fac *= st[j] - '0';
if (st[j + 1] != '*') {
sum += fac;
fac = 1;
}
}
}
sum = sum + fac * last;
if (sum > ans) ans = sum;
}
for (int i = 1; i < o; i++)
for (int j = i + 1; j <= o; j++) {
st[f[j]] = '.';
sum = 0;
fac = 1;
for (int k = f[i] + 1; k < f[j]; k++) {
if (st[k] >= '0' && st[k] <= '9') {
fac *= st[k] - '0';
if (st[k + 1] != '*') {
sum += fac;
fac = 1;
}
}
}
st[f[j]] = '*';
long long last = sum;
sum = 0;
fac = 1;
for (int k = 0; k < f[i]; k++) {
if (st[k] >= '0' && st[k] <= '9') {
fac *= st[k] - '0';
if (st[k + 1] != '*') {
sum += fac;
fac = 1;
}
}
}
fac *= last;
for (int k = f[j] + 1; k < len; k++) {
if (st[k] >= '0' && st[k] <= '9') {
fac *= st[k] - '0';
if (st[k + 1] != '*') {
sum += fac;
fac = 1;
}
}
}
if (sum > ans) ans = sum;
}
printf("%I64d\n", ans);
return 0;
}
| 5 |
#include<iostream>
#include<vector>
using namespace std;
long long n,k;
vector<long long> a;
bool C(long long x){
long long num=0;
for(int i=0;i<n;++i){
num+=(a[i]+x-1)/x-1;
}
return num<=k;
}
int main(){
cin>>n>>k;
a.resize(n);
for(long long i=0;i<n;++i) cin>>a[i];
long long l=0,r=1e9+7;
while(r-l>1){
long long mid=(r+l)/2;
(C(mid)?r:l)=mid;
}
cout<<r<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > a;
vector<pair<int, int> > res;
int pos[111111];
int rpos[111111];
vector<bool> c;
int n;
bool good(int x) {
while (x != 0) {
int d = x % 10;
if (d != 4 && d != 7) return false;
x /= 10;
}
return true;
}
void fix(int from, int to) {
if (rpos[to] == rpos[from]) return;
res.push_back(make_pair(rpos[to], rpos[from]));
swap(pos[rpos[to]], pos[rpos[from]]);
swap(rpos[from], rpos[to]);
}
void change(int ind, int luck) {
int from = a[ind].second;
int to = pos[ind];
if (from == to) return;
if (c[from] || c[to]) {
fix(from, to);
} else {
fix(to, luck);
fix(luck, from);
}
}
int main() {
cin >> n;
bool ans = false;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
bool g = good(x);
a.push_back(make_pair(x, i));
c.push_back(g);
ans |= g;
}
sort(a.begin(), a.end());
if (!ans) {
for (int i = 0; i < n; i++) {
if (a[i].second != i) {
cout << -1;
return 0;
}
}
cout << 0 << endl;
return 0;
}
int ind = 0;
for (int i = 0; i < n; i++) {
if (c[i]) ind = i;
}
for (int i = 0; i < n; i++) {
pos[i] = i;
}
for (int i = 0; i < n; i++) {
rpos[i] = i;
}
for (int i = 0; i < n; i++) {
change(i, ind);
}
cout << res.size() << endl;
for (int i = 0; i < res.size(); i++) {
cout << res[i].first + 1 << ' ' << res[i].second + 1 << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100, maxc = 115, inf = 1e9 + 10;
long long t, n, f[100], dp1[100], dp2[100];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
f[0] = f[1] = 1;
for (int i = 2; f[i - 1] < (long long)1e18; i++) f[i] = f[i - 1] + f[i - 2];
cin >> t;
while (t--) {
int k = 0;
cin >> n;
vector<int> v;
v.clear();
for (int i = 90; i > 0 && n > 0; i--)
if (n >= f[i] && f[i] > 0) v.push_back(i), n -= f[i], k++;
v.push_back(0);
reverse(v.begin(), v.end());
dp1[0] = 1;
dp2[0] = 0;
for (int i = 1; i <= k; i++) {
dp1[i] = dp1[i - 1] + dp2[i - 1];
dp2[i] = dp1[i - 1] * ((v[i] - v[i - 1] - 1) / 2) +
dp2[i - 1] * ((v[i] - v[i - 1]) / 2);
}
cout << dp1[k] + dp2[k] << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 330;
const int mod = 1e9 + 7;
string s[N];
int st[N], en[N];
int n, m;
int len[N];
string fi[N], se[N];
bitset<20000> ok[N][14];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
len[i] = s[i].length();
for (int k = 0; k < min((int)s[i].length(), 13); ++k) fi[i] += s[i][k];
for (int k = max(0, (int)s[i].length() - 13); k < s[i].length(); ++k)
se[i] += s[i][k];
for (int j = 0; j < s[i].length(); ++j) {
int val = 0;
for (int k = 1; k <= 13 && k + j - 1 < (int)s[i].length(); ++k) {
val = val * 2 + s[i][k + j - 1] - '0';
if (val < 20000) ok[i][k][val] = 1;
}
}
}
cin >> m;
for (int i = n + 1; i <= m + n; ++i) {
int a, b;
cin >> a >> b;
int cur_ans = 0;
for (int k = 1; k <= 13; ++k) {
ok[i][k] |= (ok[a][k] | ok[b][k]);
for (int pref = 1; pref <= k; ++pref) {
if (se[a].length() < pref) continue;
int p = (int)se[a].length() - pref;
int val = 0;
for (int u = 0; u < pref; ++u) val = val * 2 + se[a][p++] - '0';
if ((int)fi[b].length() < k - pref) continue;
for (int u = 0; u + pref < k; ++u) val = val * 2 + fi[b][u] - '0';
if (val < 20000) ok[i][k][val] = 1;
}
if (ok[i][k].count() == (1 << k)) cur_ans = max(cur_ans, k);
}
int p = 0;
fi[i] = fi[a];
se[i] = se[b];
while (fi[i].length() < 13 && p < fi[b].length()) fi[i] += fi[b][p++];
p = (int)se[a].length() - 1;
while (se[i].length() < 13 && p >= 0) {
se[i] = se[a][p] + se[i];
p--;
}
cout << cur_ans << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)(1e9) + 7;
const int SIZE = 2020;
int d, n;
int a[SIZE];
vector<int> path[SIZE];
long long f[SIZE];
bool vis[SIZE];
void dfs(int x, int root) {
f[x] = 1;
for (int i = 0; i < path[x].size(); i++) {
int son = path[x][i];
if (vis[son] || !(a[son] >= a[root] && a[son] <= a[root] + d) ||
(a[son] == a[root] && son < root))
continue;
vis[son] = 1;
dfs(son, root);
f[x] = (f[x] * (f[son] + 1)) % MOD;
}
}
int main() {
int x, y;
while (cin >> d >> n) {
long long ans = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
path[i].clear();
}
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
x--, y--;
path[x].push_back(y);
path[y].push_back(x);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
f[j] = 0;
vis[j] = 0;
}
vis[i] = 1;
dfs(i, i);
ans = (ans + f[i]) % MOD;
}
cout << ans << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int HASH = 1000000007;
map<int, int> node, sum;
int h, q;
double dfs(int root, int v) {
if (sum[root] <= v) {
return v;
}
return (dfs(root << 1, max(v, node[root] + sum[root << 1 | 1])) +
dfs(root << 1 | 1, max(v, node[root] + sum[root << 1]))) /
2.0;
}
int main() {
scanf("%d %d", &h, &q);
while (q--) {
char str[10];
scanf("%s", str);
if (str[0] == 'a') {
int p, v;
scanf("%d %d", &p, &v);
node[p] += v;
while (p) {
sum[p] += v;
p >>= 1;
}
} else {
printf("%.10f\n", dfs(1, 0));
}
}
return 0;
}
| 4 |
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int a[303030];
void jud(int l,int r)
{
int now1=0,now2=0;
for(int i=l;i<=r;i+=2)
{
if(i<a[i])
{
if(a[i]>now1)
now1=a[i];
else
{
printf("No");
exit(0);
}
}
else
{
if(a[i]>now2)
now2=a[i];
else
{
printf("No");
exit(0);
}
}
if(a[i]<l||a[i]>r)
{
printf("No");
exit(0);
}
}
}
int main()
{
int n,l=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
a[n+1]=n+1;
a[n+2]=n+2;
n+=2;
for(int i=1;i<=n;i++)
{
if(a[i]!=i)
{
if(l==0)
l=i;
else if((i-l)&1)
{
jud(l,i-1);
l=i;
}
}
else
{
if(l&&((i-l)&1)==0)
{
jud(l,i-2);
l=0;
}
}
}
printf("Yes");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, sum[100009], vis[100009], m, n, d, k, mn, mx, ans = 1;
vector<long long> v[100009];
map<long long, long long> mp;
char par[1000009];
int main() {
cin >> n;
mp[n]++;
n++;
while (1) {
if (n % 10 == 0) {
n /= 10;
} else {
if (mp[n] > 0) {
break;
} else {
mp[n]++;
n++;
}
}
}
cout << mp.size();
}
| 1 |
#include <bits/stdc++.h>
int main() {
int n, bars[1000], i, j;
int total, lgst = 1, lcheck;
while (scanf("%d", &n) != EOF) {
total = n;
lgst = 1;
for (i = 0; i < n; i++) {
scanf("%d", &bars[i]);
}
lcheck = 1;
for (i = 0; i < n; i++) {
lgst = 1;
for (j = i + 1; j < n; j++) {
if (bars[j] == -1) {
continue;
} else if (bars[i] == bars[j]) {
bars[j] = -1;
lgst++;
total--;
}
}
if (lgst > lcheck) {
lcheck = lgst;
}
}
printf("%d %d\n", lcheck, total);
}
scanf(" ");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
long long int power(long long int x, long long int y) {
long long int ans = 1;
x %= 1000000007;
while (y) {
if (y & 1) ans = (x * ans) % 1000000007;
x = (x * x) % 1000000007;
y >>= 1;
}
return ans;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
void solve() {
string str;
cin >> str;
long long int curr[3];
cin >> curr[0] >> curr[1] >> curr[2];
long long int cost[3];
cin >> cost[0] >> cost[1] >> cost[2];
long long int money;
cin >> money;
long long int pres[3] = {0};
for (char c : str) {
if (c == 'B')
pres[0]++;
else if (c == 'S')
pres[1]++;
else
pres[2]++;
}
long long int lo = 0;
long long int ans = 0;
long long int hi = 1e14;
while (lo < hi) {
long long int mid = hi - (hi - lo) / 2;
long long int sum = 0;
for (int i = 0; i < 3; i++) {
long long int req = mid * pres[i];
long long int rem = 0;
if (req > curr[i]) rem = req - curr[i];
sum += (rem * cost[i]);
}
if (sum > money) {
hi = mid - 1;
} else {
lo = mid;
}
}
cout << lo << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int x, ma = 0, mi = 101, n, m, mm = 101;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &x), ma = max(ma, x), mi = min(mi, x);
ma = max(ma, mi * 2);
for (int i = 1; i <= m; i++) scanf("%d", &x), mm = min(mm, x);
if (mm > ma)
printf("%d", ma);
else
printf("-1");
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;
class LowerEnvelope{ typedef long long LL; int s, t; vector<LL> deq_a; vector<LL> deq_b; inline bool check(LL a1, LL b1, LL a2, LL b2, LL a3, LL b3) const { return (a2 - a1) * (b3 - b2) >= (b2 - b1) * (a3 - a2); } public: LowerEnvelope(int n) : s(0), t(0), deq_a(n), deq_b(n) {} void push(LL a, LL b){ while(s + 1 < t && check(deq_a[t - 2], deq_b[t - 2], deq_a[t - 1], deq_b[t - 1], a, b)) t--; deq_a[t] = a; deq_b[t++] = b; } LL minimum(LL x){ assert(s < t); while(s + 1 < t && deq_a[s] * x + deq_b[s] >= deq_a[s + 1] * x + deq_b[s + 1]) s++; return deq_a[s] * x + deq_b[s]; } };
LL INF = 10000000000000000LL;
int main(){
int N, M, D, X;
cin >> N >> M >> D >> X;
vector<int> p(N);
REP(i, N) cin >> p[i];
vector<vector<int>> right(D);
for(int i = 0; i < M; i++){
int a, b;
cin >> a >> b;
int x = a;
int y = abs(b);
for(int d = 0; d < D; d++){
right[d].push_back(x - y);
x += X;
}
}
REP(d, D) sort(right[d].begin(), right[d].end());
int memo[101][10001];
memset(memo, -1, sizeof(memo));
auto w = [&](int d, int i) -> int {
if(memo[d][i] != -1) return memo[d][i];
auto itr = lower_bound(right[d].begin(), right[d].end(), p[i]);
return memo[d][i] = M - (itr - right[d].begin());
};
vector<LL> cur(N, INF);
cur[0] = 0;
for(int d = 0; d < D; d++){
vector<LL> next(N, INF);
LowerEnvelope lowenv(N);
for(int i = 0; i < N; i++){
if(cur[i] < INF){
lowenv.push(w(d, i), cur[i] - (long long)w(d, i) * p[i]);
}
next[i] = lowenv.minimum(p[i]);
}
cur.swap(next);
}
cout << cur[N - 1] << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long tree[4 * 1000000];
void upd(int i, int s, int e, int s1, int e1, long long v) {
if (s > e1 || s1 > e) return;
if (s >= s1 && e <= e1) {
tree[i] = (tree[i] + v % mod) % mod;
return;
}
upd((i * 2), s, ((s + e) / 2), s1, e1, v);
upd((i * 2 + 1), ((s + e) / 2) + 1, e, s1, e1, v);
tree[i] = (tree[(i * 2)] + tree[(i * 2 + 1)]) % mod;
}
long long qur(int i, int s, int e, int s1, int e1) {
if (s > e1 || s1 > e) return 0;
if (s >= s1 && e <= e1) {
return tree[i];
}
long long p1 = qur((i * 2), s, ((s + e) / 2), s1, e1);
long long p2 = qur((i * 2 + 1), ((s + e) / 2) + 1, e, s1, e1);
return (p1 + p2) % mod;
}
long long pos[1000000 + 1];
int a[100000 + 2];
int n;
int mx = 1000000 + 1;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < 4 * 1000000; i++) tree[i] = 0;
for (int i = 0; i < 1000000 + 1; i++) pos[i] = 0;
long long ans = 0;
for (int i = 0; i < n; i++) {
long long v =
(((1 + qur(1, 0, mx, 0, a[i])) % mod) * (long long)a[i]) % mod;
long long k = ((v - pos[a[i]]) % mod + mod) % mod;
ans += k;
ans %= mod;
pos[a[i]] += k;
pos[a[i]] %= mod;
upd(1, 0, mx, a[i], a[i], k);
}
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, l, r, i, j, x, ans = -1;
void work(long long x) {
if (x > n && x <= 2 * n) ans = max(ans, x - n);
}
int main() {
cin >> n >> l >> r >> m;
r = (r - l + 1 + n) % n;
if (m < r) {
puts("-1");
return 0;
}
for (i = n; i <= m && i <= 2 * n; i = j + 1) {
j = min(2 * n, m / (m / i));
x = min(2 * n,
min((m - r) / (m / i), (m + 2 * n - 2 * r + 1) / (m / i + 1)));
if (x >= i && m - m / i * x <= r + min(r, x - n)) ans = x - n;
}
if (!r)
for (i = 1; i * i <= m + 1; i++)
if ((m + 1) % i == 0) {
work(i);
work((m + 1) / i);
}
if (m >= r && m <= 2 * r) ans = max(ans, m + (m < 2 * r) + n - 2 * r);
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a1[200000], a2[200000];
int s1[200000], s2[200000], d1[200000], d2[200000];
long long rot(long long q, long long w, long long e, long long r, long long t,
long long y) {
return (e - q) * (y - w) - (t - q) * (r - w);
}
int cmp(long long c1, long long c2, long long v1, long long v2) {
long long S = rot(s1[0], s2[0], c1, c2, v1, v2);
if (S < 0) {
return -1;
}
if (S > 0) {
return 1;
}
long long len1 = (c1 - s1[0]) * (c1 - s1[0]) + (c2 - s2[0]) * (c2 - s2[0]);
long long len2 = (v1 - s1[0]) * (v1 - s1[0]) + (v2 - s2[0]) * (v2 - s2[0]);
if (len1 < len2) {
return -1;
}
if (len1 > len2) {
return 1;
}
return 0;
}
void qs(int q, int w) {
int e = q, r = w, y = q + (rand() % (w - q + 1));
int t1 = s1[y], t2 = s2[y];
do {
while (cmp(s1[e], s2[e], t1, t2) < 0) e++;
while (cmp(s1[r], s2[r], t1, t2) > 0) r--;
if (e <= r) {
swap(s1[e], s1[r]);
swap(s2[e], s2[r]);
e++;
r--;
}
} while (e <= r);
if (q < r) qs(q, r);
if (e < w) qs(e, w);
}
int cmp2(int c1, int c2, int v1, int v2) {
if (c1 < v1) {
return -1;
}
if (c1 > v1) {
return 1;
}
if (c2 < v2) {
return -1;
}
if (c2 > v2) {
return 1;
}
return 0;
}
void qs2(int q, int w) {
int e = q, r = w, y = q + (rand() % (w - q + 1));
int t1 = a1[y], t2 = a2[y];
do {
while (cmp2(a1[e], a2[e], t1, t2) < 0) e++;
while (cmp2(a1[r], a2[r], t1, t2) > 0) r--;
if (e <= r) {
swap(a1[e], a1[r]);
swap(a2[e], a2[r]);
e++;
r--;
}
} while (e <= r);
if (q < r) qs2(q, r);
if (e < w) qs2(e, w);
}
int q, lAns1, lAns2, numV;
int sch(int e, int r) {
int c1 = 0, c, v;
for (v = 1 << 17; v; v >>= 1) {
c = c1 + v;
if ((c < numV) && (cmp2(a1[c], a2[c], e, r) <= 0)) {
c1 = c;
}
}
if ((a1[c1] != e) || (a2[c1] != r)) {
return 0;
}
return 1;
}
void check(int e, int r, int de, int dr) {
if (sch(e + de, r) + sch(e, r + dr) == 2) {
lAns1 = (1 + de) / 2;
lAns2 = (1 + dr) / 2;
}
}
int main() {
int w, e, r, t;
for (scanf("%d", &q); q; scanf("%d", &q)) {
scanf("%d", &numV);
for (w = 0; w < numV; w++) {
scanf("%d%d", &s1[w], &s2[w]);
s1[w]--;
s2[w]--;
a1[w] = s1[w];
a2[w] = s2[w];
}
qs2(0, numV - 1);
for (w = 1; w < numV; w++) {
if ((s2[w] < s2[0]) || ((s2[w] == s2[0]) && (s1[w] < s1[0]))) {
swap(s1[w], s1[0]);
swap(s2[w], s2[0]);
}
}
qs(1, numV - 1);
t = 0;
for (w = 1; w < numV; w++) {
while (!((t == 0) ||
(rot(s1[t - 1], s2[t - 1], s1[t], s2[t], s1[w], s2[w]) < 0))) {
t--;
}
t++;
s1[t] = s1[w];
s2[t] = s2[w];
}
t++;
for (w = 0; w < t; w++) {
check(s1[w], s2[w], 1, 1);
check(s1[w], s2[w], 1, -1);
check(s1[w], s2[w], -1, 1);
check(s1[w], s2[w], -1, -1);
d1[w] = s1[w] + lAns1;
d2[w] = s2[w] + lAns2;
}
r = 0;
for (w = 1; w < t; w++) {
if ((d1[w] != d1[r]) || (d2[w] != d2[r])) {
r++;
d1[r] = d1[w];
d2[r] = d2[w];
}
}
while ((d1[r] == d1[0]) && (d2[r] == d2[0])) {
r--;
}
t = r + 1;
r = 0;
for (e = 1; e < t; e++) {
if ((d1[e] < d1[r]) || ((d1[e] == d1[r]) && (d2[e] < d2[r]))) {
r = e;
}
}
cout << t << "\n";
e = r;
do {
printf("%d %d\n", d1[e], d2[e]);
e++;
if (e == t) {
e = 0;
}
} while (e != r);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main() {
int n, m, i, t, x, y;
scanf("%d %d", &n, &m);
if ((m == 3) && (n > 4)) {
printf("-1\n");
return 0;
}
x = 0;
y = 0;
t = 0;
for (i = 0; i < m; i++) {
printf("%d %d\n", x, y);
x += 2;
y += t;
t += 5;
}
if (n > m) printf("1 1\n");
if (n == 2 * m) {
printf("1000000 2\n");
n--;
};
x = 2;
y = 1;
t = 5;
for (i = 1; i < n - m; i++) {
printf("%d %d\n", x, y);
x += 2;
y += t;
t += 5;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2000010, MOD = 1e9 + 7;
int a[200020];
int b[N];
bool have[N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) scanf("%d", a + i);
sort(a, a + n);
n = unique(a, a + n) - a;
int mx = a[n - 1];
mx <<= 1;
int ans = 0;
int i = a[0] == 1;
for (; i < n; i++) {
for (int j = a[i] + a[i]; j < mx; j += a[i]) {
if (j - a[n - 1] >= a[i]) break;
int l = 0, r = n - 1, ret;
while (l <= r) {
int mid = l + r >> 1;
if (a[mid] < j) {
ret = mid;
l = mid + 1;
} else
r = mid - 1;
}
ret = a[ret];
if (j - ret >= a[i]) continue;
ans = max(ans, a[i] - (j - ret));
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
if (a == b) {
printf("%d 0\n", a);
}
if (a > b) {
printf("%d %d\n", b, (a - b) / 2);
}
if (a < b) {
printf("%d %d\n", a, (b - a) / 2);
}
return 0;
}
| 1 |
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#define REP(i,n)for (int i=0;i<(n);i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define ll long long
using namespace std;
typedef pair<int,int> P;
string fld[10];
void to_fld(string s){
int p=0;
REP(i,s.size()){
if(s[i]=='/'){
p++;
}else if(s[i]=='b'){
fld[p]+='b';
}else{
int c=s[i]-'0';
REP(j,c)fld[p]+='.';
}
}
}
string to_string(){
int i=0;
string s="";
while(fld[i]!=""){
int c=0;
REP(j,fld[i].size()){
if(fld[i][j]=='b'){
if(c>0){
s+=c+'0';
c=0;
}
s+='b';
}else{
c++;
}
}
if(c>0){
s+=c+'0';
}
s+='/';
i++;
}
return s;
}
int main(){
string s;
while(1){
cin>>s;
if(s=="#")break;
int a,b,c,d;
cin>>a>>b>>c>>d;
REP(i,10)fld[i]="";
to_fld(s);
fld[--a][--b]='.';
fld[--c][--d]='b';
s=to_string();
s.erase(s.size()-1);
cout<<s<<endl;
}
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.