diff --git a/16diagonals.py b/16diagonals.py new file mode 100644 index 0000000..e1de8ed --- /dev/null +++ b/16diagonals.py @@ -0,0 +1,58 @@ +g = [[-1]*5 for i in range(5)] +def poss(c, i, j): + if c == '\\': + if i>0 and g[i-1][j] == '/': + return False + if j>0 and g[i][j-1] == '/': + return False + if i > 0 and j > 0 and g[i-1][j-1] == '\\': + return False + if i < 4 and j < 4 and g[i+1][j+1] == '\\': + return False + else: + if i>0 and g[i-1][j] == '\\': + return False + if j>0 and g[i][j-1] == '\\': + return False + if i > 0 and j > 0 and g[i-1][j-1] == '/': + return False + if i < 4 and j < 4 and g[i+1][j+1] == '/': + return False + return True +mx = 0 + +def printG(): + for row in g: + print(row) + +def kill(i, j, sol, size): + global mx, g + # mx = max(mx, len(sol)) + if len(sol) == size: + print(g) + return + if i >= 5 or j >= 5 or (i, j) in sol: + return + print (i, j) + # printG() + c = g[i][j] + if poss('/', i, j): + sol.append((i, j)) + g[i][j] = '/' + kill(i+1, j, sol, size) + kill(i, j+1, sol, size) + # g[i][j] = c + sol.pop(-1) + if poss('\\', i, j): + sol.append((i, j)) + g[i][j] = '\\' + kill(i+1, j, sol, size) + kill(i, j+1, sol, size) + # g[i][j] = c + sol.pop(-1) + + kill(i+1, j, sol, size) + kill(i, j+1, sol, size) + g[i][j] = c +kill(0, 0, [], 16) +# print(mx) \ No newline at end of file diff --git a/2ndBestMST.cpp b/2ndBestMST.cpp new file mode 100644 index 0000000..bbb988b --- /dev/null +++ b/2ndBestMST.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +using namespace std; +const int nax = (int)1e3 + 5; +int par[nax][23], mx[nax][23], level[nax]; +int LG; +vector > g[nax]; + +void dfs(int s, int p, int w) { + par[s][0] = p, mx[s][0] = w; + for (int lg=1; lg=0; i--) { + if (diff & (1<=0; i--) { + if (par[u][i] != par[v][i]) { + u = par[u][i], v = par[v][i]; + } + } + return par[u][0]; +} + +// u -> from, v -> to +int max_query(int u, int v) { + if (level[u] < level[v]) swap(u, v); + int diff = level[u] - level[v]; + int ans = 0; + for (int i=LG; i>=0; i--) { + if (diff & (1<=0; i--) { + if (par[u][i] != par[v][i]) { + ans = max(ans, max(mx[u][i], mx[v][i])); + u = par[u][i], v = par[v][i]; + } + } + return max(ans, mx[u][0]); +} + +int main() { + int n, m, u, v, w; + cin >> n >> m; + for (int i=0; i> u >> v >> w; + g[u].push_back(make_pair(v, w)); + g[v].push_back(make_pair(u, w)); + } + while ((1 << LG) < n) LG += 1; + dfs(1, 0, 0); + cin >> u >> v; + while (u != -1) { + int _lca = lca(u, v); + int ans = max(max_query(u, _lca), max_query(v, _lca)); + cout << _lca << " max: " << ans << "\n"; + + cin >> u >> v; + } + return 0; +} \ No newline at end of file diff --git a/2seals.cpp b/2seals.cpp new file mode 100644 index 0000000..819aceb --- /dev/null +++ b/2seals.cpp @@ -0,0 +1,58 @@ +#include +#include +using namespace std; + +struct Seal { + int x, y; + Seal(int a, int b): x(a), y(b) {} + int area() { + return x*y; + } +}; + +vector arr; +int n, a, b, ans; + +bool canFit(int i) { + return (max(arr[i].x, arr[i].y) <= max(a, b)); +} + +void cal(int i, int j, int a, int b) { + if ((arr[i].x + arr[j].x) <= a and max(arr[i].y, arr[j].y)<=b) { + ans = max(ans, arr[i].area()+arr[j].area()); + } + else if ((arr[i].x + arr[j].y) <= a and max(arr[i].y, arr[j].x)<=b) { + ans = max(ans, arr[i].area()+arr[j].area()); + } + else if ((arr[i].y + arr[j].x) <= a and max(arr[i].x, arr[j].y)<=b) { + ans = max(ans, arr[i].area()+arr[j].area()); + } + else if ((arr[i].y + arr[j].y) <= a and max(arr[i].x, arr[j].x)<=b) { + ans = max(ans, arr[i].area()+arr[j].area()); + } +} + +int main() { + int x, y, i=0; + cin >> n >> a >> b; + while (i++> x >> y; + arr.push_back(Seal(x, y)); + } + // sort(arr.begin(), arr.end(), [](Seal& x, Seal& y) { + // return x.area() > y.area(); + // }); + for (i=0; i +using namespace std; +const int maxn = 1e5 + 5; +vector g[maxn]; +vector *vec[maxn]; +int cnt[maxn], sz[maxn], col[maxn]; + +int computeSubtreeSizes(int u, int p) { + for (int v: g[u]) { + if (v != p) { + sz[u] += computeSubtreeSizes(v, u); + } + } + sz[u]++; + return sz[u]; +} + + +void dfs(int v, int p, bool keep){ + int mx = -1, bigChild = -1; + for(auto u : g[v]) + if(u != p && sz[u] > mx) + mx = sz[u], bigChild = u; + for(auto u : g[v]) + if(u != p && u != bigChild) + dfs(u, v, 0); + if(bigChild != -1) + dfs(bigChild, v, 1), vec[v] = vec[bigChild]; + else + vec[v] = new vector (); + vec[v]->push_back(v); + cnt[ col[v] ]++; + for(auto u : g[v]) + if(u != p && u != bigChild) + for(auto x : *vec[u]){ + cnt[ col[x] ]++; + vec[v] -> push_back(x); + } + //now (*cnt[v])[c] is the number of vertices in subtree of vertex v that has color c. You can answer the queries easily. + // note that in this step *vec[v] contains all of the subtree of vertex v. + if(keep == 0) + for(auto u : *vec[v]) + cnt[ col[u] ]--; +} \ No newline at end of file diff --git a/HLD/lomsat_gelral.cpp b/HLD/lomsat_gelral.cpp new file mode 100644 index 0000000..cf1f4cd --- /dev/null +++ b/HLD/lomsat_gelral.cpp @@ -0,0 +1,57 @@ +#include +#define pb push_back +using namespace std; +const int maxn=1e5+12; +int n,col[maxn],sz[maxn],maxx,cnt[maxn]; +long long ans[maxn],can; +bool badboy[maxn]; +vectorg[maxn]; +void computeSubtreeSize(int v=0,int p=-1){ + sz[v]=1; + for(auto &u:g[v]) + if(u!=p)computeSubtreeSize(u,v),sz[v]+=sz[u]; +} + +void relax(int x){ + if(maxx<++cnt[x]) + maxx=cnt[x],can=x; + else if(maxx==cnt[x]) + can+=x; +} +void merge(int v,int p=-1){ + relax(col[v]); + for(auto &u:g[v]) + if(u!=p && !badboy[u]) + merge(u,v); +} +void remove(int v,int p=-1){ + cnt[col[v]]--; + for(auto &u:g[v]) + if(u!=p && !badboy[u]) + remove(u,v); +} +void dfs(int v=0,int p=-1,bool hrh=0){//HRH?! mipasandam! + int mx=0,big=-1; + for(auto &u:g[v]) + if(u!=p && sz[u]>mx) + mx=sz[u],big=u; + for(auto &u:g[v]) + if(u!=p && u!=big) + dfs(u,v,1); + if(big+1) + dfs(big,v),badboy[big]=1; + merge(v,p); + if(big+1) + badboy[big]=0; + ans[v]=can; + if(hrh) + remove(v,p),maxx=can=0; +} +int main(){ + scanf("%d",&n); + for_each(col,col+n,[](int &x){scanf("%d",&x);}); + for(int i=1,v,u;i +using namespace std; + +int main() { + int n; + cin >> n; + map mp; + vector a(n), b(n); + for (int& x: a) { + cin >> x; + mp[x] = 1; + } + for (int& y: b) { + cin >> y; + mp[y] = 1; + } + int cnt = 0; + for (int i=0; i +using namespace std; + +int main() { + int n, m; + cin >> n >> m; + int dp[n+1][m+1]; + for (int i=0; i<=m; i++) dp[0][i] = 0; + for (int i=0; i<=n; i++) dp[i][0] = 0; + string s; + for (int i=1; i<=n; i++) { + cin >> s; + for (int j=1; j<=m; j++) { + dp[i][j] = s[j-1]-'0'; + } + } + for (int i=1; i<=n; i++) { + for (int j=1; j<=m; j++) { + dp[i][j] += dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]; + } + } + int ans = 0; + for (int i=1; i<=n; i++) { + for (int j=1; j<=m; j++) { + for (int k=i; k<=n; k++) { + for (int l=j; l<=m; l++) { + int isOk = !(dp[k][l] - dp[k][j-1] - dp[i-1][l] + dp[i-1][j-1]); + if (isOk) { + ans = max(ans, 2*(k-i+1 + l-j+1)); + } + } + } + } + } + cout << ans << "\n"; +} \ No newline at end of file diff --git a/birds.cpp b/birds.cpp new file mode 100644 index 0000000..f022a65 --- /dev/null +++ b/birds.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; +const int mx = 1e4 + 2; +long long c[mx], cost[mx]; +long long X, W, B, n; +long long dp[mx/10 + 1][mx]; + +int main() { + cin >> n >> W >> B >> X; + for (int i=0; i> c[i]; + for (int i=0; i> cost[i]; + memset(dp, -1, sizeof(dp)); + dp[0][0] = W; + for (int i=0; i=0; k++) { + dp[i+1][j+k] = max(dp[i+1][j+k], min(dp[i][j]-k*cost[i]+X, W+(k+j)*B)); + } + } + } + int ans = 0; + for (int j=0; j=0) ans = j; + cout << ans << "\n"; + return 0; +} \ No newline at end of file diff --git a/colorfulPoints.cpp b/colorfulPoints.cpp new file mode 100644 index 0000000..5e9e6dd --- /dev/null +++ b/colorfulPoints.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +struct DS{ + int ch, count; +}; +const int mx = 1000003; +DS ds[mx]; +int ind; +string s; + +int main() { + cin >> s; + int n = s.length(); + for (int i=0; i 1) { + int left=0; + for (int i=1; i<=ind; i++) { + int x = ds[i].ch; + int y = ds[i].count-1-(i!=0 and i!=ind); + if (y<=0) continue; + if (x != ds[left].ch) ds[++left].ch = x; + ds[left].count = y; + } + ind = left; + ans++; + } + cout << ans << endl; + return 0; +} \ No newline at end of file diff --git a/convenient.cpp b/convenient.cpp new file mode 100644 index 0000000..f8af96c --- /dev/null +++ b/convenient.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main() { + int n; ios_base::sync_with_stdio( 0 ), cin.tie( 0 ), cout.tie( 0 ), cin >> n; + + auto next = [ n ] ( auto &x ) { if ( --x < 0 ) x += n; }; + + int a[ n ], s, f, t, u, v, sum = 0, max_sum = 0; + + for( int i = 0; i < n; i++ ) + cin >> a[ i ]; + + cin >> s >> f, u = s - 1, v = f - 2; + + for( int i = u; i <= v; i++ ) + sum += a[ i ]; + + for( int i = 1; i <= n; i++ ) + { + if ( sum > max_sum ) + max_sum = sum, t = i; + + sum -= a[ v ], next( u ), next( v ), sum += a[ u ]; + } + + cout << t << "\n"; + return 0; +} \ No newline at end of file diff --git a/demo.cpp b/demo.cpp new file mode 100644 index 0000000..84c4fa4 --- /dev/null +++ b/demo.cpp @@ -0,0 +1,9 @@ +#include +using namespace std; + +int main() { + int n; + cin >> n; + cout << n << "\n"; + return 0; +} \ No newline at end of file diff --git a/ehabAndMahmoudArray.cpp b/ehabAndMahmoudArray.cpp new file mode 100644 index 0000000..2664029 --- /dev/null +++ b/ehabAndMahmoudArray.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; +const int mx = 2e6 + 5; +vector mul[mx]; +bool prime[mx], erased[mx]; +int main() { + int n; + cin >> n; + set poss; + for (int i=2; i> in; + int out = *poss.begin(); + if (gt) { + out = *poss.lower_bound(in); + if (out != in) gt = false; + } + + cout << out << " "; + for (int u: mul[out]) { + for (int v=u; v +using namespace std; + +#define sd(a) scanf("%d",&a) +#define ss(a) scanf("%s",&a) +#define sl(a) scanf("%lld",&a) +#define clr(a) memset(a,0,sizeof(a)) +#define debug(a) printf("check%d\n",a) +#define F first +#define S second +#define MP make_pair +#define PB push_back +#define ll long long +#define INF 1000000000000000000 +vector digits; +ll p10[20]; +int main() +{ + // freopen("A1.in","r",stdin); + // freopen("A1.out","w",stdout); + int t,i; + p10[0] = 1; + for(i=1;i<18;i++) + p10[i] = p10[i-1]*10; + sd(t); + for(int tt=1;tt<=t;tt++) + { + ll n, n1,l = 0, r = 0; + digits.clear(); + sl(n); + n1 = n; + while(n>0) + { + digits.PB(n%10); + n/=10; + } + for(i=(int)digits.size()-1;i>=0;i--) + { + if(digits[i]&1) + break; + l += digits[i]*p10[i]; + r += digits[i]*p10[i]; + } + if(i>=0) + { + l += (digits[i]-1)*p10[i]; + r += (digits[i]+1)*p10[i]; + if(digits[i] == 9) r = INF; + i--; + for(;i>=0;i--) + l += 8*p10[i]; + } + ll ans = min(llabs(n1-l),llabs(n1-r)); + printf("Case #%d: %lld\n",tt,ans); + } +} diff --git a/fair_game.cpp b/fair_game.cpp new file mode 100644 index 0000000..56dafd9 --- /dev/null +++ b/fair_game.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +int main() { + int n; + cin >> n; + map mp; + for (int i=0; i> v; + mp[v]++; + } + // bool poss = true; + int cnt = 0, sum1=0, sum2=0; + vector first, sec; + for (auto x: mp) { + if (sum1 < sum2) sum1 += x.second, first.push_back(x.first); + else sum2 += x.second, sec.push_back(x.first); + cnt++; + } + if ((cnt>2 || cnt==1) || sum1!=sum2) { + cout << "NO\n"; + return 0; + } + cout << "YES\n"; + for (auto x: mp) { + cout << x.first << " "; + } + cout << "\n"; + return 0; +} \ No newline at end of file diff --git a/fair_nut.cpp b/fair_nut.cpp new file mode 100644 index 0000000..63225fa --- /dev/null +++ b/fair_nut.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +using namespace std; + +struct edge { + int node, length; + edge(int _node = -1, int _length = -1) : node(_node), length(_length) {} +}; + +int N; +vector gas; +vector> adj; +long long best; + +long long dfs(int node, int parent) { + long long down = gas[node]; + + for (edge &e : adj[node]) + if (e.node != parent) { + long long child = dfs(e.node, node) - e.length; + best = max(best, down + child); + down = max(down, child + gas[node]); + } + + best = max(best, down); + return down; +} + +int main() { + scanf("%d", &N); + gas.resize(N); + adj.assign(N, {}); + + for (int &w : gas) + scanf("%d", &w); + + for (int i = 0; i < N - 1; i++) { + int u, v, c; + scanf("%d %d %d", &u, &v, &c); + u--; v--; + adj[u].emplace_back(v, c); + adj[v].emplace_back(u, c); + } + + best = 0; + dfs(0, -1); + printf("%lld\n", best); +} \ No newline at end of file diff --git a/fifa_fafa.cpp b/fifa_fafa.cpp new file mode 100644 index 0000000..a371391 --- /dev/null +++ b/fifa_fafa.cpp @@ -0,0 +1,164 @@ +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include т мом +// #include +// #include + +// #include +// #include + +// #define y0 sdkfaslhagaklsldk + +// #define y1 aasdfasdfasdf +// #define yn askfhwqriuperikldjk +// #define j1 assdgsdgasghsf +// #define tm sdfjahlfasfh +// #define lr asgasgash +// #define norm asdfasdgasdgsd +// #define have adsgagshdshfhds +// #define ends asdgahhfdsfshdshfd + +// #define eps 1e-8 +// #define M_PI 3.141592653589793 +// #define bsize 512 + +// #define ldouble long double +// using namespace std; + +// #define bs 1000000007 + +// const int N = 400031; + +// long double R,x1,y1,x2,y2; + +// long double get_dist(long double x,long double y){ +// return sqrt(x*x+y*y); +// } + +// bool outside(){ +// return get_dist(x2,y2)>R; +// } + +// void show_answer(long double x,long double y,long double r){ +// x+=x1; +// y+=y1; +// cout.precision(20); +// cout<>R>>x1>>y1>>x2>>y2; +// x2-=x1; +// y2-=y1; +// if (outside()){ +// show_answer(0,0,R); +// } +// else +// { +// long double D=get_dist(x2,y2); +// long double dir=atan2(y2,x2); +// dir=dir+M_PI; +// long double diam=R+D; +// long double rad=diam/2; +// rad-=D; +// long double ax=rad*cos(dir); +// long double ay=rad*sin(dir); +// //cout.precision(20); +// show_answer(ax,ay,rad+D); +// } +// // cin.get(); cin.get(); +// return 0; +// } + +#include +#define f first +#define s second +using namespace std; +const double eps = 1e-9; +double x1, x2, Y1, y2, x, y, R; +typedef pair point; + +struct Circle { + point center; + double radius; + Circle(point p, double r): center(p), radius(r) {} +}; + +struct Line { + double m, c; + Line(double m_, double c_): m(m_), c(c_){} + + double getYCorrespoindingTo(double x) { + return m*x + c; + } +}; + +struct LineCircleInterSection { + point p1, p2, p; + double a, b, c; + LineCircleInterSection(Line l, Circle cr) { + a = 1 + 1.0f * pow(l.m, 2); + b = 2.0f * (cr.center.f - l.m*l.c + l.m*cr.center.s); + c = pow(cr.center.f, 2) + pow(cr.center.s, 2) + pow(l.c, 2) - l.c * cr.center.s - pow(cr.radius, 2); + double disc = pow(b, 2) - 4 * a * c; + double x1 = 1.0f * (b + sqrt(disc)) / (2 * a); + double x2 = 1.0f * (b - sqrt(disc)) / (2 * a); + p1 = point(x1, l.getYCorrespoindingTo(x1)); + p2 = point(x2, l.getYCorrespoindingTo(x2)); + } + + double dist(point p11, point p22) { + return sqrt(pow(p11.f-p22.f, 2) + pow(p11.s-p22.s, 2)); + } + void print(point x) { + cout << "[" << x.f << ", " << x.s << "]\n"; + } + point getCenter(point p_) { + print(point((p_.f+p2.f) / 2, (p_.s+p2.s) / 2)); + print(point((p_.f+p1.f) / 2, (p_.s+p1.s) / 2)); + if (dist(p_, p1)+eps < dist(p_, p2)) { + return point((p_.f+p2.f) / 2, (p_.s+p2.s) / 2); + } + return point((p_.f+p1.f) / 2, (p_.s+p1.s) / 2); + } +}; + +int main() { + cin >> R >> x1 >> Y1 >> x2 >> y2; + auto dist = [&]()->double { + return 1.0f * sqrt(pow(x1-x2, 2)+pow(Y1-y2, 2)); + }; + cout.precision(20); + if(dist() < eps) { + cout << (x1 + R/2) << " " << Y1 << " " << R/2 << "\n"; + } + else if (dist() < R) { + double m = 1.0f * (y2-Y1) / (x2-x1); + double c = Y1 - m*x1; + Line l(m, c); + Circle cr(point(x1, Y1), R); + LineCircleInterSection ds(l, cr); + point p = ds.getCenter(point(x2, y2)); + double r = (R + dist()) /2; + cout << p.f << " " << p.s << " " << r << "\n"; + } else { + cout << x1 << " " << Y1 << " " << R << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/gen.cpp b/gen.cpp new file mode 100644 index 0000000..97f1ce4 --- /dev/null +++ b/gen.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; +int get(char* s) { + int n = strlen(s); + int out = 0; + for (int i=0; i +const double EPS = 1e-9; + +double DEG_to_RAD(double x) +{ + return x * M_PI / 180; +} + +struct point +{ + double x, y; + point() { x = y = 0.0; } + point(double _x, double _y) : x(_x), y(_y) {} + + bool operator<(point other) const + { // override less than operator + if (fabs(x - other.x) > EPS) + return x < other.x; + return y < other.y; + } + + bool operator==(point other) const + { + return (fabs(x - other.x) < EPS && (fabs(y - other.y) < EPS)); + } + + double distTo(point p2) + { // Euclidean distance // hypot(dx, dy) returns sqrt(dx * dx + dy * dy) + return hypot(x - p2.x, y - p2.y); + } + + point rotate(point p, double theta) + { + double rad = DEG_to_RAD(theta); // multiply theta with PI / 180.0 + return point(p.x * cos(rad) - p.y * sin(rad), p.x * sin(rad) + p.y * cos(rad)); + } +}; +// useful for sorting // first criteria , by x-coordinate // second criteria, by y-coordinate + +struct line +{ + double a, b, c; + + void pointsToLine(point p1, point p2, line &l) + { + if (fabs(p1.x - p2.x) < EPS) + { // vertical line is fine + l.a = 1.0; + l.b = 0.0; + l.c = -p1.x; + } + else + { + // default values + l.a = -(double)(p1.y - p2.y) / (p1.x - p2.x); + l.b = 1.0; // IMPORTANT: we fix the value of b to 1.0 l.c = -(double)(l.a * p1.x) - p1.y; + } + } + + bool areParallel(line l1, line l2) + { // check coefficients a & b + return (fabs(l1.a - l2.a) < EPS) && (fabs(l1.b - l2.b) < EPS); + } + bool areSame(line l1, line l2) + { // also check coefficient c + return areParallel(l1, l2) && (fabs(l1.c - l2.c) < EPS); + } + bool areIntersect(line l1, line l2, point &p) + { + if (areParallel(l1, l2)) + return false; // no intersection // solve system of 2 linear algebraic equations with 2 unknowns + p.x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b); + // special case: test for vertical line to avoid division by zero + if (fabs(l1.b) > EPS) + p.y = -(l1.a * p.x + l1.c); + else + p.y = -(l2.a * p.x + l2.c); + return true; + } +}; + +struct vec +{ + double x, y; // name: ‘vec’ is different from STL vector + vec(double _x, double _y) : x(_x), y(_y) {} + vec toVec(point a, point b) + { // convert 2 points to vector a->b + return vec(b.x - a.x, b.y - a.y); + } + vec scale(vec v, double s) + { + return vec(v.x * s, v.y * s); + } + // nonnegative s = [<1 .. 1 .. >1] // shorter.same.longer + point translate(point p, vec v) + { + return point(p.x + v.x, p.y + v.y); // translate p according to vF + } + + double dot(vec a, vec b) { + return (a.x * b.x + a.y * b.y); + } + + double magnitudeOf(vec v) { + return v.x * v.x + v.y * v.y; + } + + // returns the distance from p to the line defined by + // two points a and b (a and b must be different) + // the closest point is stored in the 4th parameter (byref) + double distToLine(point p, point a, point b, point &c) + { + // formula: c = a + u * ab + vec ap = toVec(a, p), ab = toVec(a, b); + double u = dot(ap, ab) / magnitudeOf(ab); + c = translate(a, scale(ab, u)); // translate a to c + return p.distTo(c); // Euclidean distance between p and c + } +}; \ No newline at end of file diff --git a/guessTheNumber.cpp b/guessTheNumber.cpp new file mode 100644 index 0000000..7b65bcc --- /dev/null +++ b/guessTheNumber.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main(){ + string op; + int l = 1, r = (int)1e6; + while(1) { + int mid = (l+r) >> 1; + cout << mid << "\n"; + fflush(stdout); + cin >> op; cin.ignore(); + if (l>r) { + cout << "! " << r << "\n"; + break; + } + if (op == "<") { + r = mid-1; + } else { + l = mid+1; + } + } + fflush(stdout); + return 0; +} \ No newline at end of file diff --git a/indent b/indent new file mode 100755 index 0000000..e9554d9 Binary files /dev/null and b/indent differ diff --git a/input.in b/input.in new file mode 100644 index 0000000..fe990aa --- /dev/null +++ b/input.in @@ -0,0 +1,2001 @@ +2000 +s +f +f +f +f +f +s +s +s +f +f +s +s +f +s +s +s +f +f +f +f +f +s +s +f +s +f +s +f +s +s +s +s +f +s +s +s +s +f +s +f +s +f +s +f +f +f +f +s +f +f +f +f +s +f +f +f +s +s +s +s +s +s +f +s +f +f +f +f +f +f +f +s +f +f +s +s +f +f +s +f +f +f +f +f +s +s +s +f +f +s +s +f +f +s +s +f +s +s +s +f +f +f +s +s +f +f +f +f +s +s +f +f +s +s +f +f +s +s +s +f +f +s +f +s +f +f +f +f +f +f +f +f +f +f +s +f +s +s +s +f +f +s +s +s +f +s +f +f +f +f +s +s +s +s +f +f +f +s +s +f +f +f +s +f +f +s +f +s +s +f +f +f +f +f +s +f +s +s +f +f +s +f +f +f +s +s +f +f +f +f +s +f +f +s +s +f +f +s +s +f +f +f +s +f +s +s +s +s +s +f +s +f +f +s +f +s +f +s +s +s +s +f +f +s +f +s +f +f +s +f +f +s +f +f +f +s +f +s +f +f +f +s +s +f +f +s +s +f +s +s +s +f +s +f +s +f +f +s +f +f +s +s +f +f +s +s +s +f +s +s +f +f +f +f +f +s +f +s +f +s +s +f +f +f +f +f +s +f +f +f +f +s +f +s +s +f +s +s +f +f +f +s +s +f +s +s +s +s +s +s +f +f +s +s +s +f +s +f +f +f +f +f +s +s +s +s +f +s +f +f +f +s +f +f +f +s +s +s +f +f +f +s +s +s +s +f +f +f +f +s +f +s +s +s +s +f +f +s +f +f +s +s +s +f +s +s +s +f +f +f +f +s +s +f +f +f +f +s +s +f +f +f +f +f +s +s +s +f +f +f +s +f +s +s +s +f +f +f +s +f +s +s +f +f +s +f +f +f +s +f +f +s +f +s +s +f +s +s +s +s +s +s +s +s +f +s +s +s +f +s +s +f +f +s +s +s +s +s +f +s +f +s +f +f +f +s +f +s +s +s +f +f +f +s +s +f +f +s +s +s +f +f +s +f +s +f +s +s +f +f +s +s +s +s +f +f +f +f +f +f +f +s +s +f +s +f +s +s +s +f +f +s +s +s +f +f +s +f +s +f +s +s +s +f +s +s +f +f +f +f +s +f +f +f +s +f +f +f +f +s +f +f +s +f +s +s +f +s +f +f +s +s +s +s +s +f +s +f +f +s +s +f +f +f +s +f +s +f +f +f +f +s +f +f +s +s +f +s +f +f +s +s +s +f +f +f +f +s +s +f +f +f +f +s +f +s +s +s +f +s +f +f +s +s +s +s +s +f +s +s +f +s +s +s +f +f +s +f +f +f +s +s +s +f +f +s +f +f +s +f +f +f +s +s +f +f +s +f +f +s +f +s +s +s +s +f +s +f +f +s +s +f +s +s +s +s +s +s +s +s +f +s +f +f +s +f +f +f +s +f +f +s +f +f +s +s +f +f +f +f +f +f +s +s +f +s +f +s +f +s +f +f +s +s +s +s +f +f +s +s +f +f +f +s +f +s +s +s +f +f +s +s +s +s +s +f +s +s +f +s +s +f +s +f +f +s +f +f +f +f +f +f +s +s +s +f +f +f +s +f +f +f +f +f +f +f +f +f +s +f +f +f +s +f +s +s +f +s +f +s +s +f +f +s +s +s +f +f +s +s +s +f +s +f +f +f +f +s +f +f +s +s +f +f +s +s +f +f +f +f +s +s +s +f +f +s +f +f +f +f +s +s +f +f +s +f +s +f +s +s +f +f +f +f +s +f +f +f +s +s +s +f +s +f +s +f +f +f +f +s +f +f +f +s +s +s +s +s +f +s +s +s +s +f +f +s +s +f +f +f +s +s +s +f +s +s +s +f +s +s +f +s +s +f +s +s +f +f +f +f +f +s +f +f +f +s +f +f +s +f +s +f +s +s +s +f +s +f +s +s +s +s +f +f +f +s +s +f +f +f +f +s +f +s +s +s +f +s +s +f +f +f +s +f +f +s +f +s +f +s +s +s +f +f +s +s +f +s +s +s +s +f +s +f +s +f +f +s +f +s +s +f +s +f +s +s +s +f +s +f +f +f +s +s +s +s +s +f +s +s +f +s +f +s +s +s +s +f +f +s +s +s +s +s +f +f +f +s +f +f +s +s +s +s +f +f +s +s +f +f +s +s +f +f +s +f +f +s +f +s +s +s +s +s +f +s +s +f +s +f +s +f +s +s +s +f +f +s +f +s +s +f +s +f +s +s +f +f +s +f +s +s +f +f +s +s +f +f +f +f +f +f +f +f +s +s +s +s +f +f +f +f +s +s +f +s +s +s +s +f +f +f +s +f +f +s +f +s +s +f +f +s +f +f +f +s +s +s +s +s +f +f +s +s +f +s +f +s +s +f +f +f +f +f +s +s +s +s +f +s +f +s +f +f +s +s +s +s +f +s +s +f +f +f +f +f +s +f +f +s +f +s +s +s +f +f +s +s +s +f +s +f +f +s +f +f +f +s +f +f +s +f +f +s +s +s +f +s +s +f +s +s +f +f +s +f +s +s +f +s +s +s +f +s +f +f +s +s +s +f +s +s +s +f +s +f +f +f +f +s +s +s +s +f +s +f +s +s +s +f +s +s +f +s +f +f +f +s +s +f +s +f +s +s +s +s +s +s +s +s +s +s +f +s +s +s +s +f +f +f +f +s +s +s +s +f +s +s +s +s +s +f +f +s +s +f +f +s +s +s +s +f +s +s +s +f +f +s +f +s +s +f +s +f +s +s +s +s +s +s +s +f +f +f +s +f +s +f +f +f +f +s +s +f +s +f +s +f +f +s +s +f +f +s +f +f +f +s +s +f +s +f +s +s +s +f +s +s +f +s +f +s +f +f +f +f +f +f +f +f +s +f +f +f +s +f +f +s +s +s +f +f +s +s +s +s +f +f +f +s +f +f +f +s +f +f +f +s +s +f +s +f +s +f +s +f +s +f +f +s +f +s +s +f +s +s +f +f +s +s +s +f +s +s +s +f +s +f +s +f +s +f +f +s +s +s +f +f +f +f +s +f +f +s +f +f +s +s +s +s +s +s +f +f +f +f +f +s +f +f +f +s +s +f +f +f +f +s +s +f +f +f +f +s +f +s +f +s +f +s +f +s +s +s +s +s +f +f +f +s +s +s +f +f +f +f +f +f +s +s +s +f +f +s +s +s +f +s +s +f +s +f +s +f +s +s +f +s +f +s +f +s +f +s +f +f +s +f +s +s +s +s +f +s +s +s +s +s +f +s +f +f +f +f +s +s +s +f +s +f +s +f +f +s +f +f +s +f +s +f +f +s +s +s +f +s +s +s +f +f +s +f +s +f +f +f +f +s +s +s +s +s +f +s +s +s +s +f +f +s +s +s +f +s +f +f +f +s +s +f +f +s +f +s +f +s +s +f +f +s +s +f +s +s +s +s +s +f +f +f +s +f +s +f +f +f +s +f +s +s +s +f +f +f +f +f +s +f +s +s +f +f +s +f +f +s +f +s +s +s +f +s +f +s +f +f +f +s +f +f +f +s +f +f +s +s +f +f +s +s +s +f +s +s +s +f +s +f +f +f +s +s +f +f +s +s +s +f +s +s +f +s +s +s +f +s +s +s +f +s +f +s +f +s +s +f +s +f +f +s +s +s +f +s +f +f +f +f +s +f +f +f +f +f +f +f +s +f +f +s +f +s +f +s +s +f +s +f +f +f +s +s +s +s +f +f +s +s +f +f +f +f +f +s +s +s +s +s +s +f +f +f +f +f +s +f +s +f +f +f +f +s +f +s +f +s +f +s +f +s +f +f +s +s +f +f +s +s +s +s +s +f +s +f +s +f +s +f +s +f +s +f +f +f +s +s +f +f +f +s +s +s +f +f +s +f +f +f +s +f +s +s +f +f +s +s +f +s +s +f +f +s +s +f +f +s +s +s +s +f +s +s +f +f +f +f +f +f +s +f +f +f +s +f +s +f +f +f +s +f +f +s +s +s +s +f +s +s +s +s +s +s +s +s +s +s +s +s +f +s +f +f +s +f +s +s +s +s +s +s +f +s +f +f +f +s +s +s +f +f +s +f +f +f +f +s +s +s +f +s +s +f +f +f +f +f +f +f +f +s +s +f +s +s +f +s +f +f +s +s +s +f +s +f +f +f +f +f +f +f +f +f +f +s +f +f +f +f +s +s +s +s +s +f +s +s +s +s +f +f +f +f +f +f +s +f +f +s +f +s +f +s +s +f +s +s +s +s +f +s +f +s +f +f +s +s +f +s +f +f +s +s +s +f +f +s +s +f +s +s +f +s +f +s +s +f +s +s +s +f +s +s +f +f +s +f +s +s +s +s +f +f +f +f +s +s +s +s +f +f +f +s +f +s +f +s +f +f +f +s +f +s +f +s +f +s +f +f +f +f +f +s +f +f +s +s +f +f +s +f +f +s +s +s +s +s +f +s +s +f +f +s +s +f +f +f +s +s +f diff --git a/laser.cpp b/laser.cpp new file mode 100644 index 0000000..1a4f66a --- /dev/null +++ b/laser.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; +using ll = long long; +int main() { + ll n, u; + cin >> n >> u; + ll E[n]; + for (int i=0; i> E[i]; + } + + ll i=0, j = 1; + long double eff = 0, eps = 1e-9; + while (i < n) { + while (j= 3) { + // cout << i << " " << j << " " << E[j]-E[i]; + if (E[j]-E[i] <= u) { + long double tmp = 1.0f * (E[j]-E[i+1]) / (E[j]-E[i]); + if (eps + eff < tmp ) eff = tmp; + } + if (E[j-1]-E[i] <= u and (j-i)>=3) { + long double tmp = 1.0f * (E[j-1]-E[i+1]) / (E[j-1]-E[i]); + if (eps + eff < tmp ) eff = tmp; + } + // cout << " " << eff << "\n"; + } + i++; + } + if (eff == 0.0f) eff = -1.0; + printf("%.12Lf\n", eff); + return 0; +} \ No newline at end of file diff --git a/lecture_sleep.cpp b/lecture_sleep.cpp new file mode 100644 index 0000000..10b1853 --- /dev/null +++ b/lecture_sleep.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int n, k; + cin >> n >> k; + long a[n+1], a_[n+1]; + int t[n]; + a[0] = a_[0] = 0; + for (int i=0; i> a[i+1]; + a[i+1] += a[i]; + a_[i+1] = 0; + } + long ans = 0; + for (int i=0; i> t[i]; + // t[i+1] += t[i]; + if (t[i]) ans += (a[i+1]-a[i]); + else a_[i+1] += (a[i+1]-a[i]); + a_[i+1] += a_[i]; + // cout << a_[i+1] << " \n"[i==n-1]; + } + // cout << ans << "\n"; + long total = ans; + for (int i=1; i<=n-k+1; i++) { + ans = max(ans, total + a_[i+k-1] - a_[i-1]); + // cout << i << " : " << ans << "\n"; + } + cout << ans << "\n"; + return 0; +} \ No newline at end of file diff --git a/lids.cpp b/lids.cpp new file mode 100644 index 0000000..89f84fd --- /dev/null +++ b/lids.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +using namespace std; + +int dp[2][2][11][10][10], mx, k; +vector num1, num2, num; +int f(int eq, int nz, int curL, int last, int ind, int n) { + if (ind == num.size()) { + if (curL == k && nz==1) return 1; + return 0; + } + if (dp[eq][nz][curL][last][ind] != -1) return dp[eq][nz][curL][last][ind]; + int lmt = 9; + if (eq == 0) { + lmt = num[ind]; + } + int ans = 0; + for (int i=0; i<=lmt; i++) { + int neq = eq; + int n_curL = curL; + int nLast = last; + int nnz = nz; + int nn = n*10 + i; + if (i>0) nnz = 1; + if (i < num[ind] || eq) neq = 1; + //ignoring the current digit + ans += f(neq, nnz, n_curL, nLast, ind+1, nn); + if (i > last) { + nLast = i; + ans += f(neq, nnz, n_curL+1, nLast, ind+1, nn); + } + } + (dp[eq][nz][curL][last][ind]=ans); + return ans; +} + +vector getNum(int a) { + vector ans; + while (a > 0) { + ans.push_back(a%10); + a /= 10; + } + reverse(ans.begin(), ans.end()); + return ans; +} + +void solve(int a, int b) { + if (a > b) swap(a, b); + num1 = getNum(a-1); + num2 = getNum(b); + vector temp; + while (temp.size()+num1.size() < num2.size()) temp.push_back(0); + for_each(num1.begin(), num1.end(), [&](int x) { + temp.push_back(x); + }); + num1 = temp; + // cout << num1.size() << num2.size() << endl; + for (k=1; k<=min(9, (int)num2.size()); k++) { + memset(dp, -1, sizeof(dp)); + int ans = 0; + num = num2; + // for (int i=0; i<=num[0]; i++) { + ans += f(0, 0, 0, 0, 0, 0); + // } + num = num1; + memset(dp, -1, sizeof(dp)); + // for (int i=0; i<=num[0]; i++) { + ans -= f(0, 0, 0, 0, 0, 0); + // } + cout << k << " " << ans << endl; + } +} + +int main() { + int a, b; + cin >> a >> b; + solve(a, b); + return 0; +} \ No newline at end of file diff --git a/lucky-dip.cpp b/lucky-dip.cpp new file mode 100644 index 0000000..a2bab63 --- /dev/null +++ b/lucky-dip.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +#define LL long long + +#define maxn 20100 +#define INF 0x33333333 + +int v[maxn]; + +int main() +{ + freopen("B-small-practice.in", "r", stdin); + int t, cas = 0; + scanf("%d", &t); + while(t--){ + cas++; + int n, k; + scanf("%d %d", &n, &k); + for(int i = 0; i < n; i++){ + scanf("%d", &v[i]); + } + sort(v, v + n); + double ans = 0; + + auto f = [](double a, double b){return a + b;}; + double s = accumulate(v, v + n, 0.0, f); + double mid = (n & 1) ? v[n / 2]: (v[n / 2] + v[n / 2 - 1]) / 2; + double av = s * 1.0 / n; + if(k == 0){ + ans = av; + }else{ + double prev = av; + for(int draw = 1; draw <= k; draw++){ + double cur = 0.0; + for(int i = 0; i < n; i++){ + if(v[i] <= prev){ + cur += prev; + }else{ + cur += v[i]; + } + } + cur /= n; + prev = cur; + } + ans = prev; + } + printf("Case #%d: %.6f\n", cas, ans); + } + return 0; +} \ No newline at end of file diff --git a/magicNums.cpp b/magicNums.cpp new file mode 100644 index 0000000..b6b6950 --- /dev/null +++ b/magicNums.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +using namespace std; +int m, d; +char a[2007], b[2007]; +int len; +int dp[2007][2007][2][2]; +const int MOD = 1e9 + 7; +int dfs(int p, int r, int x, int y) { + // cout << p << " " << r << " " << x << " " << y << endl; + if (p == len) return r == 0; + int &ret = dp[p][r][x][y]; + if (ret != -1) return ret; + ret = 0; + int L = x == 1 ? a[p] - '0' : 0; + int R = y == 1 ? b[p] - '0' : 9; + for (int i = L; i <= R; ++ i) { + if ((p & 1) && i != d) continue; + if (!(p & 1) && i == d) continue; + ret += dfs(p + 1, (r * 10 + i) % m, x && i == L, y && i == R); + if (ret >= MOD) ret -= MOD; + } + return ret % MOD; +} +int main() { + scanf("%d%d%s%s", &m, &d, a, b); + len = strlen(a); + memset(dp, -1, sizeof(dp)); + printf("%d\n", dfs(0, 0, 1, 1)); +} \ No newline at end of file diff --git a/milking_order.cpp b/milking_order.cpp new file mode 100644 index 0000000..4e2aee7 --- /dev/null +++ b/milking_order.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +using namespace std; + +vector suc[100002]; +int n; +int pred[100002], result[100002]; +vector > edge[50004]; + +bool can(int k) { + for (int i=0; i q; + for (int i=0; i> n >> m; + for (int i = 0; i < m; i++) { + int d; + cin >> d; + int last; + for (int j = 0; j < d; j++) { + int e; + cin >> e; + e--; + if (j > 0) { + edge[i].push_back(make_pair(last, e)); + } + last = e; + } + } + + // Binary search + // Invariant: lo <= ans < hi + int lo = 0; + int hi = m+1; + while (hi > lo+1) { + int mid = (lo + hi) / 2; + if (can(mid)) { + lo = mid; + } else { + hi = mid; + } + } + + int ans = lo; + + // Run this again, to get the answer + // into the `result` array. + can(ans); + + for (int i = 0; i < n; i++) { + cout << result[i]+1 << " \n"[i==n-1]; + } + return 0; +} diff --git a/mountains.cpp b/mountains.cpp new file mode 100644 index 0000000..9c3cf94 --- /dev/null +++ b/mountains.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; +struct point{ + int x, y; + point(): x(0), y(0) {} + int diff() const { + return x-y; + } + int plus() const { + return x+y; + } + bool operator<(const point& other) const { + return (other.diff() == diff()) ? (plus() > other.plus()) : (diff()> n; + point peaks[n]; + for (int i=0; i> peaks[i].x >> peaks[i].y; + } + sort(peaks, peaks+n); + int mx = -1234, ans=0; + for (int i=0; i= mx) ans++, mx = peaks[i].plus(); + } + cout << ans << "\n"; + return 0; +} \ No newline at end of file diff --git a/mst b/mst new file mode 100755 index 0000000..f9a7309 Binary files /dev/null and b/mst differ diff --git a/out_of_sorts.cpp b/out_of_sorts.cpp new file mode 100644 index 0000000..2280938 --- /dev/null +++ b/out_of_sorts.cpp @@ -0,0 +1,36 @@ +#include +#include +using namespace std; + +int bit[100005], n; +pair a[100005]; + +void update(int x) { + for (; x0; ) { ans += bit[x]; x -= (x & -x); } + return ans; +} + +int main() { + ifstream cin("sort.in"); + ofstream cout("sort.out"); + cin >> n; + for (int i=1; i<=n; i++) { + cin >> a[i].first; + a[i].second = i; + } + auto amax = [&](int& f, int s) -> void { if (f < s) f = s; }; + sort(a+1, a+1+n); + int ans = 1; + for (int i=1; i +using namespace std; + +int main() { + int n; + cin >> n; + string s; + cin >> s; + set st; + int ans = 0; + for (int i=0; i='A' and s[i]<='Z') { + ans = max(ans, (int)st.size()); + st.clear(); + } else { + st.insert(s[i]); + } + } + if (st.size() > 0) ans = max(ans, (int)st.size()); + cout << ans << endl; + return 0; +} \ No newline at end of file diff --git a/python_indentation.cpp b/python_indentation.cpp new file mode 100644 index 0000000..2b06709 --- /dev/null +++ b/python_indentation.cpp @@ -0,0 +1,49 @@ +#include +#include +using namespace std; +const int mx = 5 * 1e3 + 2; +const int mod = 1e9 + 7; +int dp[mx][mx]; +int a[mx], n; +int f(int pos, int nest) { + if(nest < 0){ + return 0; + } + if(pos > n){ + return 1; + } + if(dp[pos][nest] != -1){ + return dp[pos][nest]; + } + int res = f(pos , nest - 1); + if(a[pos-1] == 0){ + res += f(pos + 1 , nest); + } + else{ + res += f(pos + 1 , nest + 1); + res -= f(pos + 1 , nest); + } + if(res >= mod){ + res -= mod; + } + if(res < 0){ + res += mod; + } + return dp[pos][nest] = res; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(NULL); + cin >> n; + for (int i=0; i> c; + if (c=='f') a[i] = 1; + else a[i] = 0; + } + memset(dp, -1, sizeof(dp)); + cout << f(1, 0) << "\n"; + + return 0; +} \ No newline at end of file diff --git a/q_number_of_palin.cpp b/q_number_of_palin.cpp new file mode 100644 index 0000000..dcb24b5 --- /dev/null +++ b/q_number_of_palin.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +int f[5003][5003]; +bool dp[5001][5001]; +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + string s; + cin >> s; + for (int i=0; i> q; + for (int l=3; l<=s.size(); l++) { + for (int i=0; i> l >> r; + cout << (f[l-1][r-1]) << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/rest_stops.cpp b/rest_stops.cpp new file mode 100644 index 0000000..eefe3ea --- /dev/null +++ b/rest_stops.cpp @@ -0,0 +1,30 @@ +// #include +#include +#include +#include +using namespace std; +typedef pair pii; +int main() { + ifstream cin("reststops.in"); + ofstream cout("reststops.out"); + int l, n, rf, rb; + cin >> l >> n >> rf >> rb; + int mul = rf-rb; + vector p(n); + for (int i=0; i> p[i].first >> p[i].second; + } + sort(p.begin(), p.end(), [&](pii a, pii b) { + return a.second > b.second; + }); + + long long ans = 1ll*mul*p[0].first*p[0].second; + int covered = p[0].first; + for (int i=1; i +using namespace std; + +struct rmq { + vector a; + vector tr1, tr2; + int n; + rmq(vector b) { + n = b.size(); + a = b; + tr1.resize(4*n); + tr2.resize(4*n); + build(1, 0, n-1); + } + + int getSize(int x) { + int i=1; + while (i<<1 <= n) i<<=1; + return i<<1; + } + + bool f1(int i, int j) { + return a[i] < a[j]; + } + + bool f2(int i, int j) { + return a[i] > a[j]; + } + + void build(int node, int l, int r) { + if (l==r) { + tr1[node] = tr2[node] = l; + return; + } + int left = node<<1, right = left|1; + build(left, l, (l+r)>>1); + build(right, ((l+r)>>1) + 1, r); + tr1[node] = f1(tr1[node<<1], tr1[(node<<1)|1]) ? tr1[left] : tr1[right]; + tr2[node] = f2(tr2[node<<1], tr2[(node<<1)|1]) ? tr2[left] : tr2[right]; + } + + int mn(int l, int r) { + return mn(1, 0, n-1, l, r); + } + + int mx(int l, int r) { + return mx(1, 0, n-1, l, r); + } + + int mn(int p, int l, int r, int i, int j) { + if (i>r || j=r) return tr1[p]; + int left = p << 1, right = left|1, mid = (l+r)>>1; + int lans = mn(left, l, mid, i, j); + int rans = mn(right, mid+1, r, i, j); + if (lans != -1 and rans!=-1) { + return f1(lans, rans) ? lans : rans; + } else if(lans == -1) return rans; + else return lans; + } + + int mx(int p, int l, int r, int i, int j) { + if (i>r || j=r) return tr2[p]; + int left = p << 1, right = left|1, mid = (l+r)>>1; + int lans = mx(left, l, mid, i, j); + int rans = mx(right, mid+1, r, i, j); + if (lans != -1 and rans!=-1) { + return f2(lans, rans) ? lans : rans; + } else if(lans == -1) return rans; + else return lans; + } +}; + +int main() { + vector a; + srand(time(0)); + for (int i=0; i<10; i++) { + a.push_back((rand()%100 + 1)); + cout << a.back() << " "; + } + cout << "\n"; + rmq ds(a); + for (int i=0; i<10; i++) { + int l = rand()%10; + int r = rand()%10; + if (l > r) swap(l, r); + int mn = rand()%2; + if (mn) { + cout << "min from " << l+1 << " to " << r+1 << " is " << a[ds.mn(l, r)] << "\n"; + } else { + cout << "max from " << l+1 << " to " << r+1 << " is " << a[ds.mx(l, r)] << "\n"; + } + } + return 0; +} \ No newline at end of file diff --git a/round_subset.cpp b/round_subset.cpp new file mode 100644 index 0000000..9f53114 --- /dev/null +++ b/round_subset.cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; +const int N = 205; +const int M = 30 * N;// can go 5^30 each number +int dp[N][M]; +int n , k; +long long arr[N]; +int ans; +int get(long long val , int p){ + int res = 0; + while(val % p == 0){ + ++res; + val /= p; + } + return res; +} +int main(){ + scanf("%d %d" , &n , &k); + for(int i = 0 ; i < N ; ++i){ + for(int j = 0 ; j < M ; ++j){ + dp[i][j] = -64 * N; + } + } + dp[0][0] = 0; + for(int i = 1 ; i <= n ; ++i){ + scanf("%lld" , arr + i); + int pw2 = get(arr[i] , 2); + int pw5 = get(arr[i] , 5); + for(int j = min(k , i) ; j >= 1 ; --j){ + for(int l = n * 30 ; l >= pw5 ; --l){ + dp[j][l] = max(dp[j][l] , dp[j - 1][l - pw5] + pw2); + } + } + } + for(int l = n * 30 ; l >= 0 ; --l){ + ans = max(ans , min(l , dp[k][l])); + } + printf("%d\n" , ans); +} \ No newline at end of file diff --git a/sort.in b/sort.in new file mode 100644 index 0000000..5059fef --- /dev/null +++ b/sort.in @@ -0,0 +1,6 @@ +5 +1 +8 +5 +3 +2 diff --git a/sort.out b/sort.out new file mode 100644 index 0000000..7cabb01 --- /dev/null +++ b/sort.out @@ -0,0 +1,6 @@ +0 +1 +2 +1 +1 +2 diff --git a/sorts.in b/sorts.in new file mode 100644 index 0000000..5059fef --- /dev/null +++ b/sorts.in @@ -0,0 +1,6 @@ +5 +1 +8 +5 +3 +2 diff --git a/star_sky.cpp b/star_sky.cpp new file mode 100644 index 0000000..71e34df --- /dev/null +++ b/star_sky.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +int f[101][101][11]; +int main() { + int n, q, c, x1, y1, x2, y2, t; + cin >> n >> q >> c; + for (int i=1; i<=n; i++) { + cin >> x1 >> y1 >> t; + f[x1][y1][t]++; + } + for(int i=1; i<=100; i++){ + for(int j=1; j<=100; j++){ + for(int k=0; k<=10; k++){ + f[i][j][k] += f[i-1][j][k] + f[i][j-1][k]-f[i-1][j-1][k]; + } + } + } + while (q--) { + cin >> t >> x1 >> y1 >> x2 >> y2; + int temp[11]; + for(int i=0; i<=10; i++){ + temp[i] = f[x2][y2][i] + f[x1-1][y1-1][i] - f[x1-1][y2][i] - f[x2][y1-1][i]; + } + long long ans=0; + for(int i=0; i<=10; i++){ + ans += 1ll*(i+t)%(c+1) * temp[i]; + } + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/swap_adjacent.cpp b/swap_adjacent.cpp new file mode 100644 index 0000000..525db49 --- /dev/null +++ b/swap_adjacent.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +int main() { + int n, j=0; + cin >> n; + string s; + int a[n]; + for (int i=0; i> a[i]; + cin >> s; + for (int i=0; i+1 +#include +#include +#include +#include +using namespace std; +const int mx = 401; + +int grid[mx][mx]; +int vis[mx][mx]; +int n; + +bool safe(int i, int j) { + return (i=0) && (j=0) && (grid[i][j]==1); +} + +bool kill(int i, int j) { + if (vis[i][j] == 1) return false; + if (!safe(i, j)) return false; + if (i == (n-1)) return true; + vis[i][j] = 1; + if (kill(i-1, j-1)) return true; + if (kill(i-1, j)) return true; + if (kill(i, j-1)) return true; + if (kill(i, j+1)) return true; + if (kill(i+1, j+1)) return true; + if (kill(i+1, j)) return true; + return false; +} + + +bool solve() { + memset(grid, -1, sizeof grid); + memset(vis, -1, sizeof vis); + string s; + // cout << " solving\n"; + for (int i=0; i> s; + for (int j=0; j> n; + while (n != 0) { + cout << cnt << " "; + if (solve()) { + cout << "B\n"; + } else { + cout << "W\n"; + } + cnt++; + cin >> n; + } +} \ No newline at end of file diff --git a/uva352.cpp b/uva352.cpp new file mode 100644 index 0000000..43e184a --- /dev/null +++ b/uva352.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +using namespace std; +const int mx = 401; + +int grid[mx][mx]; +int vis[mx][mx]; +int n; + +bool safe(int i, int j) { + return (i=0) && (j=0) && (grid[i][j]==1); +} + +void kill(int i, int j) { + if (vis[i][j] == 1) return; + if (!safe(i, j)) return ; + + vis[i][j] = 1; + // cout << i << " " << j << endl; + + kill(i-1, j-1); + kill(i-1, j); + kill(i-1, j+1); + + kill(i, j-1); + kill(i, j+1); + + kill(i+1, j-1); + kill(i+1, j); + kill(i+1, j+1); + return; +} + + +int solve() { + // memset(grid, -1, sizeof grid); + memset(vis, 0, sizeof vis); + string s; + // cout << " solving\n"; + for (int i=0; i> s; + for (int j=0; j> n) { + cout << "Image number " << cnt << " contains " << solve() << " war eagles.\n"; + cnt++; + } +} \ No newline at end of file diff --git a/vacation.cpp b/vacation.cpp new file mode 100644 index 0000000..3d91eaa --- /dev/null +++ b/vacation.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +int main() { + int n; + cin >> n; + int a[n]; + for (int i=0; i> a[i]; + } + int evg=0,oddg=0,evs=0,odds=0, bothe=0, bothodd=0; + for (int i=0;i>1)&1; + if ((a[i]&3) ==3) bothe++; + } else { + oddg +=a[i]&1; + odds += (a[i]>>1)&1; + if ((a[i]&3)==3) bothodd++; + } + } + // cout << evg << " " << evs << " "< +using namespace std; + +int main() { + int n, d; + string s; + cin >> n >> d >> s; + // cout << s << "\n"; + int dp[n+1]; + for (int i=0; i<=n; i++) dp[i] = 1234567890; + dp[0] = dp[1] = 0; + for (int i=1; i<=n; i++) { + if (s[i-1] == '1') { + for (int j=max(1, i-d); j