2023牛客寒假算法基础集训营1(10/13)
admin
2024-05-13 00:25:30
0次

World Final? World Cup! (I)

后面不用踢当且仅当即便后面全进,也赶不上另一方,注意还能踢几次球的计算

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {string s;cin >> s;int len = s.size();int cnt1 = 0, cnt2 = 0, ans = -1;for (int i = 0; i < len; i++) {if (i & 1) {if (s[i] == '1') {cnt2++;}if (cnt1 > cnt2 + 5 - (i + 1) / 2 || cnt2 > cnt1 + 5 - (i + 1) / 2) {ans = i + 1;break;}} else {if (s[i] == '1') {cnt1++;}if (cnt1 > cnt2 + 5 - i / 2 || cnt2 > cnt1 + 4 - i / 2) {ans = i + 1;break;}}}cout << ans << '\n';}return 0;
}

现在是,学术时间 (I)

贪心地想,只要每个教授发表自己的,引用量大于等于1即可获得1点H指数,所以统计非正数即可

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {int n;cin >> n;int ans = 0;vector a(n + 1);for (int i = 1; i <= n; i++) {cin >> a[i];if (a[i] > 0) {ans++;}}cout << ans << '\n';}return 0;
}

现在是,学术时间 (II)

根据数据范围发现,点一定都在第一象限内,所以讨论p点的相对位置即可

AC代码:

#include 
using namespace std;
using LL = long long;
struct point {long long x, y;point operator + (const point& p) {return {x + p.x, y + p.y};}point operator - (const point& p) {return {x - p.x, y - p.y};}double dot(const point& p) const {return 1.0 * x * p.x + 1.0 * y * p.y;}double cross(const point& p) const {return 1.0 * x * p.y - 1.0 * y * p.x;}double dist(const point& p) const {return sqrt(1.0 * (x - p.x) * (x - p.x) + 1.0 * (y - p.y) * (y - p.y));}
};
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {point a, b, o;o.x = 0;o.y = 0;cin >> a.x >> a.y >> b.x >> b.y;point c, d;c.x = a.x;c.y = 0;d.y = a.y;d.x = 0;double ans;function calc = [&](point x, point y) {double res = 1.0 * abs(x.x - y.x) * abs(x.y - y.y);return res;};if (b.x >= a.x && b.y >= a.y) {ans = (1.0 * a.x * a.y) / (1.0 * b.x * b.y);} else if (b.x < a.x && b.y >= a.y) {ans = max((calc(b, o) - calc(b, d)) / (calc(b, d) + 1.0 * a.x * a.y), (calc(b, c) - calc(b, a)) / (calc(b, a) + 1.0 * a.x * a.y));} else if (b.x >= a.x && b.y < a.y) {ans = max((calc(b, d) - calc(b, a)) / (calc(b, a) + 1.0 * a.x * a.y), (calc(b, o) - calc(b, c)) / (calc(b, c) + 1.0 * a.x * a.y));} else {ans = max({calc(b, o), calc(b, a), calc(b, c), calc(b, d)}) / (1.0 * a.x * a.y);}cout << fixed << setprecision(10) << ans << '\n';}return 0;
}

鸡算几何

根据题意,判断有没有操作三,就判断铁丝是否发生了镜像的变化,因为铁丝是不会发生形变的,而且ABC和DEF不一定对应,就考虑叉积判断修改C和F是否都在AB和DE线段的逆时针方向,然后再判断此时AB和DE,BC和EF长度是否相等

AC代码:

#include 
using namespace std;
using LL = long long;
struct point {double x, y;point operator + (const point& p) {return {x + p.x, y + p.y};}point operator - (const point& p) {return {x - p.x, y - p.y};}double dot(const point& p) const {return 1.0 * x * p.x + 1.0 * y * p.y;}double cross(const point& p) const {return 1.0 * x * p.y - 1.0 * y * p.x;}double dist(const point& p) const {return sqrt(1.0 * (x - p.x) * (x - p.x) + 1.0 * (y - p.y) * (y - p.y));}
};
const double eps = 1e-8;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {point a, b, c, d, e, f;cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y;cin >> d.x >> d.y >> e.x >> e.y >> f.x >> f.y;point x, y, i, j;x.x = a.x - b.x;x.y = a.y - b.y;y.x = c.x - b.x;y.y = c.y - b.y;i.x = d.x - e.x;i.y = d.y - e.y;j.x = f.x - e.x;j.y = f.y - e.y;if (x.cross(y) < 0) {point z = a;a = c;c = z;}if (i.cross(j) < 0) {point z = d;d = f;f = z;}x.x = a.x - b.x;x.y = a.y - b.y;y.x = c.x - b.x;y.y = c.y - b.y;i.x = d.x - e.x;i.y = d.y - e.y;j.x = f.x - e.x;j.y = f.y - e.y;if (sqrt(x.dot(x)) - sqrt(i.dot(i)) > eps || sqrt(y.dot(y)) - sqrt(j.dot(j)) > eps) {cout << "YES\n";} else {cout << "NO\n";}}return 0;
}

