A. Dead Pixel
用坏点两边较长的长乘以整个宽
用坏点两边较长的宽乘以整个长
取max
#include <bits/stdc++.h>
using namespace std;
int t,a,b,x,y;
int main(){
cin>>t;
while(t--){
cin>>a>>b>>x>>y;
int chang=max(a-x-1,x);
int kuan=max(b-y-1,y);
int tt=max(chang*b,kuan*a);
cout<<tt<<endl;
}
return 0;
}
B. Homecoming
随便写了下,凑了凑数据就过了。。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
int t,a,b,p;
string s;
int main(){
cin>>t;
while(t--){
cin>>a>>b>>p>>s;
char tt;
int sum=0;
int changdu=s.length();
for(int i=changdu-2;i>=0;i--){
char tt=s[i];
if(tt=='B')sum+=b;
else sum+=a;
if(sum>p){
int sss=min(i+2,changdu);
cout<<sss<<endl;
break;
}
bool flag=true;
for(int j=i-1;j>=0;j--){
if(s[j]!=tt){
flag=false;
i=j+1;
break;
}
}
if(flag==true||i==0){
cout<<1<<endl;
break;
}
}
}
return 0;
}
C. Restoring Permutation
每个数字后面找能放的大于等于它的最小的数
#include <bits/stdc++.h>
using namespace std;
int t,n;
string s;
int a[105];
int b[205];
int c[205];
int main(){
cin>>t;
while(t--){
memset(c,0,sizeof c);
cin>>n;
bool flag;
bool aflag=false;
for(int i=1;i<=n;i++){
cin>>a[i];
c[a[i]]=1;
}
for(int i=1;i<=n;i++){
b[i*2-1]=a[i];
flag=true;
for(int j=a[i]+1;j<=n*2;j++){
if(c[j]==0){
flag=false;
b[i*2]=j;
c[j]=1;
break;
}
}
if(flag==true){
aflag=true;
break;
}
}
if(aflag==true)cout<<-1<<endl;
else{
for(int i=1;i<=n*2;i++){
if(i!=n*2)cout<<b[i]<<' ';
else cout<<b[i];
}
cout<<endl;
}
}
return 0;
}
D. Recommendations
用优先队列找出相同first里最大的second
时间加上这些first里除了最大的second外的所有second
比如n类别有4个,输入了一个n+2类别,那在一轮操作中,我把最大的元素留在原地,把剩余的3个元素都+1,再把这个3个元素的最大元素留在n+1,把剩余的2个元素都+1变成n+2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
int n;
struct node{
int f,s;
}a[200010];
priority_queue<int>h;
int cmp(node a,node b){
if(a.f==b.f)return a.s<b.s;
return a.f<b.f;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i].f;
for(int i=1;i<=n;i++)cin>>a[i].s;
sort(a+1,a+n+1,cmp);
int nowf=-1;
ll sum=0,ans=0;//sum储存数值之和
for(int i=1;i<=n;i++){
while(nowf<a[i].f){//如果输入了一个更大的类别,把队列中的类别更新到该类别。
if(h.empty()){//如果之前是空的或者队列中的元素都被操作完,确定新的类别
nowf=a[i].f;
break;
}
nowf++;//每操作一遍,类别+1
if(!h.empty()){//如果没操作完
sum-=h.top();//把最大的数值留在该类别,剩余的类别值都+1
h.pop();//弹出最大的
}
ans+=sum;
}
h.push(a[i].s);//把同一类别的压进队列
sum+=a[i].s;//统计当前队列数值之和
}
while(!h.empty()){//最后对队列剩余元素进行一轮操作
nowf++;
if(!h.empty()){
sum-=h.top();
h.pop();
}
ans+=sum;
}
cout<<ans;
return 0;
}