Edu 87
A. Alarm Clock
\(a, b, c, d\left(1 \leq a, b, c, d \leq 10^{9}\right)\),需要睡够\(a\)分钟,第一次睡\(b\)分钟,然后闹钟在\(c\)分钟后响,\(d\)分钟后再次入睡
判断一下能不能完成,输出花费的时间
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fi first
#define se second
#define ll long long
#define pb push_back
typedef pair<long long,long long> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef double db;
const ll mod=1e9+7;
ll powmod(ll a,ll b,ll p){ll res=1;a%=p;while(b){if(b&1) res=res*a%p;a=a*a%p;b>>=1;}return res;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
int _;
int main(){
for(scanf("%d",&_);_;_--){
ll a,b,c,d;
ll ans=0;
cin>>a>>b>>c>>d;
if(b>=a) { cout<<b<<endl;continue;}
if(d>=c) {puts("-1");continue;}
a-=b;
ans+=b;
ans+= (a/(c-d) * c);
if(a%(c-d) != 0) ans+=c;
cout<<ans<<endl;
}
}
B. Ternary String
一个字符串\(s(1 \leq|s| \leq 200000)\),由1,2,3构成,找到包含这三个的最短连续子串
遍历一下,记录三个字符的最后位置每次更新一下答案
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fi first
#define se second
#define ll long long
#define pb push_back
typedef pair<long long,long long> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef double db;
const ll mod=1e9+7;
ll powmod(ll a,ll b,ll p){ll res=1;a%=p;while(b){if(b&1) res=res*a%p;a=a*a%p;b>>=1;}return res;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
int _;
int main(){
for(scanf("%d",&_);_;_--){
string s;
cin>>s;
map<char,bool>rec;
rep(i,0,s.size()) {rec[s[i]]=true;}
bool ok=1;
if(rec['1'] && rec['2'] && rec['3']) ok=1;
else ok=0;
if(!ok){puts("0");continue;}
int ans=1e10;
int one=0,two=0,three=0;
rep(i,0,s.size()){
if(s[i]=='1') one=max(one,i+1);
else if(s[i]=='2') two=max(two,i+1);
else three=max(three,i+1);
int mi=min(min(one,two),three);
int mx=max(max(one,two),three);
if(mi!=0 && mx != 0)
ans=min(ans,mx-mi+1);
}
cout<<ans<<endl;
}
}
C1. Simple Polygon Embedding
\(n(2 \leq n \leq 200)\)条边的正多变形,\(n\)为偶数,求能将图形嵌入的最小正方形的面积
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fi first
#define se second
#define ll long long
#define pb push_back
typedef pair<long long,long long> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef double db;
const ll mod=1e9+7;
const double pi = 3.1415926;
ll powmod(ll a,ll b,ll p){ll res=1;a%=p;while(b){if(b&1) res=res*a%p;a=a*a%p;b>>=1;}return res;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
int _;
int main(){
for(scanf("%d",&_);_;_--){
double n;
cin>>n;
double a=90/n;
double l=0.5/tan(a * pi/180);
printf("%.7lf\n",l*2);
}
}
\(n(2 \leq n \leq 200)\)条边的正多变形,\(n\)为奇数,求能将图形嵌入的最小正方形的面积
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fi first
#define se second
#define ll long long
#define pb push_back
typedef pair<long long,long long> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef double db;
const ll mod=1e9+7;
const double pi = 3.1415926;
ll powmod(ll a,ll b,ll p){ll res=1;a%=p;while(b){if(b&1) res=res*a%p;a=a*a%p;b>>=1;}return res;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
int _;
int main(){
int t;
cin>>t;
while(t--){
double n;
cin>>n;
double a= 90/n * pi /180;
double l = 0.5 /sin (a);
double c = cos (a/2);
printf("%.7lf\n",l*c*2);
}
}
D. Multiset
\(n \text { and } q\left(1 \leq n, q \leq 10^{6}\right)\),初始长度为\(n\)的序列,\(a_{1}, a_{2}, \ldots, a_{n}\left(1 \leq a_{1} \leq a_{2} \leq \cdots \leq a_{n} \leq n\right)\),
\(q\)个询问每个询问一个\(k\),如果\(1 \leq k_{i} \leq n\),在序列中插入\(k_{i}\),\(k_{i}<0\)则删除第\(k_{i}\)个元素
在值域上建立树状数组,树状数组统计每个数存在的个数,增加直接在对应值增加即可,删除二分前缀和为\(k\)的即第\(k\)大的数
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fi first
#define se second
#define ll long long
#define pb push_back
typedef pair<long long,long long> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef double db;
const ll mod=1e9+7;
ll powmod(ll a,ll b,ll p){ll res=1;a%=p;while(b){if(b&1) res=res*a%p;a=a*a%p;b>>=1;}return res;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
int _;
const int N=1e6+10;
int c[N],a[N];
int lowbit(int x){return x&(-x);}
void add(int x,int y){
for(int i=x;i<N;i+=lowbit(i))
c[i]+=y;
}
int ask(int x){
int res=0;
for(int i=x;i;i-=lowbit(i))
res+=c[i];
return res;
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
rep(i,1,n+1) {
int x;
scanf("%d",&x);
add(x,1);
}
while(q--){
int k;
scanf("%d",&k);
if(k>0){
add(k,1);
}
else{
k=-k;
int l=1,r=N;
while(l<r){
int mid=l+r>>1;
if(ask(mid) >= k)
r=mid;
else l=mid+1;
}
add(l,-1);
}
}
if(ask(N) == 0) puts("0");
else{
int l=1,r=N;
while(l<r){
int mid=l+r>>1;
if(ask(mid) !=0)
r=mid;
else l=mid+1;
}
printf("%d\n",l);
}
}
Code