1
#include<bits/stdc++.h>
using namespace std;
int n,arr[1010];
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,arr+n);
int ans=arr[n/2],small=0,big=0;
for(int i=0;i<n;i++){
if(arr[i]<ans) small++;
if(arr[i]>ans) big++;
}
if(small==big){
cout<<ans<<endl;
}
else{
cout<<-1<<endl;
}
}
2
#include<bits/stdc++.h>
using namespace std;
int s,t;
int main(){
cin>>t;
if(t<=3500){
s=t;
}
else if(t<=4955){
s=(t-3500)/0.97+3500;
}
else if(t<=7655){
s=(t-4955)/0.9+5000;
}
else if(t<=11255){
s=(t-7655)/0.8+8000;
}
else if(t<=30755){
s=(t-11255)/0.75+12500;
}
else if(t<=44755){
s=(t-30755)/0.7+38500;
}
else if(t<=61005){
s=(t-44755)/0.65+58500;
}
else{
s=(t-61005)/0.55+83500;
}
cout<<s<<endl;
}
3
#include<bits/stdc++.h>
using namespace std;
map<string,pair<int,int> > permMap; //(permission_id,type)
map<string,int> roleMap;// (role_id, )
map<string,int> userMap;// (user_id, )
string perm,role,user;
int Stoi(string s){
return atoi(s.c_str());
}
int main(){
//Construct permission to id mapping
int P;
cin>>P;
for(int p=0;p<P;p++){
cin>>perm;
int pos=perm.find(':');
int type=pos==-1;
permMap[perm.substr(0,pos)]={p,type};
}
int R;
cin>>R;
vector<vector<int> > rolePerms(R,vector<int>(P,-1));
for(int r=0;r<R;r++){
//construct role to id mapping
cin>>role;
roleMap[role]=r;
//construct role permission table
int N;
cin>>N;
for(int n=0;n<N;n++){
cin>>perm;
int pos=perm.find(':');
int permId=permMap[perm.substr(0,pos)].first;
int level=1;
if(pos<perm.size()) level=Stoi(perm.substr(pos+1,-1));
rolePerms[r][permId]=max(rolePerms[r][permId],level);
}
}
int U;
cin>>U;
vector<vector<int> > userPerms(U,vector<int>(P,-1));
for(int u=0;u<U;u++){
//construct user to id mapping
cin>>user;
userMap[user]=u;
//construct user permission table
int N;
cin>>N;
for(int n=0;n<N;n++){
cin>>role;
int roleId=roleMap[role];
for(int p=0;p<P;p++){
userPerms[u][p]=max(userPerms[u][p],rolePerms[roleId][p]);
}
}
}
int Q;
cin>>Q;
for(int q=0;q<Q;q++){
cin>>user;
cin>>perm;
int pos=perm.find(':');
string permName=perm.substr(0,pos);
if(userMap.find(user)==userMap.end()||permMap.find(permName)==permMap.end()){
cout<<"false"<<endl;continue;
}
int userId=userMap[user];
int permId=permMap[permName].first;
int level=Stoi(perm.substr(pos+1,-1));
if(userPerms[userId][permId]==-1){
cout<<"false"<<endl;continue;
}
if(permMap[permName].second==1){
if(userPerms[userId][permId]==1){
cout<<"true"<<endl;
}
else{
cout<<"false"<<endl;
}
}
else{
if(pos==-1){
cout<<userPerms[userId][permId]<<endl;
}
else if(level<=userPerms[userId][permId]){
cout<<"true"<<endl;
}
else{
cout<<"false"<<endl;
}
}
}
}
4(区间DP)
石子合并问题
- 由于字典序,输入的数在顺序上不能改变
- 对于每次合并,前半部分最高位补0,后半部分最高位补1,每次合并的代价为区间内字母出现的频数
状态转移方程:
d
p
[
i
]
[
j
]
dp[i][j]
dp[i][j]表示区间
[
i
,
j
]
[i,j]
[i,j]的长度最小值
d
p
[
i
]
[
j
]
=
m
i
n
{
d
p
[
i
]
[
k
]
+
d
p
[
k
+
1
]
[
j
]
+
s
u
m
[
j
]
−
s
u
m
[
i
−
1
]
}
,
k
∈
[
i
,
j
−
1
]
dp[i][j]=min\{dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1] \},k\in[i,j-1]
dp[i][j]=min{dp[i][k]+dp[k+1][j]+sum[j]−sum[i−1]},k∈[i,j−1]
#include<bits/stdc++.h>
using namespace std;
const int INF=1<<30;
int dp[1010][1010],a[1010],sum[1010];
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
for(int len=2;len<=n;len++){
for(int i=1;i<=n-len+1;i++){
int l=i,r=i+len-1;
dp[l][r]=INF;
for(int k=l;k<r;k++){
dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]+sum[r]-sum[i-1]);
}
}
}
cout<<dp[1][n]<<endl;
}