杭州电子科技大学2018多校测试第一场
A Maximum Multiple
打表规律题
#include<bits/stdc++.h>
using namespace std;
/*int work(int k)
{
int ans=0,pi=0,pj=0,pt=0;
for (int i=1; i<k; i++) if (k%i==0)
{
for (int j=1; j<(k-i); j++) if (k%j==0)
{
int t=k-i-j;
if (k%t==0)
{
if (i*j*t>ans)
{
ans=i*j*t;
pi=i; pj=j; pt=t;
}
}
}
}
cout<<k<<" "<<pi<<" "<<pj<<" "<<pt<<" "<<ans<<endl;
}
int main()
{
for (int i=1; i<=100; i++)
{
work(i);
}
}*/
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
long long x,ans;
scanf("%lld",&x);
if (x%3==0)
{
ans=(x/3)*(x/3)*(x/3);
}
else if (x%4==0)
{
ans=(x/2)*(x/4)*(x/4);
}
else ans=-1;
printf("%lld
",ans);
}
}
B Balanced Sequence
贪心
/*
第一反应感觉可以模拟贪心一下
处理完当前段必加答案之后感觉是dp
之后发现DP就会很麻烦
看题解发现贪心相关
*/
#include<bits/stdc++.h>
#define N 1000005
using namespace std;
char ch[N];
struct Node{
int left,right;
}a[N];
int n;
/*bool cmp(Node a,Node b)
{
if (a.left>=a.right && b.left<=b.right) return true;
else if (a.left>=a.right && b.left>=b.right) return a.right<b.right;
else if (a.left<=b.right && b.left>=b.right) return false;
else if (a.left<=b.right && b.left<=b.right) return a.left>b.left;
return true;
}*/
bool cmp(Node a, Node b){
if(a.left > a.right) //判断'('大于')'时,如果a,b都满足,则谁的')'小谁排前面,否则a排前面
return b.left > b.right? a.right<b.right:true;
return b.left <= b.right?a.left>b.left:false;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
int preleft=0,preright=0,ans=0;
for (int i=1; i<=n; i++)
{
scanf("%s",ch);
int len=strlen(ch);
int nowleft=0,nowright=0,nowans=0;
for (int i=0; i<len; i++)
{
if (ch[i]=='(') nowleft++;
else
{
if (nowleft>0) nowleft--,nowans++;
else nowright++;
}
}
ans+=nowans;
a[i].left=nowleft; a[i].right=nowright;
}
sort(a+1,a+n+1,cmp);
preleft=a[1].left; preright=a[1].right;
for (int i=2; i<=n; i++)
{
int tmp=min(preleft,a[i].right);
ans+=tmp;
preleft=preleft+a[i].left-tmp; preright=preright+a[i].right-tmp;
}
printf("%d
",ans*2);
}
return 0;
}
C Triangle Partition
排序
#include<bits/stdc++.h>
#define N 30005
using namespace std;
struct Node{
int x,y,pos;
}a[N];
int n;
bool cmp(Node A,Node B)
{
if (A.x!=B.x) return A.x<B.x;
else return A.y<B.y;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
n=n*3;
for (int i=1; i<=n; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].pos=i;
}
sort(a+1,a+n+1,cmp);
for (int i=1; i<n; i++) printf("%d ",a[i].pos);
printf("%d
",a[n].pos);
}
return 0;
}
D Distinct Values
去重+贪心+模拟
#include<bits/stdc++.h>
#define N 1000005
using namespace std;
struct Node {
int l,r;
}a[N],b[N];
int ans[N];
bool vis[N];
int n,m;
bool cmp(Node a, Node b)
{
if (a.l==b.l) return a.r>b.r; else return a.l<b.l;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (int i=1; i<=m; i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
}
sort(a+1,a+m+1,cmp);
int maxn=0,tot=0;
for (int i=1; i<=m; i++)
{
if (maxn>=a[i].r) continue;
else
{
b[++tot]=a[i];
// cout<<" ssssss "<<a[i].l<<" "<<a[i].r<<endl;
maxn=a[i].r;
}
}
//cout<<tot<<endl;
for (int i=1; i<=n; i++) ans[i]=1;
int pre=0;
for (int i=1; i<=tot; i++)
{
if (b[i].l>pre)
{
int tmp=0;
for (int j=b[i].l; j<=b[i].r; j++) ans[j]=++tmp;
pre=b[i].r;
}
else
{
for (int j=b[i].l; j<=pre; j++)
{
vis[ans[j]]=1;
}
int tmp=1;
for (int j=pre+1; j<=b[i].r; j++)
{
while (vis[tmp]!=0) tmp++;
ans[j]=tmp;
vis[tmp]=1;
}
for (int j=b[i].l; j<=b[i].r; j++)
{
vis[ans[j]]=0;
}
pre=b[i].r;
}
}
for (int i=1; i<n; i++) printf("%d ",ans[i]);
printf("%d
",ans[n]);
}
return 0;
}