1.中间数
o(n平方)模拟即可,可以预排序o(n)
#include<iostream>
#include<vector>
using namespace std;
#define N 1010
int a[N];
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
int l=0,b=0;
for(int j=0;j<n;j++){
if(a[j]>a[i]) b++;
if(a[j]<a[i]) l++;
}
if(b==l) {cout<<a[i];return 0;}
}
cout<<-1;
return 0;
}
2.工资计算
枚举原工资,若税后工资+税额=原工资,则找到答案
#include<iostream>
#include<vector>
using namespace std;
#define N 100000
int n;
int a[7]={1500,3000,4500,26000,20000,25000,100000};
int b[7]={3,10,20,25,30,35,45};
int get(int t){
int tt=t;
int res=0;
t-=3500;
for(int i=0;t>0&&i<7;i++){
if(t>=a[i]){
res+=a[i]/100*b[i];
t-=a[i];
}else{
res+=t/100*b[i];
t=0;
}
}
return res;
}
int main(){
cin>>n;
int i=0;
while(1){
if(get(i)+n==i) {cout<<i<<endl; return 0;}
i+=100;
}
return 0;
}
3.权限查询
一直爆WA,死活都只有50分,哭了
后来发现是原来具有高等级权限后就自动具有了低等级权限
#include<iostream>
#include<map>
using namespace std;
#define type map<string,int> //-1代表拥有该权限,0-9代表权限值
map<string,type > users,roles;
void add_roles(type &user,type &role){
for(type::iterator it=role.begin();it!=role.end();it++){
if(user.find(it->first)!=user.end()){
if(it->second==-1)
user[it->first]=-1;
else
user[it->first]=max(user[it->first],it->second);
}else user[it->first]=it->second;
}
}
int k;
int main(){
cin>>k;
string s;
while(k--) cin>>s;
cin>>k;
while(k--) {
string name;
cin>>name;
type role;
int n;
cin>>n;
while(n--){
string p;
cin>>p;
if(p.find(":")!=-1){
role[p.substr(0,p.length()-2)]=max(role[p.substr(0,p.length()-2)],p[p.length()-1]-'0');
}else{
role[p]=-1;
}
}
add_roles(roles[name],role);
}
cin>>k;
while(k--) {
string name;
cin>>name;
type user;
int n;
cin>>n;
while(n--){
string p;
cin>>p;
add_roles(user,roles[p]);
}
users[name]=user;
}
cin>>k;
while(k--) {
string u,p;
cin>>u>>p;
if(users.find(u)==users.end()){
cout<<"false"<<endl;
continue;
}
type &user=users[u];
if(p.find(":")!=-1){
if(user.find(p.substr(0,p.length()-2))==user.end()){
cout<<"false"<<endl;
continue ;
}
int priv=user[p.substr(0,p.length()-2)];
if(priv!=p[p.length()-1]-'0') {
cout<<"false"<<endl;
}else{
cout<<"true"<<endl;
}
}else {
if(user.find(p)==user.end()){
cout<<"false"<<endl;
continue ;
}
if(user[p]==-1)
cout<<"true"<<endl;
else cout<<user[p]<<endl;
}
}
}
4.压缩编码
- 设f[i][j]为将区间 i到j连接好后的最优树,f[i][j]=min(f[i][k]+f[k+1][j]+s[j]-s[i-1])
- s为前缀和
- 先枚举区间长度len
- 再枚举起点i
- 得到终点 j=i+len-1 (闭区间)
- 初始化f[i][j]=INF
- 枚举中间点k,f[i][j]=min(f[i][k]+f[k+1][j]+s[j]-s[i-1])
- over!(y总nb!)
#include<iostream>
#include<cstring>
using namespace std;
#define N 1010
int s[N],f[N][N];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i];
s[i]+=s[i-1];
}
for(int len=2;len<=n;len++){
for(int i=1;i+len-1<=n;i++){
int j=i+len-1;
f[i][j]=0x3f3f3f3f;
for(int k=i;k<=j;k++){
f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]+s[j]-s[i-1]);
}
}
}
cout<<f[1][n];
return 0;
}