john
commited on
Commit
•
c4b0eef
1
Parent(s):
f70c1a8
first commit
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .DS_Store +0 -0
- spoj_cpp_clean/ABA12A/ABA12A-13.cpp +20 -0
- spoj_cpp_clean/ABA12C/ABA12C-13.cpp +54 -0
- spoj_cpp_clean/ABA12C/ABA12C-8.cpp +157 -0
- spoj_cpp_clean/ABA12D/ABA12D-13.cpp +28 -0
- spoj_cpp_clean/ABA12E/ABA12E-13.cpp +59 -0
- spoj_cpp_clean/ABCDEF/ABCDEF-9.cpp +51 -0
- spoj_cpp_clean/ABCPATH/ABCPATH-13.cpp +67 -0
- spoj_cpp_clean/ABCPATH/ABCPATH-16.cpp +51 -0
- spoj_cpp_clean/ABSP1/ABSP1-3.cpp +47 -0
- spoj_cpp_clean/ABSYS/ABSYS-11.cpp +71 -0
- spoj_cpp_clean/ABSYS/ABSYS-16.cpp +40 -0
- spoj_cpp_clean/ABSYS/ABSYS-5.cpp +38 -0
- spoj_cpp_clean/ABSYS/ABSYS-7.cpp +102 -0
- spoj_cpp_clean/ABSYS/ABSYS-9.cpp +118 -0
- spoj_cpp_clean/ACMAKER/ACMAKER-3.cpp +93 -0
- spoj_cpp_clean/ACODE/ACODE-1.cpp +57 -0
- spoj_cpp_clean/ACODE/ACODE-11.cpp +58 -0
- spoj_cpp_clean/ACODE/ACODE-13.cpp +61 -0
- spoj_cpp_clean/ACODE/ACODE-16.cpp +38 -0
- spoj_cpp_clean/ACODE/ACODE-19.cpp +63 -0
- spoj_cpp_clean/ACODE/ACODE-5.cpp +39 -0
- spoj_cpp_clean/ACODE/ACODE-8.cpp +152 -0
- spoj_cpp_clean/ACODE/ACODE-9.cpp +77 -0
- spoj_cpp_clean/ACP10D/ACP10D-13.cpp +62 -0
- spoj_cpp_clean/ACPC10A/ACPC10A-11.cpp +27 -0
- spoj_cpp_clean/ACPC10A/ACPC10A-13.cpp +48 -0
- spoj_cpp_clean/ACPC10A/ACPC10A-16.cpp +22 -0
- spoj_cpp_clean/ACPC10A/ACPC10A-19.cpp +83 -0
- spoj_cpp_clean/ACPC10A/ACPC10A-5.cpp +24 -0
- spoj_cpp_clean/ACPC10A/ACPC10A-9.cpp +38 -0
- spoj_cpp_clean/ACPC10D/ACPC10D-9.cpp +89 -0
- spoj_cpp_clean/ACQUIRE/ACQUIRE-14.cpp +112 -0
- spoj_cpp_clean/ACTIV/ACTIV-14.cpp +83 -0
- spoj_cpp_clean/ADABRANC/ADABRANC-14.cpp +96 -0
- spoj_cpp_clean/ADACOINS/ADACOINS-14.cpp +32 -0
- spoj_cpp_clean/ADACYCLE/ADACYCLE-14.cpp +94 -0
- spoj_cpp_clean/ADADUNG/ADADUNG-9.cpp +59 -0
- spoj_cpp_clean/ADAFIELD/ADAFIELD-14.cpp +66 -0
- spoj_cpp_clean/ADAGAME/ADAGAME-14.cpp +117 -0
- spoj_cpp_clean/ADAGAME/ADAGAME-9.cpp +111 -0
- spoj_cpp_clean/ADAGAME2/ADAGAME2-9.cpp +107 -0
- spoj_cpp_clean/ADAINDEX/ADAINDEX-9.cpp +136 -0
- spoj_cpp_clean/ADAMATCH/ADAMATCH-14.cpp +165 -0
- spoj_cpp_clean/ADAMOLD/ADAMOLD-14.cpp +62 -0
- spoj_cpp_clean/ADAORANG/ADAORANG-14.cpp +100 -0
- spoj_cpp_clean/ADAORANG/ADAORANG-8.cpp +196 -0
- spoj_cpp_clean/ADAPLANT/ADAPLANT-20.cpp +101 -0
- spoj_cpp_clean/ADAPLUS/ADAPLUS-9.cpp +119 -0
- spoj_cpp_clean/ADAQUEUE/ADAQUEUE-20.cpp +91 -0
.DS_Store
ADDED
Binary file (10.2 kB). View file
|
|
spoj_cpp_clean/ABA12A/ABA12A-13.cpp
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <algorithm>
|
2 |
+
#include <vector>
|
3 |
+
#include <string>
|
4 |
+
|
5 |
+
#include <stdio.h>
|
6 |
+
#include <string.h>
|
7 |
+
#include <stdlib.h>
|
8 |
+
#include <math.h>
|
9 |
+
|
10 |
+
using namespace std;
|
11 |
+
typedef long long i64;
|
12 |
+
|
13 |
+
int main(){
|
14 |
+
int T; scanf("%d", &T);
|
15 |
+
for(int t=0; t < T; ++t){
|
16 |
+
i64 a, b; scanf("%lld %lld", &a, &b);
|
17 |
+
printf("%lld\n",b);
|
18 |
+
}
|
19 |
+
return 0;
|
20 |
+
}
|
spoj_cpp_clean/ABA12C/ABA12C-13.cpp
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <algorithm>
|
2 |
+
#include <vector>
|
3 |
+
#include <string>
|
4 |
+
|
5 |
+
#include <stdio.h>
|
6 |
+
#include <string.h>
|
7 |
+
#include <stdlib.h>
|
8 |
+
#include <math.h>
|
9 |
+
|
10 |
+
using namespace std;
|
11 |
+
typedef long long i64;
|
12 |
+
const int INF=0x3f3f3f3f;
|
13 |
+
|
14 |
+
int memo[105][105][105];
|
15 |
+
bool mark[105][105][105];
|
16 |
+
|
17 |
+
int f(vector <int> &v, int n, int i, int j, int w){
|
18 |
+
if(i >= (int)v.size()){
|
19 |
+
if(j <= n && w == 0)return 0;
|
20 |
+
else return INF;
|
21 |
+
}
|
22 |
+
|
23 |
+
int &best = memo[i][j][w];
|
24 |
+
if(mark[i][j][w])return best;
|
25 |
+
mark[i][j][w] = true;
|
26 |
+
|
27 |
+
best = f(v,n,i+1,j,w);
|
28 |
+
if(v[i] != -1){
|
29 |
+
for(int next=1; next + j <= n; ++next){
|
30 |
+
int nextW = w - (next * (i+1));
|
31 |
+
if(nextW >= 0)
|
32 |
+
best = min(best, f(v,n,i+1,next+j,nextW) + (next*v[i]));
|
33 |
+
}
|
34 |
+
}
|
35 |
+
|
36 |
+
return best;
|
37 |
+
}
|
38 |
+
|
39 |
+
int main(){
|
40 |
+
//freopen("data.in", "r", stdin);
|
41 |
+
int T; scanf("%d", &T);
|
42 |
+
for(int t=0; t < T; ++t){
|
43 |
+
int n, k; scanf("%d %d", &n, &k);
|
44 |
+
vector <int> v(k);
|
45 |
+
for(int i=0; i<k; ++i)
|
46 |
+
scanf("%d", &v[i]);
|
47 |
+
memset(mark, false, sizeof(mark));
|
48 |
+
|
49 |
+
int res = f(v, n, 0, 0, k);
|
50 |
+
if(res >= INF)res = -1;
|
51 |
+
printf("%d\n",res);
|
52 |
+
}
|
53 |
+
return 0;
|
54 |
+
}
|
spoj_cpp_clean/ABA12C/ABA12C-8.cpp
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/***************************************************
|
2 |
+
* Problem Name : ABA12C - Buying Apples!.cpp
|
3 |
+
* Problem Link : https://www.spoj.com/problems/ABA12C/
|
4 |
+
* OJ : Spoj
|
5 |
+
* Verdict : AC
|
6 |
+
* Date : 2020-02-19
|
7 |
+
* Problem Type : DP
|
8 |
+
* Author Name : Saikat Sharma
|
9 |
+
* University : CSE, MBSTU
|
10 |
+
***************************************************/
|
11 |
+
#include <iostream>
|
12 |
+
#include <cstdio>
|
13 |
+
#include <cmath>
|
14 |
+
#include <algorithm>
|
15 |
+
#include <climits>
|
16 |
+
#include <cstring>
|
17 |
+
#include <string>
|
18 |
+
#include <sstream>
|
19 |
+
#include <vector>
|
20 |
+
#include <queue>
|
21 |
+
#include <list>
|
22 |
+
#include <unordered_map>
|
23 |
+
#include <unordered_set>
|
24 |
+
#include <cstdlib>
|
25 |
+
#include <deque>
|
26 |
+
#include <stack>
|
27 |
+
#include <bitset>
|
28 |
+
#include <cassert>
|
29 |
+
#include <map>
|
30 |
+
#include <set>
|
31 |
+
#include <cassert>
|
32 |
+
#include <iomanip>
|
33 |
+
#include <random>
|
34 |
+
#include <ext/pb_ds/assoc_container.hpp>
|
35 |
+
#include <ext/pb_ds/tree_policy.hpp>
|
36 |
+
|
37 |
+
using namespace std;
|
38 |
+
using namespace __gnu_pbds;
|
39 |
+
typedef long long ll;
|
40 |
+
typedef unsigned long long ull;
|
41 |
+
|
42 |
+
#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
|
43 |
+
#define __FileRead freopen ("input.txt", "r", stdin)
|
44 |
+
#define __FileWrite freopen ("output.txt", "w", stdout)
|
45 |
+
#define SET(a,v) memset(a,v,sizeof(a))
|
46 |
+
#define SZ(v) (int)v.size()
|
47 |
+
#define pii pair<int,int>
|
48 |
+
#define pil pair <int, ll>
|
49 |
+
#define pli pair <ll, int>
|
50 |
+
#define pll pair <ll, ll>
|
51 |
+
#define debug cout <<"######\n"
|
52 |
+
#define debug1(x) cout <<"### " << x << " ###\n"
|
53 |
+
#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n"
|
54 |
+
#define nl cout << "\n";
|
55 |
+
#define sp cout << " ";
|
56 |
+
#define sl(n) scanf("%lld", &n)
|
57 |
+
#define sf(n) scanf("%lf", &n)
|
58 |
+
#define si(n) scanf("%d", &n)
|
59 |
+
#define ss(n) scanf("%s", n)
|
60 |
+
#define pf(n) scanf("%d", n)
|
61 |
+
#define pfl(n) scanf("%lld", n)
|
62 |
+
#define all(v) v.begin(), v.end()
|
63 |
+
#define rall(v) v.begin(), v.end()
|
64 |
+
#define srt(v) sort(v.begin(), v.end())
|
65 |
+
#define r_srt(v) sort(v.rbegin(), v.rend())
|
66 |
+
#define rev(v) reverse(v.begin(), v.end())
|
67 |
+
#define Sqr(x) ((x)*(x))
|
68 |
+
#define Mod(x, m) ((((x) % (m)) + (m)) % (m))
|
69 |
+
#define max3(a, b, c) max(a, max(b, c))
|
70 |
+
#define min3(a, b, c) min(a, min(b, c))
|
71 |
+
#define pb push_back
|
72 |
+
#define mk make_pair
|
73 |
+
#define MAX 100005
|
74 |
+
#define INF 1000000009
|
75 |
+
#define MOD 1000000007
|
76 |
+
|
77 |
+
template<class T>
|
78 |
+
using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
|
79 |
+
template<typename T>
|
80 |
+
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
|
81 |
+
tree_order_statistics_node_update>;
|
82 |
+
|
83 |
+
template <typename T> string toString ( T Number ) {
|
84 |
+
stringstream ss;
|
85 |
+
ss << Number;
|
86 |
+
return ss.str();
|
87 |
+
}
|
88 |
+
template<typename T> int toInt (T str) {
|
89 |
+
stringstream ss;
|
90 |
+
ss << str;
|
91 |
+
int num;
|
92 |
+
ss >> num;
|
93 |
+
return num;
|
94 |
+
}
|
95 |
+
ll lcm (ll a, ll b) {
|
96 |
+
return a * b / __gcd (a, b);
|
97 |
+
}
|
98 |
+
/************************************ Code Start Here ******************************************************/
|
99 |
+
const int MAX_N = 105;
|
100 |
+
|
101 |
+
int n, k;
|
102 |
+
ll ar[MAX_N];
|
103 |
+
ll dp[MAX_N][MAX_N][MAX_N];
|
104 |
+
|
105 |
+
ll fun (int pos, int kinche, int koto) {
|
106 |
+
if (pos > k) {
|
107 |
+
if (koto == k) {
|
108 |
+
return 0LL;
|
109 |
+
|
110 |
+
} else {
|
111 |
+
return INF;
|
112 |
+
}
|
113 |
+
}
|
114 |
+
|
115 |
+
ll &res = dp[pos][kinche][koto];
|
116 |
+
|
117 |
+
if (res != -1) return res;
|
118 |
+
|
119 |
+
ll res1 = INF, res2 = INF;
|
120 |
+
|
121 |
+
if (ar[pos] != -1) {
|
122 |
+
if (kinche < n && koto + pos <= k) {
|
123 |
+
res1 = ar[pos] + fun (pos, kinche + 1, koto + pos);
|
124 |
+
}
|
125 |
+
}
|
126 |
+
|
127 |
+
res2 = fun (pos + 1, kinche, koto);
|
128 |
+
return res = min (res1, res2);
|
129 |
+
}
|
130 |
+
|
131 |
+
int main () {
|
132 |
+
__FastIO;
|
133 |
+
//~ cout << setprecision (10);
|
134 |
+
//~ cout << fixed;
|
135 |
+
int tc;
|
136 |
+
cin >> tc;
|
137 |
+
|
138 |
+
while (tc--) {
|
139 |
+
cin >> n >> k;
|
140 |
+
|
141 |
+
for (int i = 1; i <= k; i++) {
|
142 |
+
cin >> ar[i];
|
143 |
+
}
|
144 |
+
|
145 |
+
SET (dp, -1);
|
146 |
+
ll res = fun (1, 0, 0);
|
147 |
+
|
148 |
+
if (res == INF) {
|
149 |
+
cout << -1 << "\n";
|
150 |
+
|
151 |
+
} else {
|
152 |
+
cout << res << "\n";
|
153 |
+
}
|
154 |
+
}
|
155 |
+
|
156 |
+
return 0;
|
157 |
+
}
|
spoj_cpp_clean/ABA12D/ABA12D-13.cpp
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <algorithm>
|
2 |
+
#include <vector>
|
3 |
+
#include <string>
|
4 |
+
#include <queue>
|
5 |
+
|
6 |
+
#include <stdio.h>
|
7 |
+
#include <string.h>
|
8 |
+
#include <stdlib.h>
|
9 |
+
#include <math.h>
|
10 |
+
|
11 |
+
using namespace std;
|
12 |
+
typedef long long i64;
|
13 |
+
|
14 |
+
int v[] = {2, 4, 9, 16, 25, 64, 289, 729, 1681, 2401, 3481, 4096, 5041, 7921, 10201, 15625, 17161, 27889, 28561, 29929, 65536, 83521, 85849, 146689, 262144, 279841, 458329, 491401, 531441, 552049, 579121, 597529, 683929, 703921, 707281, 734449, 829921};
|
15 |
+
|
16 |
+
int main(){
|
17 |
+
//freopen("data.in", "r", stdin);
|
18 |
+
int T; scanf("%d", &T);
|
19 |
+
for(int t=0; t<T; ++t){
|
20 |
+
int a, b; scanf("%d %d", &a, &b);
|
21 |
+
int n = (sizeof(v)/sizeof(int));
|
22 |
+
int res = 0;
|
23 |
+
for(int i=0; i<n; ++i)
|
24 |
+
if(v[i] >= a && v[i] <= b)++res;
|
25 |
+
printf("%d\n",res);
|
26 |
+
}
|
27 |
+
return 0;
|
28 |
+
}
|
spoj_cpp_clean/ABA12E/ABA12E-13.cpp
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <algorithm>
|
2 |
+
#include <vector>
|
3 |
+
#include <string>
|
4 |
+
#include <queue>
|
5 |
+
#include <map>
|
6 |
+
|
7 |
+
#include <stdio.h>
|
8 |
+
#include <string.h>
|
9 |
+
#include <stdlib.h>
|
10 |
+
#include <math.h>
|
11 |
+
|
12 |
+
using namespace std;
|
13 |
+
typedef long long i64;
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
//calculate how many sums are below sum
|
18 |
+
int g(vector <i64> &v, int n, i64 sum){
|
19 |
+
int res = 0;
|
20 |
+
for(int i=1; i<n; ++i){
|
21 |
+
int low = i, high = n, mid;
|
22 |
+
while(low < high){
|
23 |
+
int mid = (low + high)/2;
|
24 |
+
if(v[mid] - v[i-1] <= sum)low = mid+1;
|
25 |
+
else high = mid;
|
26 |
+
}
|
27 |
+
res += low-i;
|
28 |
+
}
|
29 |
+
return res;
|
30 |
+
}
|
31 |
+
|
32 |
+
//estimate the sum that will be the kth
|
33 |
+
i64 f(vector <i64> &v, int n, int k){
|
34 |
+
i64 low = 1LL, high = v[n-1], mid;
|
35 |
+
//printf("DEBUG: %lld\n",high);
|
36 |
+
while(low < high){
|
37 |
+
mid = (low + high)/2LL;
|
38 |
+
//printf("DEBUG: low=%lld mid=%lld high=%lld\n", low,mid,high);
|
39 |
+
int w = g(v,n, mid);
|
40 |
+
//printf("DEBUG: w=%d\n",w);
|
41 |
+
if(w < k)low = mid+1;
|
42 |
+
else high = mid;
|
43 |
+
}
|
44 |
+
return low;
|
45 |
+
}
|
46 |
+
|
47 |
+
int main(){
|
48 |
+
//freopen("data_large.in", "r", stdin);
|
49 |
+
int n, k; scanf("%d %d", &n, &k);
|
50 |
+
vector <i64> v(n+1);
|
51 |
+
i64 high = 0LL;
|
52 |
+
for(int i=1; i<=n; ++i){
|
53 |
+
scanf("%lld", &v[i]);
|
54 |
+
v[i] += v[i-1];
|
55 |
+
}
|
56 |
+
i64 res = f(v, n+1, k);
|
57 |
+
printf("%lld\n",res);
|
58 |
+
return 0;
|
59 |
+
}
|
spoj_cpp_clean/ABCDEF/ABCDEF-9.cpp
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include <iostream>
|
13 |
+
#include <algorithm>
|
14 |
+
#include <vector>
|
15 |
+
#include <utility>
|
16 |
+
|
17 |
+
using namespace std;
|
18 |
+
|
19 |
+
int main()
|
20 |
+
{
|
21 |
+
int n;
|
22 |
+
cin >> n;
|
23 |
+
int a[n];
|
24 |
+
vector<int> v1, v2;
|
25 |
+
for(int i = 0; i < n; i++)
|
26 |
+
cin >> a[i];
|
27 |
+
for(int i = 0; i < n; i++)
|
28 |
+
for(int j = 0; j < n; j++)
|
29 |
+
for(int k = 0; k < n; k++)
|
30 |
+
v1.push_back(a[i]*a[j]+a[k]);
|
31 |
+
|
32 |
+
for(int i = 0; i < n; i++)
|
33 |
+
for(int j = 0; j < n; j++)
|
34 |
+
for(int k = 0; k < n; k++) {
|
35 |
+
if(a[i])
|
36 |
+
v2.push_back(a[i]*(a[j]+a[k]));
|
37 |
+
}
|
38 |
+
|
39 |
+
sort(v1.begin(), v1.end());
|
40 |
+
sort(v2.begin(), v2.end());
|
41 |
+
|
42 |
+
long long count = 0;
|
43 |
+
pair<vector<int>::iterator, vector<int>::iterator> bounds;
|
44 |
+
for(vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
|
45 |
+
bounds = equal_range(v2.begin(), v2.end(), *it);
|
46 |
+
count += bounds.second-bounds.first;
|
47 |
+
}
|
48 |
+
cout << count << endl;
|
49 |
+
|
50 |
+
return 0;
|
51 |
+
}
|
spoj_cpp_clean/ABCPATH/ABCPATH-13.cpp
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <algorithm>
|
2 |
+
#include <vector>
|
3 |
+
#include <string>
|
4 |
+
#include <queue>
|
5 |
+
|
6 |
+
#include <stdio.h>
|
7 |
+
#include <string.h>
|
8 |
+
#include <stdlib.h>
|
9 |
+
#include <math.h>
|
10 |
+
|
11 |
+
using namespace std;
|
12 |
+
typedef long long i64;
|
13 |
+
|
14 |
+
char grid[60][60];
|
15 |
+
bool mark[60][60];
|
16 |
+
|
17 |
+
struct state_t{
|
18 |
+
int i, j, k;
|
19 |
+
char x;
|
20 |
+
state_t(int _i, int _j, int _k, char _x):
|
21 |
+
i(_i), j(_j),k(_k), x(_x){}
|
22 |
+
};
|
23 |
+
|
24 |
+
int movi[8] = {-1,-1,-1,0,0,1,1,1};
|
25 |
+
int movj[8] = {-1,0,1,-1,1,-1,0,1};
|
26 |
+
|
27 |
+
int f(int i, int j, int w, int h){
|
28 |
+
memset(mark, false, sizeof(mark));
|
29 |
+
queue <state_t> q;
|
30 |
+
q.push(state_t(i,j,1,'A'));
|
31 |
+
mark[i][j] = true;
|
32 |
+
int best = 1;
|
33 |
+
while(!q.empty()){
|
34 |
+
state_t st = q.front(); q.pop();
|
35 |
+
best = max(best, st.k);
|
36 |
+
if(st.x < 'Z'){
|
37 |
+
for(int s=0; s<8; ++s){
|
38 |
+
int ni = st.i + movi[s], nj = st.j+movj[s];
|
39 |
+
if(ni >= 0 && ni < h && nj >= 0 && nj < w && !mark[ni][nj] && grid[ni][nj] == st.x + 1){
|
40 |
+
q.push(state_t(ni,nj,st.k+1,st.x+1));
|
41 |
+
mark[ni][nj] = true;
|
42 |
+
}
|
43 |
+
}
|
44 |
+
}
|
45 |
+
}
|
46 |
+
return best;
|
47 |
+
}
|
48 |
+
|
49 |
+
int main(){
|
50 |
+
//freopen("data.in", "r", stdin);
|
51 |
+
int w, h, t=1;
|
52 |
+
while(scanf("%d %d", &h, &w)==2 && (w || h)){
|
53 |
+
//printf("%d %d\n",h,w);
|
54 |
+
for(int i=0; i<h; ++i)
|
55 |
+
scanf("%s", &grid[i]);
|
56 |
+
int best = 0;
|
57 |
+
for(int i=0; i<h; ++i){
|
58 |
+
for(int j=0; j<w; ++j){
|
59 |
+
if(grid[i][j] == 'A'){
|
60 |
+
best = max(best, f(i,j, w, h));
|
61 |
+
}
|
62 |
+
}
|
63 |
+
}
|
64 |
+
printf("Case %d: %d\n",t++, best);
|
65 |
+
}
|
66 |
+
return 0;
|
67 |
+
}
|
spoj_cpp_clean/ABCPATH/ABCPATH-16.cpp
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include<bits/stdc++.h>
|
2 |
+
|
3 |
+
using namespace std;
|
4 |
+
|
5 |
+
char a[51][51];
|
6 |
+
int dp[51][51]={};
|
7 |
+
|
8 |
+
int h,w;
|
9 |
+
int dr[8] = {-1,-1,-1,0,0,1,1,1,};
|
10 |
+
int dc[8] = {-1,1,0,-1,1,-1,1,0};
|
11 |
+
|
12 |
+
int dfs(int r, int c){
|
13 |
+
if(dp[r][c]!=0) return dp[r][c];
|
14 |
+
for(int i=0;i<8;i++){
|
15 |
+
if(r+dr[i]<0||c+dc[i]<0||r+dr[i]>=h||c+dc[i]>=w) continue;
|
16 |
+
if((int)(a[r+dr[i]][c+dc[i]]-a[r][c])==1) dp[r][c]=max(dp[r][c], 1+dfs(r+dr[i],c+dc[i]));
|
17 |
+
}
|
18 |
+
return dp[r][c];
|
19 |
+
}
|
20 |
+
|
21 |
+
int main(){
|
22 |
+
int t=1,ans=0,base=0;
|
23 |
+
cin>>h>>w;
|
24 |
+
while(h!=0&&w!=0){
|
25 |
+
base=0;
|
26 |
+
ans=0;
|
27 |
+
memset(dp,0,sizeof(dp));
|
28 |
+
for(int i=0;i<h;i++){
|
29 |
+
for(int j=0;j<w;j++){
|
30 |
+
cin>>a[i][j];
|
31 |
+
}
|
32 |
+
}
|
33 |
+
for(int i=0;i<h;i++){
|
34 |
+
for(int j=0;j<w;j++){
|
35 |
+
if(a[i][j]=='A'){
|
36 |
+
base=1;
|
37 |
+
ans=max(ans,dfs(i,j));
|
38 |
+
}
|
39 |
+
}
|
40 |
+
}
|
41 |
+
cout<<"Case "<<t++<<": "<<ans+base<<endl;
|
42 |
+
/*
|
43 |
+
for(int i=0;i<h;i++){
|
44 |
+
for(int j=0;j<w;j++){
|
45 |
+
cout<<dp[i][j]<<" ";
|
46 |
+
}
|
47 |
+
cout<<endl;
|
48 |
+
}*/
|
49 |
+
cin>>h>>w;
|
50 |
+
}
|
51 |
+
}
|
spoj_cpp_clean/ABSP1/ABSP1-3.cpp
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include<stdio.h>
|
2 |
+
#include<iostream>
|
3 |
+
#include<cmath>
|
4 |
+
#include<algorithm>
|
5 |
+
#include<cstring>
|
6 |
+
#include<map>
|
7 |
+
#include<set>
|
8 |
+
#include<vector>
|
9 |
+
#include<utility>
|
10 |
+
#include<math.h>
|
11 |
+
|
12 |
+
#define sd(x) scanf("%d",&x);
|
13 |
+
#define sd2(x,y) scanf("%d %d",&x,&y);
|
14 |
+
#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z);
|
15 |
+
#define sull(x) scanf("%ull",&x);
|
16 |
+
#define print(x) printf("%d\n",x);
|
17 |
+
#define print2(x,y) printf("%d %d\n",x,y);
|
18 |
+
#define print3(x,y,z) printf("%d %d %d\n",x,y,z);
|
19 |
+
#define printull(x) printf("%ull\n",x);
|
20 |
+
|
21 |
+
using namespace std;
|
22 |
+
|
23 |
+
int main(){
|
24 |
+
int t, n, fact;
|
25 |
+
long long nums[10000], sum[10000], total;
|
26 |
+
sd(t);
|
27 |
+
while(t--){
|
28 |
+
sd(n);
|
29 |
+
if(n == 0 or n == 1){
|
30 |
+
printf("0\n");
|
31 |
+
continue;
|
32 |
+
}
|
33 |
+
for(int i = 0; i < n; i++){
|
34 |
+
scanf("%lld",&nums[i]);
|
35 |
+
}
|
36 |
+
sum[n-1] = nums[n-1];
|
37 |
+
for(int i = n-2; i >= 0; i--){
|
38 |
+
sum[i] = sum[i+1] + nums[i];
|
39 |
+
}
|
40 |
+
total = 0;
|
41 |
+
for(int i = 0; i < n-1; i++){
|
42 |
+
total += abs((n-i-1)*nums[i] - sum[i+1]);
|
43 |
+
}
|
44 |
+
printf("%lld\n",total);
|
45 |
+
}
|
46 |
+
return 0;
|
47 |
+
}
|
spoj_cpp_clean/ABSYS/ABSYS-11.cpp
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* Written by Nitin Kumar Maharana
|
3 |
+
* nitin.maharana@gmail.com
|
4 |
+
*/
|
5 |
+
|
6 |
+
#include <iostream>
|
7 |
+
#include <string>
|
8 |
+
|
9 |
+
using namespace std;
|
10 |
+
|
11 |
+
bool isNumber(char *str)
|
12 |
+
{
|
13 |
+
bool flag = true;
|
14 |
+
|
15 |
+
for(int i = 0; str[i] != '\0'; i++)
|
16 |
+
{
|
17 |
+
if(str[i] < '0' || str[i] > '9')
|
18 |
+
{
|
19 |
+
flag = false;
|
20 |
+
break;
|
21 |
+
}
|
22 |
+
}
|
23 |
+
|
24 |
+
return flag;
|
25 |
+
}
|
26 |
+
|
27 |
+
int toNumber(char *str)
|
28 |
+
{
|
29 |
+
int number;
|
30 |
+
|
31 |
+
number = 0;
|
32 |
+
|
33 |
+
for(int i = 0; str[i] != '\0'; i++)
|
34 |
+
number = (number * 10) + (str[i] - '0');
|
35 |
+
|
36 |
+
return number;
|
37 |
+
}
|
38 |
+
|
39 |
+
int main(void)
|
40 |
+
{
|
41 |
+
int t;
|
42 |
+
char number1[30], number2[30], sum[30];
|
43 |
+
int n1, n2, summ;
|
44 |
+
char plus, equal;
|
45 |
+
|
46 |
+
cin >> t;
|
47 |
+
|
48 |
+
while(t--)
|
49 |
+
{
|
50 |
+
cin >> number1 >> plus >> number2 >> equal >> sum;
|
51 |
+
|
52 |
+
n1 = n2 = summ = -1;
|
53 |
+
|
54 |
+
if(isNumber(number1))
|
55 |
+
n1 = toNumber(number1);
|
56 |
+
|
57 |
+
if(isNumber(number2))
|
58 |
+
n2 = toNumber(number2);
|
59 |
+
|
60 |
+
if(isNumber(sum))
|
61 |
+
summ = toNumber(sum);
|
62 |
+
|
63 |
+
n1 = (n1 == -1) ? (summ - n2) : n1;
|
64 |
+
n2 = (n2 == -1) ? (summ - n1) : n2;
|
65 |
+
summ = (summ == -1) ? (n1 + n2) : summ;
|
66 |
+
|
67 |
+
cout << n1 << " + " << n2 << " = " << summ << endl;
|
68 |
+
}
|
69 |
+
|
70 |
+
return 0;
|
71 |
+
}
|
spoj_cpp_clean/ABSYS/ABSYS-16.cpp
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include<bits/stdc++.h>
|
2 |
+
#define ll long long
|
3 |
+
|
4 |
+
using namespace std;
|
5 |
+
|
6 |
+
void solve(){
|
7 |
+
vector<string> s(5);
|
8 |
+
for(ll i=0;i<5;i++) cin>>s[i];
|
9 |
+
if(s[0].find("machula")!=string::npos){
|
10 |
+
ll x = stoi(s[2]);
|
11 |
+
ll y = stoi(s[4]);
|
12 |
+
cout<<y-x<<" ";
|
13 |
+
for(ll i=1;i<5;i++)
|
14 |
+
cout<<s[i]<<" ";
|
15 |
+
cout<<endl;
|
16 |
+
}
|
17 |
+
if(s[2].find("machula")!=string::npos){
|
18 |
+
ll x = stoi(s[0]);
|
19 |
+
ll y = stoi(s[4]);
|
20 |
+
cout<<s[0]<<" "<<s[1]<<" "<<y-x<<" ";
|
21 |
+
for(ll i=3;i<5;i++)
|
22 |
+
cout<<s[i]<<" ";
|
23 |
+
cout<<endl;
|
24 |
+
}
|
25 |
+
if(s[4].find("machula")!=string::npos){
|
26 |
+
ll x = stoi(s[0]);
|
27 |
+
ll y = stoi(s[2]);
|
28 |
+
for(ll i=0;i<4;i++)
|
29 |
+
cout<<s[i]<<" ";
|
30 |
+
cout<<y+x<<endl;
|
31 |
+
}
|
32 |
+
}
|
33 |
+
|
34 |
+
int main(){
|
35 |
+
ll t;
|
36 |
+
cin>>t;
|
37 |
+
while(t--)
|
38 |
+
solve();
|
39 |
+
return 0;
|
40 |
+
}
|
spoj_cpp_clean/ABSYS/ABSYS-5.cpp
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <iostream>
|
2 |
+
#include <cstdio>
|
3 |
+
#include <string>
|
4 |
+
#include <cstring>
|
5 |
+
#include <cstdlib>
|
6 |
+
|
7 |
+
using namespace std;
|
8 |
+
|
9 |
+
int main()
|
10 |
+
{
|
11 |
+
int test,num1,num2,num3,i;
|
12 |
+
char str1[100],str2[100],str3[100],op1[2],op2[2];
|
13 |
+
cin>>test;
|
14 |
+
while(test--)
|
15 |
+
{
|
16 |
+
cin>>str1>>op1>>str2>>op2>>str3;
|
17 |
+
if(strchr(str1,'m')!=NULL)
|
18 |
+
{
|
19 |
+
num2=atoi(str2);
|
20 |
+
num3=atoi(str3);
|
21 |
+
num1=num3-num2;
|
22 |
+
}
|
23 |
+
else if(strchr(str2,'m')!=NULL)
|
24 |
+
{
|
25 |
+
num1=atoi(str1);
|
26 |
+
num3=atoi(str3);
|
27 |
+
num2=num3-num1;
|
28 |
+
}
|
29 |
+
else
|
30 |
+
{
|
31 |
+
num1=atoi(str1);
|
32 |
+
num2=atoi(str2);
|
33 |
+
num3=num1+num2;
|
34 |
+
}
|
35 |
+
cout<<num1<<" + "<<num2<<" = "<<num3<<endl;
|
36 |
+
}
|
37 |
+
return 0;
|
38 |
+
}
|
spoj_cpp_clean/ABSYS/ABSYS-7.cpp
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <cmath>
|
2 |
+
#include <cctype>
|
3 |
+
#include <cstdio>
|
4 |
+
#include <cstring>
|
5 |
+
#include <cstdlib>
|
6 |
+
#include <iostream>
|
7 |
+
#include <algorithm>
|
8 |
+
|
9 |
+
#define MOD 1000000007LL
|
10 |
+
#define LL long long
|
11 |
+
#define ULL unsigned long long
|
12 |
+
#define MAX(a,b) ((a)>(b)?(a):(b))
|
13 |
+
#define MIN(a,b) ((a)<(b)?(a):(b))
|
14 |
+
#define ABS(x) ((x)<0?-(x):(x))
|
15 |
+
#define si(n) scanf("%d",&n)
|
16 |
+
#define sf(n) scanf("%f",&n)
|
17 |
+
#define sl(n) scanf("%lld",&n)
|
18 |
+
#define slu(n) scanf("%llu",&n)
|
19 |
+
#define sd(n) scanf("%lf",&n)
|
20 |
+
#define ss(n) scanf("%s",n)
|
21 |
+
#define pnl printf("\n")//print new line
|
22 |
+
#define REP(n) for(int i=0;i<(n);i++)//repeat n times
|
23 |
+
#define FOR(a,b) for(int i=(a);i<(b);i++)//end not included
|
24 |
+
#define FORR(n) for(int i=(n);i>=0;i--)//reverse for
|
25 |
+
|
26 |
+
using namespace std;
|
27 |
+
|
28 |
+
int main()
|
29 |
+
{
|
30 |
+
int t=1;
|
31 |
+
scanf("%d",&t);
|
32 |
+
while(t--)
|
33 |
+
{
|
34 |
+
char c;
|
35 |
+
int f1=0,f2=0,f3=0;
|
36 |
+
int n1=0,n2=0,n3=0;
|
37 |
+
int p=0;
|
38 |
+
scanf("\n%c",&c);
|
39 |
+
while(c!='\n')
|
40 |
+
{
|
41 |
+
if(p==0)
|
42 |
+
{
|
43 |
+
if(c>=48 && c<=57)
|
44 |
+
{
|
45 |
+
n1=n1*10 + (c-48);
|
46 |
+
}
|
47 |
+
else
|
48 |
+
{
|
49 |
+
p++;
|
50 |
+
if(c=='m')
|
51 |
+
f1=1;
|
52 |
+
while(c!='+')
|
53 |
+
scanf("%c",&c);
|
54 |
+
scanf("%c",&c);
|
55 |
+
}
|
56 |
+
}
|
57 |
+
else if(p==1)
|
58 |
+
{
|
59 |
+
if(c>=48 && c<=57)
|
60 |
+
{
|
61 |
+
n2=n2*10 + (c-48);
|
62 |
+
}
|
63 |
+
else
|
64 |
+
{
|
65 |
+
p++;
|
66 |
+
if(c=='m')
|
67 |
+
f2=1;
|
68 |
+
while(c!='=')
|
69 |
+
scanf("%c",&c);
|
70 |
+
scanf("%c",&c);
|
71 |
+
}
|
72 |
+
}
|
73 |
+
else if(p==2)
|
74 |
+
{
|
75 |
+
if(c>=48 && c<=57)
|
76 |
+
{
|
77 |
+
n3=n3*10 + (c-48);
|
78 |
+
}
|
79 |
+
else
|
80 |
+
{
|
81 |
+
p++;
|
82 |
+
if(c=='m')
|
83 |
+
f3=1;
|
84 |
+
}
|
85 |
+
}
|
86 |
+
scanf("%c",&c);
|
87 |
+
}
|
88 |
+
if(f1==1)
|
89 |
+
{
|
90 |
+
printf("%d + %d = %d\n",(n3-n2),n2,n3);
|
91 |
+
}
|
92 |
+
else if(f2==1)
|
93 |
+
{
|
94 |
+
printf("%d + %d = %d\n",n1,(n3-n1),n3);
|
95 |
+
}
|
96 |
+
else if(f3==1)
|
97 |
+
{
|
98 |
+
printf("%d + %d = %d\n",n1,n2,(n1+n2));
|
99 |
+
}
|
100 |
+
}
|
101 |
+
return 0;
|
102 |
+
}
|
spoj_cpp_clean/ABSYS/ABSYS-9.cpp
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include <stdio.h>
|
13 |
+
#include <string>
|
14 |
+
#include <ctype.h>
|
15 |
+
|
16 |
+
#define gc() getchar_unlocked()
|
17 |
+
|
18 |
+
using namespace std;
|
19 |
+
|
20 |
+
inline int first()
|
21 |
+
{
|
22 |
+
string A;
|
23 |
+
char c;
|
24 |
+
|
25 |
+
c = gc();
|
26 |
+
while(isspace(c))
|
27 |
+
c = gc();
|
28 |
+
|
29 |
+
while(c!=' ')
|
30 |
+
{
|
31 |
+
if(isalpha(c))
|
32 |
+
{
|
33 |
+
while(c!=' ')
|
34 |
+
c = gc();
|
35 |
+
return -1;
|
36 |
+
}
|
37 |
+
A.push_back(c);
|
38 |
+
c = gc();
|
39 |
+
}
|
40 |
+
|
41 |
+
return stoi(A);
|
42 |
+
}
|
43 |
+
|
44 |
+
inline int second()
|
45 |
+
{
|
46 |
+
string A;
|
47 |
+
char c;
|
48 |
+
|
49 |
+
gc();
|
50 |
+
gc();
|
51 |
+
c = gc();
|
52 |
+
|
53 |
+
while(c!=' ')
|
54 |
+
{
|
55 |
+
if(isalpha(c))
|
56 |
+
{
|
57 |
+
while(c!=' ')
|
58 |
+
c = gc();
|
59 |
+
return -1;
|
60 |
+
}
|
61 |
+
A.push_back(c);
|
62 |
+
c = gc();
|
63 |
+
}
|
64 |
+
|
65 |
+
return stoi(A);
|
66 |
+
}
|
67 |
+
|
68 |
+
inline int third()
|
69 |
+
{
|
70 |
+
string A;
|
71 |
+
char c;
|
72 |
+
|
73 |
+
gc();
|
74 |
+
gc();
|
75 |
+
c = gc();
|
76 |
+
|
77 |
+
while(c!='\n' && c!=EOF)
|
78 |
+
{
|
79 |
+
if(isalpha(c))
|
80 |
+
{
|
81 |
+
while(c!='\n' && c!=EOF)
|
82 |
+
c = gc();
|
83 |
+
return -1;
|
84 |
+
}
|
85 |
+
A.push_back(c);
|
86 |
+
c = gc();
|
87 |
+
}
|
88 |
+
|
89 |
+
return stoi(A);
|
90 |
+
}
|
91 |
+
|
92 |
+
int main()
|
93 |
+
{
|
94 |
+
int T;
|
95 |
+
int f, s, t;
|
96 |
+
|
97 |
+
//freopen("input.txt","r",stdin);
|
98 |
+
|
99 |
+
scanf("%d", &T);
|
100 |
+
|
101 |
+
while(T--)
|
102 |
+
{
|
103 |
+
f = first();
|
104 |
+
s = second();
|
105 |
+
t = third();
|
106 |
+
|
107 |
+
if(f == -1)
|
108 |
+
f = t - s;
|
109 |
+
if(s == -1)
|
110 |
+
s = t - f;
|
111 |
+
if(t == -1)
|
112 |
+
t = f + s;
|
113 |
+
|
114 |
+
printf("%d + %d = %d\n", f, s, t);
|
115 |
+
}
|
116 |
+
|
117 |
+
return 0;
|
118 |
+
}
|
spoj_cpp_clean/ACMAKER/ACMAKER-3.cpp
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include<stdio.h>
|
2 |
+
#include<iostream>
|
3 |
+
#include<cmath>
|
4 |
+
#include<algorithm>
|
5 |
+
#include<cstring>
|
6 |
+
#include<map>
|
7 |
+
#include<set>
|
8 |
+
#include<vector>
|
9 |
+
#include<utility>
|
10 |
+
#include<math.h>
|
11 |
+
#include<sstream>
|
12 |
+
|
13 |
+
#define sd(x) scanf("%d",&x);
|
14 |
+
#define sd2(x,y) scanf("%d %d",&x,&y);
|
15 |
+
#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z);
|
16 |
+
#define sull(x) scanf("%ull",&x);
|
17 |
+
#define print(x) printf("%d\n",x);
|
18 |
+
#define print2(x,y) printf("%d %d\n",x,y);
|
19 |
+
#define print3(x,y,z) printf("%d %d %d\n",x,y,z);
|
20 |
+
#define printull(x) printf("%ull\n",x);
|
21 |
+
|
22 |
+
using namespace std;
|
23 |
+
|
24 |
+
// Uglyyyyyyyyyyyy
|
25 |
+
|
26 |
+
int main(){
|
27 |
+
int n, i, j, k, dp[151][151], count[151][151], w;
|
28 |
+
count[0][0] = 1;
|
29 |
+
for(i = 1; i <= 150; i++)
|
30 |
+
count[0][i] = 0;
|
31 |
+
for(i = 1; i <= 150; i++)
|
32 |
+
count[i][0] = 1;
|
33 |
+
string input, acronym, up_acronym, word;
|
34 |
+
set<string> insig;
|
35 |
+
vector<string> words;
|
36 |
+
sd(n);
|
37 |
+
while(n != 0){
|
38 |
+
insig.clear();
|
39 |
+
for(i = 0; i < n; i++){
|
40 |
+
cin>>word;
|
41 |
+
insig.insert(word);
|
42 |
+
}
|
43 |
+
getline(cin,input);
|
44 |
+
while(getline(cin,input) and input != "LAST CASE"){
|
45 |
+
words.clear();
|
46 |
+
stringstream ss;
|
47 |
+
|
48 |
+
ss<<input;
|
49 |
+
ss>>up_acronym;
|
50 |
+
acronym = "";
|
51 |
+
for(i = 0; i < up_acronym.length(); i++) acronym += tolower(up_acronym[i]);
|
52 |
+
|
53 |
+
|
54 |
+
while(ss>>word){
|
55 |
+
if(insig.find(word) == insig.end()){
|
56 |
+
words.push_back(word);
|
57 |
+
}
|
58 |
+
}
|
59 |
+
|
60 |
+
int l1 = words.size(), l2 = acronym.length() ;
|
61 |
+
|
62 |
+
for(i = 0; i <= l1; i++)
|
63 |
+
for(j = 0; j <= l2; j++)
|
64 |
+
dp[i][j] = 0;
|
65 |
+
|
66 |
+
dp[0][0] = 1;
|
67 |
+
|
68 |
+
for(i = 0; i < l1; i++){
|
69 |
+
for(j = 1; j <= l2; j++){
|
70 |
+
for(k = 1; k <= l2 - j + 1; k++){
|
71 |
+
|
72 |
+
for(int p = 1; p <= words[i].length(); p++){
|
73 |
+
for(int q = 1; q <= k; q++){
|
74 |
+
count[p][q] = count[p-1][q];
|
75 |
+
if(words[i][p-1] == acronym[j+q-2]) count[p][q] += count[p-1][q-1];
|
76 |
+
}
|
77 |
+
}
|
78 |
+
w = count[words[i].length()][k];
|
79 |
+
dp[i+1][j+k-1] += w * dp[i][j-1];
|
80 |
+
}
|
81 |
+
}
|
82 |
+
}
|
83 |
+
if(dp[l1][l2]){
|
84 |
+
cout<<up_acronym<<" can be formed in "<<dp[l1][l2]<<" ways"<<endl;
|
85 |
+
}
|
86 |
+
else{
|
87 |
+
cout<<up_acronym<<" is not a valid abbreviation"<<endl;
|
88 |
+
}
|
89 |
+
}
|
90 |
+
sd(n);
|
91 |
+
}
|
92 |
+
return 0;
|
93 |
+
}
|
spoj_cpp_clean/ACODE/ACODE-1.cpp
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <iostream>
|
2 |
+
#include <cstdio>
|
3 |
+
#include <cstring>
|
4 |
+
#include <cstdlib>
|
5 |
+
#define MAX_SIZE 5010
|
6 |
+
|
7 |
+
char *input;
|
8 |
+
int size;
|
9 |
+
|
10 |
+
unsigned long long cache[MAX_SIZE];
|
11 |
+
|
12 |
+
unsigned long long rec(int i) {
|
13 |
+
// std::cout << "i = " << i << std::endl;
|
14 |
+
if (i >= size - 1) {
|
15 |
+
if (i == size - 1 && input[i] == '0') return -1;
|
16 |
+
return 0;
|
17 |
+
}
|
18 |
+
if (cache[i] != -1) return cache[i];
|
19 |
+
|
20 |
+
unsigned long long ans;
|
21 |
+
switch (input[i]) {
|
22 |
+
case '0':
|
23 |
+
ans = - 1;
|
24 |
+
break;
|
25 |
+
case '2':
|
26 |
+
if (input[i+1] > '6') {
|
27 |
+
ans = rec(i + 1);
|
28 |
+
break;
|
29 |
+
}
|
30 |
+
case '1':
|
31 |
+
ans = 1 + rec(i + 1) + rec(i + 2);
|
32 |
+
break;
|
33 |
+
default:
|
34 |
+
ans = rec(i + 1);
|
35 |
+
break;
|
36 |
+
}
|
37 |
+
|
38 |
+
return cache[i] = ans;
|
39 |
+
}
|
40 |
+
|
41 |
+
int main() {
|
42 |
+
size_t max_size = MAX_SIZE;
|
43 |
+
input = (char*) malloc(sizeof(char) * (MAX_SIZE + 1));
|
44 |
+
|
45 |
+
while (true) {
|
46 |
+
size = getline(&input, &max_size, stdin);
|
47 |
+
--size;
|
48 |
+
input[size] = '\0';
|
49 |
+
// std::cout << "'" << input << "' " << size << " size" << std::endl;
|
50 |
+
|
51 |
+
memset(cache, -1, sizeof(unsigned long long) * MAX_SIZE);
|
52 |
+
|
53 |
+
if (input[0] == '0') break;
|
54 |
+
|
55 |
+
std::cout << 1 + rec(0) << std::endl;
|
56 |
+
}
|
57 |
+
}
|
spoj_cpp_clean/ACODE/ACODE-11.cpp
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* Written by Nitin Kumar Maharana
|
3 |
+
* nitin.maharana@gmail.com
|
4 |
+
*/
|
5 |
+
|
6 |
+
#include <iostream>
|
7 |
+
#include <string>
|
8 |
+
#include <map>
|
9 |
+
|
10 |
+
using namespace std;
|
11 |
+
|
12 |
+
string input;
|
13 |
+
map<int, unsigned long long> memory;
|
14 |
+
map<int, unsigned long long>::iterator it;
|
15 |
+
int len;
|
16 |
+
|
17 |
+
unsigned long long findNumDecoding(int index)
|
18 |
+
{
|
19 |
+
if(index == len || index == len-1)
|
20 |
+
return 1ull;
|
21 |
+
|
22 |
+
it = memory.find(index);
|
23 |
+
|
24 |
+
if(it != memory.end())
|
25 |
+
return (it->second);
|
26 |
+
|
27 |
+
unsigned long long result;
|
28 |
+
|
29 |
+
if(input[index] > '2' || (input[index] == '2' && input[index+1] > '6') || (index+2 < len && input[index+2] == '0'))
|
30 |
+
result = findNumDecoding(index+1);
|
31 |
+
else if(input[index+1] == '0')
|
32 |
+
result = findNumDecoding(index+2);
|
33 |
+
else
|
34 |
+
result = (findNumDecoding(index+1) + findNumDecoding(index+2));
|
35 |
+
|
36 |
+
memory.insert(make_pair(index, result));
|
37 |
+
|
38 |
+
return result;
|
39 |
+
}
|
40 |
+
|
41 |
+
int main(void)
|
42 |
+
{
|
43 |
+
cin >> input;
|
44 |
+
|
45 |
+
while(input != "0")
|
46 |
+
{
|
47 |
+
len = input.length();
|
48 |
+
|
49 |
+
cout << findNumDecoding(0) << endl;
|
50 |
+
|
51 |
+
input.clear();
|
52 |
+
memory.clear();
|
53 |
+
|
54 |
+
cin >> input;
|
55 |
+
}
|
56 |
+
|
57 |
+
return 0;
|
58 |
+
}
|
spoj_cpp_clean/ACODE/ACODE-13.cpp
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma warning(disable:4018) // signed/unsigned mistatch
|
2 |
+
#pragma warning(disable:4244) // w64 to int cast
|
3 |
+
#pragma warning(disable:4267) // big to small -- possible loss of data
|
4 |
+
#pragma warning(disable:4786) // long identifiers
|
5 |
+
#pragma warning(disable:4800) // forcing int to bool
|
6 |
+
#pragma warning(disable:4996) // deprecations
|
7 |
+
#include "assert.h"
|
8 |
+
#include "ctype.h"
|
9 |
+
#include "float.h"
|
10 |
+
#include "math.h"
|
11 |
+
#include "stdio.h"
|
12 |
+
#include "string.h"
|
13 |
+
#include "stdlib.h"
|
14 |
+
#include "stdarg.h"
|
15 |
+
#include "time.h"
|
16 |
+
#include "algorithm"
|
17 |
+
#include "numeric"
|
18 |
+
#include "functional"
|
19 |
+
#include "utility"
|
20 |
+
#include "bitset"
|
21 |
+
#include "vector"
|
22 |
+
#include "list"
|
23 |
+
#include "set"
|
24 |
+
#include "map"
|
25 |
+
#include "queue"
|
26 |
+
#include "stack"
|
27 |
+
#include "string"
|
28 |
+
#include "sstream"
|
29 |
+
#include "iostream"
|
30 |
+
#define all(v) (v).begin(), (v).end()
|
31 |
+
typedef long long i64;
|
32 |
+
template <class T> void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());}
|
33 |
+
using namespace std;
|
34 |
+
|
35 |
+
i64 memo[5001];
|
36 |
+
bool mark[5001];
|
37 |
+
char buffer[5001];
|
38 |
+
int n;
|
39 |
+
|
40 |
+
i64 f(int k){
|
41 |
+
if(k >= n)return 1;
|
42 |
+
if(buffer[k] == '0')return 0;
|
43 |
+
i64 &count = memo[k];
|
44 |
+
if(mark[k])return count;
|
45 |
+
mark[k] = true;
|
46 |
+
count = f(k+1);
|
47 |
+
if((buffer[k] == '1' || buffer[k] == '2') && k+2 <= n)
|
48 |
+
count += f(k+2);
|
49 |
+
return count;
|
50 |
+
}
|
51 |
+
|
52 |
+
int main(){
|
53 |
+
freopen("data.in","r",stdin);
|
54 |
+
while(scanf("%s", buffer) == 1 && buffer[0] != '0'){
|
55 |
+
n = strlen(buffer);
|
56 |
+
memset(mark,false,sizeof(mark));
|
57 |
+
i64 res = f(0);
|
58 |
+
printf("%lld\n",res);
|
59 |
+
}
|
60 |
+
return 0;
|
61 |
+
}
|
spoj_cpp_clean/ACODE/ACODE-16.cpp
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// [amitdu6ey]
|
2 |
+
// g++ -std=c++11 -o2 -Wall filename.cpp -o filename
|
3 |
+
#include <bits/stdc++.h>
|
4 |
+
#define ll long long
|
5 |
+
|
6 |
+
using namespace std;
|
7 |
+
|
8 |
+
void solve(string a){
|
9 |
+
ll n = a.length();
|
10 |
+
vector<ll> dp(n,0);
|
11 |
+
dp[0]=1;
|
12 |
+
|
13 |
+
ll num = (a[0]-'0')*10 + (a[1]-'0');
|
14 |
+
if(num<=26) dp[1]=2;
|
15 |
+
else dp[1]=1;
|
16 |
+
|
17 |
+
for(int i=2;i<n;i++){
|
18 |
+
ll y = (a[i-1]-'0')*10 + (a[i]-'0');
|
19 |
+
if(y<=26){
|
20 |
+
dp[i]=dp[i-1]+dp[i-2];
|
21 |
+
}
|
22 |
+
else{
|
23 |
+
dp[i]=dp[i-1];
|
24 |
+
}
|
25 |
+
}
|
26 |
+
cout<<dp[n-1]<<"\n";
|
27 |
+
return;
|
28 |
+
}
|
29 |
+
|
30 |
+
int main(){
|
31 |
+
while(true){
|
32 |
+
string x;
|
33 |
+
cin>>x;
|
34 |
+
if(x=="0") break;
|
35 |
+
solve(x);
|
36 |
+
}
|
37 |
+
return 0;
|
38 |
+
}
|
spoj_cpp_clean/ACODE/ACODE-19.cpp
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
#define endl "\n"
|
3 |
+
#define ll long long
|
4 |
+
#define mod 1000000007
|
5 |
+
#define MAX 1000001
|
6 |
+
#define PI 3.14159265
|
7 |
+
|
8 |
+
using namespace std;
|
9 |
+
|
10 |
+
int main()
|
11 |
+
{
|
12 |
+
ios_base::sync_with_stdio(0);
|
13 |
+
cin.tie(0); cout.tie(0);
|
14 |
+
|
15 |
+
//ll t;cin>>t;
|
16 |
+
ll dp[5001];
|
17 |
+
char num[5001];
|
18 |
+
while (true) {
|
19 |
+
cin >> num;
|
20 |
+
if (num[0] == '0') {
|
21 |
+
return 0;
|
22 |
+
}
|
23 |
+
int l = strlen(num);
|
24 |
+
dp[0] = 1;
|
25 |
+
if (l == 1) {
|
26 |
+
cout << "1" << endl;
|
27 |
+
continue;
|
28 |
+
}
|
29 |
+
dp[1] = 1;
|
30 |
+
int c1, c2;
|
31 |
+
c2 = num[0] - '0';
|
32 |
+
c1 = num[1] - '0';
|
33 |
+
if (c1 == 0) {
|
34 |
+
if (c2 == 1 || c2 == 2) {
|
35 |
+
dp[1] = 1;
|
36 |
+
}
|
37 |
+
else {
|
38 |
+
dp[1] = 0;
|
39 |
+
}
|
40 |
+
}
|
41 |
+
else {
|
42 |
+
if (c2 == 1 || (c2 == 2 && c1 <= 6)) {
|
43 |
+
dp[1] = 2;
|
44 |
+
}
|
45 |
+
else {
|
46 |
+
dp[1] = 1;
|
47 |
+
}
|
48 |
+
}
|
49 |
+
for (int i = 2; i < l; i++) {
|
50 |
+
dp[i] = 0;
|
51 |
+
c1 = num[i] - '0';
|
52 |
+
c2 = num[i - 1] - '0';
|
53 |
+
if (c1 != 0) {
|
54 |
+
dp[i] += dp[i - 1];
|
55 |
+
}
|
56 |
+
if (c2 == 1 || (c2 == 2 && c1 < 7)) {
|
57 |
+
dp[i] += dp[i - 2];
|
58 |
+
}
|
59 |
+
}
|
60 |
+
cout << dp[l - 1] << endl;
|
61 |
+
}
|
62 |
+
return 0;
|
63 |
+
}
|
spoj_cpp_clean/ACODE/ACODE-5.cpp
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <iostream>
|
2 |
+
#include <string>
|
3 |
+
#include <map>
|
4 |
+
|
5 |
+
// Evaluate for "226210", other than that the code is self explanatory
|
6 |
+
using namespace std;
|
7 |
+
|
8 |
+
map<int, long long>dp;
|
9 |
+
|
10 |
+
int main()
|
11 |
+
{
|
12 |
+
string input;
|
13 |
+
int i,strLength;
|
14 |
+
cin>>input;
|
15 |
+
|
16 |
+
while(input[0]!='0')
|
17 |
+
{
|
18 |
+
strLength=input.length();
|
19 |
+
dp[0]=1;
|
20 |
+
dp[1]=1;
|
21 |
+
for(i=1;i<strLength;i++)
|
22 |
+
{
|
23 |
+
if(input[i]!='0')
|
24 |
+
{
|
25 |
+
if(input[i-1]!='0'&&(input[i-1]-48)*10+(input[i]-48)<=26)
|
26 |
+
{
|
27 |
+
dp[i+1]=dp[i]+dp[i-1];
|
28 |
+
}
|
29 |
+
else
|
30 |
+
dp[i+1]=dp[i];
|
31 |
+
}
|
32 |
+
else
|
33 |
+
dp[i+1]=dp[i-1];
|
34 |
+
}
|
35 |
+
cout<<dp[i]<<endl;
|
36 |
+
cin>>input;
|
37 |
+
}
|
38 |
+
return 0;
|
39 |
+
}
|
spoj_cpp_clean/ACODE/ACODE-8.cpp
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/***************************************************
|
2 |
+
* Problem Name : ACODE - Alphacode.cpp
|
3 |
+
* Problem Link : https://www.spoj.com/problems/ACODE/
|
4 |
+
* OJ : Spoj
|
5 |
+
* Verdict : AC
|
6 |
+
* Date : 2020-02-19
|
7 |
+
* Problem Type : DP
|
8 |
+
* Author Name : Saikat Sharma
|
9 |
+
* University : CSE, MBSTU
|
10 |
+
***************************************************/
|
11 |
+
#include <iostream>
|
12 |
+
#include <cstdio>
|
13 |
+
#include <cmath>
|
14 |
+
#include <algorithm>
|
15 |
+
#include <climits>
|
16 |
+
#include <cstring>
|
17 |
+
#include <string>
|
18 |
+
#include <sstream>
|
19 |
+
#include <vector>
|
20 |
+
#include <queue>
|
21 |
+
#include <list>
|
22 |
+
#include <unordered_map>
|
23 |
+
#include <unordered_set>
|
24 |
+
#include <cstdlib>
|
25 |
+
#include <deque>
|
26 |
+
#include <stack>
|
27 |
+
#include <bitset>
|
28 |
+
#include <cassert>
|
29 |
+
#include <map>
|
30 |
+
#include <set>
|
31 |
+
#include <cassert>
|
32 |
+
#include <iomanip>
|
33 |
+
#include <random>
|
34 |
+
#include <ext/pb_ds/assoc_container.hpp>
|
35 |
+
#include <ext/pb_ds/tree_policy.hpp>
|
36 |
+
|
37 |
+
using namespace std;
|
38 |
+
using namespace __gnu_pbds;
|
39 |
+
typedef long long ll;
|
40 |
+
typedef unsigned long long ull;
|
41 |
+
|
42 |
+
#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
|
43 |
+
#define __FileRead freopen ("input.txt", "r", stdin)
|
44 |
+
#define __FileWrite freopen ("output.txt", "w", stdout)
|
45 |
+
#define SET(a,v) memset(a,v,sizeof(a))
|
46 |
+
#define SZ(v) (int)v.size()
|
47 |
+
#define pii pair<int,int>
|
48 |
+
#define pil pair <int, ll>
|
49 |
+
#define pli pair <ll, int>
|
50 |
+
#define pll pair <ll, ll>
|
51 |
+
#define debug cout <<"######\n"
|
52 |
+
#define debug1(x) cout <<"### " << x << " ###\n"
|
53 |
+
#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n"
|
54 |
+
#define nl cout << "\n";
|
55 |
+
#define sp cout << " ";
|
56 |
+
#define sl(n) scanf("%lld", &n)
|
57 |
+
#define sf(n) scanf("%lf", &n)
|
58 |
+
#define si(n) scanf("%d", &n)
|
59 |
+
#define ss(n) scanf("%s", n)
|
60 |
+
#define pf(n) scanf("%d", n)
|
61 |
+
#define pfl(n) scanf("%lld", n)
|
62 |
+
#define all(v) v.begin(), v.end()
|
63 |
+
#define rall(v) v.begin(), v.end()
|
64 |
+
#define srt(v) sort(v.begin(), v.end())
|
65 |
+
#define r_srt(v) sort(v.rbegin(), v.rend())
|
66 |
+
#define rev(v) reverse(v.begin(), v.end())
|
67 |
+
#define Sqr(x) ((x)*(x))
|
68 |
+
#define Mod(x, m) ((((x) % (m)) + (m)) % (m))
|
69 |
+
#define max3(a, b, c) max(a, max(b, c))
|
70 |
+
#define min3(a, b, c) min(a, min(b, c))
|
71 |
+
#define pb push_back
|
72 |
+
#define mk make_pair
|
73 |
+
#define MAX 100005
|
74 |
+
#define INF 1000000009
|
75 |
+
#define MOD 1000000007
|
76 |
+
|
77 |
+
template<class T>
|
78 |
+
using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
|
79 |
+
template<typename T>
|
80 |
+
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
|
81 |
+
tree_order_statistics_node_update>;
|
82 |
+
|
83 |
+
template <typename T> string toString ( T Number ) {
|
84 |
+
stringstream ss;
|
85 |
+
ss << Number;
|
86 |
+
return ss.str();
|
87 |
+
}
|
88 |
+
template<typename T> int toInt (T str) {
|
89 |
+
stringstream ss;
|
90 |
+
ss << str;
|
91 |
+
int num;
|
92 |
+
ss >> num;
|
93 |
+
return num;
|
94 |
+
}
|
95 |
+
ll lcm (ll a, ll b) {
|
96 |
+
return a * b / __gcd (a, b);
|
97 |
+
}
|
98 |
+
/************************************ Code Start Here ******************************************************/
|
99 |
+
const int MAX_N = 5005;
|
100 |
+
|
101 |
+
string str;
|
102 |
+
ll dp[MAX_N][105];
|
103 |
+
|
104 |
+
ll fun (int pos, int x) {
|
105 |
+
if (pos >= (int) str.size() ) {
|
106 |
+
if (x <= 26 && x > 0) return 1LL;
|
107 |
+
else return 0LL;
|
108 |
+
}
|
109 |
+
|
110 |
+
ll &res = dp[pos][x];
|
111 |
+
|
112 |
+
if (res != -1) return res;
|
113 |
+
|
114 |
+
ll res1 = 0, res2 = 0;
|
115 |
+
|
116 |
+
if (str[pos] != '0') {
|
117 |
+
if (pos + 1 < (int) str.size() ) {
|
118 |
+
if (str[pos + 1] != '0') {
|
119 |
+
res1 = fun (pos + 1, str[pos] - '0');
|
120 |
+
int val = (str[pos] - '0') * 10 + (str[pos + 1] - '0');
|
121 |
+
|
122 |
+
if (val <= 26) res2 = fun (pos + 2, val);
|
123 |
+
|
124 |
+
} else {
|
125 |
+
int val = (str[pos] - '0') * 10 + (str[pos + 1] - '0');
|
126 |
+
|
127 |
+
if (val <= 26) res2 = fun (pos + 2, val);
|
128 |
+
}
|
129 |
+
|
130 |
+
} else {
|
131 |
+
res1 = fun (pos + 1, str[pos] - '0');
|
132 |
+
}
|
133 |
+
}
|
134 |
+
|
135 |
+
return res = res1 + res2;
|
136 |
+
}
|
137 |
+
|
138 |
+
int main () {
|
139 |
+
__FastIO;
|
140 |
+
//~ cout << setprecision (10);
|
141 |
+
//~ cout << fixed;
|
142 |
+
while (cin >> str) {
|
143 |
+
if (str == "0") break;
|
144 |
+
|
145 |
+
SET (dp, -1);
|
146 |
+
ll res = fun (0, 0);
|
147 |
+
cout << res << "\n";
|
148 |
+
}
|
149 |
+
|
150 |
+
return 0;
|
151 |
+
}
|
152 |
+
//1013
|
spoj_cpp_clean/ACODE/ACODE-9.cpp
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include <stdio.h>
|
13 |
+
#include <string.h>
|
14 |
+
|
15 |
+
char str[50000];
|
16 |
+
unsigned long long DP[50000];
|
17 |
+
|
18 |
+
bool init()
|
19 |
+
{
|
20 |
+
scanf("%s", str);
|
21 |
+
|
22 |
+
if(strcmp(str, "0")==0)
|
23 |
+
return 0;
|
24 |
+
|
25 |
+
memset(DP, 0, sizeof(DP));
|
26 |
+
|
27 |
+
return 1;
|
28 |
+
}
|
29 |
+
|
30 |
+
|
31 |
+
inline bool valid(int i, int j)
|
32 |
+
{
|
33 |
+
if(i==j)
|
34 |
+
{
|
35 |
+
if(str[i]=='0')
|
36 |
+
return 0;
|
37 |
+
return 1;
|
38 |
+
}
|
39 |
+
|
40 |
+
if(str[i]=='0')
|
41 |
+
return 0;
|
42 |
+
|
43 |
+
if((str[i]-'0')*10 + (str[j]-'0') <=26)
|
44 |
+
return 1;
|
45 |
+
|
46 |
+
return 0;
|
47 |
+
}
|
48 |
+
|
49 |
+
int main()
|
50 |
+
{
|
51 |
+
int i;
|
52 |
+
|
53 |
+
//freopen("input.txt","r",stdin);
|
54 |
+
|
55 |
+
while(init())
|
56 |
+
{
|
57 |
+
DP[0]=1;
|
58 |
+
|
59 |
+
if(valid(1, 1))
|
60 |
+
DP[1]+=DP[0];
|
61 |
+
if(valid(0, 1))
|
62 |
+
DP[1]++;
|
63 |
+
|
64 |
+
for(i=2; i<strlen(str); i++)
|
65 |
+
{
|
66 |
+
if(valid(i, i))
|
67 |
+
DP[i]+=DP[i-1];
|
68 |
+
if(valid(i-1, i))
|
69 |
+
DP[i]+=DP[i-2];
|
70 |
+
}
|
71 |
+
|
72 |
+
printf("%lld\n", DP[strlen(str)-1]);
|
73 |
+
}
|
74 |
+
|
75 |
+
|
76 |
+
return 0;
|
77 |
+
}
|
spoj_cpp_clean/ACP10D/ACP10D-13.cpp
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <algorithm>
|
2 |
+
#include <vector>
|
3 |
+
#include <string>
|
4 |
+
#include <queue>
|
5 |
+
#include <map>
|
6 |
+
|
7 |
+
#include <stdio.h>
|
8 |
+
#include <string.h>
|
9 |
+
#include <stdlib.h>
|
10 |
+
#include <math.h>
|
11 |
+
|
12 |
+
using namespace std;
|
13 |
+
typedef long long i64;
|
14 |
+
const int INF = 0x3f3f3f3f;
|
15 |
+
|
16 |
+
int costs[100002][3];
|
17 |
+
int memo[100002][3];
|
18 |
+
bool mark[100002][3];
|
19 |
+
|
20 |
+
int f(int n, int i, int j){
|
21 |
+
if(i == n-1 && j == 1)return costs[i][j];
|
22 |
+
int &best = memo[i][j];
|
23 |
+
if(mark[i][j])return best;
|
24 |
+
mark[i][j] = true;
|
25 |
+
best = INF;
|
26 |
+
|
27 |
+
//trans
|
28 |
+
if(j == 0){
|
29 |
+
best = min (best, f(n,i,j+1) + costs[i][j]);
|
30 |
+
if(i+1 < n){
|
31 |
+
best = min(best, f(n, i+1, j) + costs[i][j]);
|
32 |
+
best = min(best, f(n, i+1, j+1) + costs[i][j]);
|
33 |
+
}
|
34 |
+
}else if(j == 1){
|
35 |
+
best = min(best, f(n,i,j+1) + costs[i][j]);
|
36 |
+
if(i+1 < n){
|
37 |
+
best = min(best, f(n, i+1, j-1) + costs[i][j]);
|
38 |
+
best = min(best, f(n, i+1, j) + costs[i][j]);
|
39 |
+
best = min(best, f(n, i+1, j+1) + costs[i][j]);
|
40 |
+
}
|
41 |
+
}else{
|
42 |
+
if(i+1 < n){
|
43 |
+
best = min(best, f(n, i+1, j-1)+ costs[i][j]);
|
44 |
+
best = min(best, f(n, i+1, j) + costs[i][j]);
|
45 |
+
}
|
46 |
+
}
|
47 |
+
return best;
|
48 |
+
}
|
49 |
+
|
50 |
+
int main(){
|
51 |
+
//freopen("data.in", "r", stdin);
|
52 |
+
int n;
|
53 |
+
for(int k=1; scanf("%d", &n)==1 && n; ++k){
|
54 |
+
for(int i=0; i<n; ++i){
|
55 |
+
scanf("%d %d %d", &costs[i][0], &costs[i][1], &costs[i][2]);
|
56 |
+
}
|
57 |
+
memset(mark, false, sizeof(mark));
|
58 |
+
int res = f(n, 0, 1);
|
59 |
+
printf("%d. %d\n",k,res);
|
60 |
+
}
|
61 |
+
return 0;
|
62 |
+
}
|
spoj_cpp_clean/ACPC10A/ACPC10A-11.cpp
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* Written by Nitin Kumar Maharana
|
3 |
+
* nitin.maharana@gmail.com
|
4 |
+
*/
|
5 |
+
|
6 |
+
#include <iostream>
|
7 |
+
|
8 |
+
using namespace std;
|
9 |
+
|
10 |
+
int main(void)
|
11 |
+
{
|
12 |
+
int a1, a2, a3;
|
13 |
+
|
14 |
+
cin >> a1 >> a2 >> a3;
|
15 |
+
|
16 |
+
while(a1 != 0 || a2 != 0 || a3 != 0)
|
17 |
+
{
|
18 |
+
if((a3 - a2) == (a2 - a1))
|
19 |
+
cout << "AP " << (a3 + (a2-a1)) << endl;
|
20 |
+
else
|
21 |
+
cout << "GP " << (a3 * (a2/a1)) << endl;
|
22 |
+
|
23 |
+
cin >> a1 >> a2 >> a3;
|
24 |
+
}
|
25 |
+
|
26 |
+
return 0;
|
27 |
+
}
|
spoj_cpp_clean/ACPC10A/ACPC10A-13.cpp
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma warning(disable:4018) // signed/unsigned mistatch
|
2 |
+
#pragma warning(disable:4244) // w64 to int cast
|
3 |
+
#pragma warning(disable:4267) // big to small -- possible loss of data
|
4 |
+
#pragma warning(disable:4786) // long identifiers
|
5 |
+
#pragma warning(disable:4800) // forcing int to bool
|
6 |
+
#pragma warning(disable:4996) // deprecations
|
7 |
+
#include "assert.h"
|
8 |
+
#include "ctype.h"
|
9 |
+
#include "float.h"
|
10 |
+
#include "math.h"
|
11 |
+
#include "stdio.h"
|
12 |
+
#include "string.h"
|
13 |
+
#include "stdlib.h"
|
14 |
+
#include "stdarg.h"
|
15 |
+
#include "time.h"
|
16 |
+
#include "algorithm"
|
17 |
+
#include "numeric"
|
18 |
+
#include "functional"
|
19 |
+
#include "utility"
|
20 |
+
#include "bitset"
|
21 |
+
#include "vector"
|
22 |
+
#include "list"
|
23 |
+
#include "set"
|
24 |
+
#include "map"
|
25 |
+
#include "queue"
|
26 |
+
#include "stack"
|
27 |
+
#include "string"
|
28 |
+
#include "sstream"
|
29 |
+
#include "iostream"
|
30 |
+
#define all(v) (v).begin(), (v).end()
|
31 |
+
typedef long long i64;
|
32 |
+
template <class T> void make_unique(T& v) {sort(all(v)); v.resize(unique(all(v)) - v.begin());}
|
33 |
+
using namespace std;
|
34 |
+
|
35 |
+
int main(){
|
36 |
+
freopen("data.in","r",stdin);
|
37 |
+
int a, b, c;
|
38 |
+
while(scanf("%d %d %d", &a, &b, &c)==3 &&(a||b||c)){
|
39 |
+
int rate = b - a;
|
40 |
+
if(b + rate == c){
|
41 |
+
printf("AP %d\n", c+rate);
|
42 |
+
}else{
|
43 |
+
rate = b/a;
|
44 |
+
printf("GP %d\n",c*rate);
|
45 |
+
}
|
46 |
+
}
|
47 |
+
return 0;
|
48 |
+
}
|
spoj_cpp_clean/ACPC10A/ACPC10A-16.cpp
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <iostream>
|
2 |
+
#include<stdio.h>
|
3 |
+
#include<math.h>
|
4 |
+
#define ld long double
|
5 |
+
#define m 100000007
|
6 |
+
using namespace std;
|
7 |
+
void check(ld a1,ld a2,ld a3){
|
8 |
+
if(2*a2==(a1+a3)&&(a1!=0)&&(a2!=0)&&(a3!=0))
|
9 |
+
cout<<"AP"<<" "<<a3+(a3-a2)<<endl;
|
10 |
+
else if(a2==sqrt(a1*a3)&&(a1!=0)&&(a2!=0)&&(a3!=0))
|
11 |
+
cout<<"GP"<<" "<<a3*(a3/a2)<<endl;
|
12 |
+
}
|
13 |
+
|
14 |
+
int main() {
|
15 |
+
ld a1,a2,a3;
|
16 |
+
cin>>a1>>a2>>a3;
|
17 |
+
while(a1!=0 || a2!=0 || a3!=0){
|
18 |
+
check(a1,a2,a3);
|
19 |
+
cin>>a1>>a2>>a3;
|
20 |
+
}
|
21 |
+
|
22 |
+
return 0;
|
spoj_cpp_clean/ACPC10A/ACPC10A-19.cpp
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
#define ll long long
|
3 |
+
#define endl "\n"
|
4 |
+
#define MAX 100001
|
5 |
+
#define mod 1000000007
|
6 |
+
using namespace std;
|
7 |
+
|
8 |
+
int main()
|
9 |
+
{
|
10 |
+
ios_base::sync_with_stdio(0);
|
11 |
+
cin.tie(0);
|
12 |
+
cout.tie(0);
|
13 |
+
while(true){
|
14 |
+
int x,y,z;
|
15 |
+
cin>>x>>y>>z;
|
16 |
+
if(x==0 && y==0 && z==0){
|
17 |
+
break;
|
18 |
+
}
|
19 |
+
else{
|
20 |
+
cout<<endl;
|
21 |
+
}
|
22 |
+
|
23 |
+
int d, r;
|
24 |
+
ll next;
|
25 |
+
|
26 |
+
if((y-x)==(z-y)){
|
27 |
+
// case of AP
|
28 |
+
d = y-x;
|
29 |
+
next = z + d;
|
30 |
+
cout<<"AP "<<next;
|
31 |
+
continue;
|
32 |
+
}
|
33 |
+
else{
|
34 |
+
// case of GP
|
35 |
+
if(x>0 && y>0){
|
36 |
+
if(y<x)//decreasing
|
37 |
+
{
|
38 |
+
r = (x/y);
|
39 |
+
next = (z/r);
|
40 |
+
}
|
41 |
+
else // increasing
|
42 |
+
{
|
43 |
+
r = (y/x);
|
44 |
+
next = z*r;
|
45 |
+
}
|
46 |
+
}
|
47 |
+
else if(x<0 && y<0){ // negative numbers
|
48 |
+
if(y<x){ //increasing in negative numbers
|
49 |
+
r = (y/x);
|
50 |
+
next = z*r;
|
51 |
+
}
|
52 |
+
else{ // decreasing in negative numbers
|
53 |
+
r = (x/y);
|
54 |
+
next = (z/r);
|
55 |
+
}
|
56 |
+
}
|
57 |
+
else{
|
58 |
+
if(x<0){
|
59 |
+
if(x<z){
|
60 |
+
r = x/y;
|
61 |
+
next = z/r;
|
62 |
+
}
|
63 |
+
else{
|
64 |
+
r = y/x;
|
65 |
+
next = z*r;
|
66 |
+
}
|
67 |
+
}
|
68 |
+
else{
|
69 |
+
if(x<z){
|
70 |
+
r = y/x;
|
71 |
+
next = z*r;
|
72 |
+
}
|
73 |
+
else{
|
74 |
+
r = y/z;
|
75 |
+
next = z/r;
|
76 |
+
}
|
77 |
+
}
|
78 |
+
}
|
79 |
+
cout<<"GP "<<next;
|
80 |
+
}
|
81 |
+
}
|
82 |
+
return 0;
|
83 |
+
}
|
spoj_cpp_clean/ACPC10A/ACPC10A-5.cpp
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <iostream>
|
2 |
+
|
3 |
+
using namespace std;
|
4 |
+
|
5 |
+
int main()
|
6 |
+
{
|
7 |
+
int a,b,c,d;
|
8 |
+
cin>>a>>b>>c;
|
9 |
+
while(a!=0||b!=0||c!=0)
|
10 |
+
{
|
11 |
+
d=b-a;
|
12 |
+
if(d==(c-b))
|
13 |
+
{
|
14 |
+
cout<<"AP "<<(c+d)<<endl;
|
15 |
+
}
|
16 |
+
else
|
17 |
+
{
|
18 |
+
d=b/a;
|
19 |
+
cout<<"GP "<<(c*d)<<endl;
|
20 |
+
}
|
21 |
+
cin>>a>>b>>c;
|
22 |
+
}
|
23 |
+
return 0;
|
24 |
+
}
|
spoj_cpp_clean/ACPC10A/ACPC10A-9.cpp
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include<stdio.h>
|
13 |
+
|
14 |
+
int main()
|
15 |
+
{
|
16 |
+
int l,m,n;
|
17 |
+
|
18 |
+
scanf("%d",&l);
|
19 |
+
scanf("%d",&m);
|
20 |
+
scanf("%d",&n);
|
21 |
+
|
22 |
+
while(1)
|
23 |
+
{
|
24 |
+
if(m==0&&n==0&&l==0)
|
25 |
+
break;
|
26 |
+
|
27 |
+
else if((2*m)==(n+l))
|
28 |
+
printf("AP %d\n",(n+(m-l)));
|
29 |
+
|
30 |
+
else
|
31 |
+
printf("GP %d\n",(n*(m/l)));
|
32 |
+
|
33 |
+
scanf("%d",&l);
|
34 |
+
scanf("%d",&m);
|
35 |
+
scanf("%d",&n);
|
36 |
+
}
|
37 |
+
return 0;
|
38 |
+
}
|
spoj_cpp_clean/ACPC10D/ACPC10D-9.cpp
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include<stdio.h>
|
13 |
+
#include<limits.h>
|
14 |
+
#define MAX 1000005
|
15 |
+
typedef long long int LL;
|
16 |
+
LL dp[MAX][4];
|
17 |
+
LL arr[MAX][4];
|
18 |
+
LL N,M;
|
19 |
+
FILE* out;
|
20 |
+
bool inbounds(int i,int j)
|
21 |
+
{
|
22 |
+
if(i>=0 && i<=N && j>=0 && j<=M)
|
23 |
+
return true;
|
24 |
+
return false;
|
25 |
+
}
|
26 |
+
LL min(LL a,LL b)
|
27 |
+
{
|
28 |
+
if(a>b)
|
29 |
+
return b;
|
30 |
+
return a;
|
31 |
+
}
|
32 |
+
void solve(int index)
|
33 |
+
{
|
34 |
+
for(int i=0;i<N;i++){
|
35 |
+
for(int j=0;j<3;j++){
|
36 |
+
dp[i][j]=INT_MAX;
|
37 |
+
}
|
38 |
+
}
|
39 |
+
for(int i=0;i<3;i++) dp[0][i]=arr[0][i];
|
40 |
+
|
41 |
+
dp[1][0]=arr[0][1]+arr[1][0];
|
42 |
+
dp[1][1]=min((arr[0][1]+arr[1][1]),(arr[0][1]+arr[0][2]+arr[1][1]));
|
43 |
+
dp[1][2]=min((arr[0][1]+arr[1][2]),(arr[0][1]+arr[0][2]+arr[1][2]));
|
44 |
+
|
45 |
+
for(int i=1;i<N;i++){
|
46 |
+
for(int j=0;j<3;j++){
|
47 |
+
if(j==0){
|
48 |
+
dp[i][j+1]=min(dp[i][j+1],dp[i][j]+arr[i][j+1]);
|
49 |
+
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+arr[i+1][j+1]);
|
50 |
+
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+arr[i+1][j]);
|
51 |
+
}
|
52 |
+
else if(j==1){
|
53 |
+
dp[i+1][j-1]=min(dp[i+1][j-1],dp[i][j]+arr[i+1][j-1]);
|
54 |
+
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+arr[i+1][j]);
|
55 |
+
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+arr[i+1][j+1]);
|
56 |
+
dp[i][j+1]=min(dp[i][j+1],dp[i][j]+arr[i][j+1]);
|
57 |
+
}
|
58 |
+
else{
|
59 |
+
dp[i+1][j-1]=min(dp[i+1][j-1],dp[i][j]+arr[i+1][j-1]);
|
60 |
+
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+arr[i+1][j]);
|
61 |
+
}
|
62 |
+
}
|
63 |
+
}
|
64 |
+
/*for(int i=0;i<N;i++){
|
65 |
+
for(int j=0;j<3;j++){
|
66 |
+
printf("%llu ",dp[i][j]);
|
67 |
+
}
|
68 |
+
printf("\n");
|
69 |
+
}*/
|
70 |
+
printf("%d. %lld\n",index,dp[N-1][1]);
|
71 |
+
}
|
72 |
+
int main()
|
73 |
+
{
|
74 |
+
//FILE *fp=fopen("tri.in","r");
|
75 |
+
FILE *fp=stdin;
|
76 |
+
int index=1;
|
77 |
+
// out=fopen("out.txt","w");
|
78 |
+
fscanf(fp,"%lld",&N);
|
79 |
+
while(N!=0){
|
80 |
+
for(int i=0;i<N;i++){
|
81 |
+
for(int j=0;j<3;j++)
|
82 |
+
fscanf(fp,"%lld",&arr[i][j]);
|
83 |
+
}
|
84 |
+
solve(index);
|
85 |
+
index++;
|
86 |
+
fscanf(fp,"%lld",&N);
|
87 |
+
}
|
88 |
+
return 0;
|
89 |
+
}
|
spoj_cpp_clean/ACQUIRE/ACQUIRE-14.cpp
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// #pragma GCC optimize("Ofast,unroll-loops")
|
2 |
+
// #pragma GCC target("avx,avx2,fma")
|
3 |
+
|
4 |
+
#include <bits/stdc++.h>
|
5 |
+
using namespace std;
|
6 |
+
|
7 |
+
#define ll long long
|
8 |
+
#define ull unsigned long long
|
9 |
+
#define dd double
|
10 |
+
#define ld long double
|
11 |
+
#define sl(n) scanf("%lld", &n)
|
12 |
+
#define si(n) scanf("%d", &n)
|
13 |
+
#define sd(n) scanf("%lf", &n)
|
14 |
+
#define pll pair <ll, ll>
|
15 |
+
#define pii pair <int, int>
|
16 |
+
#define mp make_pair
|
17 |
+
#define pb push_back
|
18 |
+
#define all(v) v.begin(), v.end()
|
19 |
+
#define inf (1LL << 62)
|
20 |
+
#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc)
|
21 |
+
#define for1(i, stop) for(ll i = 1; i <= stop; ++i)
|
22 |
+
#define for0(i, stop) for(ll i = 0; i < stop; ++i)
|
23 |
+
#define rep1(i, start) for(ll i = start; i >= 1; --i)
|
24 |
+
#define rep0(i, start) for(ll i = (start-1); i >= 0; --i)
|
25 |
+
#define ms(n, i) memset(n, i, sizeof(n))
|
26 |
+
#define casep(n) printf("Case %lld:", ++n)
|
27 |
+
#define pn printf("\n")
|
28 |
+
#define pf printf
|
29 |
+
#define EL '\n'
|
30 |
+
#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
|
31 |
+
|
32 |
+
const ll sz = 5e4 + 10;
|
33 |
+
|
34 |
+
pii ara[sz], lst[sz];
|
35 |
+
ll pnt[sz], dp[sz];
|
36 |
+
|
37 |
+
struct Line {
|
38 |
+
ll m, c;
|
39 |
+
} tree[4*sz];
|
40 |
+
|
41 |
+
bool exist[4*sz];
|
42 |
+
|
43 |
+
inline ll f(Line &line, ll x) {
|
44 |
+
return line.m*x + line.c;
|
45 |
+
}
|
46 |
+
|
47 |
+
void add(ll lo, ll hi, Line line, ll node)
|
48 |
+
{
|
49 |
+
exist[node] = 1;
|
50 |
+
|
51 |
+
if(lo == hi) {
|
52 |
+
if(f(line, pnt[lo]) < f(tree[node], pnt[lo]))
|
53 |
+
tree[node] = line;
|
54 |
+
return;
|
55 |
+
}
|
56 |
+
|
57 |
+
ll mid = lo+hi >> 1;
|
58 |
+
|
59 |
+
bool l = f(line, pnt[lo]) < f(tree[node], pnt[lo]);
|
60 |
+
bool m = f(line, pnt[mid]) < f(tree[node], pnt[mid]);
|
61 |
+
|
62 |
+
if(m) swap(tree[node], line);
|
63 |
+
|
64 |
+
if(l != m) add(lo, mid, line, node<<1);
|
65 |
+
else add(mid +1, hi, line, node<<1|1);
|
66 |
+
}
|
67 |
+
|
68 |
+
ll query(ll lo, ll hi, ll idx, ll node)
|
69 |
+
{
|
70 |
+
if(lo == hi)
|
71 |
+
return f(tree[node], pnt[idx]);
|
72 |
+
|
73 |
+
ll mid = lo+hi >> 1, ret = f(tree[node], pnt[idx]);
|
74 |
+
|
75 |
+
if(idx <= mid && exist[node<<1]) ret = min(ret, query(lo, mid, idx, node<<1));
|
76 |
+
else if(idx > mid && exist[node<<1|1]) ret = min(ret, query(mid+1, hi, idx, node<<1|1));
|
77 |
+
|
78 |
+
return ret;
|
79 |
+
}
|
80 |
+
|
81 |
+
int main()
|
82 |
+
{
|
83 |
+
ll n;
|
84 |
+
cin >> n;
|
85 |
+
|
86 |
+
for1(i, n) {
|
87 |
+
si(ara[i].first), si(ara[i].second);
|
88 |
+
}
|
89 |
+
sort(ara+1, ara+n+1);
|
90 |
+
|
91 |
+
ll mxw = 0, idx = 0;
|
92 |
+
rep1(i, n) {
|
93 |
+
if(ara[i].second > mxw)
|
94 |
+
lst[++idx] = ara[i];
|
95 |
+
|
96 |
+
mxw = max(mxw, (ll)ara[i].second);
|
97 |
+
}
|
98 |
+
reverse(lst+1, lst+idx+1);
|
99 |
+
|
100 |
+
for1(i, idx) pnt[i] = lst[i].first;
|
101 |
+
for0(i, 4*sz) tree[i] = {0, inf};
|
102 |
+
|
103 |
+
for1(i, idx) {
|
104 |
+
|
105 |
+
add(1, idx, {lst[i].second, dp[i-1]}, 1);
|
106 |
+
|
107 |
+
dp[i] = query(1, idx, i, 1);
|
108 |
+
}
|
109 |
+
|
110 |
+
cout << dp[idx] << EL;
|
111 |
+
|
112 |
+
return 0;
|
spoj_cpp_clean/ACTIV/ACTIV-14.cpp
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
using namespace std;
|
3 |
+
|
4 |
+
#define ll long long
|
5 |
+
#define mod 100000000
|
6 |
+
|
7 |
+
ll dp[100009];
|
8 |
+
|
9 |
+
struct node {
|
10 |
+
ll l, r;
|
11 |
+
} ara[100009];
|
12 |
+
|
13 |
+
bool operator <(node a, node b)
|
14 |
+
|
15 |
+
{
|
16 |
+
if(a.l != b.l)
|
17 |
+
return a.l < b.l;
|
18 |
+
return a.r < b.r;
|
19 |
+
}
|
20 |
+
|
21 |
+
ll n;
|
22 |
+
|
23 |
+
ll solve(ll i)
|
24 |
+
|
25 |
+
{
|
26 |
+
if(i > n)
|
27 |
+
return 0;
|
28 |
+
|
29 |
+
if(dp[i] != -1)
|
30 |
+
return dp[i];
|
31 |
+
|
32 |
+
ll ret = solve(i + 1) % mod;
|
33 |
+
node tmp = ara[i];
|
34 |
+
ll pos = n + 1, lo = i + 1, hi = n;
|
35 |
+
|
36 |
+
while(lo <= hi) {
|
37 |
+
ll mid = (lo + hi) / 2;
|
38 |
+
|
39 |
+
if(ara[mid].l < ara[i].r)
|
40 |
+
lo = mid + 1;
|
41 |
+
else {
|
42 |
+
pos = mid;
|
43 |
+
hi = mid - 1;
|
44 |
+
}
|
45 |
+
}
|
46 |
+
|
47 |
+
//cout << ara[i].r << " " << ara[pos].r << endl;
|
48 |
+
ret = (ret + 1 + solve(pos)) % mod;
|
49 |
+
|
50 |
+
return dp[i] = ret;
|
51 |
+
}
|
52 |
+
|
53 |
+
int main()
|
54 |
+
|
55 |
+
{
|
56 |
+
while(scanf("%lld", &n) && n != -1) {
|
57 |
+
|
58 |
+
memset(dp, -1, sizeof(dp));
|
59 |
+
|
60 |
+
for(ll i = 1; i <= n; i++)
|
61 |
+
scanf("%lld %lld", &ara[i].l, &ara[i].r);
|
62 |
+
|
63 |
+
sort(ara + 1, ara + n + 1);
|
64 |
+
|
65 |
+
ll ans = solve(1);
|
66 |
+
|
67 |
+
ll num = ans, cnt = 0;
|
68 |
+
if(num == 0)
|
69 |
+
cnt++;
|
70 |
+
while(num != 0) {
|
71 |
+
num /= 10;
|
72 |
+
cnt++;
|
73 |
+
}
|
74 |
+
|
75 |
+
ll rem = 8 - cnt;
|
76 |
+
for(ll i = 1; i <= rem; i++)
|
77 |
+
printf("0");
|
78 |
+
|
79 |
+
printf("%lld\n", ans);
|
80 |
+
}
|
81 |
+
|
82 |
+
return 0;
|
83 |
+
}
|
spoj_cpp_clean/ADABRANC/ADABRANC-14.cpp
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
using namespace std;
|
3 |
+
|
4 |
+
#define ll long long
|
5 |
+
#define pb push_back
|
6 |
+
|
7 |
+
struct node {
|
8 |
+
ll a, b, c;
|
9 |
+
node(ll _a, ll _b, ll _c) {
|
10 |
+
a = _a;
|
11 |
+
b = _b;
|
12 |
+
c = _c;
|
13 |
+
}
|
14 |
+
};
|
15 |
+
|
16 |
+
bool operator <(node a, node b) {
|
17 |
+
return a.c > b.c;
|
18 |
+
}
|
19 |
+
vector <node> edge;
|
20 |
+
|
21 |
+
struct quer {
|
22 |
+
ll a, w, i;
|
23 |
+
quer(ll _a, ll _w, ll _i) {
|
24 |
+
a = _a;
|
25 |
+
w = _w;
|
26 |
+
i = _i;
|
27 |
+
}
|
28 |
+
};
|
29 |
+
|
30 |
+
bool operator <(quer a, quer b) {
|
31 |
+
return a.w > b.w;
|
32 |
+
}
|
33 |
+
|
34 |
+
vector <quer> qry;
|
35 |
+
|
36 |
+
ll ans[300009], par[200009], sz[200009];
|
37 |
+
|
38 |
+
ll findr(ll u)
|
39 |
+
|
40 |
+
{
|
41 |
+
if(par[u] == u)
|
42 |
+
return u;
|
43 |
+
return par[u] = findr(par[u]);
|
44 |
+
}
|
45 |
+
|
46 |
+
int main()
|
47 |
+
|
48 |
+
{
|
49 |
+
ll n, m, q;
|
50 |
+
cin >> n >> m >> q;
|
51 |
+
|
52 |
+
for(ll i = 0; i < n; i++) {
|
53 |
+
par[i] = i;
|
54 |
+
sz[i] = 1;
|
55 |
+
}
|
56 |
+
|
57 |
+
for(ll i = 1; i <= m; i++) {
|
58 |
+
ll a, b, c;
|
59 |
+
scanf("%lld %lld %lld", &a, &b, &c);
|
60 |
+
edge.push_back( node(a, b, c) );
|
61 |
+
}
|
62 |
+
|
63 |
+
sort(edge.begin(), edge.end());
|
64 |
+
|
65 |
+
for(ll i = 1; i <= q; i++) {
|
66 |
+
ll a, c;
|
67 |
+
scanf("%lld %lld", &a, &c, &i);
|
68 |
+
qry.pb( quer(a, c, i) );
|
69 |
+
}
|
70 |
+
sort(qry.begin(), qry.end());
|
71 |
+
|
72 |
+
ll j = 0;
|
73 |
+
for(ll i = 0; i < q; i++) {
|
74 |
+
|
75 |
+
for(; j < edge.size(); j++) {
|
76 |
+
if(edge[j].c < qry[i].w)
|
77 |
+
break;
|
78 |
+
|
79 |
+
ll u = findr(edge[j].a);
|
80 |
+
ll v = findr(edge[j].b);
|
81 |
+
|
82 |
+
if(u == v)
|
83 |
+
continue;
|
84 |
+
|
85 |
+
par[v] = u;
|
86 |
+
sz[u] += sz[v];
|
87 |
+
}
|
88 |
+
|
89 |
+
ans[ qry[i].i ] = sz[ findr(qry[i].a) ];
|
90 |
+
}
|
91 |
+
|
92 |
+
for(ll i = 1; i <= q; i++)
|
93 |
+
printf("%lld\n", ans[i]);
|
94 |
+
|
95 |
+
return 0;
|
96 |
+
}
|
spoj_cpp_clean/ADACOINS/ADACOINS-14.cpp
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
using namespace std;
|
3 |
+
|
4 |
+
#define ll long long
|
5 |
+
|
6 |
+
bitset <100009> posSum;
|
7 |
+
ll cumsum[100009];
|
8 |
+
|
9 |
+
int main()
|
10 |
+
|
11 |
+
{
|
12 |
+
ll n, q;
|
13 |
+
cin >> n >> q;
|
14 |
+
|
15 |
+
posSum[0] = 1;
|
16 |
+
for(ll i = 1; i <= n; i++) {
|
17 |
+
ll in;
|
18 |
+
scanf("%lld", &in);
|
19 |
+
posSum |= (posSum << in);
|
20 |
+
}
|
21 |
+
|
22 |
+
for(ll i = 1; i <= 100000; i++)
|
23 |
+
cumsum[i] = posSum[i] + cumsum[i - 1];
|
24 |
+
|
25 |
+
for(ll i = 1; i <= q; i++) {
|
26 |
+
ll l, r;
|
27 |
+
scanf("%lld %lld", &l, &r);
|
28 |
+
printf("%lld\n", cumsum[r] - cumsum[l - 1]);
|
29 |
+
}
|
30 |
+
|
31 |
+
return 0;
|
32 |
+
}
|
spoj_cpp_clean/ADACYCLE/ADACYCLE-14.cpp
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
using namespace std;
|
3 |
+
|
4 |
+
#define ll int
|
5 |
+
#define pii pair <int, int>
|
6 |
+
const int inf = 1e9;
|
7 |
+
ll mat[2009][2009];
|
8 |
+
vector <ll> g[2009];
|
9 |
+
vector <pii> newinp[2009];
|
10 |
+
ll dist[2009], ans[2009];
|
11 |
+
|
12 |
+
struct node {
|
13 |
+
ll u, c;
|
14 |
+
};
|
15 |
+
|
16 |
+
bool operator <(node a, node b)
|
17 |
+
|
18 |
+
{
|
19 |
+
return a.c > b.c;
|
20 |
+
}
|
21 |
+
|
22 |
+
int main()
|
23 |
+
|
24 |
+
{
|
25 |
+
ll n;
|
26 |
+
scanf("%d", &n);
|
27 |
+
for(ll i = 1; i <= n; i++) {
|
28 |
+
for(ll j = 1; j <= n; j++) {
|
29 |
+
scanf("%d", &mat[i][j]);
|
30 |
+
if(mat[i][j]) {
|
31 |
+
newinp[i].push_back(pii(i, j));
|
32 |
+
newinp[j].push_back(pii(i, j));
|
33 |
+
g[i].push_back(j);
|
34 |
+
}
|
35 |
+
}
|
36 |
+
}
|
37 |
+
|
38 |
+
for(ll i = 1; i <= n; i++) {
|
39 |
+
if(mat[i][i]) {
|
40 |
+
ans[i] = 1;
|
41 |
+
continue;
|
42 |
+
}
|
43 |
+
|
44 |
+
for(ll j = 0; j <= 2005; j++)
|
45 |
+
dist[j] = inf;
|
46 |
+
|
47 |
+
for(ll j = 0; j < newinp[i].size(); j++) {
|
48 |
+
ll u = newinp[i][j].first, v = newinp[i][j].second;
|
49 |
+
if(u == i)
|
50 |
+
g[2001].push_back(v);
|
51 |
+
else
|
52 |
+
g[u].push_back(2001);
|
53 |
+
}
|
54 |
+
|
55 |
+
priority_queue <node> pq;
|
56 |
+
pq.push({i, 0});
|
57 |
+
dist[i] = 0;
|
58 |
+
while(!pq.empty()) {
|
59 |
+
node u = pq.top();
|
60 |
+
pq.pop();
|
61 |
+
|
62 |
+
if(dist[u.u] != u.c)
|
63 |
+
continue;
|
64 |
+
for(ll v: g[u.u]) {
|
65 |
+
if(1 + u.c < dist[v]) {
|
66 |
+
dist[v] = 1 + u.c;
|
67 |
+
pq.push({v, dist[v]});
|
68 |
+
}
|
69 |
+
}
|
70 |
+
}
|
71 |
+
if(dist[2001] != inf)
|
72 |
+
ans[i] = dist[2001];
|
73 |
+
|
74 |
+
g[2001].clear();
|
75 |
+
for(ll j = 0; j < newinp[i].size(); j++) {
|
76 |
+
ll u = newinp[i][j].first, v = newinp[i][j].second;
|
77 |
+
if(u != i) {
|
78 |
+
ll last = g[u].size() - 1;
|
79 |
+
while(g[u][last] == 2001 && last >= 0) {
|
80 |
+
g[u].pop_back();
|
81 |
+
last--;
|
82 |
+
}
|
83 |
+
}
|
84 |
+
}
|
85 |
+
}
|
86 |
+
|
87 |
+
for(ll i = 1; i <= n; i++) {
|
88 |
+
if(ans[i] > 0)
|
89 |
+
printf("%d\n", ans[i]);
|
90 |
+
else
|
91 |
+
printf("NO WAY\n");
|
92 |
+
}
|
93 |
+
|
94 |
+
return 0;
|
spoj_cpp_clean/ADADUNG/ADADUNG-9.cpp
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include <iostream>
|
13 |
+
#include <cstring>
|
14 |
+
|
15 |
+
#define ll long long
|
16 |
+
#define MAXN 10000007
|
17 |
+
using namespace std;
|
18 |
+
|
19 |
+
const ll MOD = 1000000007;
|
20 |
+
ll memo[MAXN];
|
21 |
+
|
22 |
+
ll derangement(ll num)
|
23 |
+
{
|
24 |
+
if (num <= 1)
|
25 |
+
return 0;
|
26 |
+
|
27 |
+
if (num == 2)
|
28 |
+
return 1LL;
|
29 |
+
|
30 |
+
if (memo[num])
|
31 |
+
return memo[num];
|
32 |
+
|
33 |
+
ll temp = derangement(num - 1) + derangement(num - 2);
|
34 |
+
|
35 |
+
temp %= MOD;
|
36 |
+
|
37 |
+
memo[num] = (num - 1) * temp;
|
38 |
+
memo[num] %= MOD;
|
39 |
+
|
40 |
+
return memo[num];
|
41 |
+
}
|
42 |
+
|
43 |
+
int main()
|
44 |
+
{
|
45 |
+
// freopen("input.txt", "r", stdin);
|
46 |
+
ios::sync_with_stdio(0);
|
47 |
+
|
48 |
+
int T, N;
|
49 |
+
|
50 |
+
cin >> T;
|
51 |
+
|
52 |
+
while (T--)
|
53 |
+
{
|
54 |
+
cin >> N;
|
55 |
+
cout << derangement(N) << "\n";
|
56 |
+
}
|
57 |
+
|
58 |
+
return 0;
|
59 |
+
}
|
spoj_cpp_clean/ADAFIELD/ADAFIELD-14.cpp
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
using namespace std;
|
3 |
+
|
4 |
+
#define ll long long
|
5 |
+
|
6 |
+
int main()
|
7 |
+
|
8 |
+
{
|
9 |
+
ll t;
|
10 |
+
cin >> t;
|
11 |
+
while(t--) {
|
12 |
+
ll n, m, q, line, state, px, py, nx, ny, mx, mxd, mxx;
|
13 |
+
scanf("%lld %lld %lld", &n, &m, &q);
|
14 |
+
set <ll> x, y;
|
15 |
+
multiset <ll> dif, difx;
|
16 |
+
|
17 |
+
set <ll> :: iterator it1, it2;
|
18 |
+
multiset <ll> :: iterator it3;
|
19 |
+
|
20 |
+
x.insert(0), x.insert(n);
|
21 |
+
y.insert(0), y.insert(m);
|
22 |
+
dif.insert(m);
|
23 |
+
difx.insert(n);
|
24 |
+
|
25 |
+
for(ll i = 1; i <= q; i++) {
|
26 |
+
scanf("%lld %lld", &state, &line);
|
27 |
+
if(state == 0) {
|
28 |
+
if( x.find(line) == x.end()) {
|
29 |
+
|
30 |
+
it3 = x.lower_bound(line);
|
31 |
+
px = *(--it3);
|
32 |
+
|
33 |
+
it3 = x.upper_bound(line);
|
34 |
+
nx = *it3;
|
35 |
+
|
36 |
+
difx.erase( difx.find(nx - px) );
|
37 |
+
difx.insert(line - px);
|
38 |
+
difx.insert(nx - line);
|
39 |
+
x.insert(line);
|
40 |
+
}
|
41 |
+
}
|
42 |
+
else if(y.find(line) == y.end()) {
|
43 |
+
|
44 |
+
it3 = y.lower_bound(line);
|
45 |
+
py = *(--it3);
|
46 |
+
|
47 |
+
it3 = y.upper_bound(line);
|
48 |
+
ny = *it3;
|
49 |
+
|
50 |
+
//cout << ny << " " << py << " " << line << endl;
|
51 |
+
dif.erase( dif.find(ny - py) );
|
52 |
+
dif.insert(line - py);
|
53 |
+
dif.insert(ny - line);
|
54 |
+
y.insert(line);
|
55 |
+
}
|
56 |
+
|
57 |
+
mxd = *(--dif.end());
|
58 |
+
mxx = *(--difx.end());
|
59 |
+
mx = mxd * mxx;
|
60 |
+
|
61 |
+
printf("%lld\n", mx);
|
62 |
+
}
|
63 |
+
}
|
64 |
+
|
65 |
+
return 0;
|
66 |
+
}
|
spoj_cpp_clean/ADAGAME/ADAGAME-14.cpp
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
using namespace std;
|
3 |
+
|
4 |
+
#define ll long long
|
5 |
+
|
6 |
+
ll dp[10009][105][2], n, mv;
|
7 |
+
|
8 |
+
ll solve(ll num, ll mov, ll state)
|
9 |
+
|
10 |
+
{
|
11 |
+
if(mov > mv) {
|
12 |
+
if(num > n) {
|
13 |
+
if(state == 0)
|
14 |
+
return 1;
|
15 |
+
else
|
16 |
+
return 0;
|
17 |
+
}
|
18 |
+
else {
|
19 |
+
if(state == 0)
|
20 |
+
return 0;
|
21 |
+
else
|
22 |
+
return 1;
|
23 |
+
}
|
24 |
+
}
|
25 |
+
|
26 |
+
ll &ret = dp[num][mov][state];
|
27 |
+
if(ret != -1)
|
28 |
+
return ret;
|
29 |
+
|
30 |
+
ll rt = 0;
|
31 |
+
if(num > 999) {
|
32 |
+
if(num % 10 != 9)
|
33 |
+
rt |= !solve(num + 1, mov + 1, state ^ 1);
|
34 |
+
else
|
35 |
+
rt |= !solve(num - 9, mov + 1, state ^ 1);
|
36 |
+
|
37 |
+
if(num % 100 < 90)
|
38 |
+
rt |= !solve(num + 10, mov + 1, state ^ 1);
|
39 |
+
else
|
40 |
+
rt |= !solve(num - 90, mov + 1, state ^ 1);
|
41 |
+
|
42 |
+
if(num % 1000 < 900)
|
43 |
+
rt |= !solve(num + 100, mov + 1, state ^ 1);
|
44 |
+
else
|
45 |
+
rt |= !solve(num - 900, mov + 1, state ^ 1);
|
46 |
+
|
47 |
+
if(num % 10000 < 9000)
|
48 |
+
rt |= !solve(num + 1000, mov + 1, state ^ 1);
|
49 |
+
else
|
50 |
+
rt |= !solve(num - 9000, mov + 1, state ^ 1);
|
51 |
+
}
|
52 |
+
else if(num > 99) {
|
53 |
+
if(num % 10 != 9)
|
54 |
+
rt |= !solve(num + 1, mov + 1, state ^ 1);
|
55 |
+
else
|
56 |
+
rt |= !solve(num - 9, mov + 1, state ^ 1);
|
57 |
+
|
58 |
+
if(num % 100 < 90)
|
59 |
+
rt |= !solve(num + 10, mov + 1, state ^ 1);
|
60 |
+
else
|
61 |
+
rt |= !solve(num - 90, mov + 1, state ^ 1);
|
62 |
+
|
63 |
+
if(num % 1000 < 900)
|
64 |
+
rt |= !solve(num + 100, mov + 1, state ^ 1);
|
65 |
+
else
|
66 |
+
rt |= !solve(num - 900, mov + 1, state ^ 1);
|
67 |
+
|
68 |
+
rt |= !solve(num + 1000, mov + 1, state ^ 1);
|
69 |
+
}
|
70 |
+
|
71 |
+
else if(num > 9) {
|
72 |
+
if(num % 10 != 9)
|
73 |
+
rt |= !solve(num + 1, mov + 1, state ^ 1);
|
74 |
+
else
|
75 |
+
rt |= !solve(num - 9, mov + 1, state ^ 1);
|
76 |
+
|
77 |
+
if(num % 100 < 90)
|
78 |
+
rt |= !solve(num + 10, mov + 1, state ^ 1);
|
79 |
+
else
|
80 |
+
rt |= !solve(num - 90, mov + 1, state ^ 1);
|
81 |
+
|
82 |
+
rt |= !solve(num + 100, mov + 1, state ^ 1);
|
83 |
+
rt |= !solve(num + 1000, mov + 1, state ^ 1);
|
84 |
+
}
|
85 |
+
|
86 |
+
else {
|
87 |
+
if(num % 10 != 9)
|
88 |
+
rt |= !solve(num + 1, mov + 1, state ^ 1);
|
89 |
+
else
|
90 |
+
rt |= !solve(num - 9, mov + 1, state ^ 1);
|
91 |
+
|
92 |
+
rt |= !solve(num + 10, mov + 1, state ^ 1);
|
93 |
+
rt |= !solve(num + 100, mov + 1, state ^ 1);
|
94 |
+
rt |= !solve(num + 1000, mov + 1, state ^ 1);
|
95 |
+
}
|
96 |
+
|
97 |
+
//cout << num << " " << rt << " " << state << endl;
|
98 |
+
return ret = rt;
|
99 |
+
}
|
100 |
+
|
101 |
+
int main()
|
102 |
+
|
103 |
+
{
|
104 |
+
ll t;
|
105 |
+
cin >> t;
|
106 |
+
while(t--) {
|
107 |
+
memset(dp, -1, sizeof(dp));
|
108 |
+
scanf("%lld %lld", &n, &mv);
|
109 |
+
|
110 |
+
if( solve(n, 1, 0) == 1)
|
111 |
+
printf("Ada\n");
|
112 |
+
else
|
113 |
+
printf("Vinit\n");
|
114 |
+
}
|
115 |
+
|
116 |
+
return 0;
|
117 |
+
}
|
spoj_cpp_clean/ADAGAME/ADAGAME-9.cpp
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include <stdio.h>
|
13 |
+
#include <string.h>
|
14 |
+
|
15 |
+
#define gc() getchar()
|
16 |
+
using namespace std;
|
17 |
+
|
18 |
+
short a, b, c, d;
|
19 |
+
int memo[10][10][10][10][107]; //0 = ada wins
|
20 |
+
int N;
|
21 |
+
|
22 |
+
inline void init()
|
23 |
+
{
|
24 |
+
a = gc() - '0';
|
25 |
+
b = gc() - '0';
|
26 |
+
c = gc() - '0';
|
27 |
+
d = gc() - '0';
|
28 |
+
|
29 |
+
scanf("%d\n", &N);
|
30 |
+
|
31 |
+
memset(memo, -1, sizeof(memo));
|
32 |
+
}
|
33 |
+
|
34 |
+
inline int inc(int num)
|
35 |
+
{
|
36 |
+
if (num != 9)
|
37 |
+
return num + 1;
|
38 |
+
|
39 |
+
return 0;
|
40 |
+
}
|
41 |
+
|
42 |
+
bool solve(int aa, int bb, int cc, int dd, int n, bool player) //0 = ada, 1 = vinit
|
43 |
+
{
|
44 |
+
if (memo[aa][bb][cc][dd][n] != -1)
|
45 |
+
return memo[aa][bb][cc][dd][n];
|
46 |
+
|
47 |
+
if (!n)
|
48 |
+
{
|
49 |
+
if (aa * 1000 + bb * 100 + cc * 10 + d > a * 1000 + b * 100 + c * 10 + d)
|
50 |
+
{
|
51 |
+
memo[aa][bb][cc][dd][0] = 0;
|
52 |
+
return 0;
|
53 |
+
}
|
54 |
+
|
55 |
+
else
|
56 |
+
{
|
57 |
+
memo[aa][bb][cc][dd][0] = 1;
|
58 |
+
return 1;
|
59 |
+
}
|
60 |
+
}
|
61 |
+
|
62 |
+
if (!player) // an pezei o ada
|
63 |
+
{
|
64 |
+
if (!solve(inc(aa), bb, cc, dd, n - 1, !player) || !solve(aa, inc(bb), cc, dd, n - 1, !player) || !solve(aa, bb, inc(cc), dd, n - 1, !player) || !solve(aa, bb, cc, inc(dd), n - 1, !player))
|
65 |
+
{
|
66 |
+
memo[aa][bb][cc][dd][n] = 0;
|
67 |
+
return 0;
|
68 |
+
}
|
69 |
+
|
70 |
+
else
|
71 |
+
{
|
72 |
+
memo[aa][bb][cc][dd][n] = 1;
|
73 |
+
return 1;
|
74 |
+
}
|
75 |
+
}
|
76 |
+
|
77 |
+
else //an pezei o vinit
|
78 |
+
{
|
79 |
+
if (solve(inc(aa), bb, cc, dd, n - 1, !player) || solve(aa, inc(bb), cc, dd, n - 1, !player) || solve(aa, bb, inc(cc), dd, n - 1, !player) || solve(aa, bb, cc, inc(dd), n - 1, !player))
|
80 |
+
{
|
81 |
+
memo[aa][bb][cc][dd][n] = 1;
|
82 |
+
return 1;
|
83 |
+
}
|
84 |
+
|
85 |
+
else
|
86 |
+
{
|
87 |
+
memo[aa][bb][cc][dd][n] = 0;
|
88 |
+
return 0;
|
89 |
+
}
|
90 |
+
}
|
91 |
+
}
|
92 |
+
|
93 |
+
int main()
|
94 |
+
{
|
95 |
+
int T;
|
96 |
+
|
97 |
+
// freopen("input.txt", "r", stdin);
|
98 |
+
|
99 |
+
scanf("%d\n", &T);
|
100 |
+
|
101 |
+
while (T--)
|
102 |
+
{
|
103 |
+
init();
|
104 |
+
if (!solve(a, b, c, d, N, 0))
|
105 |
+
printf("Ada\n");
|
106 |
+
else
|
107 |
+
printf("Vinit\n");
|
108 |
+
}
|
109 |
+
|
110 |
+
return 0;
|
111 |
+
}
|
spoj_cpp_clean/ADAGAME2/ADAGAME2-9.cpp
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include <stdio.h>
|
13 |
+
#include <ctype.h>
|
14 |
+
|
15 |
+
#define gc() getchar_unlocked()
|
16 |
+
#define MAXN 100000
|
17 |
+
#define MAXM 30
|
18 |
+
using namespace std;
|
19 |
+
|
20 |
+
int N, M;
|
21 |
+
int DP[MAXN + 7];
|
22 |
+
int steps[MAXM + 7];
|
23 |
+
|
24 |
+
inline int FAST_IO()
|
25 |
+
{
|
26 |
+
char ch;
|
27 |
+
int val = 0;
|
28 |
+
ch = gc();
|
29 |
+
while (isspace(ch) && ch != EOF)
|
30 |
+
ch = gc();
|
31 |
+
val = 0;
|
32 |
+
while (isdigit(ch))
|
33 |
+
{
|
34 |
+
val = (val * 10) + (ch - '0');
|
35 |
+
ch = gc();
|
36 |
+
}
|
37 |
+
return val;
|
38 |
+
}
|
39 |
+
|
40 |
+
inline void init()
|
41 |
+
{
|
42 |
+
int i;
|
43 |
+
|
44 |
+
N = FAST_IO();
|
45 |
+
M = FAST_IO();
|
46 |
+
|
47 |
+
for (i = 0; i < M; i++)
|
48 |
+
steps[i] = FAST_IO();
|
49 |
+
}
|
50 |
+
|
51 |
+
inline int grundy(int pos)
|
52 |
+
{
|
53 |
+
int i;
|
54 |
+
bool a[50] = { 0 };
|
55 |
+
|
56 |
+
for (i = 0; i < M; i++)
|
57 |
+
if (steps[i] <= pos)
|
58 |
+
a[DP[pos - steps[i]]] = 1;
|
59 |
+
|
60 |
+
for (i = 0; i < 50; i++)
|
61 |
+
if (!a[i])
|
62 |
+
return i;
|
63 |
+
|
64 |
+
return 0;
|
65 |
+
}
|
66 |
+
|
67 |
+
inline void calc_dp()
|
68 |
+
{
|
69 |
+
int i;
|
70 |
+
|
71 |
+
for (i = 1; i <= MAXN; i++)
|
72 |
+
DP[i] = grundy(i);
|
73 |
+
}
|
74 |
+
|
75 |
+
inline bool solve()
|
76 |
+
{
|
77 |
+
int num, rval = 0;
|
78 |
+
|
79 |
+
while (N--)
|
80 |
+
{
|
81 |
+
num = FAST_IO();
|
82 |
+
rval ^= DP[num];
|
83 |
+
}
|
84 |
+
|
85 |
+
return rval;
|
86 |
+
}
|
87 |
+
|
88 |
+
int main()
|
89 |
+
{
|
90 |
+
int T;
|
91 |
+
|
92 |
+
// freopen("input.txt", "r", stdin);
|
93 |
+
|
94 |
+
T = FAST_IO();
|
95 |
+
|
96 |
+
while (T--)
|
97 |
+
{
|
98 |
+
init();
|
99 |
+
calc_dp();
|
100 |
+
if (!solve())
|
101 |
+
printf("Vinit\n");
|
102 |
+
else
|
103 |
+
printf("Ada\n");
|
104 |
+
}
|
105 |
+
|
106 |
+
return 0;
|
107 |
+
}
|
spoj_cpp_clean/ADAINDEX/ADAINDEX-9.cpp
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include <stdio.h>
|
13 |
+
#include <stdlib.h>
|
14 |
+
#include <string>
|
15 |
+
#include <ctype.h>
|
16 |
+
|
17 |
+
#define gc() getchar()
|
18 |
+
using namespace std;
|
19 |
+
|
20 |
+
struct ver
|
21 |
+
{
|
22 |
+
int words;
|
23 |
+
int prefixes;
|
24 |
+
struct ver **edges;
|
25 |
+
};
|
26 |
+
|
27 |
+
void initialize(ver **vertex)
|
28 |
+
{
|
29 |
+
int i;
|
30 |
+
|
31 |
+
(*vertex) = (struct ver*)malloc(sizeof(struct ver));
|
32 |
+
(*vertex)->words = 0;
|
33 |
+
(*vertex)->prefixes = 0;
|
34 |
+
(*vertex)->edges = (struct ver**)malloc(sizeof(ver) * 26);
|
35 |
+
|
36 |
+
for (i = 0; i<26; i++)
|
37 |
+
(*vertex)->edges[i] = NULL;
|
38 |
+
}
|
39 |
+
|
40 |
+
void addword(ver **vertex, string &word, int pos = 0)
|
41 |
+
{
|
42 |
+
if (pos == word.size())
|
43 |
+
{
|
44 |
+
(*vertex)->words++;
|
45 |
+
(*vertex)->prefixes++;
|
46 |
+
return;
|
47 |
+
}
|
48 |
+
|
49 |
+
char k = word[pos];
|
50 |
+
|
51 |
+
(*vertex)->prefixes++;
|
52 |
+
if ((*vertex)->edges[k - 'a'] == NULL)
|
53 |
+
initialize(&(*vertex)->edges[k - 'a']);
|
54 |
+
|
55 |
+
addword(&(*vertex)->edges[k - 'a'], word, pos + 1);
|
56 |
+
}
|
57 |
+
|
58 |
+
int countwords(ver *vertex, string &word, int pos = 0)
|
59 |
+
{
|
60 |
+
if (pos == word.size())
|
61 |
+
return vertex->words;
|
62 |
+
|
63 |
+
char k = word[pos];
|
64 |
+
|
65 |
+
if (vertex->edges[k - 'a'] == NULL)
|
66 |
+
return 0;
|
67 |
+
|
68 |
+
return countwords(vertex->edges[k - 'a'], word, pos + 1);
|
69 |
+
}
|
70 |
+
|
71 |
+
int countprefixes(ver *vertex, string &word, int pos = 0)
|
72 |
+
{
|
73 |
+
if (pos == word.size())
|
74 |
+
return vertex->prefixes;
|
75 |
+
|
76 |
+
char k = word[pos];
|
77 |
+
|
78 |
+
if (vertex->edges[k - 'a'] == NULL)
|
79 |
+
return 0;
|
80 |
+
|
81 |
+
return countprefixes(vertex->edges[k - 'a'], word, pos + 1);
|
82 |
+
}
|
83 |
+
|
84 |
+
inline void next_string(string &A)
|
85 |
+
{
|
86 |
+
A.clear();
|
87 |
+
|
88 |
+
char c = gc();
|
89 |
+
|
90 |
+
while (isspace(c))
|
91 |
+
c = gc();
|
92 |
+
|
93 |
+
while (!isspace(c) && c != EOF)
|
94 |
+
{
|
95 |
+
A.push_back(c);
|
96 |
+
c = gc();
|
97 |
+
}
|
98 |
+
}
|
99 |
+
|
100 |
+
struct ver *trie;
|
101 |
+
int N, Q;
|
102 |
+
|
103 |
+
inline void init()
|
104 |
+
{
|
105 |
+
string S;
|
106 |
+
|
107 |
+
scanf("%d %d", &N, &Q);
|
108 |
+
|
109 |
+
initialize(&trie);
|
110 |
+
|
111 |
+
while (N--)
|
112 |
+
{
|
113 |
+
next_string(S);
|
114 |
+
addword(&trie, S);
|
115 |
+
}
|
116 |
+
}
|
117 |
+
|
118 |
+
inline int query()
|
119 |
+
{
|
120 |
+
string S;
|
121 |
+
|
122 |
+
next_string(S);
|
123 |
+
return countprefixes(trie, S);
|
124 |
+
}
|
125 |
+
|
126 |
+
int main()
|
127 |
+
{
|
128 |
+
// freopen("input.txt", "r", stdin);
|
129 |
+
|
130 |
+
init();
|
131 |
+
|
132 |
+
while (Q--)
|
133 |
+
printf("%d\n", query());
|
134 |
+
|
135 |
+
return 0;
|
136 |
+
}
|
spoj_cpp_clean/ADAMATCH/ADAMATCH-14.cpp
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// #pragma GCC optimize("Ofast,unroll-loops")
|
2 |
+
// #pragma GCC target("avx,avx2,fma")
|
3 |
+
|
4 |
+
#include <bits/stdc++.h>
|
5 |
+
using namespace std;
|
6 |
+
|
7 |
+
#define ll long long
|
8 |
+
#define ull unsigned long long
|
9 |
+
#define dd double
|
10 |
+
#define ld long double
|
11 |
+
#define sl(n) scanf("%lld", &n)
|
12 |
+
#define si(n) scanf("%d", &n)
|
13 |
+
#define sd(n) scanf("%lf", &n)
|
14 |
+
#define pll pair <ll, ll>
|
15 |
+
#define pii pair <int, int>
|
16 |
+
#define mp make_pair
|
17 |
+
#define pb push_back
|
18 |
+
#define all(v) v.begin(), v.end()
|
19 |
+
#define inf (1LL << 62)
|
20 |
+
#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc)
|
21 |
+
#define for1(i, stop) for(ll i = 1; i <= stop; ++i)
|
22 |
+
#define for0(i, stop) for(ll i = 0; i < stop; ++i)
|
23 |
+
#define rep1(i, start) for(ll i = start; i >= 1; --i)
|
24 |
+
#define rep0(i, start) for(ll i = (start-1); i >= 0; --i)
|
25 |
+
#define ms(n, i) memset(n, i, sizeof(n))
|
26 |
+
#define casep(n) printf("Case %lld:", ++n)
|
27 |
+
#define pn printf("\n")
|
28 |
+
#define pf printf
|
29 |
+
#define EL '\n'
|
30 |
+
#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
|
31 |
+
|
32 |
+
/***
|
33 |
+
* Multiply (7x^2 + 8x^1 + 9x^0) with (6x^1 + 5x^0)
|
34 |
+
* ans = 42x^3 + 83x^2 + 94x^1 + 45x^0
|
35 |
+
* A = {9, 8, 7}
|
36 |
+
* B = {5, 6}
|
37 |
+
* V = multiply(A,B)
|
38 |
+
* V = {45, 94, 83, 42}
|
39 |
+
***/
|
40 |
+
|
41 |
+
/*** Tricks
|
42 |
+
* Use vector < bool > if you need to check only the status of the sum
|
43 |
+
* Use bigmod if the power is over same polynomial && power is big
|
44 |
+
* Use long double if you need more precision
|
45 |
+
* Use long long for overflow
|
46 |
+
***/
|
47 |
+
typedef vector <int> vi;
|
48 |
+
const double PI = 2.0 * acos(0.0);
|
49 |
+
using cd = complex<double>;
|
50 |
+
|
51 |
+
void fft(vector<cd> & a, bool invert = 0)
|
52 |
+
|
53 |
+
{
|
54 |
+
int n = a.size();
|
55 |
+
|
56 |
+
for (int i = 1, j = 0; i < n; i++) {
|
57 |
+
int bit = n >> 1;
|
58 |
+
for (; j & bit; bit >>= 1)
|
59 |
+
j ^= bit;
|
60 |
+
j ^= bit;
|
61 |
+
|
62 |
+
if (i < j)
|
63 |
+
swap(a[i], a[j]);
|
64 |
+
}
|
65 |
+
|
66 |
+
for (int len = 2; len <= n; len <<= 1) {
|
67 |
+
double ang = 2 * PI / len * (invert ? -1 : 1);
|
68 |
+
cd wlen(cos(ang), sin(ang));
|
69 |
+
for (int i = 0; i < n; i += len) {
|
70 |
+
cd w(1);
|
71 |
+
for (int j = 0; j < len / 2; j++) {
|
72 |
+
cd u = a[i+j], v = a[i+j+len/2] * w;
|
73 |
+
a[i+j] = u + v;
|
74 |
+
a[i+j+len/2] = u - v;
|
75 |
+
w *= wlen;
|
76 |
+
}
|
77 |
+
}
|
78 |
+
}
|
79 |
+
|
80 |
+
if (invert) {
|
81 |
+
for (cd & x : a)
|
82 |
+
x /= n;
|
83 |
+
}
|
84 |
+
}
|
85 |
+
|
86 |
+
void ifft(vector <cd> & p)
|
87 |
+
|
88 |
+
{
|
89 |
+
fft(p, 1);
|
90 |
+
}
|
91 |
+
|
92 |
+
vi multiply(vi const& a, vi const& b)
|
93 |
+
{
|
94 |
+
|
95 |
+
vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
|
96 |
+
int n = 1;
|
97 |
+
while (n < a.size() + b.size())
|
98 |
+
n <<= 1;
|
99 |
+
fa.resize(n);
|
100 |
+
fb.resize(n);
|
101 |
+
|
102 |
+
fft(fa);
|
103 |
+
fft(fb);
|
104 |
+
for (int i = 0; i < n; i++)
|
105 |
+
fa[i] *= fb[i];
|
106 |
+
ifft(fa);
|
107 |
+
|
108 |
+
vi result(n);
|
109 |
+
for (int i = 0; i < n; i++)
|
110 |
+
result[i] = round(fa[i].real());
|
111 |
+
|
112 |
+
return result;
|
113 |
+
}
|
114 |
+
|
115 |
+
const ll sz = 5e5 + 10, offset = 5e5;
|
116 |
+
char s[sz], r[sz];
|
117 |
+
|
118 |
+
vi num1[4], num2[4], res[4];
|
119 |
+
|
120 |
+
int main()
|
121 |
+
{
|
122 |
+
scanf("%s %s", s, r);
|
123 |
+
|
124 |
+
ll len1 = strlen(s), len2 = strlen(r);
|
125 |
+
|
126 |
+
for0(i, 4)
|
127 |
+
num1[i].resize(len1+1,0), num2[i].resize(len2+1+offset,0);
|
128 |
+
|
129 |
+
for(ll i = 0; s[i] != '\0'; i++) {
|
130 |
+
if(s[i] == 'A')
|
131 |
+
num1[0][i]++;
|
132 |
+
else if(s[i] == 'C')
|
133 |
+
num1[1][i]++;
|
134 |
+
else if(s[i] == 'T')
|
135 |
+
num1[2][i]++;
|
136 |
+
else
|
137 |
+
num1[3][i]++;
|
138 |
+
}
|
139 |
+
|
140 |
+
for(ll i = 0; r[i] != '\0'; i++) {
|
141 |
+
|
142 |
+
if(r[i] == 'A')
|
143 |
+
num2[0][-i + offset]++;
|
144 |
+
else if(r[i] == 'C')
|
145 |
+
num2[1][-i + offset]++;
|
146 |
+
else if(r[i] == 'T')
|
147 |
+
num2[2][-i + offset]++;
|
148 |
+
else
|
149 |
+
num2[3][-i + offset]++;
|
150 |
+
}
|
151 |
+
|
152 |
+
for0(i, 4) res[i] = multiply(num1[i], num2[i]);
|
153 |
+
|
154 |
+
ll ans = inf;
|
155 |
+
for(ll i = 0; i <= len1 - len2; i++) {
|
156 |
+
ll match = 0;
|
157 |
+
for0(j, 4) match += res[j][i+offset];
|
158 |
+
|
159 |
+
ans = min(ans, len2-match);
|
160 |
+
}
|
161 |
+
|
162 |
+
cout << ans << EL;
|
163 |
+
|
164 |
+
return 0;
|
165 |
+
}
|
spoj_cpp_clean/ADAMOLD/ADAMOLD-14.cpp
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
using namespace std;
|
3 |
+
|
4 |
+
#define ll long long
|
5 |
+
const ll inf = 1e18;
|
6 |
+
|
7 |
+
ll n, k, ara[5009];
|
8 |
+
|
9 |
+
ll xorcs[5009][5009], xorsum[5009][5009], dp[5009][5009];
|
10 |
+
|
11 |
+
void solve(ll lo, ll hi, ll left, ll right, ll part)
|
12 |
+
|
13 |
+
{
|
14 |
+
if(lo > hi) return;
|
15 |
+
ll mid = (lo + hi) / 2;
|
16 |
+
|
17 |
+
dp[mid][part] = inf;
|
18 |
+
ll indx;
|
19 |
+
for(ll i = left; i <= right && i <= mid; i++) {
|
20 |
+
|
21 |
+
ll c = dp[i - 1][part - 1] + xorsum[i][mid];
|
22 |
+
if(c < dp[mid][part]) {
|
23 |
+
dp[mid][part] = c;
|
24 |
+
indx = i;
|
25 |
+
}
|
26 |
+
}
|
27 |
+
|
28 |
+
solve(lo, mid - 1, left, indx, part);
|
29 |
+
solve(mid + 1, hi, indx, right, part);
|
30 |
+
}
|
31 |
+
|
32 |
+
int main()
|
33 |
+
|
34 |
+
{
|
35 |
+
scanf("%lld %lld", &n, &k);
|
36 |
+
k++;
|
37 |
+
for(ll i = 1; i <= n; i++) scanf("%lld", &ara[i]);
|
38 |
+
|
39 |
+
for(ll i = 1; i <= n; i++) {
|
40 |
+
for(ll j = i + 1; j <= n; j++)
|
41 |
+
xorcs[i][j] = (ara[i] ^ ara[j]) + xorcs[i][j - 1];
|
42 |
+
}
|
43 |
+
|
44 |
+
for(ll i = 1; i <= n; i++) {
|
45 |
+
ll sum = 0;
|
46 |
+
for(ll j = i - 1; j >= 1; j--) {
|
47 |
+
sum += xorcs[j][i];
|
48 |
+
xorsum[j][i] = sum;
|
49 |
+
}
|
50 |
+
}
|
51 |
+
|
52 |
+
for(ll i = 0; i <= 5002; i++) {
|
53 |
+
dp[i][0] = inf;
|
54 |
+
}
|
55 |
+
dp[0][0] = 0;
|
56 |
+
|
57 |
+
for(ll i = 1; i <= k; i++)
|
58 |
+
solve(1, n, 1, n, i);
|
59 |
+
|
60 |
+
cout << dp[n][k] << endl;
|
61 |
+
|
62 |
+
return 0;
|
spoj_cpp_clean/ADAORANG/ADAORANG-14.cpp
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <bits/stdc++.h>
|
2 |
+
using namespace std;
|
3 |
+
|
4 |
+
#define ll int
|
5 |
+
#define pb push_back
|
6 |
+
ll col[450009], fst[450009], start[450009], nodeOf[450009], tme[1000009], lca[3000009];
|
7 |
+
vector <ll> graph[450009];
|
8 |
+
ll TM = 1, indx = 0;
|
9 |
+
|
10 |
+
bitset <255> cl[450009];
|
11 |
+
|
12 |
+
void dfs(ll u, ll p)
|
13 |
+
|
14 |
+
{
|
15 |
+
start[u] = TM;
|
16 |
+
nodeOf[TM] = u;
|
17 |
+
TM++;
|
18 |
+
tme[indx] = start[u];
|
19 |
+
fst[u] = indx;
|
20 |
+
indx++;
|
21 |
+
|
22 |
+
cl[u][ col[u] ] = 1;
|
23 |
+
for(ll i = 0; i < graph[u].size(); i++) {
|
24 |
+
|
25 |
+
if(graph[u][i] != p) {
|
26 |
+
dfs( graph[u][i] , u);
|
27 |
+
tme[indx++] = start[u];
|
28 |
+
cl[u] |= cl[ graph[u][i] ];
|
29 |
+
}
|
30 |
+
}
|
31 |
+
}
|
32 |
+
|
33 |
+
void build(ll lo, ll hi, ll node)
|
34 |
+
|
35 |
+
{
|
36 |
+
if(lo == hi) {
|
37 |
+
lca[node] = tme[lo];
|
38 |
+
return;
|
39 |
+
}
|
40 |
+
|
41 |
+
ll mid = (lo + hi) / 2;
|
42 |
+
build(lo, mid, 2 * node);
|
43 |
+
build(mid + 1, hi, 2 * node + 1);
|
44 |
+
lca[node] = min(lca[2 * node], lca[2 * node + 1]);
|
45 |
+
}
|
46 |
+
|
47 |
+
ll query(ll lo, ll hi, ll left, ll right, ll node)
|
48 |
+
|
49 |
+
{
|
50 |
+
if(lo > right || hi < left)
|
51 |
+
return 1e18;
|
52 |
+
if(lo >= left && hi <= right)
|
53 |
+
return lca[node];
|
54 |
+
|
55 |
+
ll mid = (lo + hi) / 2;
|
56 |
+
ll p1 = query(lo, mid, left, right, 2 * node);
|
57 |
+
ll p2 = query(mid + 1, hi, left, right, 2 * node + 1);
|
58 |
+
|
59 |
+
return min(p1, p2);
|
60 |
+
}
|
61 |
+
|
62 |
+
int main()
|
63 |
+
|
64 |
+
{
|
65 |
+
ll t, n, q, root, u, v, l, r;
|
66 |
+
cin >> t;
|
67 |
+
while(t--) {
|
68 |
+
scanf("%d %d %d", &n, &q, &root);
|
69 |
+
for(ll i = 0; i < n; i++)
|
70 |
+
scanf("%d", &col[i]);
|
71 |
+
|
72 |
+
for(ll i = 1; i < n; i++) {
|
73 |
+
scanf("%d %d", &u, &v);
|
74 |
+
graph[u].pb(v);
|
75 |
+
graph[v].pb(u);
|
76 |
+
}
|
77 |
+
|
78 |
+
dfs(root, -1);
|
79 |
+
build(0, indx - 1, 1);
|
80 |
+
//cout << 5;
|
81 |
+
for(ll i = 1; i <= q; i++) {
|
82 |
+
scanf("%d %d", &l, &r);
|
83 |
+
//cout << fst[l] << " " <<fst[r] << endl;
|
84 |
+
if(fst[l] > fst[r])
|
85 |
+
swap(l, r);
|
86 |
+
ll lc = query(0, indx - 1, fst[l], fst[r], 1);
|
87 |
+
//cout << cl[4].count() << endl;
|
88 |
+
printf("%d\n", cl[ nodeOf[lc] ].count());
|
89 |
+
}
|
90 |
+
|
91 |
+
for(ll i = 0; i < n; i++) {
|
92 |
+
graph[i].clear();
|
93 |
+
cl[i].reset();
|
94 |
+
}
|
95 |
+
TM = 1;
|
96 |
+
indx = 0;
|
97 |
+
}
|
98 |
+
|
99 |
+
return 0;
|
100 |
+
}
|
spoj_cpp_clean/ADAORANG/ADAORANG-8.cpp
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/***************************************************
|
2 |
+
* Problem Name : ADAORANG - Ada and Orange Tree.cpp
|
3 |
+
* Problem Link : https://www.spoj.com/problems/ADAORANG/
|
4 |
+
* OJ : Spoj
|
5 |
+
* Verdict : AC
|
6 |
+
* Date : 2019-09-19
|
7 |
+
* Problem Type : LCA, Bitset
|
8 |
+
* Author Name : Saikat Sharma
|
9 |
+
* University : CSE, MBSTU
|
10 |
+
***************************************************/
|
11 |
+
#include <iostream>
|
12 |
+
#include <cstdio>
|
13 |
+
#include <cmath>
|
14 |
+
#include <algorithm>
|
15 |
+
#include <climits>
|
16 |
+
#include <cstring>
|
17 |
+
#include <string>
|
18 |
+
#include <sstream>
|
19 |
+
#include <vector>
|
20 |
+
#include <queue>
|
21 |
+
#include <list>
|
22 |
+
#include <unordered_map>
|
23 |
+
#include <unordered_set>
|
24 |
+
#include <cstdlib>
|
25 |
+
#include <deque>
|
26 |
+
#include <stack>
|
27 |
+
#include <bitset>
|
28 |
+
#include <cassert>
|
29 |
+
#include <map>
|
30 |
+
#include <set>
|
31 |
+
#include <cassert>
|
32 |
+
#include <iomanip>
|
33 |
+
#include <random>
|
34 |
+
#include <ext/pb_ds/assoc_container.hpp>
|
35 |
+
#include <ext/pb_ds/tree_policy.hpp>
|
36 |
+
|
37 |
+
using namespace std;
|
38 |
+
using namespace __gnu_pbds;
|
39 |
+
typedef long long ll;
|
40 |
+
typedef unsigned long long ull;
|
41 |
+
|
42 |
+
#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
|
43 |
+
#define __FileRead freopen ("input.txt", "r", stdin)
|
44 |
+
#define __FileWrite freopen ("output.txt", "w", stdout)
|
45 |
+
#define SET(a,v) memset(a,v,sizeof(a))
|
46 |
+
#define SZ(v) (int)v.size()
|
47 |
+
#define pii pair<int,int>
|
48 |
+
#define pil pair <int, ll>
|
49 |
+
#define pli pair <ll, int>
|
50 |
+
#define pll pair <ll, ll>
|
51 |
+
#define debug cout <<"######\n"
|
52 |
+
#define debug1(x) cout <<"### " << x << " ###\n"
|
53 |
+
#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n"
|
54 |
+
#define nl cout << "\n";
|
55 |
+
#define sp cout << " ";
|
56 |
+
#define sl(n) scanf("%lld", &n)
|
57 |
+
#define sf(n) scanf("%lf", &n)
|
58 |
+
#define si(n) scanf("%d", &n)
|
59 |
+
#define ss(n) scanf("%s", n)
|
60 |
+
#define pf(n) scanf("%d", n)
|
61 |
+
#define pfl(n) scanf("%lld", n)
|
62 |
+
#define all(v) v.begin(), v.end()
|
63 |
+
#define rall(v) v.begin(), v.end()
|
64 |
+
#define srt(v) sort(v.begin(), v.end())
|
65 |
+
#define r_srt(v) sort(v.rbegin(), v.rend())
|
66 |
+
#define rev(v) reverse(v.begin(), v.end())
|
67 |
+
#define Sqr(x) ((x)*(x))
|
68 |
+
#define Mod(x, m) ((((x) % (m)) + (m)) % (m))
|
69 |
+
#define max3(a, b, c) max(a, max(b, c))
|
70 |
+
#define min3(a, b, c) min(a, min(b, c))
|
71 |
+
#define pb push_back
|
72 |
+
#define mk make_pair
|
73 |
+
#define MAX 450010
|
74 |
+
#define INF 1000000009
|
75 |
+
#define MOD 1000000007
|
76 |
+
|
77 |
+
template<class T>
|
78 |
+
using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
|
79 |
+
template<typename T>
|
80 |
+
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
|
81 |
+
tree_order_statistics_node_update>;
|
82 |
+
|
83 |
+
template <typename T> string toString ( T Number ) {
|
84 |
+
stringstream ss;
|
85 |
+
ss << Number;
|
86 |
+
return ss.str();
|
87 |
+
}
|
88 |
+
template<typename T> int toInt (T str) {
|
89 |
+
stringstream ss;
|
90 |
+
ss << str;
|
91 |
+
int num;
|
92 |
+
ss >> num;
|
93 |
+
return num;
|
94 |
+
}
|
95 |
+
/************************************ Code Start Here ******************************************************/
|
96 |
+
vector<vector<int> >adj;
|
97 |
+
bitset<251> ans[MAX];
|
98 |
+
int par[MAX], ar[MAX];
|
99 |
+
int table[MAX][20];
|
100 |
+
int level[MAX];
|
101 |
+
void dfs (int u, int p) {
|
102 |
+
for (int v : adj[u]) {
|
103 |
+
if (v == p) continue;
|
104 |
+
|
105 |
+
par[v] = u;
|
106 |
+
level[v] = level[u] + 1;
|
107 |
+
dfs (v, u);
|
108 |
+
ans[u] |= ans[v];
|
109 |
+
}
|
110 |
+
}
|
111 |
+
void build (int n) {
|
112 |
+
for (int i = 1; i <= n; i++) {
|
113 |
+
table[i][0] = par[i];
|
114 |
+
}
|
115 |
+
|
116 |
+
for (int j = 1; (1 << j) < n; j++) {
|
117 |
+
for (int i = 1; i <= n; i++) {
|
118 |
+
if (table[i][j - 1] != -1) {
|
119 |
+
table[i][j] = table[table[i][j - 1]][j - 1];
|
120 |
+
}
|
121 |
+
}
|
122 |
+
}
|
123 |
+
}
|
124 |
+
int query (int u, int v) {
|
125 |
+
if (level[u] < level[v]) swap (u, v);
|
126 |
+
|
127 |
+
int k = log2 (level[u]);
|
128 |
+
|
129 |
+
for (int i = k; i >= 0; i--) {
|
130 |
+
if ( (level[u] - (1 << i) ) >= level[v]) {
|
131 |
+
u = table[u][i];
|
132 |
+
}
|
133 |
+
}
|
134 |
+
|
135 |
+
if (u == v) return u;
|
136 |
+
|
137 |
+
for (int i = k; i >= 0; i--) {
|
138 |
+
if (table[u][i] != -1 && table[u][i] != table[v][i]) {
|
139 |
+
u = table[u][i];
|
140 |
+
v = table[v][i];
|
141 |
+
}
|
142 |
+
}
|
143 |
+
|
144 |
+
return par[u];
|
145 |
+
}
|
146 |
+
|
147 |
+
int main () {
|
148 |
+
__FastIO;
|
149 |
+
//~ cout << setprecision (10);
|
150 |
+
//~ cout << fixed;
|
151 |
+
int tc, n, q, r;
|
152 |
+
cin >> tc;
|
153 |
+
|
154 |
+
while (tc--) {
|
155 |
+
cin >> n >> q >> r;
|
156 |
+
r++;
|
157 |
+
adj.resize (n + 1);
|
158 |
+
|
159 |
+
for (int i = 0; i <= n; i++) {
|
160 |
+
ans[i].reset();
|
161 |
+
}
|
162 |
+
|
163 |
+
for (int i = 1; i <= n; i++) {
|
164 |
+
cin >> ar[i];
|
165 |
+
ans[i][ar[i]] = 1;
|
166 |
+
}
|
167 |
+
|
168 |
+
for (int i = 1; i < n; i++) {
|
169 |
+
int u, v;
|
170 |
+
cin >> u >> v;
|
171 |
+
u++, v++;
|
172 |
+
adj[u].pb (v);
|
173 |
+
adj[v].pb (u);
|
174 |
+
}
|
175 |
+
|
176 |
+
par[r] = -1;
|
177 |
+
level[r] = 0;
|
178 |
+
SET (table, -1);
|
179 |
+
dfs (r, -1);
|
180 |
+
build (n);
|
181 |
+
|
182 |
+
while (q--) {
|
183 |
+
int u, v;
|
184 |
+
cin >> u >> v;
|
185 |
+
u++, v++;
|
186 |
+
int nod = query (u, v);
|
187 |
+
cout << ans[nod].count() << "\n";
|
188 |
+
}
|
189 |
+
|
190 |
+
for (int i = 0; i <= n; i++) {
|
191 |
+
adj[i].clear();
|
192 |
+
}
|
193 |
+
}
|
194 |
+
|
195 |
+
return 0;
|
196 |
+
}
|
spoj_cpp_clean/ADAPLANT/ADAPLANT-20.cpp
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Author : Mr_Sohel
|
3 |
+
* Task :
|
4 |
+
* Algo : Multiset + Sliding Window
|
5 |
+
**/
|
6 |
+
#include <bits/stdc++.h>
|
7 |
+
|
8 |
+
#define endl '\n'
|
9 |
+
#define sqr(x) (x) * (x)
|
10 |
+
#define gcd(x,y) __gcd(x,y)
|
11 |
+
#define lcm(x,y) ((x/gcd(x,y)) * y)
|
12 |
+
#define pb push_back
|
13 |
+
#define pf push_front
|
14 |
+
#define mk make_pair
|
15 |
+
#define fi first
|
16 |
+
#define se second
|
17 |
+
#define sz(x) (int)x.size()
|
18 |
+
#define all(x) (x).begin(),(x).end()
|
19 |
+
#define rall(x) (x).rbegin(),(x).rend()
|
20 |
+
#define prec(x) fixed<<setprecision(x)
|
21 |
+
#define min3(a,b,c) min(a,min(b,c))
|
22 |
+
#define max3(a,b,c) max(a,max(b,c))
|
23 |
+
#define min4(a,b,c,d) min(a,min(b,min(c,d)))
|
24 |
+
#define max4(a,b,c,d) max(a,max(b,max(c,d)))
|
25 |
+
#define unsyncIO ios_base::sync_with_stdio(false); cin.tie(nullptr)
|
26 |
+
|
27 |
+
using namespace std;
|
28 |
+
|
29 |
+
using ll = long long;
|
30 |
+
using db = double;
|
31 |
+
using ld = long double;
|
32 |
+
using ull = unsigned long long;
|
33 |
+
using pii = pair<int, int>;
|
34 |
+
using pll = pair<ll, ll>;
|
35 |
+
|
36 |
+
const ld PI = acos((ld) - 1);
|
37 |
+
const int MOD = 1e9 + 7;
|
38 |
+
const ll INF = 2e18 + 1;
|
39 |
+
const ld EPS = 1e-9;
|
40 |
+
const int MX = 1e5 + 2;
|
41 |
+
|
42 |
+
#ifdef LOCAL
|
43 |
+
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
|
44 |
+
template < typename Arg1 >
|
45 |
+
void __f(const char* name, Arg1&& arg1) {
|
46 |
+
cerr << name << " = " << arg1 << endl;
|
47 |
+
}
|
48 |
+
template < typename Arg1, typename... Args>
|
49 |
+
void __f(const char* names, Arg1&& arg1, Args&&... args) {
|
50 |
+
const char* comma = strchr(names + 1, ',');
|
51 |
+
cerr.write(names, comma - names) << " = " << arg1 << " | ";
|
52 |
+
__f(comma + 1, args...);
|
53 |
+
}
|
54 |
+
#else
|
55 |
+
#define debug(...)
|
56 |
+
#endif
|
57 |
+
|
58 |
+
int a[MX];
|
59 |
+
|
60 |
+
int main() {
|
61 |
+
|
62 |
+
#ifdef LOCAL
|
63 |
+
clock_t tStart = clock();
|
64 |
+
freopen("in.txt", "r", stdin);
|
65 |
+
freopen("out.txt", "w", stdout);
|
66 |
+
#endif
|
67 |
+
unsyncIO;
|
68 |
+
int t; cin >> t;
|
69 |
+
while (t--) {
|
70 |
+
int n, k;
|
71 |
+
cin >> n >> k;
|
72 |
+
for (int i = 0; i < n; i++) cin >> a[i];
|
73 |
+
int i = 0, j = k + 1, ans = 0;
|
74 |
+
multiset<int> mp; // i wil try with multimap later
|
75 |
+
for (i = 0; i <= j and i < n; i++) {
|
76 |
+
mp.insert(a[i]);
|
77 |
+
}
|
78 |
+
i = 0;
|
79 |
+
auto f = mp.begin(), l = --mp.end();
|
80 |
+
// l--;
|
81 |
+
ans = max(ans, *l - *f);
|
82 |
+
while (j < n) {
|
83 |
+
f = mp.begin(), l = --mp.end();
|
84 |
+
ans = max(ans, (*l - *f));
|
85 |
+
/*for (auto in : mp) {
|
86 |
+
cout << in << " ";
|
87 |
+
}
|
88 |
+
cout << "ans = " << ans << endl;*/
|
89 |
+
auto x = mp.find(a[i]);
|
90 |
+
mp.erase(x);
|
91 |
+
j++;
|
92 |
+
i++;
|
93 |
+
mp.insert(a[j]);
|
94 |
+
}
|
95 |
+
cout << ans << endl;
|
96 |
+
}
|
97 |
+
#ifdef LOCAL
|
98 |
+
cerr << "\nRuntime: " << (ld) (clock() - tStart) / CLOCKS_PER_SEC << " Seconds" << endl;
|
99 |
+
#endif
|
100 |
+
return 0;
|
101 |
+
}
|
spoj_cpp_clean/ADAPLUS/ADAPLUS-9.cpp
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
****************************************************************
|
3 |
+
****************************************************************
|
4 |
+
-> Coded by Stavros Chryselis
|
5 |
+
-> Visit my github for more solved problems over multiple sites
|
6 |
+
-> https://github.com/StavrosChryselis
|
7 |
+
-> Feel free to email me at stavrikios@gmail.com
|
8 |
+
****************************************************************
|
9 |
+
****************************************************************
|
10 |
+
*/
|
11 |
+
|
12 |
+
#include <stdio.h>
|
13 |
+
#include <ctype.h>
|
14 |
+
#include <algorithm>
|
15 |
+
#include <string.h>
|
16 |
+
|
17 |
+
#define gc() getchar()
|
18 |
+
#define MAXN 2048
|
19 |
+
using namespace std;
|
20 |
+
|
21 |
+
inline char next_char()
|
22 |
+
{
|
23 |
+
char c = gc();
|
24 |
+
|
25 |
+
while (isspace(c))
|
26 |
+
c = gc();
|
27 |
+
|
28 |
+
return c;
|
29 |
+
}
|
30 |
+
|
31 |
+
char board[MAXN][MAXN];
|
32 |
+
int maxleft[MAXN][MAXN];
|
33 |
+
int maxright[MAXN][MAXN];
|
34 |
+
int maxup[MAXN][MAXN];
|
35 |
+
int maxdown[MAXN][MAXN];
|
36 |
+
|
37 |
+
int N;
|
38 |
+
|
39 |
+
inline void init()
|
40 |
+
{
|
41 |
+
int i, j;
|
42 |
+
|
43 |
+
scanf("%d", &N);
|
44 |
+
|
45 |
+
for (i = 1; i <= N; i++)
|
46 |
+
for (j = 1; j <= N; j++)
|
47 |
+
board[i][j] = next_char();
|
48 |
+
|
49 |
+
memset(maxleft, 0, sizeof(maxleft));
|
50 |
+
memset(maxright, 0, sizeof(maxright));
|
51 |
+
memset(maxdown, 0, sizeof(maxdown));
|
52 |
+
memset(maxup, 0, sizeof(maxup));
|
53 |
+
|
54 |
+
for (i = 1; i <= N; i++)
|
55 |
+
for (j = 1; j <= N; j++)
|
56 |
+
{
|
57 |
+
if (board[i][j] == '#')
|
58 |
+
maxleft[i][j] = maxleft[i][j - 1] + 1;
|
59 |
+
else
|
60 |
+
maxleft[i][j] = 0;
|
61 |
+
}
|
62 |
+
|
63 |
+
for (i = 1; i <= N; i++)
|
64 |
+
for (j = N; j >= 1; j--)
|
65 |
+
{
|
66 |
+
if (board[i][j] == '#')
|
67 |
+
maxright[i][j] = maxright[i][j + 1] + 1;
|
68 |
+
else
|
69 |
+
maxright[i][j] = 0;
|
70 |
+
}
|
71 |
+
|
72 |
+
for (j = 1; j <= N; j++)
|
73 |
+
for (i = 1; i <= N; i++)
|
74 |
+
{
|
75 |
+
if (board[i][j] == '#')
|
76 |
+
maxup[i][j] = maxup[i - 1][j] + 1;
|
77 |
+
else
|
78 |
+
maxup[i][j] = 0;
|
79 |
+
}
|
80 |
+
|
81 |
+
for (j = 1; j <= N; j++)
|
82 |
+
for (i = N; i >= 1; i--)
|
83 |
+
{
|
84 |
+
if (board[i][j] == '#')
|
85 |
+
maxdown[i][j] = maxdown[i + 1][j] + 1;
|
86 |
+
else
|
87 |
+
maxdown[i][j] = 0;
|
88 |
+
}
|
89 |
+
}
|
90 |
+
|
91 |
+
inline int solve()
|
92 |
+
{
|
93 |
+
int ans = 0;
|
94 |
+
int i, j;
|
95 |
+
|
96 |
+
for (i = 1; i <= N; i++)
|
97 |
+
for (j = 1; j <= N; j++)
|
98 |
+
if(board[i][j] == '#')
|
99 |
+
ans = max(ans, 1 + min(min(maxleft[i][j - 1], maxright[i][j + 1]), min(maxup[i - 1][j], maxdown[i + 1][j])));
|
100 |
+
|
101 |
+
return ans;
|
102 |
+
}
|
103 |
+
|
104 |
+
int main()
|
105 |
+
{
|
106 |
+
int T;
|
107 |
+
|
108 |
+
// freopen("input.txt", "r", stdin);
|
109 |
+
|
110 |
+
scanf("%d", &T);
|
111 |
+
|
112 |
+
while (T--)
|
113 |
+
{
|
114 |
+
init();
|
115 |
+
printf("%d\n", solve());
|
116 |
+
}
|
117 |
+
|
118 |
+
return 0;
|
119 |
+
}
|
spoj_cpp_clean/ADAQUEUE/ADAQUEUE-20.cpp
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Author: Sohel Rana
|
3 |
+
* Date: 2022-05-25 21:07:34
|
4 |
+
* Task: G_-_Ada_and_Queue
|
5 |
+
**/
|
6 |
+
#include <bits/stdc++.h>
|
7 |
+
#define endl '\n'
|
8 |
+
#define sqr(x) (x) * (x)
|
9 |
+
#define gcd(x,y) __gcd(x, y)
|
10 |
+
#define lcm(x,y) ((x/gcd(x,y)) * y)
|
11 |
+
#define pf(x) push_front(x)
|
12 |
+
#define pb(x) push_back(x)
|
13 |
+
#define eb(x) emplace_back(x)
|
14 |
+
#define all(x) (x).begin(), (x).end()
|
15 |
+
#define rall(x) (x).rbegin(), (x).rend()
|
16 |
+
#define sz(x) (int)x.size()
|
17 |
+
#define prec(x) fixed<<setprecision(x)
|
18 |
+
#define debug(x) cerr<<#x<<" = "<<(x)<< endl
|
19 |
+
#define debug2(x,y) cerr<<#x<<" = "<<(x)<<","<<#y<<" = "<<(y)<< endl
|
20 |
+
#define unsyncIO ios_base::sync_with_stdio(false); cin.tie(nullptr)
|
21 |
+
|
22 |
+
using ll = long long;
|
23 |
+
using db = double;
|
24 |
+
using ld = long double;
|
25 |
+
using ull = unsigned long long;
|
26 |
+
|
27 |
+
const ld pi = acos((ld)-1);
|
28 |
+
const int mod = 1e9+7;
|
29 |
+
const ll inf = 1e18;
|
30 |
+
const ld eps = 1e-9;
|
31 |
+
const int mx = 1e5;
|
32 |
+
|
33 |
+
using namespace std;
|
34 |
+
|
35 |
+
|
36 |
+
int main()
|
37 |
+
{
|
38 |
+
//freopen("in.txt", "r", stdin);
|
39 |
+
//freopen("out.txt", "w", stdout);
|
40 |
+
unsyncIO;
|
41 |
+
deque<int> q;
|
42 |
+
int flag = 0;
|
43 |
+
int t;
|
44 |
+
cin >> t;
|
45 |
+
while (t--) {
|
46 |
+
string s;
|
47 |
+
int item;
|
48 |
+
cin >> s;
|
49 |
+
if (s == "toFront"){
|
50 |
+
cin >> item;
|
51 |
+
if (flag % 2 == 1) {
|
52 |
+
q.push_back(item);
|
53 |
+
} else
|
54 |
+
q.push_front(item);
|
55 |
+
} else if (s == "push_back") {
|
56 |
+
cin >> item;
|
57 |
+
if (flag % 2 == 1) {
|
58 |
+
q.push_front(item);
|
59 |
+
} else
|
60 |
+
q.push_back(item);
|
61 |
+
} else if (s == "front") {
|
62 |
+
if (q.empty()){
|
63 |
+
cout << "No job for Ada?" << endl;
|
64 |
+
} else {
|
65 |
+
if (flag % 2 == 1) {
|
66 |
+
cout << q.back() << endl;
|
67 |
+
q.pop_back();
|
68 |
+
} else {
|
69 |
+
cout << q.front()<<endl;
|
70 |
+
q.pop_front();
|
71 |
+
}
|
72 |
+
}
|
73 |
+
} else if (s == "back") {
|
74 |
+
if (q.empty()) {
|
75 |
+
cout << "No job for Ada?" << endl;
|
76 |
+
} else {
|
77 |
+
if (flag % 2 == 1) {
|
78 |
+
cout << q.front() << endl;
|
79 |
+
q.pop_front();
|
80 |
+
} else {
|
81 |
+
cout << q.back() << endl;
|
82 |
+
q.pop_back();
|
83 |
+
}
|
84 |
+
}
|
85 |
+
} else if (s == "reverse") {
|
86 |
+
flag++;
|
87 |
+
}
|
88 |
+
|
89 |
+
}
|
90 |
+
return 0;
|
91 |
+
}
|