比赛地址:ShanDong Multi-University Training #3 - Virtual Judge
比赛地址:ShanDong Multi-University Training #3 - Virtual Judge
A - Booby Prize 不难发现,最后释放的蚂蚁力量值必定是这个区间的最大公约数,而且这个最大公约数是这个区间的最小值才符合题意,只需要用线段树处理一下区间GCD、区间最小值、区间最小值的个数。如果区间gcd与区间最小值相同,就可以输出区间长度减去最小值的个数,反之,输出区间的长度
代码:
(参考Codeforces 474F(分块)_hollowstory的博客-CSDN博客
#includeB - New String 写一个bool类型的cmp函数,然后使用带cmp限制的sort即可using namespace std; const int N = 1e5+6; const int M = 506; int n,t,s[N],l,r; int unit,siz,ans[M][M],gans[M][M]; int g,temp,cnt; int gcd(int a,int b) { return b ? gcd(b, a % b) : a; } int main() { memset(ans,0,sizeof ans); memset(gans, 0, sizeof gans); scanf("%d",&n): for(int i=1;i<=n;i++) { cin>>a[i]; } unit = sqrt(n); siz = (n + unit) / unit; for(int i = 0; i <= n; i += unit){ g = s[i + 1], cnt = 0, temp; for(int j = i; j <= n; j++){ temp = gcd(g, s[j]); if(g != temp) cnt = 0, g = temp; if(s[j] == g) cnt++; if(j % unit == 0) ans[i / unit][j / unit] = cnt, gans[i / unit][j / unit] = g; } ans[i / unit][siz] = cnt; gans[i / unit][siz] = g; } scanf("%d", &t); while(t--){ scanf("%d%d", &l, &r); int x = (l + unit) / unit * unit, y = r / unit * unit; int res; if(x >= y) g = s[l], res = 1, x = y = l; else g = gans[x / unit][y / unit], res = ans[x / unit][y / unit]; for(int i = l; i < x; i++){ temp = gcd(g, s[i]); if(g != temp) res = 0, g = temp; if(g == s[i]) res++; } for(int i = y + 1; i <= r; i++){ temp = gcd(g, s[i]); if(g != temp) res = 0, g = temp; if(g == s[i]) res++; } printf("%dn", r - l + 1 - res); } return 0; }
代码:
#includeC - Easy Problem emmm...看一眼范围,发现不能爆搜,结果题解说就是爆搜(QAQusing namespace std; #define MAX 300000 + 50 int n, m, k, op; string s; map mp; struct ran { string s; bool operator < (const ran &x)const{ for(int i = 0; i < s.size() && i < x.s.size(); ++i){ if(mp[s[i]] < mp[x.s[i]])return true; else if(mp[s[i]] > mp[x.s[i]])return false; else continue; } return s.size() < x.s.size(); } }tr[MAX]; void work(){ cin >> s; for(int i = 0; i < s.size(); ++i){ mp[s[i]] = i + 1; } cin >> n; for(int i = 1; i <= n; ++i){ cin >> tr[i].s; } sort(tr + 1, tr + 1 + n); cin >> k; cout << tr[k].s << endl; } int main() { work(); return 0; }
#includeG - Optimal Biking Strategy(DP + 二分)typedef long long ll; using namespace std; const int N = 60; char g[N][N];int n; bool vis[N][N][N][N]; struct node { int x1, y1; int x2, y2; int cnt; }; int xa,xb,ya,yb; void bfs() { queue q; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, 1, -1}; q.push({xa, ya, xb, yb, 0}); while(q.size()) { auto t = q.front(); q.pop(); if(t.x1 == t.x2 && t.y1 == t.y2) { cout << t.cnt << 'n'; return; } for (int i = 0; i < 4; i ++ ) { int nx1 = t.x1 + dx[i]; int ny1 = t.y1 + dy[i]; int nx2 = t.x2 + dx[i]; int ny2 = t.y2 + dy[i]; if(nx1 < 1 || nx1 > n)nx1 = t.x1; if(nx2 < 1 || nx2 > n)nx2 = t.x2; if(ny1 < 1 || ny1 > n)ny1 = t.y1; if(ny2 < 1 || ny2 > n)ny2 = t.y2; if(g[nx1][ny1] == '*')nx1 = t.x1, ny1 = t.y1; if(g[nx2][ny2] == '*')nx2 = t.x2, ny2 = t.y2; if(vis[nx1][ny1][nx2][ny2])continue; vis[nx1][ny1][nx2][ny2] = 1; q.push({nx1, ny1, nx2, ny2, t.cnt + 1}); } } cout << "no solutionn"; } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); cin >> n; for (int i = 1; i <= n; i ++ ) { for (int j = 1; j <= n; j ++ ) { cin >> g[i][j]; if(g[i][j] == 'a') { xa = i; ya = j; } else if(g[i][j] == 'b') { xb = i; yb = j; } } } bfs(); return 0; }
代码( sjh orz):
#includetypedef long long ll; using namespace std; const int N = 1e6 + 10; int f[N][N]; int a[N]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, p, s, t; cin >> n >> p >> s; for (int i = 1; i <= n; i ++ ) cin >> a[i]; cin >> t; for (int i = 0; i < t; i ++ ) f[0][i] = 0; for (int i = 1; i <= n; i ++ ) for (int j = 0; j <= t; j ++ ) { f[i][j] = f[i - 1][j] + a[i] - a[i - 1]; for (int k = 1; k <= j; k ++ ) { int mx = k * s; int ans = -1; int l = 1, r = i; while(l <= r) { int mid = (l + r)>>1; if(a[i] - a[mid] < mx) { r = mid - 1; ans = mid; } else if(a[i] - a[mid] == mx) { ans = mid; break; } else l = mid + 1; } if(ans == -1)continue; f[i][j] = min(f[i][j], f[ans][j - k]); } } int ans = 0x3f3f3f3f; for (int i = 1; i <= n; i ++ ) { ans = min(ans, f[i][t] + p - a[i]); } cout << ans << 'n'; return 0; }



