题意:这题没什么要说的
思路:就是要看清题意
#include
using namespace std;
#define pi 3.1415926
#define X first
#define Y second
#define Ysanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define endl "\n"
#define int long long
#define pb push_back
typedef pair PII;
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
const int N = 1e6 + 6000, M = 1010, inf = 0x3f3f3f3f, mod = 1e9 + 7;
string s;
void solve()
{
cin >> s;
cout << s << endl;
}
signed main()
{
Ysanqian;
int T;
T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}
题意:就是现在氧气少年想让文本输入框中恰好有 n 个 1,请求出他需要做的最少的操作次数。
思路:枚举就可以了看n在那个范围内,操作一次得到2个,操作2次可以得到3到4个,操作3次可以得到5到8个
#include
using namespace std;
#define pi 3.1415926
#define X first
#define Y second
#define Ysanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define endl "\n"
#define int long long
#define pb push_back
typedef pair PII;
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
const int N = 1e6 + 6000, M = 1010, inf = 0x3f3f3f3f, mod = 1e9 + 7;
int n;
int m;
void solve()
{
cin >> n;
if (n == 1)
cout << 0 << endl;
else
for (int i = 0; i <= 30; i++)
{
if (n <= pow(2, i) && n > pow(2, i - 1))
{
cout << i << endl;
break;
}
}
}
signed main()
{
Ysanqian;
int T;
// T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
思路:
由 a mod b = m 得 a = k*b + m,b > m , k ≥ 0
由 a+b=n 得 ( k+ 1 ) b + m = n ,( k + 1 ) ≥ 1
(k + 1)≥1,b>m,得(k + 1 ) b>m
则得 n>2m
所以当n<=2*m时无解
而当有解时我们发现,a = m, b = n - m,且n - m>m,即a mod b=m
所以这是一种可行的方式
#include
using namespace std;
#define pi 3.1415926
#define X first
#define Y second
#define Ysanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define endl "\n"
#define int long long
#define pb push_back
typedef pair PII;
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
const int N = 1e6 + 6000, M = 1010, inf = 0x3f3f3f3f, mod = 1e9 + 7;
int n, m;
void solve()
{
cin >> n >> m;
if (n <= 2 * m)
cout << -1 << endl;
else
cout << m << ' ' << n - m << endl;
}
signed main()
{
Ysanqian;
int T;
// T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
思路:我们想当前x <=1 或者a[i] <= 1时我们肯定选择加法,其余的全都是乘法,这样就会使x最大,然而在mod的情况下,我们并不知道x到底是确实0为0,还是为mod,所以我们用一个flag ,记录当前x未mod的值,不用管其是否爆了,只管它是否大于1,最后在取余
#include
using namespace std;
#define pi 3.1415926
#define X first
#define Y second
#define endl "\n"
#define int long long
#define pb push_back
typedef pair PII;
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define Ysanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e6 + 6000, M = 1010, inf = 0x3f3f3f3f, mod = 998244353;
int n, a[N];
int b[N];
void solve()
{
int n;
cin >> n;
int x = 0, flag = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (!flag || a[i] <= 1)
x += a[i];
else
x *= a[i];
if (x >= 2)
flag++;
x %= mod;
}
cout << x << "\n";
}
signed main()
{
Ysanqian;
int T;
// T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
思路:这题是后来补的(说实话这题我一看就觉得用栈写,但是不对)
这是官方题解,可能不是很好理解,我们举两个例子就行
若左括号的数量与右括号的数量不相等,那么无解。
如果有解,以下是其中一种最优策略:
对于每一个左括号:
对于每一个右括号:
记录当前左右括号的数量,和当前未配对右括号的数量即可。
(这图片我还调不正里,可以下载旋转一下)图片
有这两个例子我们可以贪心的得出,对于一个未匹配的有括号我们总是让其和后边出现的第一个左括号匹配,不管其是否匹配这样交换最少
对于左括号我们记录其次数,遇到右括号抵消一个,而当右括号积累时我就可以计算其对答案的贡献了
#include
using namespace std;
#define pi 3.1415926
#define X first
#define Y second
#define endl "\n"
#define int long long
#define pb push_back
typedef pair PII;
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define Ysanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e6 + 6000, M = 1010, inf = 0x3f3f3f3f, mod = 998244353;
int n;
string s;
void solve()
{
int res = 0, ans = 0, sum = 0;
cin >> n;
cin >> s;
for (int i = 0; i < n; i++)
if (s[i] == '(')
res++;
if (2 * res != n)
cout << -1 << endl;
else
{
for (int i = 0; i < n; i++)
{
if (s[i] == '(')
sum++;
else
{
sum--;
if (sum < 0)
ans -= sum;
}
}
cout << ans << endl;
}
}
signed main()
{
Ysanqian;
int T;
// T = 1;
cin >> T;
while (T--)
solve();
return 0;
}