题意:有 n n n 个苹果,你可以 x x x 元买一个,也可以 y y y 元买三个,问你最少话多少钱恰好买下这些苹果。
思路:暴力枚举买的个数即可。
#include
using namespace std;
signed main() {
int x, y, n, mi = 1e9;
cin >> x >> y >> n;
for (int i = 0; i <= 100; i ++) {
int si = i;
int sj = n - i;
if (sj >= 0 && sj % 3 == 0) {
int j = sj / 3;
mi = min(i * x + j * y, mi);
}
}
cout << mi << '\n';
}
题意:有 n n n 个洞穴,第 i i i 个洞穴到第 i + 1 i+1 i+1 个洞穴需要花费 a i a_i ai 分钟。初始时间限制为 T T T,有 m m m 个奖励洞穴,进入这个洞穴 x i x_i xi T T T 增加 y i y_i yi。是否可以在时间限制内从 1 1 1 到达 n n n 号洞穴?
思路:暴力模拟即可。坑比较多。
#include
#define int long long
using namespace std;
const int N = 1e6 + 10;
int a[N], b[N];
signed main() {
int n, m, t;
cin >> n >> m >> t;
for (int i = 1; i < n; i ++)
cin >> a[i];
while (m --) {
int x, y;
cin >> x >> y;
b[x] += y;
}
for (int i = 1; i < n; i ++) {
t -= a[i];
if (t <= 0) {
cout << "No\n";
return 0;
}
t += b[i + 1];
}
cout << "Yes\n";
}
题意:
给你一个正方形网格, ( i , j ) (i,j) (i,j) 位置上有一个字符:
如果字符为
U向上走。
如果字符为D向下走。
如果字符为L向左走。
如果字符为R向右走。
一开始你在 ( 1 , 1 ) (1,1) (1,1)。如果走的时候出了界,那么输出最后一次在棋盘里的坐标;如果永远无法走完输出 − 1 -1 −1。
题解:暴力枚举即可。无法走完就是死循环,开一个标记数组记录所有走过的坐标即可。
#include
#define int long long
using namespace std;
char s[555][555];
void print(int x, int y) {
cout << x << ' ' << y << '\n';
exit(0);
}
signed main() {
int h, w;
cin >> h >> w;
for (int i = 1; i <= h; i ++)
cin >> (s[i] + 1);
set <pair <int, int> > se;
int x = 1, y = 1;
while (true) {
if (s[x][y] == 'U') {
if (x > 1)
x --;
else
print(x, y);
}
else if (s[x][y] == 'D') {
if (x < h)
x ++;
else
print(x, y);
}
else if (s[x][y] == 'L') {
if (y > 1)
y --;
else
print(x, y);
}
else if (s[x][y] == 'R') {
if (y < w)
y ++;
else
print(x, y);
}
else {
cout << "-1\n";
return 0;
}
if (se.count(make_pair(x, y))) {
cout << "-1\n";
return 0;
}
se.insert(make_pair(x, y));
}
}
题意:给你一个长度为
n
n
n 的序列,问你是否存在四元组
(
a
,
b
,
c
,
d
)
(a,b,c,d)
(a,b,c,d) 满足
∑
i
=
a
b
−
1
=
P
\sum_{i=a}^{b-1}=P
∑i=ab−1=P,
∑
i
=
b
c
−
1
=
Q
\sum_{i=b}^{c-1}=Q
∑i=bc−1=Q,
∑
i
=
c
d
−
1
=
R
\sum_{i=c}^{d-1}=R
∑i=cd−1=R。如果有输出 Yes,否则输出 No。注意数组下标从
0
0
0 开始。
思路1:前缀和套三个二分查找,时间复杂度 O ( n × log n ) O(n\times \log n) O(n×logn)。没调出来。
#include
#define int __int128
using namespace std;
const int N = 2e6 + 10;
int a[N];
__int128 s[N];
set <pair <int, char> > se[N];
const int zlt = 1e18;
int read(){
int x=0,flag=0;
char a=getchar();
while(a>'9'||a<'0'){
if(a=='-')flag=1;
a=getchar();
}
while(a>='0'&&a<='9'){
x=x*10+a-'0';
a=getchar();
}
return flag?-x:x;
}
void write(int x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9){
write(x/10);
}
putchar(x%10+'0');
}
signed main() {
int n, p, q, rr;
n = read();
p = read();
q = read();
rr = read();
int id = 0;
for (int i = 0; i < n; i ++) {
a[i] = read();
if (!i) {
s[i] = a[i];
} else {
s[i] = s[i - 1] + a[i];
int l = id, r = i;
int best = -1;
while (l <= r) {
int mid = l + r >> 1;
__int128 qz = mid - 1;
if (qz < 0)
qz = 0;
else
qz = s[qz];
if (s[i] - qz == p) {
best = mid;
break;
}
else if (s[i] - qz > p)
l = mid + 1;
else
r = mid - 1;
}
if (best != -1)
se[i].insert(make_pair(best - 1, 'P'));
l = id, r = i;
best = -1;
while (l <= r) {
int mid = l + r >> 1;
__int128 qz = mid - 1;
if (qz < 0)
qz = 0;
else
qz = s[qz];
if (s[i] - qz == q) {
best = mid;
break;
}
else if (s[i] - qz > q)
l = mid + 1;
else
r = mid - 1;
}
if (best != -1)
se[i].insert(make_pair(best - 1, 'Q'));
l = id, r = i;
best = -1;
while (l <= r) {
int mid = l + r >> 1;
__int128 qz = mid - 1;
if (qz < 0)
qz = 0;
else
qz = s[qz];
if (s[i] - qz == rr) {
best = mid;
break;
}
else if (s[i] - qz > rr)
l = mid + 1;
else
r = mid - 1;
}
if (best != -1)
se[i].insert(make_pair(best - 1, 'R'));
}
}
for (int i = 1; i < n; i ++) {
for (auto &[u, v] : se[i]) {
if (v == 'R' && u != -1) {
for (auto &[u1, v1] : se[u]) {
if (v1 == 'Q' && u1 != -1) {
for (auto &[u2, v2] : se[u1]) {
if (v2 == 'P') {
cout << "Yes\n";
return 0;
}
}
}
}
}
}
}
cout << "No\n";
}
思路 2 2 2:用一个 set 记录前缀和,然后累加即可。
#include
#define int long long
using namespace std;
int a[2323233];
signed main() {
int n, p, q, r;
cin >> n >> p >> q >> r;
int sum = 0;
set <int> se;
se.insert(0);
for (int i = 1; i <= n; i ++) {
int x;
cin >> x;
sum += x;
a[i] = sum;
se.insert(sum);
}
for (int i = 0, x = a[0]; i <= n; i ++, x = a[i])
if (se.count(x) && se.count(x + p) && se.count(x + p + q) && se.count(x + p + q + r)) {
cout << "Yes\n";
return 0;
}
cout << "No\n";
return 0;
}
2022-08-27 10:02:22 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 400 557 Byte 152 ms 14528 KB Detail
2022-08-27 10:01:31 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 540 Byte 151 ms 14508 KB Detail
2022-08-27 09:57:39 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 540 Byte 156 ms 14592 KB Detail
2022-08-27 09:56:12 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 538 Byte 113 ms 14536 KB Detail
2022-08-27 09:35:23 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2578 Byte 164 ms 144816 KB Detail
2022-08-27 09:32:05 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2227 Byte 194 ms 135124 KB Detail
2022-08-27 09:29:32 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2249 Byte 181 ms 133388 KB Detail
2022-08-27 09:27:47 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2254 Byte 197 ms 135064 KB Detail
2022-08-27 09:26:33 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2248 Byte 189 ms 133548 KB Detail
2022-08-27 09:24:44 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2259 Byte 184 ms 133432 KB Detail
2022-08-27 09:07:50 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 1990 Byte 181 ms 133516 KB Detail
2022-08-27 09:03:57 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 1982 Byte 181 ms 133408 KB Detail
2022-08-27 09:02:46 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 1994 Byte 186 ms 133388 KB Detail
2022-08-27 08:58:06 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2169 Byte 182 ms 133392 KB Detail
2022-08-27 08:55:46 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2161 Byte 260 ms 133256 KB Detail
2022-08-27 08:51:33 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2161 Byte 181 ms 133588 KB Detail
2022-08-27 08:42:56 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2138 Byte 265 ms 133160 KB Detail
2022-08-27 08:42:02 D - Iroha and Haiku (New ABC Edition) ****** C++ (GCC 9.2.1) 0 2101 Byte 215 ms 48860 KB Detail