ABC278 题解

ABC278 题解

1500  pts 1500 ~ \text{pts} 1500 pts,可以上 2000 2000 2000,Rank 1400 1400 1400 左右。

A

题面

给定长 n n n 的数组,在后面插入 k k k 0 0 0,求最后 n n n 项。

题解

计算可得,需要输出原数组的后 max ⁡ ( n − k , 0 ) \max(n - k, 0) max(nk,0) 项。

需要输出 min ⁡ ( k , n ) \min(k, n) min(k,n) 0 0 0

Code

主要代码:

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cn(n); cn(k);
	int a[n]; repn(i, 0, n, 1) cm(a[i]);
	repn(i, min(n, k), n, 1){
		cout << a[i] << " ";
	}
	repn(i, 0, min(n, k), 1) cout << 0 << " ";
	return 0;
}

B

题面

时间 a b ‾ : c d ‾ \overline{ab}:\overline{cd} ab:cd 被称为合法的当且仅当:

  • a b ‾ : c d ‾ \overline{ab}:\overline{cd} ab:cd 符合 24 24 24 小时制。
  • a c ‾ : b d ‾ \overline{ac}:\overline{bd} ac:bd 也符合 24 24 24 小时制。

给定 a b ‾ \overline{ab} ab c d ‾ \overline{cd} cd,求未来最近的合法时间。

题解

暴力即可。注意一天共有 1440 1440 1440 分钟

下一分钟时,需要改变小时数。

Code

#include 
#define int long long
// FOR templates.
#define rep(i, s, n, k) for(int i = s;i <= n;i += k)
#define repn(i, s, n, k) for(int i = s;i < n;i += k)
#define pre(i, s, n, k) for(int i = s;i >= n;i -= k)
#define pren(i, s, n, k) for(int i = s;i > n;i -= k)
// Abbr for STL.
#define pii pair<int, int>
#define pdd pair<double, double>
#define mpi map<int, int>
#define vc vector<int>
// IO templates, proven very useful.
#define cn(n) int n;cin >> n
#define cm(n) cin >> n
// Abbr for funcs.
#define pb push_back
#define mset memset
// #define files
using namespace std;
const int MAXN = 0x3f3f3f3f3f3f3f3fLL;
const int MOD1 = 1000000007LL;
const int MOD2 = 998244353LL;
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int gcd(int a, int b) {if(b == 0) return a;return gcd(b, a % b);}
inline int lowbit(int x) {return x & (-x);}
inline int lcm(int a, int b) {return a * b / gcd(a, b);}

signed main(){
#ifdef files
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cn(h); cn(m);
	repn(i, 0, 1440, 1){
		int a, b, c, d;
		a = h / 10, b = h % 10, c = m / 10, d = m % 10;
		if(a * 10 + c < 24 && b * 10 + d < 60){
			cout << h << " " << m << endl;
			return 0;
		}
		m++; if(m == 60) m = 0, h++;
		if(h == 24) h = 0;
	}
	return 0;
}

/*
 *  things to check
 *  1.  int overflow or long long memory need
 *  2.  recursion/array/binary search/dp/loop bounds
 *  3.  precision
 *  4.  special cases(n=1,bounds)
 *  5.  delete debug statements
 *  6.  initialize(especially multi-tests)
 *  7.  = or == , n or m ,++ or -- , i or j , > or >= , < or <=
 *  8.  keep it simple and stupid
 *  9.  do not delete, use // instead
 *  10. operator priority
 *  11. is there anything extra to output?
 *  12. THINK TWICE CODE ONCE, THINK ONCE DEBUG FOREVER
 *  13. submit ONCE, AC once. submit twice, WA forever
 *  14. calm down and you'll get good rank
 *  15. even a bit wrong scores zero
 *  16. ...
 **/

C

题面

一共 n n n 个用户和 q q q 个操作。操作如下:

  • 1 a b a a a 用户关注了 b b b 用户;
  • 2 a b a a a 用户取关了 b b b 用户;
  • 3 a b:询问 a a a b b b 是否互关

题解

用一个 map, int> 存储关系。

映射可以极好地存储各种复杂的数据。

Code

