1197A - Лестница своими руками
Idea: adedalic
Tutorial
Tutorial is loading...
Solution (adedalic)
fun main(args: Array<String>) {
val T = readLine()!!.toInt()
for (tc in 1..T) {
val n = readLine()!!.toInt()
val a = readLine()!!.split(' ').map { it.toInt() }.sortedDescending()
println(minOf(a[1] - 1, n - 2))
}
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
vector<int> a(n);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
int pos = max_element(a.begin(), a.end()) - a.begin();
bool res = true;
for(int i = 0; i < pos; i++)
res &= (a[i] < a[i + 1]);
for(int i = pos; i < n - 1; i++)
res &= (a[i] > a[i + 1]);
if(res)
puts("YES");
else
puts("NO");
}
Idea: Roms
Tutorial
Tutorial is loading...
Solution (Roms)
#include<bits/stdc++.h>
using namespace std;
const int N = int(3e5) + 99;
int n, k;
int a[N];
int main(){
cin >> n >> k;
for(int i = 0; i < n; ++i)
cin >> a[i];
vector <int> v;
for(int i = 1; i < n; ++i) v.push_back(a[i - 1] - a[i]);
sort(v.begin(), v.end());
int res = a[n - 1] - a[0];
for(int i = 0; i < k - 1; ++i) res += v[i];
cout << res << endl;
return 0;
}
1197D - Очередная задача на подотрезки
Idea: Roms
Tutorial
Tutorial is loading...
Solution (Roms)
#include <bits/stdc++.h>
using namespace std;
const int N = int(3e5) + 99;
int n, m, k;
int a[N];
long long bst[N];
long long psum[N];
long long sum(int l, int r){
l = max(l, 0);
return psum[r] - (l == 0? 0 : psum[l - 1]);
}
int main() {
cin >> n >> m >> k;
for(int i = 0; i < n; ++i){
cin >> a[i];
psum[i] = a[i] + (i == 0? 0 : psum[i - 1]);
}
long long res = 0;
for(int len = 1; len <= m && len <= n; ++len)
res = max(res, sum(0, len - 1) - k);
for(int i = 0; i < n; ++i){
if(i + 1 >= m){
long long nbst = sum(i - m + 1, i) - k;
if(i - m >= 0) nbst += bst[i - m];
bst[i] = max(bst[i], + nbst);
}
for(int len = 0; len < m && i + len < n; ++len)
res = max(res, bst[i] + sum(i + 1, i + len) - k * (len > 0));
}
cout << res << endl;
return 0;
}
Idea: adedalic
Tutorial
Tutorial is loading...
Solution (adedalic)
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())
#define x first
#define y second
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
const int INF = int(1e9) + 555;
const li INF64 = li(1e18);
const ld EPS = 1e-9;
const int MOD = int(1e9) + 7;
int norm(int a) {
if(a >= MOD) a -= MOD;
if(a < 0) a += MOD;
return a;
}
pt combine(const pt &a, const pt &b) {
if(a.x < b.x)
return a;
if(a.x > b.x)
return b;
return {a.x, norm(a.y + b.y)};
}
int n;
vector<pt> p;
inline bool read() {
if(!(cin >> n))
return false;
p.resize(n);
fore(i, 0, n)
cin >> p[i].x >> p[i].y;
return true;
}
vector<pt> T;
void setVal(int pos, const pt &val) {
T[pos += n] = val;
for(pos >>= 1; pos > 0; pos >>= 1)
T[pos] = combine(T[2 * pos], T[2 * pos + 1]);
}
pt getMin(int l, int r) {
pt ans = {INF, 0};
for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
if(l & 1)
ans = combine(T[l++], ans);
if(r & 1)
ans = combine(T[--r], ans);
}
return ans;
}
inline void solve() {
auto comp = [](const pt &a, const pt &b) {
if(a.y != b.y)
return a.y < b.y;
return a.x < b.x;
};
sort(p.begin(), p.end(), comp);
T.assign(2 * n, {INF, 0});
for (int i = n - 1; i >= 0; i--) {
int pos = int(lower_bound(p.begin(), p.end(), pt(0, p[i].x), comp) - p.begin());
if(pos >= n) {
setVal(i, {p[i].y, 1});
continue;
}
pt bst = getMin(pos, n);
setVal(i, {bst.x - (p[i].x - p[i].y), bst.y});
}
cout << getMin(0, n).y << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
if(read()) {
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int x, int y)
{
return (x + y) % MOD;
}
int mul(int x, int y)
{
return (x * 1ll * y) % MOD;
}
typedef vector<int> vec;
typedef vector<vec> mat;
vec mul(const mat& a, const vec& b)
{
int n = a.size();
int m = b.size();
vector<int> c(m);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
c[i] = add(c[i], mul(b[j], a[i][j]));
return c;
}
mat add(const mat& a, const mat& b)
{
int n = a.size();
int m = a[0].size();
mat c(n, vec(m, 0));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
c[i][j] = add(a[i][j], b[i][j]);
return c;
}
mat mul(const mat& a, const mat& b)
{
int x = a.size();
int y = b.size();
int z = b[0].size();
mat c(x, vec(z, 0));
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
for(int k = 0; k < z; k++)
c[i][k] = add(c[i][k], mul(a[i][j], b[j][k]));
return c;
}
mat binpow(mat a, int d)
{
int n = a.size();
mat c = mat(n, vec(n, 0));
for(int i = 0; i < n; i++) c[i][i] = 1;
while(d > 0)
{
if(d % 2 == 1) c = mul(c, a);
a = mul(a, a);
d /= 2;
}
return c;
}
int f[3][3];
int extend(int color, vector<int> last_numbers)
{
vector<int> used(4, 0);
for(int i = 0; i < 3; i++)
if(f[color][i])
used[last_numbers[i]] = 1;
for(int i = 0; i <= 3; i++)
if(used[i] == 0)
return i;
return 3;
}
vector<int> extend_state(int color, vector<int> last_numbers)
{
int z = extend(color, last_numbers);
last_numbers.insert(last_numbers.begin(), z);
last_numbers.pop_back();
return last_numbers;
}
vector<int> int2state(int x)
{
vector<int> res;
for(int i = 0; i < 3; i++)
{
res.push_back(x % 4);
x /= 4;
}
return res;
}
int state2int(const vector<int>& x)
{
int res = 0;
int deg = 1;
for(auto y : x)
{
res += deg * y;
deg *= 4;
}
return res;
}
mat form_matrix(int color)
{
mat res(64, vec(64, 0));
for(int i = 0; i < 64; i++)
{
int j = state2int(extend_state(color, int2state(i)));
res[j][i] = add(res[j][i], 1);
}
return res;
}
mat color_matrices[3];
mat full_matrix;
vector<pair<int, int> > colored[1043];
int len[1043];
int dp[1043][4];
mat full_pows[31];
void precalc_pows()
{
full_pows[0] = full_matrix;
for(int i = 0; i <= 30; i++)
full_pows[i + 1] = mul(full_pows[i], full_pows[i]);
}
vec powmul(int d, vec b)
{
for(int i = 0; i <= 30; i++)
{
if(d % 2 == 1)
b = mul(full_pows[i], b);
d /= 2;
}
return b;
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> len[i];
int m;
cin >> m;
for(int i = 0; i < m; i++)
{
int x, y, c;
cin >> x >> y >> c;
--x;
--y;
--c;
colored[x].push_back(make_pair(y, c));
}
for(int i = 0; i < n; i++)
sort(colored[i].begin(), colored[i].end());
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
cin >> f[i][j];
for(int i = 0; i < 3; i++)
color_matrices[i] = form_matrix(i);
full_matrix = color_matrices[0];
for(int i = 1; i < 3; i++)
full_matrix = add(full_matrix, color_matrices[i]);
precalc_pows();
dp[0][0] = 1;
for(int i = 0; i < n; i++)
{
vec cur(64);
cur[state2int({3, 3, 3})] = 1;
int last = 0;
for(auto x : colored[i])
{
cur = powmul(x.first - last, cur);
cur = mul(color_matrices[x.second], cur);
last = x.first + 1;
}
cur = powmul(len[i] - last, cur);
for(int j = 0; j < 4; j++)
for(int k = 0; k < 64; k++)
{
vector<int> s = int2state(k);
dp[i + 1][j ^ s[0]] = add(dp[i + 1][j ^ s[0]], mul(dp[i][j], cur[k]));
}
}
cout << dp[n][0] << endl;
}