A. Prepend and Append

用ans记录n的值,然后双指针从前后判断是否一个为0一个为1,是的话则ans-2,否则退出循环即可.

#include<bits/stdc++.h>

using namespace std;
int t,n;
char a[2010];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while(t--)
{
cin >> n;
cin >> a;
int ans = n;
for(int i=0,j=n-1;i<=n/2,j>=n/2;i++,j--)
{
if(a[i] == '0' && a[j] == '1' || a[i] == '1' && a[j] == '0')
ans -= 2;
else
break;
}
cout << ans << '\n';
} return 0;
}

B. Distinct Split

数组s1记录子串a中出现的字符个数,数组s2记录子串b中出现的字符个数,然后将两个子串中出现的字符次数记录到数组s1中,遍历字符串,每次将si对应字符的数量从s1中减去1,然后数组s2中加1,即用i将字符串分成两个子串,然后枚举1-26个字母,取每次f(a) + f(b) 的最大值即可.

#include<bits/stdc++.h>
#define int long long using namespace std; const int N = 2e5+10;
int t,n;
string a;
int s1[N],s2[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while(t--)
{
int res = 0;
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
cin >> n;
cin >> a;
for(int i = 0; i < n; i ++)
s1[a[i] - 'a' ] ++ ;
for(int i = 0; i < n; i++)
{
s1[a[i] - 'a'] --;
s2[a[i] - 'a'] ++;
int ss1,ss2;
ss1 = ss2 = 0;
for(int j = 0; j < 26; j++)
{
if(s1[j]) ss1 ++;
if(s2[j]) ss2 ++;
}
res = max(res, ss1 + ss2);
}
cout << res << '\n'; }
return 0;
}

C. Negatives and Positives

题意是给定一个长度为n的数组a,每次可以将相邻的两个数取相反数,即a[i] = - a[i]; a[i+1] = - a[i+1];

问数组和的最大值,共有t次询问.

即,每次我们可以找两个相邻的数,判断这两个数的和是否为负数,是的话就取相反数.

#include<bits/stdc++.h>
#define int long long using namespace std; const int N = 2e5+10;
int n,t,a[N],ans;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while(t--)
{
ans = 0;
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> a[i];
ans += a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<n;i+=2)
{
if(a[i] + a[i+1] < 0)
{
ans -= (a[i] * 2);//因为之前ans+的时候是加的负数,所以这里取反加回来要乘2
ans -= (a[i+1] * 2);
}
}
cout << ans << "\n";
}
return 0;
}

D. Range Update Point Query

对于1e9以内的数,数位和最大是9*9=81,最多变成三次数位和就会变成一位数,所以我们记录每个数进行操作1的次数,然后对于查询的数进行对应次数的修改即可,所以变成了区间修改+单点查询问题

#include<bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f3f using namespace std; const int N = 2e5+10; int n,q,m = 1e5,a[N],sum[N],ans;
int lowbit(int x)
{
return x & (-x);
}
void updata(int x,int y)
{
for(int i = x; i <= n; i += lowbit(i))
sum[i] += y;
}
int shu(int x)
{
int res = 0;
while(x)
{
res += x % 10;
x /= 10;
}
return res;
}
int qianzui(int x)
{
int res = 0;
for(int i = x;i ;i -= lowbit(i))
res += sum[i];
return res;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--)
{
cin >> n >> q;
for(int i = 1;i <= n;i ++)
{
cin >> a[i];
sum[i] = 0;
}
for(int i = 1;i <= q;i ++)
{
int m;
cin >> m;
if(m == 2)
{
int l;
cin >> l;
int ans = a[l];
if(a[l] >= 10)
{
for(int i = 1;i <= qianzui(l);i ++)
{
ans = shu(ans);
if(ans < 10)
break;
}
}
cout << ans << '\n';
}
else
{
int l ,r;
cin >> l >> r;
updata(l,1);
updata(r + 1, -1);
}
}
}
return 0;
}

E. Dima and Guards

题意:

有一个正整数n和 4 组正整数 a,b,c,d求是否有一组数据满足x + y = n  , 其中x >= min(a, b), y >= min(c, d), .如果存在这样的x和y,则输出这一组的x 和 y 即(n - x),否则输出-1;

