codeforces747
这一场周赛打的非常菜了。
代码啊代码
题目想得太复杂了。
不够仔细
C [优先队列]
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e6+50;
struct node
{
int ser[105];
int ed;
int st;
int ct;
int id;
int ans=0;
void init()
{
for(int i=1;i<=100;i++) ser[i]=-1;
}
bool operator <(const node&oth)const
{
return oth.ed<ed;
}
};
int n,q;
int now=0;
int nser[105];
node a;
void sset(int x,int id)
{
int cnt=0;
int all=0;
for(int i=1;i<=n;i++)
{
if(cnt==x)
{
break;
}
if(nser[i]==-1)
{
cnt++;
all+=i;
a.ser[i]=nser[i]=1;
}
}
a.ans=all;
}
void undo(node x)
{
for(int i=1;i<=n;i++)
{
if(x.ser[i]==1)
{
nser[i]=-1;
now++;
}
}
}
int main()
{
scanf("%d%d",&n,&q);
memset(nser,-1,sizeof(nser));
priority_queue<node>pq;
now = n;
for(int i=1;i<=q;i++)
{
int t,k,d;
scanf("%d%d%d",&t,&k,&d);
node x;
x.init();
x.st = t; x.ct = k; x.ed=t+d-1;
//cout<<x.ed<<endl;
x.id=i;
a=x;
while(!pq.empty()&&pq.top().ed<a.st)
{
undo(pq.top());
pq.pop();
}
//cout<<now<<endl;
if(now>=a.ct)
{
sset(a.ct,a.id);
//for(int i=1;i<=n;i++) cout<<i<<" "<<a.ser[i]<<" "<<endl;
pq.push(a);
now-= a.ct;
}
else a.ans=-1;
printf("%d\n",a.ans);
}
return 0;
}
D【细节题,贪心】
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+50;
int n,k;
int a[maxn];
int st[maxn];
int ed[maxn];
vector<int>v;
int main()
{
scanf("%d%d",&n,&k);
v.clear();
int ct=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int j=1;
while(a[j]>=0)j++;
int p=0;
while(j<=n)
{
st[p] =j;
while(a[j]<0&&j<=n) j++;
ed[p] =j;
while(a[j]>=0&&j<=n){
j++;
}
p++;
}
if(p==0) printf("0\n");
else if(p==1)
{
// if(k>=n) printf("0\n");
ct=0;
for(int i=st[0];i<ed[0];i++)
{
ct++;
}
int m = n+1-ed[0];
if(ct+m<=k) printf("1\n");
else if(ct<=k)printf("2\n");
else printf("-1\n");
}
else
{
int day=0;
for(int i=0;i<p;i++) day+=ed[i]-st[i];
for(int i=1;i<p;i++)
{
// cout<<st[i-1]<<" "<<ed[i-1]<<endl;
// cout<<st[i]<<" "<<ed[i]<<endl;
v.push_back(st[i]-ed[i-1]);
}
int tmp = n+1-ed[p-1];
int ans=p*2;
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
if(day+v[i]<=k)
{
ans-=2;
day+=v[i];
}
}
if(tmp+day<=k) ans--;
if(day>k) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
E【细节题,dfs】
#include <bits/stdc++.h>
using namespace std;
vector<string>v;
vector<int>t;
const int maxn=1000010;
int getnum(string k)
{
int num=0;
for(int i=0;i<k.size();i++)
{
num=num*10;
num+=k[i]-'0';
}
return num;
}
void solve(string s)
{
string k;
for(int i=0;i<s.size();i++)
{
if(s[i]!=',') k+=s[i];
else
{
if(k[0]>='0'&&k[0]<='9') t.push_back(getnum(k));
else v.push_back(k);
k="";
}
}
if(k[0]>='0'&&k[0]<='9') t.push_back(k[0]-'0');
else v.push_back(k);
}
vector<string>ans[maxn];
int tp=0;
int allans=-1;
void dfs(int d)
{
allans=max(allans,d);
//cout<<v[tp]<<" "<<d<<endl;
ans[d].push_back(v[tp]);
int m = tp;
for(int x =1;x<=t[m];x++)
{
++tp;
dfs(d+1);
}
}
int main()
{
string s;
getline(cin,s);
solve(s);
for(int i=0;i<v.size();)
{
dfs(0);
i=++tp;
}
cout<<allans+1<<endl;
for(int i=0;i<=allans;i++)
{
for(int j=0;j<ans[i].size();j++)
{
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
return 0;
}