鸡玩炸蛋人

因为图是无向图,所以先考虑没有一个炸弹的情况时,在一个联通块里的方案数是联通块大小的平方,那么答案就是所有联通块大小平方的和,如果一个联通块里存在炸弹,那么答案为存在炸弹的联通块大小的平方,如果多个联通块存在炸弹,无解

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;vector> G(n + 1);for (int i = 0; i < m; i++) {int u, v;cin >> u >> v;G[u].push_back(v);G[v].push_back(u);}int root = -1;vector c(n + 1);vector vis(n + 1);for (int i = 1; i <= n; i++) {cin >> c[i];if (c[i] > 0) {root = i;}}int sz = 0;function dfs = [&](int u) {for (auto v : G[u]) {if (!vis[v]) {vis[v] = true;sz++;dfs(v);}}};LL ans = 0;if (root == -1) {for (int i = 1; i <= n; i++) {if (!vis[i]) {vis[i] = true;sz = 1;dfs(i);ans = ans + 1LL * sz * sz;}}} else {vis[root] = true;sz = 1;dfs(root);for (int i = 1; i <= n; i++) {if (c[i] > 0 && !vis[i]) {cout << "0\n";return 0;}}ans = ans + 1LL * sz * sz;}cout << ans << '\n';return 0;
}

鸡格线

通过暴力跑f(x)=round(10*sqrt(x))发现,0无论跑多少次都是0,[1,99]跑有限次就达到99不再变化,[100,∞]也是跑有限次达到100后就不再变化,所以只需要存储下来非0的下标,以及多少次操作后就不再变化的操作数,操作时只修改还会变化的,否则从数组中删去,并修改所有数的和

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;vector a(n + 1), b, c;vector vis(n + 1);LL sum = 0;for (int i = 1; i <= n; i++) {cin >> a[i];int x = a[i];int cnt = 0;if (x >= 100) {while (x != 100) {x = round(10 * sqrt(x));cnt++;}b.push_back(i);c.push_back(cnt);} else if (x > 0) {while (x != 99) {x = round(10 * sqrt(x));cnt++;}b.push_back(i);c.push_back(cnt);}sum += a[i];}while (m--) {int op;cin >> op;if (op == 2) {cout << sum << '\n';} else {int l, r, k;cin >> l >> r >> k;int p = lower_bound(b.begin(), b.end(), l) - b.begin();int q = upper_bound(b.begin(), b.end(), r) - b.begin();int len = b.size();if (len == 0) {continue;}if (p == q) {continue;} else {vector z;for (int i = p; i < q; i++) {c[i] -= k;if (c[i] <= 0) {z.push_back(i);if (a[b[i]] >= 100) {sum -= a[b[i]];sum += 100;} else {sum -= a[b[i]];sum += 99;}} else {sum -= a[b[i]];for (int j = 0; j < k; j++) {a[b[i]] = round(10 * sqrt(a[b[i]]));}sum += a[b[i]];}}int len1 = z.size();int cnt = 0;for (int i = 0; i < len1; i++) {b.erase(z[i] + b.begin() - cnt);c.erase(z[i] + c.begin() - cnt);cnt++;}}}}return 0;
}

本题主要考察了DFS

因为这张图是完整的,所以考虑一个边有多少个凸出来的以及需要有多少凹进去的与他对应,没有恰好相等的,一定需要最后一块拼图给补上缺口

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {int n;cin >> n;n *= n;vector a(n);vector cnt1(4), cnt2(4);for (int i = 1; i < n; i++) {cin >> a[i];for (int j = 0; j < 4; j++) {if (a[i][j] == '1') {cnt1[j]++;} else if (a[i][j] == '2') {cnt2[j]++;}}}int ans = 10;for (int i = 0; i < 4; i++) {if (cnt1[i] == cnt2[(i + 2) % 4]) {} else {if (cnt1[i] > cnt2[(i + 2) % 4]) {ans++;} else {ans--;}}}cout << ans << '\n';}return 0;
}

本题主要考察了dp

贪心地考虑,类似于100100100...1111放置,这样一定能保证坏区间是最小的

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;string s = "";for (int i = 1; i <= n; i++) {if (n - i >= m) {if (i % 3 == 1) {s += '1';m--;} else {s += '0';}} else {while (m) {s += '1';m--;}break;}}int ans = 0;for (int i = 0; i < n - 2; i++) {int cnt = (s[i] == '1' ? 1 : 0) + (s[i + 1] == '1' ? 1 : 0) + (s[i + 2] == '1' ? 1 : 0);if (cnt >= 2) {ans++;}}cout << ans << '\n';return 0;
}

本题主要考察了运气

因为问团队和问第几个人相对独立,所以分别考虑他们两个各自期望操作数是多少

问团队:一次问出,两次问出,三次问出,四次问出,四次问出,最多需要四次就能判断出,那么期望就是(1+2+3+4+4)/5