对于每一个 x 和 y,都保证要 min(a,b) <= x ; min(c,d) <= y, min(a,b) <= x; min(c,d) <= y。所以我们可以对 x 和 y 都取最小值,判断是否成立,如果成立,那么就输出当前的序号、最小的 x 和 n−x,最后结束程序即可。不然就输出-1,应为这已经是最小值,两数相加的和只会更大,而不会变小。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a,b,c,d;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i=1;i<=4;i++)
{
cin >> a >> b >> c >> d;
int x = min(a, b), y = min(c, d);
if(x + y <= n)
{
cout << i << ' ' << x << ' ' << n - x << '\n';
return 0;
}
}
cout << -1 << '\n';
return 0;
}

F. Dima and To-do List

题意:

emm可以理解为有n个任务,每个任务需要花费ai精力,你需要做n/k个任务, 然后你只能做间隔k个的任务,问消耗最少精力时,第一个任务的位置.

从前k个任务开始循环, 记录每次做完n/k个任务的总精力, 如果找到最小的消耗总精力就记录 i ;

值得注意的是minn比较时要开得够大不然过不了全部数据()

#include<bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+10;
int n,k,a[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
scanf("%lld%lld",&n,&k);
for(int i=1;i<=n;i++)
scanf("%lld",a+i);
int ans , minn = INF , sum = 0;
for(int i=1;i<=k;i++)
{
sum = 0;
for(int j=i;j<=n;j+=k)
sum += a[j];
if(sum < minn)
{
minn = sum ;
ans = i;
}
}
printf("%lld\n",ans);
return 0;
}

G. Dima and Salad

这是一道背包题, 题中给的公式可转化成 a[i] - k * b[i] = 0; 即将a[i] 当作是价值, a[i] - k * b[i] 当作是重量, 0为容量, 因为a[i] - k * b[i] 有正有负, 所以可以用两个dp数组来记录, p[i] 表示质量和为i时最大值,q[i]表示质量和为 -i 时的最大值. 最后取p[i] + q[i] 中最大值即可.

#include<bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+10;
int n,k,m = 1e5,a[N],b[N],c[N],ans;
int p[N], q[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i=1;i<=m;i++)
p[i] = q[i] = -1e9;
for(int i=1;i<=n;i++)
cin >> a[i];
for(int i=1;i<=n;i++)
{
cin >> b[i];
c[i] = a[i] - k * b[i];
}
for(int i=1;i<=n;i++)
{
if(c[i]>=0)
{
for(int j = m; j>= c[i]; j--)
p[j] = max(p[j], p[j - c[i]] + a[i]);
}
else
{
for(int j = m; j>= -c[i]; j--)
q[j] = max(q[j], q[j + c[i]] + a[i]);
}
}
for(int i=0;i<=m;i++)
ans = max(ans, p[i] + q[i]);
if(!ans)
puts("-1");
else
cout << ans << '\n';
return 0;
}

H. Dima and Trap Graph

题意:

现有一个n个点m条边的无向图, 每条边有一个区间[l,r], 求从1到n路径组成的边中, 使得该边的区间范围最大.

即从1到n的路上找到ri - li + 1最大值, 开始时先对左边 l 降序排序, 按顺序依次选中所有区间内左端点小于等于 l 的边,直到1能够到达n为止,最后把所有的ri - li + 1取max即可.

#include<bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f3f using namespace std; const int N = 3e5+10; int n,m;
struct node{
int u,v;
int l,r;
}edge[N];
int p[2010];
int find(int x)
{
return x == p[x] ? p[x] : (p[x] = find(p[x])) ;
}
void add(int x, int y)
{
int fx = find(x), fy = find(y);
if(fx != fy) p[fx] = fy;
}
bool cmp(node x, node y)
{
return x.l < y.l;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 0;i < m;i ++)
cin >> edge[i].u >> edge[i].v >> edge[i].l >> edge[i].r;
sort(edge, edge + m, cmp);
int ans = 0;
for(int i = 0; i < m; i++)
{
for(int k = 1; k <= n;k ++)
p[k] = k;
for(int j = 0;j < m; j++)
{
if(edge[j].l > edge[i].r || edge[j].r <edge[i].r)
continue;
add(edge[j].u, edge[j].v);
if(find(1) == find(n))
{
ans = max(ans, edge[i].r - edge[j].l + 1);
break;
}
}
}
if(!ans)
puts("Nice work, Dima!");
else
cout << ans << '\n';
return 0;
}

