A
连续的k个要有准确的m个可用的天数,并且第一个可以用的 + 另外一个要求值最小
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e5 + 10;
const int INF = 1e9 + 10;
int num[105];
bool istra[105];
int n, m, p;
struct Node {
int tmp, id;
bool operator < (const Node &w) const {
return tmp < w.tmp;
}
}node[105];
int main(){
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = 0; i < n; ++i) {
scanf("%d", num + i);
}
scanf("%d", &p);
mst(istra, false);
for(int x, i = 0; i < p; ++i) {
scanf("%d", &x);
istra[x] = true;
}
int minx = INF, a = 0, b = 1;
int day = 0;
for(int len = m; len <= n; ++len) {
for(int i = 0; i < n; ++i) {
int l = i, r = i + len - 1;
if(r >= n) continue;
int cnt = 0;
int xday = 0;
for(int j = l; j <= r; ++j) {
if(istra[j]) continue;
node[cnt].tmp = num[j];
node[cnt].id = j;
xday++, cnt++;
}
if(xday != m) continue;
int x = node[0].tmp, idx = node[0].id;
sort(node + 1, node + cnt);
int y = node[1].tmp;
int idy = node[1].id;
if(x + y < minx) {
minx = x + y;
a = idx, b = idy;
}
}
}
if(a > b) swap(a, b);
printf("%d %d\n", a, b);
}
return 0;
}
C
参考了大神的思路:blackcat
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 300 + 10;
const int INF = 1e9 + 10;
int dp[qq][2], a[qq][qq];
int sum[qq], val[qq];
int Solve(int m, int p) {
int ans = -INF;
for(int i = 1; i <= m; ++i) {
int minx = INF;
int sumX = 0;
for(int j = i; j <= m; ++j) {
minx = min(minx, val[j]);
sumX += sum[j];
if(i == 1 && j == m) {
ans = max(ans, sumX - minx + p);
} else {
int tsumX = max(sumX - minx + p, sumX);
ans = max(ans, tsumX);
}
}
}
return ans;
}
int Dp(int m, int p) {
dp[1][0] = sum[1];
dp[1][1] = sum[1] - val[1] + p;
for(int i = 1; i <= m; ++i) {
dp[i][0] = max(dp[i - 1][0], 0) + sum[i];
dp[i][1] = max(dp[i - 1][1] + sum[i], max(dp[i - 1][0], 0) - val[i] + p + sum[i]);
}
int ans = -INF;
for(int i = 1; i <= m; ++i) {
ans = max(ans, max(dp[i][0], dp[i][1]));
}
return ans;
}
int main(){
int n, m, p;
while(scanf("%d%d%d", &n, &m, &p) != EOF) {
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
scanf("%d", &a[i][j]);
}
}
int ans = -INF;
for(int i = 1; i <= n; ++i) {
fill(sum + 1, sum + 1 + m, 0);
fill(val + 1, val + 1 + m, INF);
for(int j = i; j <= n; ++j) {
for(int k = 1; k <= m; ++k) {
sum[k] += a[j][k];
val[k] = min(val[k], a[j][k]);
}
if(i == 1 && j == n) {
ans = max(ans, Solve(m, p));
} else {
ans = max(ans, Dp(m, p));
}
}
}
printf("%d\n", ans);
}
return 0;
}
E
首先排除n <= 3的情况, n = 3的时候特判一下是否三点共线,不共线就是NO,n > 3的情况的话肯定是YES,我们可以先判断是否存在三点共线,如果存在就把中间一个点的染成B其他点全部染成A即可,如果没有三点贡献的情况,就用跑一下凸包把外围的点全部标记一下,然后找出中间一个点染成B其他点染成A即可,如果中间没有点的话,就这样任意找两点连成一条直线,判断这条直线左边和右边是否都存在点,如果都存在就把这个点染成B,其他点染成A
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e5 + 10;
const int INF = 1e9 + 10;
int n;
struct Point {
int x, y;
int id;
Point(){}
Point(int _x, int _y) {
x = _x, y = _y;
}
bool operator < (const Point &w) const {
if(x == w.x) return y < w.y;
return x < w.x;
}
Point operator - (const Point &w) const {
return Point(x - w.x, y - w.y);
}
int operator ^ (const Point &w) const {
return x * w.y - y * w.x;
}
int operator * (const Point &w) const {
return x * w.x + y * w.y;
}
}p[105], tmp[103];
bool vis[105];
char ans[105];
int Xmult(Point p0, Point p1, Point p2) {
return (p1 - p0) ^ (p2 - p0);
}
int main(){
int t; scanf("%d", &t);
while(t--) {
mst(ans, 0);
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d%d", &p[i].x, &p[i].y);
p[i].id = i;
}
if(n < 3) {
puts("NO");
continue;
}
if(n == 3) {
sort(p + 1, p + 1 + 3);
int a = p[3].y - p[1].y, b = p[3].x - p[1].x;
int c = p[2].y - p[1].y, d = p[2].x - p[1].x;
if(a * d == b * c) {
puts("YES");
ans[p[1].id] = ans[p[3].id] = 'A';
ans[p[2].id] = 'B';
for(int i = 1; i <= 3; ++i) {
printf("%c", ans[i]);
}
puts("");
} else {
puts("NO");
}
continue;
}
puts("YES");
bool f = false;
for(int i = 1; i <= n; ++i) {
for(int j = i + 1; j <= n; ++j) {
int a = p[j].y - p[i].y, b = p[j].x - p[i].x;
for(int k = j + 1; k <= n; ++k) {
int c = p[k].y - p[i].y, d = p[k].x - p[i].x;
if(a * d == b * c) {
f = true;
tmp[0] = p[i], tmp[1] = p[j], tmp[2] = p[k];
}
}
if(f) break;
}
if(f) break;
}
if(f) {
sort(tmp, tmp + 3);
ans[tmp[1].id] = 'B';
for(int i = 1; i <= n; ++i) {
if(ans[i] != 'B') ans[i] = 'A';
printf("%c", ans[i]);
}
puts("");
} else {
mst(vis, false);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
int a, b;
a = b = 0;
for(int k = 1; k <= n; ++k) {
if(k == i || k == j) continue;
if(Xmult(p[k], p[i], p[j]) > 0) a++;
else b++;
}
if(a == n - 2) vis[i] = vis[j] = true;
}
}
bool flag = false;
for(int i = 1; i <= n; ++i) {
if(!vis[i]) {
ans[i] = 'B';
flag = true;
break;
}
}
if(flag) {
for(int i = 1; i <= n; ++i) {
if(ans[i] != 'B') ans[i] = 'A';
printf("%c", ans[i]);
}
puts("");
continue;
}
for(int i = 1; i <= n; ++i) {
for(int j = i + 1; j <= n; ++j) {
int a, b;
a = 0, b = 0;
for(int k = 1; k <= n; ++k) {
if(k == i || k == j) continue;
if(Xmult(p[k], p[i], p[j]) > 0) a++;
else b++;
}
if(a > 0 && b > 0) {
flag = true;
ans[i] = 'B', ans[j] = 'B';
}
if(flag) break;
}
if(flag) break;
}
for(int i = 1; i <= n; ++i) {
if(ans[i] != 'B') ans[i] = 'A';
printf("%c", ans[i]);
}
puts("");
}
}
return 0;
}
G
队友写的
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL gcd(LL x,LL y) {
return y==0?x:gcd(y,x%y);
}
int main() {
LL n,m;
while(~scanf("%lld%lld",&n,&m)) {
LL x=n-1;
LL y=m-1;
LL g=gcd(x,y);
LL a=x/g;
LL b=y/g;
LL ret=x*y/g;
ret-=(ret/g)-a-b;
printf("%lld\n",ret);
}
return 0;
}
I
因为x和y可以相等,无非就找一下三种情况的最小值,第一种区间全正一定是最小正数的平方最小,第二种区间全负一定是最大负数的平方最小,第三种区间有正有负,一定是最大正数和最小负数的乘积最小,线段树维护一下四种值即可
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const LL MAXN = 1e17;
const int qq = 1 << 17;
int pminx[qq << 2], pmaxn[qq << 2];
int nminx[qq << 2], nmaxn[qq << 2];
void Init(int rt) {
pminx[rt] = INF, pmaxn[rt] = 0;
nminx[rt] = 0, nmaxn[rt] = -INF;
}
void PushUp(int rt) {
pminx[rt] = min(pminx[rt << 1], pminx[rt << 1 | 1]);
pmaxn[rt] = max(pmaxn[rt << 1], pmaxn[rt << 1 | 1]);
nminx[rt] = min(nminx[rt << 1], nminx[rt << 1 | 1]);
nmaxn[rt] = max(nmaxn[rt << 1], nmaxn[rt << 1 | 1]);
}
void Build(int rt, int l, int r) {
Init(rt);
if(l == r) {
int x;
scanf("%d", &x);
if(x > 0) pminx[rt] = pmaxn[rt] = x;
else nminx[rt] = nmaxn[rt] = x;
return;
}
int m = (l + r) >> 1;
Build(rt << 1, l, m);
Build(rt << 1 | 1, m + 1, r);
PushUp(rt);
}
void UpDate(int rt, int l, int r, int x, int y, int val) {
if(l == r) {
Init(rt);
if(val > 0) pminx[rt] = pmaxn[rt] = val;
else nminx[rt] = nmaxn[rt] = val;
// printf("%d %d %d %d\n", pmaxn[rt], pminx[rt], nmaxn[rt], nminx[rt]);
return;
}
int m = (l + r) >> 1;
if(x <= m) UpDate(rt << 1, l, m, x, y, val);
if(y > m) UpDate(rt << 1 | 1, m + 1, r, x, y, val);
PushUp(rt);
}
LL Query(int rt, int l, int r, int x, int y, int f) {
LL ans;
if(f == 1) ans = 0;
else if(f == 2) ans = INF;
else if(f == 3) ans = -INF;
else ans = 0;
if(x <= l && r <= y) {
if(f == 1) return (LL)pmaxn[rt];
else if(f == 2) return (LL)pminx[rt];
else if(f == 3) return (LL)nmaxn[rt];
else return (LL)nminx[rt];
}
int m = (l + r) >> 1;
if(x <= m) {
if(f == 1) ans = max(ans, Query(rt << 1, l, m, x, y, f));
else if(f == 2) ans = min(ans, Query(rt << 1, l, m, x, y, f));
else if(f == 3) ans = max(ans, Query(rt << 1, l, m, x, y, f));
else ans = min(ans, Query(rt << 1, l, m, x, y, f));
}
if(y > m) {
if(f == 1) ans = max(ans, Query(rt << 1 | 1, m + 1, r, x, y, f));
else if(f == 2) ans = min(ans, Query(rt << 1 | 1, m + 1, r, x, y, f));
else if(f == 3) ans = max(ans, Query(rt << 1 | 1, m + 1, r, x, y, f));
else ans = min(ans, Query(rt << 1 | 1, m + 1, r, x, y, f));
}
return ans;
}
int main(){
int t; scanf("%d", &t);
while(t--) {
int n; scanf("%d", &n);
n = 1 << n;
Build(1, 1, n);
int Q; scanf("%d", &Q);
while(Q--) {
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
l++, r++;
if(op == 2) {
UpDate(1, 1, n, l, l, r - 1);
} else {
LL maxnz = Query(1, 1, n, l, r, 1);
LL minxz = Query(1, 1, n, l, r, 2);
LL maxnf = Query(1, 1, n, l, r, 3);
LL minxf = Query(1, 1, n, l, r, 4);
// printf("%lld %lld %lld %lld\n", maxnz, minxz, maxnf, minxf);
LL ans = MAXN;
if(maxnf != -INF) {
ans = min(ans, maxnf * maxnf);
}
if(minxz != INF) {
ans = min(ans, minxz * minxz);
}
if(minxf != 0 && maxnz != 0) {
ans = min(ans, minxf * maxnz);
}
printf("%lld\n", ans);
}
}
}
return 0;
}