文章目录
题目
A. Minutes Before the New Year
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int t;
ll time=24*60;
scanf("%d",&t);
while(t--){
int h,m;
scanf("%d%d",&h,&m);
cout<<time-h*60-m<<endl;
}
}
B. Candies Division
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int t;
scanf("%d",&t);
while(t--){
ll n,k;
scanf("%lld%lld",&n,&k);
ll ans=n/k*k;
n%=k;
ans+=(n>(k/2))?k/2:n;
cout<<ans<<endl;
}
}
C. Friends and Gifts
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int ans[maxn];
vector<int>vec,vec2;
bool vis[maxn];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&ans[i]);
if(ans[i]==0){
vec.push_back(i);
}
else{
vis[ans[i]]=1;
}
}
for(int i=0;i<vec.size();i++){
if(!vis[vec[i]])
vec2.push_back(vec[i]);
}
if(vec2.size()==1){
for(int i=1;i<=n;i++){
if(ans[i]==0&&vec2[0]!=i){
ans[i]=vec2[0];
vis[vec2[0]]=1;
break;
}
}
}
else if(vec2.size()>1){
for(int i=0;i<vec2.size();i++){
ans[vec2[i]]=vec2[(i+1)%vec2.size()];
vis[vec2[i]]=1;
}
}
vec.clear();
for(int i=1;i<=n;i++){
if(ans[i]==0){
vec.push_back(i);
}
}
int j=0;
for(int i=1;i<=n;i++){
if(vis[i]==0)
ans[vec[j++]]=i;
}
for(int i=1;i<=n;i++){
printf("%d%c",ans[i],i==n?'\n':' ');
}
}
D. Christmas Trees
多起点BFS,将若干点都放在队列里即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int,int>mp;
queue<int>q;
int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
mp[x]=0;
q.push(x);
}
while(!q.empty()){
int f=q.front();
q.pop();
if(mp.count(f+1)==0){
mp[f+1]=mp[f]+1;
q.push(f+1);
if(mp.size()>=k+n)
break;
}
if(mp.count(f-1)==0){
mp[f-1]=mp[f]+1;
q.push(f-1);
if(mp.size()>=k+n)
break;
}
}
ll ans=0;
for(auto it=mp.begin();it!=mp.end();it++){
ans+=it->second;
}
cout<<ans<<endl;
int cur=0;
for(auto it=mp.begin();it!=mp.end();it++){
if(it->second!=0){
printf("%d%c",it->first,(++cur)==k?'\n':' ');
}
}
}
E. New Year Parties
分开讨论即可
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
const int maxn=2e5+10;
bool vis[maxn];
bool vis2[maxn];
vector<int>vec;
int a[maxn];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(!vis[a[i]]){
vis[a[i]]=1;
}
mp[a[i]]++;
}
int ans1=0,ans2=0;
for(int i=1;i<=n;i++){
if(vis[i]){
ans1++;
i+=2;
}
}
for(auto it=mp.begin();it!=mp.end();it++){
if(!vis2[it->first-1]){
ans2++;
it->second--;
vis2[it->first-1]=1;
}
if(it->second&&!vis2[it->first]){
ans2++;
vis2[it->first]=1;
it->second--;
}
if(it->second&&!vis2[it->first+1]){
ans2++;
vis2[it->first+1]=1;
it->second--;
}
}
cout<<ans1<<' '<<ans2<<endl;
}
F. DIY Garland
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
bool used[maxn];
int main(){
int n;
scanf("%d",&n);
int last=-1;
int high=n;
int x;
for(int i=1;i<n;i++){
scanf("%d",&x);
if(used[x]){
while(used[high])
high--;
cout<<last<<' '<<high<<endl;
used[high]=1;
high--;
}
else{
if(last==-1){
cout<<x<<endl;
}
else{
cout<<last<<' '<<x<<endl;
}
used[x]=1;
}
last=x;
}
for(int i=1;i<=n;i++){
if(!used[i]){
cout<<last<<' '<<i<<endl;
//break;
}
}
return 0;
}