链接
A:模拟
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 1e5+10;
const int mod = 1e9+7;
string s;
stack sta;
int main(){
getline(cin, s);
string str = "";
for(int i = 0; i < s.size(); i++){
if(s[i] == ' ') continue;
if(s[i] == '-') s[i] = '*';
else if(s[i] == '/') s[i] = '+';
else if(s[i] == '+') s[i] = '/';
else if(s[i] == '*') s[i] = '-';
str += s[i];
}
//cout << str << "\n";
int p = 0, op = 1, flag = 0;
for(int i = 0; i <= str.size(); i++){
if(str[i] <= '9' && str[i] >= '0' && i < str.size()){
p = p*10 + op * (str[i] - '0');
}
else{
if(flag == 1){
double q = sta.top();
sta.pop();
sta.push(p*q);
//cout << p << " " << q << "**\n";
}
else if(flag == 2){
if(p == 0){
printf("Cannot be divided by 0\n");
return 0;
}
double q = sta.top();
sta.pop();
sta.push(q/p);
//cout << q << " " << p << "\n";
}
else {
sta.push(p*1.0);
//cout << p << "\n";
}
if(i == str.size()) break;
p = 0; op = 1; flag = 0;
if(str[i] == '-') op = -1;
else if(str[i] == '*') flag = 1;
else if(str[i] == '/') flag = 2;
}
}
double ans = 0;
while(!sta.empty()){
double q = sta.top();
sta.pop();
ans += q;
}
printf("%.2lf\n", ans);
return 0;
}
B:
假设 m i n ( x a + y b , x b + y a ) = x a + y b min(x_a+y_b, x_b+y_a)=x_a+y_b min(xa+yb,xb+ya)=xa+yb, m i n ( x a + y c , x c + y a ) = x a + y c min(x_a+y_c, x_c+y_a)=x_a+y_c min(xa+yc,xc+ya)=xa+yc, 要使 x a + y b > x a + y c x_a+y_b>x_a+y_c xa+yb>xa+yc, 则 y b > y c y_b > y_c yb>yc
综上所述,当 x a + y b > x b + y a , x a + y + c > x b + y a , y b > y c x_a+y_b>x_b+y_a, x_a+y+c > x_b+y_a,y_b>y_c xa+yb>xb+ya,xa+y+c>xb+ya,yb>yc时,选a,b更优,所以按照x_a-y_a从大到小排序,对每个x选前面最大的y形成最优解,取max
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 2e5+10;
const int mod = 1e9+7;
int n;
struct node{
int a, b, pos;
bool operator < (const node& e) const {
return a-b > e.a - e.b;
}
}e[N];
int main(){
IO;
cin >> n;
for(int i = 0; i < n; i++){
cin >> e[i].a;
e[i].pos = i;
}
for(int i = 0; i < n; i++){
cin >> e[i].b;
}
sort(e, e+n);
node no = e[0];
int ans = 0;
for(int i = 1; i < n; i++){
ans = max(ans, e[i].a + no.b);
if(e[i].b > no.b){
no = e[i];
}
}
cout << ans <<"\n";
return 0;
}
D:水题
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 1e5+10;
const int mod = 1e9+7;
int t, x, h, m;
int main(){
cin >> t;
while(t--){
scanf("%d", &x);
scanf("%d:%d", &h, &m);
int tm = h*60 + m;
int y;
for(int i = 0; i <= 1e6; i++){
if(tm%10 == 7 || tm%60/10 == 7 || tm/60%10 == 7){
y = i;
break;
}
tm -= x;
if(tm < 0){
tm += 24*60;
}
}
tm %= (24*60);
h = tm/60, m = tm%60;
printf("%d %02d:%02d\n", y, h, m);
}
return 0;
}
L:
思路来自
其实感觉这题比较简单,但是过题人数好像不太多,我们先思考一下不带修改的括号匹配的过程,很明显我们可以定义一个cnt遇到 ( 就加一,遇到 ) 就减一,然后只要整个过程中cnt都大于等于零,并且最后cnt等于0就说明括号匹配是成功的。那么我们想按照题目修改后会怎么样,其实很容易发现,如果将当前位置的 ( 变为 ) 实际上就是将之后的cnt全部减二,同理如果 ) 变为 ( 其实就是将之后的cnt全部加二。那么我们很容易发现上面的过程和判断包含了区间修改,区间查询和单点查询,所以我们只要用线段树维护修改的过程,每次查询整体的最小是不是大于等于0,并且最后等于0就行了。
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 1e5+10;
const int mod = 1e9+7;
int n, m;
char s[N];
int cnt[N], Min[N*4], lazy[N*4];
void build(int o, int l, int r){
if(l == r){
Min[o] = cnt[l];
return ;
}
int mid = l+r>>1;
build(o<<1, l, mid);
build(o<<1|1, mid+1, r);
Min[o] = min(Min[o<<1], Min[o<<1|1]);
}
void push(int o){
if(lazy[o]){
lazy[o<<1] += lazy[o];
lazy[o<<1|1] += lazy[o];
Min[o<<1] += lazy[o];
Min[o<<1|1] += lazy[o];
lazy[o] = 0;
}
}
void up(int o, int l, int r, int ql, int qr, int v){
if(l >= ql && r <= qr){
Min[o] += v;
lazy[o] += v;
return ;
}
push(o);
int mid = l+r>>1;
if(mid >= ql) up(o<<1, l, mid, ql, qr, v);
if(mid < qr) up(o<<1|1, mid+1, r, ql, qr, v);
Min[o] = min(Min[o<<1], Min[o<<1|1]);
}
int qu(int o, int l, int r, int ql, int qr){
if(l >= ql && r <= qr){
return Min[o];
}
push(o);
int res = 1e9, mid = l+r>>1;
if(mid >= ql) res = min(res, qu(o<<1, l, mid, ql, qr));
if(mid < qr) res = min(res, qu(o<<1|1, mid+1, r, ql, qr));
Min[o] = min(Min[o<<1], Min[o<<1|1]);
return res;
}
int main(){
IO;
cin >> n >> m;
cin >> s+1;
cnt[0] = 0;
for(int i = 1; i <= n; i++){
if(s[i] == '('){
cnt[i] = cnt[i-1] + 1;
}
else cnt[i] = cnt[i-1] - 1;
}
build(1, 1, n);
int x;
while(m--){
cin >> x;
if(s[x] == '('){
s[x] = ')';
up(1, 1, n, x, n, -2);
}
else{
s[x] = '(';
up(1, 1, n, x, n, 2);
}
int ans1 = qu(1, 1, n, 1, n);
int ans2 = qu(1, 1, n, n, n);
if(ans1 >= 0 && ans2 == 0){
cout << "Yes\n";
}
else cout << "No\n";
}
return 0;
}
N:
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 1e6+10;
const int mod = 1e9+7;
int t, n;
ll f[N];
int main(){
IO;
f[1] = 2;
for(int i = 2; i <= 1e6; i++){
f[i] = (f[i-1]*3%mod + 2)%mod;
}
cin >> t;
while(t--){
cin >> n;
cout << f[n] << "\n";
}
return 0;
}