问第几个人:一次问出,两次问出,三次问出,三次问出,最多需要三次就能判断出,那么期望就是(1+2+3+3)/4

两者加起来2.8+2.25=4.05,答案为32

AC代码:

32

本题主要考察了找规律

01背包,n个人背包容量为m,dp[i][j]就可以表示为前i个人占用j个空间时最大价值,因为题目没有给出物品体积,即每次需要枚举[0,m]所有体积,如果不选dp[i][j]=dp[i-1][j],如果选,首先占用的空间不能超过背包目前占用空间j,并且此时手里有的仙贝数要大于0,此时(这次还没分配)手里的仙贝数就是(m-j+所选物品的体积),所以产生的价值就是 所选物品体积/此时(这次还没分配)手里的仙贝数

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;vector> dp(n + 1, vector (m + 1));for (int i = 1; i <= n; i++) {for (int j = 0; j <= m; j++) {dp[i][j] = dp[i - 1][j];for (int k = 0; k <= m; k++) {if (j >= k && m - j + k > 0) {dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + 1.0 * k / (m - j + k));}}}}double ans = 0.0;for (int i = 0; i <= m; i++) {ans = max(ans, dp[n][i]);}cout << fixed << setprecision(10) << ans << '\n';return 0;
}

相关内容

热门资讯

蜻蜓订购APP贵金属订购非法期...   蜻蜓订购APP以"投资平台"名义对外宣传,主打贵金属期货(订购)交易。该平台一方面强调自身"正规...
悟空订购APP非法期货交易欺骗...   近期,有投资者反映,“悟空订购APP”等平台以白银、贵金属订购为名,宣传所谓“高收益”“低门槛”...
小赢淘金APP期货投资骗局,平...   “小赢淘金”之流,最擅长的就是用“注册送现金”、“充值返券”的糖衣炮弹来引流。别天真地以为那是福...
八方淘金APP非法期货交易欺骗...   警惕!八方生财、每日淘金、小赢淘金、悟空订购、蜻蜓订购、斗金订购等APP均涉嫌非法期货交易!  ...
智易购APP贵金属订购交易骗局...   很多人看到“3元起投”“充值送福利”“操作简单”“随时提现”这类宣传,很容易把智易购APP当成普...
喜玛银庄APP商品订购投资骗局...   喜玛银庄APP白银、铂金“现货订购”骗局警示:无资质平台涉嫌非法期货交易,投资者爆仓后如何维权?...
吉鑫乐享APP炒贵金属订购交易...   近期,多名投资者反映在“吉鑫乐享APP”进行贵金属投资时遭遇严重亏损,甚至血本无归。看似正规的“...
易掘金APP虚假期货投资骗局,...   “易掘金”的APP它打着“高收益、灵活投资”的旗号,宣称通过“商品订购”就能轻松赚钱,吸引了不少...
在大吉订购APP订购交易亏损被...   近期,不少投资者反映在“大吉订购App”上投资白银、铂金等商品遭遇严重亏损。该平台宣称提供现货订...
铂银壹号APP商品订购交易亏损...   许多中小投资者误以为网上的“商品现货订购”是合规合法的理财渠道,殊不知这背后隐藏着巨大的合规风险...
兴鑫淘金APP贵金属订购交易亏...   白银、贵金属或其他商品类投资,除国家批准的正规期货交易场所及持牌期货公司外,市面上大量第三方交易...
铂银掘金APP无资质的违规平台...   铂银掘金等平台的投资者,往往是被“注册送现金”“充值返现券”“首单免亏”等广告话术吸引,进而下载...
八方淘金APP订购交易不合规,...   “八方淘金APP”“铂银掘金APP”“铂银壹号APP”等软件被指以“现货订购”为名,实际采用高杠...
在指尖订购APP订购商品亏损被...   指尖订购APP这类平台,表面是“订购交易”,实则是披着电商外衣的非法期货陷阱。绝大多数投资者被蒙...
在犀贝易购APP做商品订购交易...   市面上打着“现货投资”“贵金属交易”旗号的APP并不少,但真正合规、受监管的平台并不多。犀贝易购...
易达淘金APP虚假商品订购交易...   易达淘金APP存在严重的虚假宣传与违规交易行为。该平台打着“商品订购”的旗号,实则通过不合规的交...
大浪淘金APP白银订购亏损被骗...   大浪淘金APP最隐蔽的套路,在于其完全封闭的内盘交易系统。表面上,它宣称与国际贵金属市场接轨;实...
一品好购APP上订购白银出现亏...   一品好购APP原名“一品订购”,运营方为上海宝智未来信息技术有限公司。近期不少投资者反映在该平台...
九州订购APP贵金属订购投资骗...   九州订购APP正通过抖.音、快手、今日头条等平台大量投放投资广告,用“5元起投”“充值返现”“0...
坚锅淘金APP交易软件并不正规...   当前,市面上大量所谓的“现货商城订购”交易软件,早已偏离了正常的实物商品买卖,演变为高杠杆、高手...