A. Integer Moves
题意:给你一个坐标 ( x , y ) (x, y) (x,y) ,问你从 ( 0 , 0 ) (0, 0) (0,0) 开始,每次走任意整数长的步,问最少需要几步可以到达
做法:特判 ( 0 , 0 ) (0, 0) (0,0),如果可以一步到位就是1,否则是2
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
const int mod = 1e9 + 7;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while(t--)
{
int x, y;
cin >> x >> y;
int s = sqrt(x * x + y * y);
if (x == 0 && y == 0) cout << 0 << '\n';
else if (s * s == x * x + y * y) cout << 1 << '\n';
else cout << 2 << '\n';
}
return 0;
}
B. XY Sequence
题意:让你构造一个序列,每个元素的值不能大于 B B B 且前后需要满足关系,问你该序列最大的和
做法:从0开始,如果元素小于 B B B ,则加上 x x x ,反之减去 y y y 即可
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
const int mod = 1e9 + 7;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--)
{
int n, B, x, y;
cin >> n >> B >> x >> y;
vector<LL> a(n + 1);
a[0] = 0;
for(int i = 1; i <= n; i ++)
{
if(a[i - 1] + x <= B) a[i] = a[i - 1] + x;
else a[i] = a[i-1] - y;
}
cout << accumulate(a.begin(), a.end(), 0ll) <<'\n';
}
return 0;
}
C. Bracket Sequence Deletion
看到括号序列就可以联想到栈,利用栈模拟下即可
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
const int mod = 1e9 + 7;
void solve()
{
int n;
string s;
stack<char> stk;
cin >> n >> s;
int res = 0, ans = 0;
bool flag = true;
for (int i = 0; i < n; i++)
{
if (stk.empty()) stk.push(s[i]);
else
{
if (stk.top() == '(') res++, stk.pop(), ans += 2;
else
{
for (int j = i; j < n; j++)
{
if (s[j] == ')')
{
res ++, ans += j - i + 2;
while (!stk.empty()) stk.pop();
i = j;
break;
}
}
}
}
}
cout << res << " " << n - ans << '\n';
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}