#include 
#define int long long
// FOR templates.
#define rep(i, s, n, k) for(int i = s;i <= n;i += k)
#define repn(i, s, n, k) for(int i = s;i < n;i += k)
#define pre(i, s, n, k) for(int i = s;i >= n;i -= k)
#define pren(i, s, n, k) for(int i = s;i > n;i -= k)
// Abbr for STL.
#define pii pair<int, int>
#define pdd pair<double, double>
#define mpi map<int, int>
#define vc vector<int>
// IO templates, proven very useful.
#define cn(n) int n;cin >> n
#define cm(n) cin >> n
// Abbr for funcs.
#define pb push_back
#define mset memset
// #define files
using namespace std;
const int MAXN = 0x3f3f3f3f3f3f3f3fLL;
const int MOD1 = 1000000007LL;
const int MOD2 = 998244353LL;
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int gcd(int a, int b) {if(b == 0) return a;return gcd(b, a % b);}
inline int lowbit(int x) {return x & (-x);}
inline int lcm(int a, int b) {return a * b / gcd(a, b);}

map<pii, int> mp;

signed main(){
#ifdef files
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cn(n); cn(q);
	repn(i, 0, q, 1){
		cn(t); cn(a); cn(b);
		if(t == 1) mp[make_pair(a, b)] = 1;
		else if(t == 2) mp[make_pair(a, b)] = 0;
		else cout << ((mp[make_pair(a, b)] && mp[make_pair(b, a)]) ? "Yes" : "No") << endl;
	}
	return 0;
}

/*
 *  things to check
 *  1.  int overflow or long long memory need
 *  2.  recursion/array/binary search/dp/loop bounds
 *  3.  precision
 *  4.  special cases(n=1,bounds)
 *  5.  delete debug statements
 *  6.  initialize(especially multi-tests)
 *  7.  = or == , n or m ,++ or -- , i or j , > or >= , < or <=
 *  8.  keep it simple and stupid
 *  9.  do not delete, use // instead
 *  10. operator priority
 *  11. is there anything extra to output?
 *  12. THINK TWICE CODE ONCE, THINK ONCE DEBUG FOREVER
 *  13. submit ONCE, AC once. submit twice, WA forever
 *  14. calm down and you'll get good rank
 *  15. even a bit wrong scores zero
 *  16. ...
 **/

D

题面

给定数组 a a a,做以下操作:

  • 1 x:给 a a a 的所有点赋值 x x x
  • 2 i x:给 a i a_i ai 赋值 x x x
  • 3 i:单点查询。

题解

对于每个点建一个向量存储修改值、修改时间和前缀和。

查询时就用这些东西二分 upper_bound

Code

#include 
#define int long long
// FOR templates.
#define rep(i, s, n, k) for(int i = s;i <= n;i += k)
#define repn(i, s, n, k) for(int i = s;i < n;i += k)
#define pre(i, s, n, k) for(int i = s;i >= n;i -= k)
#define pren(i, s, n, k) for(int i = s;i > n;i -= k)
// Abbr for STL.
#define pii pair<int, int>
#define pdd pair<double, double>
#define mpi map<int, int>
#define vc vector<int>
// IO templates, proven very useful.
#define cn(n) int n;cin >> n
#define cm(n) cin >> n
// Abbr for funcs.
#define pb push_back
#define mset memset
// #define files
using namespace std;
const int MAXN = 0x3f3f3f3f3f3f3f3fLL;
const int MOD1 = 1000000007LL;
const int MOD2 = 998244353LL;
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int gcd(int a, int b) {if(b == 0) return a;return gcd(b, a % b);}
inline int lowbit(int x) {return x & (-x);}
inline int lcm(int a, int b) {return a * b / gcd(a, b);}

vc vec[200001], vec1[200001], vec2[200001];

signed main(){
#ifdef files
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cn(n); int a[n + 1];
	rep(i, 1, n, 1) cm(a[i]);
	rep(i, 1, n, 1){
		vec[i].pb(0); vec[i].pb(a[i]);
		vec1[i].pb(-1); vec1[i].pb(0);
		vec2[i].pb(0); vec2[i].pb(a[i]);
	}
	int lst = -1, nm = 0; cn(q);
	rep(i, 1, q, 1){
		cn(op); if(op == 1){
			cn(x); lst = i, nm = x;
		}
		else if(op == 2){
			cn(k); cn(x);
			vec[k].pb(x); vec1[k].pb(i);
			vec2[k].pb(vec2[k][vec2[k].size() - 1] + x);
		}
		else{
			cn(k);
			int pos = (int)(upper_bound(vec1[k].begin(), vec1[k].end(), lst) - vec1[k].begin());
			cout << vec2[k][vec2[k].size() - 1] - vec2[k][pos - 1] + nm << endl;
		}
	}
	return 0;
}