SMU Spring 2023 Trial Contest Round 1的更多相关文章

  1. 2015 Astar Contest - Round 3 题解

    1001 数长方形 题目大意 平面内有N条平行于坐标轴的线段,且不会在端点处相交 问共形成多少个矩形 算法思路 枚举4条线段的全部组合.分别作为矩形四条边.推断是否合法 时间复杂度: O(N4) 代码 ...

  2. Contest Round #451 (Div. 2)F/Problemset 898F Restoring the Expression

    题意: 有一个a+b=c的等式,去掉两个符号,把三个数连在一起得到一个数 给出这个数,要求还原等式,length <= 1e6 三个数不能含有前导0,保证有解 解法: 铁头过题法,分类然后各种判 ...

  3. USACO 2023 January Contest, Bronze Problem 3. Moo Operations

    这道题目灰常简单,我们先从最简单的3个字符串开始 有以下几种情况: 可以看到,只有在中间是O的情况下才有可能变成MOO 辣么我们不妨在在s串中枚举这个中间 O 每枚举到一个就看看能不能用他的本身操作次 ...

  4. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Sending messages to non-windowed applications -- AllocateHWnd, DeallocateHWnd

    http://delphi.about.com/od/windowsshellapi/l/aa093003a.htm Page 1: How Delphi dispatches messages in ...

  6. Codeforces 240 F. TorCoder

    F. TorCoder time limit per test 3 seconds memory limit per test 256 megabytes input input.txt output ...

  7. cf499B-Lecture 【map】

    http://codeforces.com/problemset/problem/499/B B. Lecture     You have a new professor of graph theo ...

  8. Codeforces 240F. TorCoder 线段树

    线段树统计和维护某一区间内的字母个数.. . . F. TorCoder time limit per test 3 seconds memory limit per test 256 megabyt ...

  9. 物联网学生科协第三届H-star现场编程比赛

    问题 A: 剪纸片 时间限制: 1 Sec 内存限制: 128 MB 题目描写叙述 这是一道简单的题目,假如你身边有一张纸.一把剪刀.在H-star的比赛现场,你会这么做: 1. 将这张纸剪成两片(平 ...

  10. [cf contest 893(edu round 33)] F - Subtree Minimum Query

    [cf contest 893(edu round 33)] F - Subtree Minimum Query time limit per test 6 seconds memory limit ...

随机推荐

  1. 解析下载blob视频

    前言 浏览器中有些视频是通过blob:https://baike.baidu.com/bf834217-9442-4c98-9ef6-0bd5f3408a4e的形式给出的.blob后面的网址不能直接访 ...

  2. 2019 香港区域赛 BDEG 题解

    B.Binary Tree 题意:给你一棵二叉树.有两个游戏者,回合制,他们每次可以删去这棵二叉树中的一棵满二叉树.求最后谁赢. 解法:每一棵满二叉树有奇数个节点,那么每次游戏者只能删去奇数个节点,所 ...

  3. SpringMVC原理(1)-文件上传请求

    我们文件上传接口只需要在方法参数上写MultipartFile类,mvc就可以帮我们把上传的文件封装为这个类的对 象供我们非常方便的操作,那它是怎么做的呢?我们一起来看看 我们发的请求默认都是由Dis ...

  4. 【资料分享】全志科技T507工业核心板硬件说明书(下)

    目    录 3 电气特性 3.1 工作环境 3.2 功耗测试 3.3 热成像图 4 机械尺寸 5 底板设计注意事项 5.1 最小系统设计 5.1.1 电源设计说明 5.1.2 系统启动配置 5.1. ...

  5. 韦东山freeRTOS系列教程之【第八章】事件组(event group)

    目录 系列教程总目录 概述 8.1 事件组概念与操作 8.1.1 事件组的概念 8.1.2 事件组的操作 8.2 事件组函数 8.2.1 创建 8.2.2 删除 8.2.3 设置事件 8.2.4 等待 ...

  6. git使用教程及常用命令

    1.初次本地git连接远程仓库,并提交代码到远程仓库 第一步 码云上创建仓库 第二步 在需要上传的项目处右键-->git bush here 第三步  初始化本地git库 git init 设定 ...

  7. Task异步多线程

    不废话,直接贴上要实现的效果和代码... [1]直接使用Lambda表达式是实现多线程: using System; using System.Collections.Generic; using S ...

  8. 写了一个json小工具,希望大家体验(Mac平台)

    用rust写了一个json小工具"JSON PICKER",欢迎大家试用: https://github.com/davelet/json-picker/releases/tag/ ...

  9. tp 模型hasOne、hasMany、belongsTo详解

    首先,这3个的大致中文意思:hasOne:有一个,加上主谓语应该是 ,A 有一个 BhasMany:有很多,A 有很多 BbelongsTo:属于, A 属于 B这里我们准备3张表来理解他们的关系:u ...

  10. suffix-icon属性隐藏el-select下拉框右边的下拉图标

    <el-form-item label="入库类型" prop="mt_type"> <el-select v-model="for ...