/*
 *  things to check
 *  1.  int overflow or long long memory need
 *  2.  recursion/array/binary search/dp/loop bounds
 *  3.  precision
 *  4.  special cases(n=1,bounds)
 *  5.  delete debug statements
 *  6.  initialize(especially multi-tests)
 *  7.  = or == , n or m ,++ or -- , i or j , > or >= , < or <=
 *  8.  keep it simple and stupid
 *  9.  do not delete, use // instead
 *  10. operator priority
 *  11. is there anything extra to output?
 *  12. THINK TWICE CODE ONCE, THINK ONCE DEBUG FOREVER
 *  13. submit ONCE, AC once. submit twice, WA forever
 *  14. calm down and you'll get good rank
 *  15. even a bit wrong scores zero
 *  16. ...
 **/

E

题面

给出一个矩阵,每个矩阵一个数 a i , j ≤ n a_{i, j} \le n ai,jn

现有一个 h × w h\times w h×w 的纸片,求当这个纸片的右上角在 ( i , j ) (i,j) (i,j) 上时剩下的数的种类。

题解

将每个颜色记录下来,建立 n n n 个不等式方程。

对于每个格子直接暴力求解。

Code

#include 
#define int long long
// FOR templates.
#define rep(i, s, n, k) for(int i = s;i <= n;i += k)
#define repn(i, s, n, k) for(int i = s;i < n;i += k)
#define pre(i, s, n, k) for(int i = s;i >= n;i -= k)
#define pren(i, s, n, k) for(int i = s;i > n;i -= k)
// Abbr for STL.
#define pii pair<int, int>
#define pdd pair<double, double>
#define mpi map<int, int>
#define vc vector<int>
// IO templates, proven very useful.
#define cn(n) int n;cin >> n
#define cm(n) cin >> n
// Abbr for funcs.
#define pb push_back
#define mset memset
// #define files
using namespace std;
const int MAXN = 0x3f3f3f3f3f3f3f3fLL;
const int MOD1 = 1000000007LL;
const int MOD2 = 998244353LL;
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int gcd(int a, int b) {if(b == 0) return a;return gcd(b, a % b);}
inline int lowbit(int x) {return x & (-x);}
inline int lcm(int a, int b) {return a * b / gcd(a, b);}

vc vec[200001], vec1[200001], vec2[200001];

signed main(){
#ifdef files
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cn(n); int a[n + 1];
	rep(i, 1, n, 1) cm(a[i]);
	rep(i, 1, n, 1){
		vec[i].pb(0); vec[i].pb(a[i]);
		vec1[i].pb(-1); vec1[i].pb(0);
		vec2[i].pb(0); vec2[i].pb(a[i]);
	}
	int lst = -1, nm = 0; cn(q);
	rep(i, 1, q, 1){
		cn(op); if(op == 1){
			cn(x); lst = i, nm = x;
		}
		else if(op == 2){
			cn(k); cn(x);
			vec[k].pb(x); vec1[k].pb(i);
			vec2[k].pb(vec2[k][vec2[k].size() - 1] + x);
		}
		else{
			cn(k);
			int pos = (int)(upper_bound(vec1[k].begin(), vec1[k].end(), lst) - vec1[k].begin());
			cout << vec2[k][vec2[k].size() - 1] - vec2[k][pos - 1] + nm << endl;
		}
	}
	return 0;
}

/*
 *  things to check
 *  1.  int overflow or long long memory need
 *  2.  recursion/array/binary search/dp/loop bounds
 *  3.  precision
 *  4.  special cases(n=1,bounds)
 *  5.  delete debug statements
 *  6.  initialize(especially multi-tests)
 *  7.  = or == , n or m ,++ or -- , i or j , > or >= , < or <=
 *  8.  keep it simple and stupid
 *  9.  do not delete, use // instead
 *  10. operator priority
 *  11. is there anything extra to output?
 *  12. THINK TWICE CODE ONCE, THINK ONCE DEBUG FOREVER
 *  13. submit ONCE, AC once. submit twice, WA forever
 *  14. calm down and you'll get good rank
 *  15. even a bit wrong scores zero
 *  16. ...
 **/

F

状压 dp (?) 咕咕咕

你可能感兴趣的:(综合题题解,算法,c++,数据结构)