OJ题解记录计划
容错声明:
①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况
②所有代码仅代表个人AC提交,不保证解法无误
③由于题目太多了,文件超过了博客园单条随笔的长度上限,而且每次编辑都十分卡顿,拆分成三个博客又比较麻烦,因此转存至<a href="http://amiya.vip/" target="_blank">点我</a>(穷人没钱配https,请不要随意攻击,万谢),本文停止更新,但不会删除,后续的AC代码统一放在<a href="http://amiya.vip/" target="_blank">别点我</a>(已经卡到写这句话花了几分钟)。
④上一条的链接中的代码是用c++生成的html,可能存在语言误判(不同语言的高亮逻辑不同)、图片消失(能找到的图片都已经补充插入),同时插件没有提供代码收起功能(有可能是我没找到)。使用本文浏览的优点:按题号找题很快(因为默认代码是收起的);使用外链浏览的优点是代码高亮,可随机选择。(写下这段话又花了几分钟,这也太卡了,再也不会编辑这篇了)。
E0001 A+B Problem
First AC: 2017-10-13 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main()
{
cin>>a>>b;
cout<<a+b;
return ;
}
E0002 一元多项式乘法
First AC: 2018-03-28 Latest Modification: 2018-03-28
#include<bits/stdc++.h>
using namespace std;
string s,t;
int ls,lt,tmp1,tmp2;
bool flag;
int a[],b[],r[];
int i,j,k;
int main()
{
while(cin>>s>>t){
if(s==""||t==""){cout<<endl;continue;}
ls=s.length();
lt=t.length();
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(r,,sizeof(r)); for(i=;i<ls;++i){
if(s[i]=='x'){
//计算系数
for(j=i-;j>=;--j)
if(s[j]=='+'||s[j]=='-')break;
flag=j>=&&s[j]=='-'? :;
for(tmp1=,k=j+;k<i;++k)tmp1=*tmp1+s[k]-'';
if(tmp1==)tmp1=;
if(flag)tmp1*=-;
//计算次数
if(s[i+]=='^')
for(tmp2=,j=i+;j<ls;++j){
if(s[j]=='+'||s[j]=='-')break;
else tmp2=*tmp2+s[j]-'';
}
else tmp2=;
a[tmp2]=tmp1;
}
}
//常数项
for(i=ls-;i>=;--i)
if(s[i]<''||s[i]>'')break;
tmp1=;
if(i==-||s[i]=='+')
for(j=i+;j<ls;++j)tmp1=*tmp1+s[j]-'';
else if(s[i]=='-'){
for(j=i+;j<ls;++j)tmp1=*tmp1+s[j]-'';
tmp1*=-;
}
a[]=tmp1; for(i=;i<lt;++i){
if(t[i]=='x'){
//计算系数
for(j=i-;j>=;--j)
if(t[j]=='+'||t[j]=='-')break;
flag=j>=&&t[j]=='-'? :;
for(tmp1=,k=j+;k<i;++k)tmp1=*tmp1+t[k]-'';
if(tmp1==)tmp1=;
if(flag)tmp1*=-;
//计算次数
if(t[i+]=='^')
for(tmp2=,j=i+;j<lt;++j){
if(t[j]=='+'||t[j]=='-')break;
else tmp2=*tmp2+t[j]-'';
}
else tmp2=;
b[tmp2]=tmp1;
}
}
//常数项
for(i=lt-;i>=;--i)
if(t[i]<''||t[i]>'')break;
tmp1=;
if(i==-||t[i]=='+')
for(j=i+;j<lt;++j)tmp1=*tmp1+t[j]-'';
else if(t[i]=='-'){
for(j=i+;j<lt;++j)tmp1=*tmp1+t[j]-'';
tmp1*=-;
}
b[]=tmp1; for(i=;i<;++i)
if(a[i])for(j=;j<;++j)
if(b[j])r[i+j]+=a[i]*b[j];
for(i=;;--i)if(r[i]){cout<<r[i];break;}
for(j=i-;j>=;--j)if(r[j])cout<<' '<<r[j];
cout<<endl;
}
return ;
}
E0003 玩具谜题
First AC: 2017-10-20 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
struct toy{
int dir;
string job;
}a[];
long n,m,d,num,cnt=;
long i;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(i=;i<=n;++i)cin>>a[i].dir>>a[i].job;
for(i=;i<=m;++i){
cin>>d>>num;
if(d==a[cnt].dir){
cnt-=num;
while(cnt<=)cnt+=n;
}
else{
cnt=(cnt+num)%n;
if(cnt==)cnt=n;
}
}
cout<<a[cnt].job;
return ;
}
E0004 Nth Largest Value
First AC: 2017-11-20 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
int T;
int a[];
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
for(j=;j>=;--j)cin>>a[j];
sort(a,a+);
cout<<i+<<' '<<a[]<<endl;
}
return ;
}
E0005 特殊的子集
First AC: 2018-04-27 Latest Modification: 2018-04-27
Note:
rst=1
n=(int)(input())
for i in range(2,n+2):
rst=rst*i
print(rst-1)
E0008 愚人节快乐
First AC: 2017-10-21 Latest Modification: 2018-02-28
Note: 打表
#include<bits/stdc++.h>
using namespace std;
int n,i;
int a[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
int main()
{
ios::sync_with_stdio(false);
cin>>n;
if(n>)cout<<"";
else for(i=;i<n;i++)cout<<a[i];
return ;
}
E0009 Alice and a Simple Problem
First AC: 2017-10-13 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
int m,n,i;
int main()
{
cin>>m>>n;
m*=n;
for(i=;i<=m;i++){
if(i%n==)cout<<i;
else if(i%n==)cout<<" "<<i<<endl;
else cout<<" "<<i;
}
return ;
}
E0010 Bob and a Binary Tree
First AC: 2018-01-08 Latest Modification: 2018-02-28
Note: 只需先确定F(n)在所在层的第几个,再每次模2返回上一层并保存左右方向即可
#include<bits/stdc++.h>
using namespace std;
int T;
long long n,tmp,up,down;
long long a[]={,};
bool b[],c[];
int i,j,k;
int main()
{
for(i=;i<;++i)a[i]=a[i-]<<;
cin>>T;
for(i=;i<=T;++i){
cin>>n;
tmp=n;
for(j=;;++j){
if(tmp>a[j])tmp-=a[j];
else break;
}
k=j;
while(j){
c[--j]=tmp&? :;
tmp=(tmp+)/;
}
up=down=;
for(j=;j<k;++j)if(c[j])down+=up;else up+=down;
cout<<"Case "<<i<<": "<<up<<'/'<<down<<endl;
}
return ;
}
E0011 Cacey and Calphabet
First AC: 2018-06-04 Latest Modification: 2018-06-04
Note: 转化为求26减输入字符串的LIS长度
#include<bits/stdc++.h>
using namespace std;
string s;
int ls,len;
int a[];
int i,j,k;
void bi(int l,int r,int n)
{
if(l+>=r){
if(n==a[l]||n==a[r])return;
int rst=r+;
while(rst&&n<a[rst-])--rst;
a[rst]=n;
len=max(len,rst);
return;
}
int mid=(l+r)/;
if(n>a[mid])bi(mid,r,n);
else if(n<a[mid])bi(l,mid,n);
else return;
}
int main()
{
memset(a,0x3f,sizeof(a));
len=;
cin>>s;
ls=s.length();
a[]=s[];
for(i=;i<ls;++i)bi(,len,s[i]);
cout<<-len<<endl;
return ;
}
E0015 Mr. Frog and Big News
First AC: 2018-01-16 Latest Modification: 2018-02-28
Note: 去括号展开,由排序不等式即得
#include<bits/stdc++.h>
using namespace std;
long n,i;
long a[],b[];
long long tmp,s;
int main()
{
while(cin>>n){
for(i=;i<n;++i)scanf("%ld",a+i);
for(i=;i<n;++i)scanf("%ld",b+i);
sort(a,a+n);
sort(b,b+n);
s=;
for(i=;i<n;++i)tmp=a[i]+b[n-i-],s+=tmp*tmp;
cout<<s<<endl;
}
return ;
}
E0017 Dr. Mouse and Elo Rating
First AC: 2018-05-18 Latest Modification: 2018-05-18
#include<bits/stdc++.h>
using namespace std;
double a[];
int Q,op,x,y;
int i,j,k;
void cal(int op,int x,int y)
{
double px,py,sx,sy,kx,ky;
px=/(+pow(,(a[y]-a[x])/));
py=/(+pow(,(a[x]-a[y])/));
if(op==)sx=sy=0.5;
else sx=,sy=;
if(a[x]<)kx=;
else if(a[x]<)kx=;
else kx=;
if(a[y]<)ky=;
else if(a[y]<)ky=;
else ky=;
a[x]+=kx*(sx-px);
a[y]+=ky*(sy-py);
}
int main()
{
for(i=;i<;++i)a[i]=;
cin>>Q;
while(Q--){
cin>>op;
if(op!=){
cin>>x>>y;
cal(op,x,y);
}
else{
cin>>x;
printf("%.4lf\n",a[x]);
}
}
return ;
}
E0018 Pokemon and Candies
First AC: 2018-11-05 Latest Modification: 2018-11-05
Note: 题意转化为操作一个初始金币数为n的堆,有2种方案取a返b和取c返d,求操作数最大值
可以不考虑同时取多次的情况,因为都可以转化为一次次取(后返金币不可能比先返更优)
不难理解对同一种取法数组合,改变两种方案执行的次序,对结果没有影响
不妨假设a>=c,能构造一个平凡解(能取c便取c),因为a>=c所以肯定是取不了a的
如果存在某个更优解,其中选过了一次取a返b的方案,这表明把取c的方案换为取a更优
那么尽可能把取c的方案都换为取a的必然更优
于是最优解只可能是如下之一:尽可能取c再考虑取a、尽可能取a再考虑取c
从而把两种策略都计算一遍,输出更优的即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,a,b,c,d,rst;
ll f(ll n,ll a,ll b,ll c,ll d)
{
ll ret=,cnt;
if(n>=a){
cnt=(n-a)/(a-b)+;
ret+=cnt;
n-=cnt*(a-b);
}
if(n>=c){
cnt=(n-c)/(c-d)+;
ret+=cnt;
n-=cnt*(c-d);
}
return ret;
}
int main()
{
while(cin>>n>>a>>b>>c>>d){
cout<<max(f(n,a,b,c,d),f(n,c,d,a,b))<<endl;
}
return ;
}
E0022 很大很大的数
First AC: 2018-04-22 Latest Modification: 2018-04-22
Note: 注意数0,1过程中前导零的去除
#include<bits/stdc++.h>
using namespace std;
string s;
int x,len,cnt,tmp;
long long rst;
int i,j;
int main()
{
cin>>s>>x;
len=s.length();
for(i=;i<len;++i)if(s[i]<'')++cnt;
if(cnt==x)cout<<s;
else if(cnt<x){
tmp=x-cnt;
for(i=len-;tmp;--i)if(s[i]>'')--tmp;
while(++i<len)s[i]='';
cout<<s;
}
else{
tmp=cnt;
for(i=len-;i>=;--i){
if(s[i]==''){
--tmp;
continue;
}
if(s[i]==''){
if(tmp<=x)break;
--tmp;
}
else if(s[i]==''){
if(++tmp<=x)break;
--tmp;
}
else if(tmp<=x)break;
}
--s[i];
for(j=i+;j<len;++j)s[j]='';
while(s[]=='')s=s.substr(,s.length());
len=s.length();
tmp=;
for(i=;i<len;++i)if(s[i]<'')++tmp;
tmp=x-tmp;
for(i=len-;tmp;--i){
if(s[i]>'')--tmp;
s[i]='';
} for(i=;i<len;++i)rst=*rst+s[i]-'';
cout<<rst;
}
return ;
}
E0023 寻找图书馆
First AC: 2018-05-08 Latest Modification: 2018-05-08
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,q,k,tmp,dis;
ll lib[];
ll i,j;
void find(ll lft,ll rgt,ll k)
{
if(lft==rgt){
if(lib[lft]==k)cout<<<<endl;
else if(lib[lft]<k){
if(lft==m-)cout<<k-lib[lft]<<endl;
else cout<<min(k-lib[lft],abs(k-lib[lft+]))<<endl;
}
else{
if(lft==)cout<<lib[lft]-k<<endl;
else cout<<min(lib[lft]-k,abs(k-lib[lft-]))<<endl;
}
return;
}
if(lft+==rgt){
if(lib[lft]==k||lib[rgt]==k)cout<<"0\n";
else if(lib[lft]<k&&lib[rgt]>k)
cout<<min(k-lib[lft],lib[rgt]-k)<<endl;
else if(lib[lft]<k){
if(rgt==m-)cout<<k-lib[rgt]<<endl;
else cout<<min(k-lib[rgt],abs(k-lib[rgt+]))<<endl;
}
else{
if(lft==)cout<<lib[lft]-k<<endl;
else cout<<min(lib[lft]-k,abs(k-lib[lft-]))<<endl;
}
return;
}
ll mid=(lft+rgt)/;
if(lib[mid]==k){
cout<<"0\n";
return;
}
if(lib[mid]<k)find(mid+,rgt,k);
else find(lft,mid-,k);
}
int main()
{
cin>>n>>m;
for(i=;i<m;++i)cin>>lib[i];
sort(lib,lib+m);
cin>>q;
while(q--){
cin>>k;
find(,m-,k);
}
return ;
}
E0024 相似的句子
First AC: 2017-12-13 Latest Modification: 2018-02-28
Note: 两组字符串处理排序后一一比对是否完全相等
#include<bits/stdc++.h>
using namespace std;
long T,m,n,flag;
long i,j,k;
struct data{
string s;
int len;
long num;
}a[],b[];
bool cmp(data a,data b)
{
return a.s<b.s;
}
int main()
{
ios::sync_with_stdio(false);
cin>>T;
for(i=;i<=T;++i){
cin>>m;
for(j=;j<m;++j){
cin>>a[j].s;
a[j].len=a[j].s.length(),a[j].num=j;
for(k=;k<a[j].len;++k)if(a[j].s[k]>'Z')a[j].s[k]-=;
}
cin>>n;
for(j=;j<n;++j){
cin>>b[j].s;
b[j].len=b[j].s.length(),b[j].num=j;
for(k=;k<b[j].len;++k)if(b[j].s[k]>'Z')b[j].s[k]-=;
}
cout<<"Case "<<i<<": ";
if(m-n){cout<<"NO\n";continue;}
sort(a,a+m,cmp),sort(b,b+n,cmp);
for(j=,flag=;j<m;++j)if(a[j].s!=b[j].s){cout<<"NO\n",flag=;break;}
if(flag)cout<<"YES\n";
}
return ;
}
E0025 天气猜猜看
First AC: 2018-04-15 Latest Modification: 2018-04-15
#include<bits/stdc++.h>
using namespace std;
double T;
int n;
string s[];
double t[];
bool jdg[];
int i;
int main()
{
cin>>T>>n;
for(i=;i<n;++i)cin>>s[i];
for(i=;i<n;++i){
if(s[i][]=='U'){
if(s[i-][]=='D')t[i-]=,jdg[i-]=;
}
else{
if(s[i-][]=='U')t[i-]=,jdg[i-]=;
}
}
if(!jdg[])t[]= ( s[][]=='U'? T+0.1:T-0.1 );
for(i=;i<n;++i){
if(!jdg[i]){
t[i]=( s[i][]=='U'? t[i-]+0.1:t[i-]-0.1 );
}
}
printf("%.1f",t[]);
for(i=;i<n;++i)printf(" %.1f",t[i]);
return ;
}
E0027 集合交并差
First AC: 2017-12-02 Latest Modification: 2018-02-28
Note: 利用集合互异性
#include<bits/stdc++.h>
using namespace std;
int a,b,c,cnt;
string x[];
struct data{
string s;
int flag,len;
}n[];
int i,j,k;
bool cmp(data m,data n)
{
if(m.flag-n.flag)return m.flag>n.flag;
if(m.flag){
if(m.len-n.len)return m.len>n.len;
for(i=;i<n.len;++i)if(m.s[i]!=n.s[i])return m.s[i]>n.s[i];
}
else{
if(m.len-n.len)return m.len<n.len;
for(i=;i<n.len;++i)if(m.s[i]!=n.s[i])return m.s[i]<n.s[i];
}
}
int main()
{
cin>>a>>b;
for(c=a+b;i<c;++i){
cin>>n[i].s;
n[i].len=n[i].s.length();
if(n[i].s[]=='-')n[i].flag=;
else n[i].flag=;
}
sort(n,n+a,cmp);
for(i=;i<a;++i){
for(cnt=,j=a;j<c;++j)if(n[i].s==n[j].s){++cnt;break;}
if(!cnt)x[k++]=n[i].s;
}
sort(n,n+c,cmp);
cout<<"{";
for(cnt=,i=;i<c;++i)if(n[i].s==n[i-].s){
if(cnt)cout<<','<<n[i].s;
else cout<<n[i].s,++cnt;
}
cout<<"}\n{";
if(c){
cout<<n[].s;
for(i=;i<c;++i)if(n[i].s!=n[i-].s)cout<<','<<n[i].s;
}
cout<<"}\n{";
if(k){
cout<<x[];
for(i=;i<k;++i)cout<<','<<x[i];
}
cout<<"}";
return ;
}
E0028 平均整数值
First AC: 2018-01-05 Latest Modification: 2018-01-05
#include<iostream>
using namespace std;
int a,b;
int main()
{
cin>>a>>b;
cout<<(a+b)/;
return ;
}
E0029 SD函数
First AC: 2018-01-05 Latest Modification: 2018-01-05
#include<iostream>
using namespace std;
int a,b;
int main()
{
cin>>a>>b;
cout<<a+b<<' '<<a-b;
return ;
}
E0030 数字字符个数
First AC: 2018-01-05 Latest Modification: 2018-01-05
#include<iostream>
using namespace std;
string s;
int ls,cnt,i;
int main()
{
cin>>s;
ls=s.length();
for(i=;i<ls;++i)if(s[i]>=''&&s[i]<='')++cnt;
cout<<cnt;
return ;
}
E0031 距离小于100!
First AC: 2018-01-05 Latest Modification: 2018-01-05
#include<iostream>
using namespace std;
long long s,i,j;
int n,cnt;
long long x,y,z;
int main()
{
cin>>n;
while(n--){
cin>>x>>y>>z;
if(x>||y>||z>)continue;
if(x*x+y*y+z*z<)++cnt;
}
cout<<cnt;
return ;
}
E0032 降序排序
First AC: 2018-01-05 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
int n,i;
int a[];
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n);
for(i=n-;i;--i)cout<<a[i]<<' ';
cout<<a[];
return ;
}
E0033 2的x次方
First AC: 2018-01-05 Latest Modification: 2018-05-30
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
int main()
{
cin>>n;
cout<<(ll)pow(,n);
return ;
}
E0034 Filter函数
First AC: 2018-01-05 Latest Modification: 2018-01-05
#include<iostream>
using namespace std;
string s,t;
int ls,lt;
int i,j,k;
int main()
{
cin>>s>>t;
ls=s.length(),lt=t.length();
for(i=;i<=ls-lt;++i){
if(s[i]==t[]){
bool flag=;
for(j=;j<lt;++j){
if(s[i+j]^t[j]){flag=;break;}
}
if(flag)i+=lt-;
else cout<<s[i];
}
else cout<<s[i];
}
for(j=i;j<ls;++j)cout<<s[j];
return ;
}
E0035 零元素占比
First AC: 2018-01-05 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
int m,n,cnt;
int a[][];
int i,j;
int main()
{
cin>>m>>n;
for(i=;i<m;++i)for(j=;j<n;++j){
cin>>a[i][j];
if(!a[i][j])++cnt;
}
printf("%.9f\n",cnt*1.0/m/n);
return ;
}
E0036 二进制1的位数
First AC: 2018-01-05 Latest Modification: 2018-01-05
#include<iostream>
using namespace std;
long long n,cnt;
int main()
{
cin>>n;
while(n){
if(n&)++cnt;
n>>=;
}
cout<<cnt<<endl;
return ;
}
E0037 奇怪的排序题
First AC: 2018-01-05 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
long long s,i,j;
struct data{
long long n;
unsigned long long tmp;
int rst;
bool flag;
}a[];
bool cmp(data a,data b)
{
if(a.rst^b.rst)return a.rst>b.rst;
return a.n<b.n;
}
int main()
{
cin>>s;
for(i=;i<s;++i){
cin>>a[i].n;
a[i].tmp=a[i].n,a[i].rst=;
if(a[i].n<)a[i].tmp=+~(-a[i].n);
while(a[i].tmp){
if(a[i].tmp&)++a[i].rst;
a[i].tmp>>=;
}
}
sort(a,a+s,cmp);
cout<<a[].n;
for(i=;i<s;++i)cout<<' '<<a[i].n;
}
E0038 二进制倒置
First AC: 2018-03-09 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int len;
string s,t;
int i;
void div()
{
/* t/=2 */
string r="";
int tmp=,len=s.length();
for(int i=;i<len;++i){
tmp=*tmp+s[i]-'';
r+=(char)(tmp/+'');
tmp&=;
}
while(len=r.length(),len>&&r[]=='')
r=r.substr(,len);
s=r;
}
void mul()
{
/* s*=2 */
string r="";
int tmp=,len=s.length();
for(int i=len-;i>=;--i){
tmp=(s[i]-'')*+tmp;
r=(char)(tmp%+'')+r;
tmp/=;
}
if(tmp)r=""+r;
while(len=r.length(),len>&&r[]=='')
r=r.substr(,len);
s=r;
}
void plu()
{
/* s+=1 */
int len=s.length(),i;
for(i=len-;i>=;--i){
if(s[i]<''){++s[i];break;}
s[i]='';
}
if(i<)s=""+s;
}
int main()
{
cin>>s;
if(s==""){cout<<;return ;}
t="";
while(s!=""){
if((s[s.length()-]-'')&)t+='';
else t+='';
div();
}
t+="",s="";
while(len=t.length(),len>&&t[]=='')
t=t.substr(,len);
len=t.length();
for(i=;i<len;++i){
mul();
if(t[i]!='')plu();
}
cout<<s;
return ;
}
E0040 文本的查找与替换
First AC: 2018-01-16 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
string s,t,r;
int lens,lent,tmp,cnt,flag;
int i,j;
int main()
{
getline(cin,s);
cin>>t>>r;
lens=s.length(),lent=t.length();
if(lent>lens){cout<<s<<endl;return ;}
tmp=lens-lent+;
for(i=;i<tmp;++i){
if(s[i]==t[]){
for(cnt=j=;j<lent;++j){
if(s[i+j]!=t[j]){cnt=;break;}
}
if(cnt)cout<<r,i+=lent-,flag=;
else cout<<s[i],flag=;
}
else cout<<s[i],flag=;
}
if(flag)for(j=i;j<lens;++j)cout<<s[j];
else for(j=tmp;j<lens;++j)cout<<s[j];
cout<<endl;
return ;
}
E0041 生成字典
First AC: 2018-01-13 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
struct data{
string s;
int len,num;
}a[];
char c;
int i,j;
bool cmp(data a,data b)
{
int tmp=a.len<b.len? a.len:b.len;
for(int i=;i<tmp;++i)if(a.s[i]^b.s[i])return a.s[i]<b.s[i];
if(a.len^b.len)return a.len<b.len;
return a.num<b.num;
}
int main()
{
while(cin>>a[i++].s)if((c=getchar())^' ')break;
for(j=;j<i;++j){
a[j].num=j;
a[j].len=a[j].s.length();
if(a[j].s[]>'Z')a[j].s[]-=;
}
sort(a,a+i,cmp);
cout<<a[].s[]<<':'<<a[].s<<endl;
for(j=;j<i;++j)
if(a[j].s!=a[j-].s)
cout<<a[j].s[]<<':'<<a[j].s<<endl;
return ;
}
E0042 简单的求和
First AC: 2018-01-13 Latest Modification: 2018-01-13
#include<iostream>
#define INT_MAX 0x7fffffff
using namespace std;
long long a,b;
int main()
{
cin>>a>>b;
if(a+b>INT_MAX)cout<<"-1";
else cout<<a+b;
return ;
}
E0043 简单的交换
First AC: 2018-01-13 Latest Modification: 2018-01-13
#include<iostream>
using namespace std;
string s,t;
int main()
{
cin>>s>>t;
cout<<t<<' '<<s;
return ;
}
E0044 标点符号统计
First AC: 2018-01-13 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
string s;
int len,i,cnt;
int main()
{
getline(cin,s);
cnt=len=s.length();
for(i=;i<len;++i)
if(s[i]==' '||(s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z'))
--cnt;
cout<<cnt;
return ;
}
E0045 最大公约数
First AC: 2018-01-13 Latest Modification: 2018-01-13
#include<iostream>
using namespace std;
long long a[],rst;
int n,i,j;
long long gcd(long long a,long long b){
long long c=;
while(c)c=a%b,a=b,b=c;
return a;
}
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
if(n==){cout<<a[];return ;}
rst=gcd(a[],a[]);
for(i=;i<n;++i){
if(rst==){cout<<;return ;}
rst=gcd(rst,a[i]);
}
cout<<rst;
return ;
}
E0046 三维坐标距离
First AC: 2018-01-13 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
int main()
{
int a,b,c,d,e,f;
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
printf("%.8f",sqrt((a-d)*(a-d)+(b-e)*(b-e)+(c-f)*(c-f)));
return ;
}
E0047 奇怪的中心点
First AC: 2018-05-15 Latest Modification: 2018-05-15
#include<bits/stdc++.h>
using namespace std;
int n,minx,miny,minz;
double mindis,tmp;
int x[],y[],z[];
int i,j,k;
double cal(int x0,int y0,int z0){
double a=x0,b=y0,c=z0,rst=;
for(int i=;i<n;++i){
rst+=sqrt(pow(a-x[i],)+pow(b-y[i],)+pow(c-z[i],));
}
return rst;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(i=;i<n;++i)cin>>x[i]>>y[i]>>z[i];
mindis=1e9;
for(i=-;i<;++i)for(j=-;j<;++j)
for(k=-;k<;++k){
tmp=cal(i,j,k);
if(tmp==mindis){
if(i<minx||(i==minx&&j<miny))
mindis=tmp,minx=i,miny=j,minz=k;
else if(i==minx&&j==miny&&k<minz)
mindis=tmp,minx=i,miny=j,minz=k;
}
else if(tmp<mindis)
mindis=tmp,minx=i,miny=j,minz=k;
}
cout<<minx<<' '<<miny<<' '<<minz;
return ;
}
E0048 奇怪的字符串排序
First AC: 2018-01-13 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
int n;
struct data{
string s;
int len;
}a[];
int i;
bool cmp(data a,data b){
if(a.s[]^b.s[])return a.s[]<b.s[];
int tmp=a.len<b.len? a.len:b.len;
for(int i=;i<tmp;++i)if(a.s[i]^b.s[i])return a.s[i]>b.s[i];
return a.len>b.len;
}
int main()
{
cin>>n;
for(;i<n;++i)cin>>a[i].s,a[i].len=a[i].s.length();
sort(a,a+n,cmp);
for(i=;i<n;++i)cout<<a[i].s<<endl;
return ;
}
E0070 十六进制
First AC: 2018-03-31 Latest Modification: 2018-11-20
#include<bits/stdc++.h>
using namespace std;
string s;
long len,tmp;
queue<long>q;
long i,j;
int main()
{
cin>>s;
len=s.length();
for(i=;i<len;++i)if(s[i]=='x')
if(s[i-]!='')++s[i];
if(s[len-]=='x')++s[len-];
for(i=;i<len;++i){
if(s[i]=='x'&&s[i+]<'g'){
for(j=i+;j<len;++j){
if(s[j]<'a')tmp=*tmp+s[j]-'';
else if(s[j]<'g')tmp=*tmp+s[j]-'a'+;
else break;
}
q.push(tmp);
tmp=;
i=j-;
}
}
if(q.empty())cout<<-;
else{
cout<<q.front();
q.pop();
while(!q.empty())cout<<' '<<q.front(),q.pop();
}
return ;
}
E0071 一个游戏
First AC: 2018-03-31 Latest Modification: 2018-03-31
Note: 一组规则是公平的当且仅当它满足以下两点
①如果数a能转化为数b,那么数a不能转化为其他任何数
②如果数a能转化为数b,那么数b不能转化为任何数
#include<bits/stdc++.h>
using namespace std;
int T,n,a,b;
bool f[][];
int cnt[];
int i,j,k;
int main()
{
cin>>T;
while(T--){
cin>>n;
memset(f,,sizeof(f));
memset(cnt,,sizeof(cnt));
while(n--){
cin>>a>>b;
if(!f[a][b])++cnt[a],f[a][b]=;
}
bool jdg=;
for(i=;i<;++i)if(cnt[i]>)jdg=;
for(i=;jdg&&i<;++i)
for(j=;jdg&&j<;++j)
if(f[i][j])
for(k=;k<;++k)
if(f[j][k]){
jdg=;
break;
}
if(jdg)cout<<"Lucky dxw!\n";
else cout<<"Poor dxw!\n";
}
return ;
}
E0072 字串变换
First AC: 2018-03-31 Latest Modification: 2018-04-03
Note: 为方便叙述,定义一个字串的简串为将它所有相邻相同字符仅保留一个所得的子串
不难发现简串有下面几个有用的性质:
①一个字串的简串是唯一的
②如果两个字串能按题述变换相互得到,那么它们的简串相同
③题述变化不改变一个字串的简串
注意到题述变换每次使字串长度变化$1$
考虑将$m$个仅由$n_{1},n_{2},\cdots,n_{m}$个字符$x$组成的字串转换为由$k$个由$x$组成的字串
显然转换总次数$rst\ge\sum_{i=1}^{m}|k-n_{i}$,由绝对值三角不等式的取等条件易知
当$k$为这$m$个数的中位数,或介于最中间两个数之间的闭区间时,$rst$取到最小值
对于输出是否为$-1$的判定,依次比较相邻两个字串的简串即可,或者直接偷懒暴力二分求得
#include<bits/stdc++.h>
using namespace std;
long n,len,cnt,rst;
string s;
struct data{
char c[];
}a[];
long i,j,k;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
if(n==)cout<<"-1";
else{
for(i=;i<n;++i){
cnt=;
cin>>s;
len=s.length();
a[i].c[]=;
for(j=;j<len;++j){
if(s[j]!=s[j-])a[i].c[++cnt]=;
else ++a[i].c[cnt];
}
}
long tmp[];
for(i=;i<=cnt;++i){
for(j=;j<n;++j)tmp[j]=a[j].c[i];
sort(tmp,tmp+n);
long _233=tmp[n/];
for(j=;j<n;++j)rst+=abs(_233-a[j].c[i]);
}
cout<<rst;
}
return ;
}
E0076 移动游戏
First AC: 2018-04-03 Latest Modification: 2018-04-03
Note: 保留前$n$次每次移动后得到的向量$(x_{i},y_{i}),0\le i<n$
则点$(a,b)$能通过有限次移动得到当且仅当存在$k,i$
使得$(a,b)=k(x_{n-1},y_{n-1})+(x_{i},y_{i}),k\in N,0\le i<n$
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
ll len,q,x,y;
struct data{
ll x,y;
}p[];
ll i,j;
int main()
{
cin>>s>>q;
len=s.length();
for(i=;i<len;++i){
if(s[i]=='U')++y;
else if(s[i]=='D')--y;
else if(s[i]=='L')--x;
else ++x;
p[++j].x=x,p[j].y=y;
}
ll stdx=p[len].x,stdy=p[len].y;
while(q--){
cin>>x>>y;
ll tmpx,tmpy;
bool jdg=;
for(i=;i<=len;++i){
tmpx=x-p[i].x;
tmpy=y-p[i].y;
if(tmpx*stdy!=tmpy*stdx)continue;
if(tmpx*stdx<||tmpy*stdy<)continue;
if((!stdx&&tmpx)||(!stdy&&tmpy))continue;
if(tmpx%stdx)continue;
jdg=;
break;
}
if(jdg)cout<<"Yes\n";
else cout<<"No\n";
}
return ;
}
E0096 进制数位和均值
First AC: 2018-05-20 Latest Modification: 2018-05-20
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,n,up,dn,tmp;
ll i;
ll cal(ll num,ll base)
{
ll ret=;
while(num){
ret+=num%base;
num/=base;
}
return ret;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
up=;
for(i=;i<n;++i)up+=cal(n,i);
dn=n-;
tmp=__gcd(up,dn);
cout<<up/tmp<<'/'<<dn/tmp<<endl;
}
return ;
}
E0097 邮件地址排序
First AC: 2018-05-20 Latest Modification: 2018-05-20
#include<bits/stdc++.h>
using namespace std;
int n;
char c;
string tmp;
struct data{
string s,t;
}a[];
int i;
bool cmp(data a,data b)
{
if(a.t!=b.t)return a.t<b.t;
return a.s>b.s;
}
int main()
{
cin>>n;
getchar();
for(i=;i<n;++i){
while(c=getchar()){
if(c=='@')break;
else tmp+=c;
}
a[i].s=tmp;
tmp="";
while(c=getchar()){
if(c=='\n'||c==EOF)break;
else tmp+=c;
}
a[i].t=tmp;
tmp="";
}
sort(a,a+n,cmp);
for(i=;i<n;++i)cout<<a[i].s<<'@'<<a[i].t<<endl;
return ;
}
E0098 遥远距离
First AC: 2018-05-20 Latest Modification: 2018-05-20
#include<bits/stdc++.h>
using namespace std;
int n,tmp;
struct data{
string s;
bool flag;
int num;
}a[];
char b[],c[],d[];
int i,j;
bool cmp(data a,data b)
{
if(a.flag!=b.flag)return a.flag<b.flag;
if(a.flag){
if(a.num!=b.num)return a.num<b.num;
return a.s<b.s;
}
if(a.num!=b.num)return a.num>b.num;
return a.s>b.s;
}
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>a[i].s;
a[i].num=a[i].s.length();
if(a[i].s[]=='-'){
a[i].flag=;
a[i].s=a[i].s.substr(,a[i].num-);
--a[i].num;
}
}
sort(a,a+n,cmp);
for(i=;i<;++i)b[i]=c[i]=d[i]='';
for(i=,j=a[].num-;j>=;--j)b[i--]=a[].s[j];
for(i=,j=a[n-].num-;j>=;--j)c[i--]=a[n-].s[j];
if(a[].flag!=a[n-].flag){
if(a[].s==""&&a[n-].s==""){
cout<<;
return ;
}
bool cf=;
for(i=;i>=;--i){
tmp=b[i]+c[i]-*''+cf;
if(tmp>)d[i]=(char)(tmp-+''),cf=;
else d[i]=char(tmp+''),cf=;
}
for(i=;;++i)if(d[i]!='')break;
for(j=i;j<;++j)cout<<d[j];
}
else if(a[].flag){
if(a[].s==a[n-].s){
cout<<;
return ;
}
bool cf=;
for(i=;i>=;--i){
tmp=c[i]-b[i]-cf;
if(tmp<)d[i]=(char)(tmp++''),cf=;
else d[i]=char(tmp+''),cf=;
}
for(i=;;++i)if(d[i]!='')break;
for(j=i;j<;++j)cout<<d[j];
}
else{
if(a[].s==a[n-].s){
cout<<;
return ;
}
bool cf=;
for(i=;i>=;--i){
tmp=b[i]-c[i]-cf;
if(tmp<)d[i]=(char)(tmp++''),cf=;
else d[i]=char(tmp+''),cf=;
}
for(i=;;++i)if(d[i]!='')break;
for(j=i;j<;++j)cout<<d[j];
}
return ;
}
E0099 幂次转换
First AC: 2018-01-13 Latest Modification: 2018-02-28
Note: 遍历base=2,3,…,64,判断开base次方是不是整数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int T;
ll n;
bool jdg;
void cal(ll n,ll i)
{
if(n<)return;
ll tmp=pow((ld)n,(ld)1.0/i)+0.5;
ll rst=;
for(int j=;j<i;++j)rst*=tmp;
if(rst==n)cout<<'='<<tmp<<'^'<<i,jdg=;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
cout<<n;
jdg=;
for(ll i=;i<;++i)cal(n,i);
if(jdg)cout<<" is powerless.";
cout<<endl;
}
return ;
}
E0100 变换种类数
First AC: 2018-05-20 Latest Modification: 2018-05-20
Note: dp[i][j]表示在前j个数字中插入加减号的结果模2×3×5×7的方案数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=***;
const ll mod=1e9+;
int T,len;
string s;
ll dp[][];
int i,j,k;
int main()
{
cin>>T;
while(T--){
cin>>s;
len=s.length();
memset(dp,,sizeof(dp));
dp[][]=;
for(i=;i<len;++i){
for(j=;j<M;++j){
ll tmp=;
for(k=i+;k<=len;++k){
tmp=(*tmp+s[k-]-'')%M;
dp[k][(j+tmp)%M]+=dp[i][j];
dp[k][(j+tmp)%M]%=mod;
dp[k][(j+M-tmp)%M]+=dp[i][j];
dp[k][(j+M-tmp)%M]%=mod;
}
}
}
ll rst=;
for(i=;i<M;++i){
if(i%&&i%&&i%&&i%)continue;
rst=(rst+dp[len][i])%mod;
}
cout<<(mod+)/*rst%mod<<endl;
}
return ;
}
E0108 A+B Problem Templated
First AC: 2018-07-17 Latest Modification: 2018-07-17
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)cout<<a+b<<endl;
}
E0116 4个值的和为0
First AC: 2018-07-17 Latest Modification: 2018-07-17
#include<bits/stdc++.h>
using namespace std;
const int N=;
int n;
long long rst;
int a[N],b[N],c[N],d[N];
map<int,int>mpl,mpr;
map<int,int>::iterator it,fd;
int i,j;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(i=;i<n;++i)cin>>a[i]>>b[i]>>c[i]>>d[i];
for(i=;i<n;++i)for(j=;j<n;++j){
it=mpl.find(a[i]+b[j]);
if(it!=mpl.end())++it->second;
else mpl.insert(pair<int,int>(a[i]+b[j],));
}
for(i=;i<n;++i)for(j=;j<n;++j){
it=mpr.find(c[i]+d[j]);
if(it!=mpr.end())++it->second;
else mpr.insert(pair<int,int>(c[i]+d[j],));
}
for(it=mpl.begin();it!=mpl.end();++it){
fd=mpr.find(-it->first);
if(fd!=mpr.end())rst+=it->second*fd->second;
}
cout<<rst;
return ;
}
E0121 经典的猜数游戏
First AC: 2018-07-12 Latest Modification: 2018-07-12
#include<bits/stdc++.h>
using namespace std;
long long lft,rgt,mid;
string s;
int main()
{
lft=-1e9;
rgt=1e9;
while(){
mid=(lft+rgt)/;
cout<<mid<<endl;
cin>>s;
if(s=="big")rgt=mid-;
else if(s=="small")lft=mid+;
else break;
}
return ;
}
E0122 游程长度编码
First AC: 2018-07-12 Latest Modification: 2018-07-12
#include<bits/stdc++.h>
using namespace std;
int T,len,cnt;
string s;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
cout<<"case #"<<i<<":\n";
len=s.length();
cnt=;
for(j=;j<=len-;++j){
if(s[j]==s[j-])cnt++;
else if(cnt>)cout<<""<<s[j-]<<cnt-<<s[j-],cnt=;
else cout<<cnt<<s[j-],cnt=;
}
if(cnt>)cout<<""<<s[len-]<<cnt-<<s[len-]<<endl;
else cout<<cnt<<s[len-]<<endl;
}
return ;
}
E0139 旅游规划
First AC: 2019-02-07 Latest Modification: 2019-02-07
#include<bits/stdc++.h>
using namespace std;
int n,m,start,destination;
int a,b,c,d;
int dis[][];
int cost[][];
int i,j,k;
int main()
{
memset(dis,0x3f,sizeof dis);
memset(cost,0x3f,sizeof cost);
for(i=;i<;++i)dis[i][i]=cost[i][i]=;
cin>>n>>m>>start>>destination;
while(m--){
cin>>a>>b>>c>>d;
if(dis[a][b]>c||(dis[a][b]==c&&cost[a][b]>d)){
dis[a][b]=c;
cost[a][b]=d;
}
if(dis[b][a]>c||(dis[b][a]==c&&cost[b][a]>d)){
dis[b][a]=c;
cost[b][a]=d;
}
}
for(k=;k<n;++k){
for(i=;i<n;++i){
for(j=;j<n;++j){
int sumd=dis[i][k]+dis[k][j];
int sumc=cost[i][k]+cost[k][j];
if(dis[i][j]>sumd||(dis[i][j]==sumd&&cost[i][j]>sumc)){
dis[i][j]=sumd;
cost[i][j]=sumc;
}
}
}
}
cout<<dis[start][destination]<<' '<<cost[start][destination];
return ;
}
E0151 冒泡排序
First AC: 2018-07-17 Latest Modification: 2018-07-17
#include<bits/stdc++.h>
using namespace std;
int n,pos;
int cntcmp,cntswp,cntbub;
int a[];
int i;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
--n;
while(n){
pos=;
for(i=;i<=n;++i){
++cntcmp;
if(a[i-]>a[i]){
a[i]^=a[i-];
a[i-]^=a[i];
a[i]^=a[i-];
pos=i-;
++cntswp;
}
}
n=pos;
++cntbub;
}
cout<<cntcmp<<' '<<cntswp<<' '<<cntbub;
return ;
}
E0152 Take a Party
First AC: 2018-07-17 Latest Modification: 2018-07-17
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll s,m,n,x,y;
ll pre[];
ll i,j;
ll find(ll x)
{
ll r=x;
while(r!=pre[r])r=pre[r];
i=x;
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
void join(ll x,ll y)
{
ll fx=find(x),fy=find(y);
if(fx!=fy)pre[fx]=fy;
}
int main()
{
ios::sync_with_stdio(false);
cin>>s>>m>>n;
for(i=;i<=s;++i)pre[i]=i;
while(m--){
cin>>x>>y;
join(x,y);
}
while(n--){
cin>>x>>y;
if(find(x)!=find(y))cout<<"0\n";
else cout<<"1\n";
}
return ;
}
E0156 大鱼吃小鱼
First AC: 2018-07-17 Latest Modification: 2018-07-17
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,a,tmp,rst=;
struct data{
ll w,a;
bool add;
}fish[];
int i;
bool cmp(data a,data b)
{
if(a.add!=b.add)return a.add>b.add;
if(a.add){
if(a.w!=b.w)return a.w<b.w;
return a.a>b.a;
}
else{
if(a.w+a.a!=b.w+b.a)return a.w+a.a>b.w+b.a;
return a.a>b.a;
}
}
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>fish[i].w>>fish[i].a;
if(fish[i].a<)fish[i].add=;
else fish[i].add=;
}
sort(fish,fish+n,cmp);
for(i=;i<n;++i){
if(fish[i].add)rst=max(rst,fish[i].w-tmp);
else rst=max(rst,max(fish[i].w-tmp,-fish[i].a-tmp));
tmp+=fish[i].a;
}
cout<<rst;
return ;
}
E0157 玩具谜题
First AC: 2018-07-17 Latest Modification: 2018-07-17
#include<bits/stdc++.h>
using namespace std;
struct toy{
int dir;
string job;
}a[];
long n,m,d,num,cnt=;
long i;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(i=;i<=n;++i)cin>>a[i].dir>>a[i].job;
for(i=;i<=m;++i){
cin>>d>>num;
if(d==a[cnt].dir){
cnt-=num;
while(cnt<=)cnt+=n;
}
else{
cnt=(cnt+num)%n;
if(cnt==)cnt=n;
}
}
cout<<a[cnt].job;
return ;
}
E0158 玩具谜题
First AC: 2018-07-17 Latest Modification: 2018-07-17
#include<bits/stdc++.h>
using namespace std;
struct toy{
int dir;
string job;
}a[];
long n,m,d,num,cnt=;
long i;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(i=;i<=n;++i)cin>>a[i].dir>>a[i].job;
for(i=;i<=m;++i){
cin>>d>>num;
if(d==a[cnt].dir){
cnt-=num;
while(cnt<=)cnt+=n;
}
else{
cnt=(cnt+num)%n;
if(cnt==)cnt=n;
}
}
cout<<a[cnt].job;
return ;
}
E0159 密码碰撞
First AC: 2018-07-17 Latest Modification: 2018-07-17
#include<bits/stdc++.h>
using namespace std;
int n,num,tmp,rst;
string s,t;
map<string,int>fd,mp;
map<string,int>::iterator it;
struct data{
string s;
int cnt,len;
}a[];
int i,j,k;
bool cmp(data a,data b)
{
return a.len>b.len;
}
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>s;
if((it=fd.find(s))!=fd.end())++a[it->second].cnt;
else{
a[num].s=s;
a[num].cnt=;
a[num].len=s.length();
fd.insert(pair<string,int>(s,num++));
}
}
fd.clear();
sort(a,a+num,cmp);
for(i=;i<num;++i){
rst+=a[i].cnt*(a[i].cnt-);
if((it=fd.find(a[i].s))!=fd.end())rst+=a[i].cnt*it->second;
mp.clear();
for(j=;j<a[i].len;++j){
for(k=j;k<a[i].len;++k){
t=a[i].s.substr(j,k-j+);
if((it=mp.find(t))==mp.end()){
mp.insert(pair<string,int>(t,));
if((it=fd.find(t))!=fd.end())it->second+=a[i].cnt;
else fd.insert(pair<string,int>(t,a[i].cnt));
}
}
}
}
cout<<rst;
return ;
}
E0163 数据结构开课啦
First AC: 2018-07-17 Latest Modification: 2018-07-17
数据结构
数据结构
数据结构
树
存储
多
多对多
E0165 三千米健身步道
First AC: 2018-08-01 Latest Modification: 2018-08-01
#include<bits/stdc++.h>
using namespace std;
int n,x,y,rst;
int cnt[],fr[],to[];
int i;
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>x>>y;
++cnt[x],++cnt[y];
fr[i]=x,to[i]=y;
}
for(i=;i<n;++i){
rst+=(cnt[fr[i]]-)*(cnt[to[i]]-);
}
cout<<rst;
return ;
}
E0168 小迷妹在哪儿
First AC: 2018-08-04 Latest Modification: 2018-08-04
#include<bits/stdc++.h>
using namespace std;
int n,T,rst;
struct data{
int a,t;
}girl[];
int dp[][];
bool jdg[][];
int i,j;
bool cmp(data a,data b)
{
return a.a*b.t>b.a*a.t;
}
int main()
{
cin>>n>>T;
for(i=;i<=n;++i)
cin>>girl[i].a>>girl[i].t;
sort(girl+,girl+n+,cmp);
jdg[][T]=;
for(i=;i<n;++i)for(j=;j<=T;++j){
if(jdg[i][j]){
dp[i+][j]=max(dp[i+][j],dp[i][j]);
jdg[i+][j]=;
if(j>=girl[i+].t){
dp[i+][j-girl[i+].t]=
max(dp[i][j]+(j-girl[i+].t)*
girl[i+].a,dp[i+][j-girl[i+].t]);
jdg[i+][j-girl[i+].t]=;
}
}
}
for(i=;i<=T;++i)if(dp[n][i]>rst)rst=dp[n][i];
cout<<rst;
return ;
}
E0170 矩形个数
First AC: 2018-08-04 Latest Modification: 2018-08-04
#include<bits/stdc++.h>
using namespace std;
int T,r,c,n,k,x,y,rst;
int mp[][];
int i,j,l;
int cnt(int x,int y)
{
int ret=,i,j,l;
for(i=x;i<=r;++i){
int tmp=;
for(j=y;j<=c;++j){
for(l=x;l<=i;++l)
if(mp[l][j])
++tmp;
if(tmp>=k){
ret+=c-j+;
break;
}
}
}
return ret;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>r>>c>>n>>k;
memset(mp,,sizeof(mp));
while(n--){
cin>>x>>y;
mp[x][y]=;
}
rst=;
for(j=;j<=r;++j)
for(l=;l<=c;++l)
rst+=cnt(j,l);
cout<<"case #"<<i<<":\n"<<rst<<endl;
}
return ;
}
E0171 考新郎
First AC: 2018-08-04 Latest Modification: 2018-08-04
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[]={,,};
ll s;
int n,m;
int i;
int main()
{
for(i=;i<;++i)a[i]=(i-)*(a[i-]+a[i-]);
cin>>n;
while(cin>>n>>m){
s=;
for(i=;i<=m;++i)s*=(n+-i),s/=i;
s*=a[m];
cout<<s<<endl;
}
return ;
}
E0172 津津骑马
First AC: 2018-08-04 Latest Modification: 2018-08-04
#include<bits/stdc++.h>
using namespace std;
int T,x,y,a[][];
int main()
{
a[][]=a[][]=;
for(int i=;i<;++i)
for(int j=;j<;++j)
a[i][j]+=a[i-][j-]+a[i-][j-];
cin>>T;
for(int i=;i<=T;++i){
cin>>x>>y;
cout<<"Chessboard #"<<i<<':'<<a[x+][y+]<<endl;
}
return ;
}
E0183 单词的划分
First AC: 2018-08-23 Latest Modification: 2018-08-23
#include<bits/stdc++.h>
using namespace std;
int T,n,ls;
string s;
string w[];
int len[];
int dp[];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>s>>n;
s=' '+s;
ls=s.length();
for(i=;i<n;++i)cin>>w[i],len[i]=w[i].length();
for(i=;i<ls;++i){
dp[i]=;
for(j=;j<n;++j)
if(i>=len[j]&&w[j]==s.substr(i-len[j]+,len[j]))
dp[i]=min(dp[i],dp[i-len[j]]+);
}
cout<<dp[ls-]<<endl;
}
return ;
}
E0180 地铁站
First AC: 2018-11-16 Latest Modification: 2018-11-16
Note: 设第二站下车人数为b,通过列出前几项可以找到规律:
的其中fib[n]是斐波那契数列,且设第一项fib[0]=1下标为0
的依题意,第n(>=4)站开出时,车上人数等于第n-1站上车人数+第1站上车人数-第2站下车人数
的即fib[n-4]a+fib[n-3]b+a-b=x,这样便可解出b,从而可得第x站开出时车上的人数,注意对x<4单独讨论
#include<bits/stdc++.h>
using namespace std;
int a,b,n,m,x;
int fib[]={,};
int i;
int main()
{
cin>>a>>n>>m>>x;
for(i=;i<;++i)fib[i]=fib[i-]+fib[i-];
if(x>=n)cout<<;
else if(n<||x<)cout<<a;
else if(n==){
if(x==)cout<<*a;
else cout<<a;
}
else{
b=(m-fib[n-]*a-a)/(fib[n-]-);
cout<<fib[x-]*a+fib[x-]*b+a-b;
}
return ;
}
E0186 完全加括号的矩阵连乘积
First AC: 2018-08-23 Latest Modification: 2018-08-23
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T,n,len,lft,rgt;
ll x[],y[];
ll dp[][];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<=n;++i)cin>>x[i]>>y[i];
memset(dp,0x3f,sizeof dp);
for(i=;i<;++i)dp[i][i]=;
for(len=;len<=n;++len){
for(rgt=len;rgt<=n;++rgt){
lft=rgt-len+;
for(i=lft+;i<=rgt;++i)
dp[lft][rgt]=min(dp[lft][rgt],dp[lft][i-]+dp[i][rgt]+x[lft]*x[i]*y[rgt]);
}
}
cout<<dp[][n]<<endl;
}
return ;
}
E0215 和你在一起
First AC: 2018-09-13 Latest Modification: 2018-09-13
#include<bits/stdc++.h>
using namespace std;
int n;
string s[];
int i;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>s[i];
sort(s,s+n);
for(i=n-;i>=;--i)cout<<s[i];
return ;
}
E1000 A+B Problem
First AC: 2017-10-09 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main()
{
while(cin>>a>>b)cout<<a+b<<endl;
return ;
}
E1001 A+B Problem (Big Integer)
First AC: 2017-09-20 Latest Modification: 2017-10-27
#include<iostream>
using namespace std;
int c[],d[],e[];
string a,b;
int lena,lenb,num;
int i,j;
int main()
{
while(cin>>a>>b){
for(i=;i<;++i)c[i]=d[i]=e[i]=;
num=,lena=a.length(),lenb=b.length();
for(i=lena-,j=;i>=;--i,--j)c[j]=(int)(a[i]-'');
for(i=lenb-,j=;i>=;--i,--j)d[j]=(int)(b[i]-'');
for(i=;i>=;--i){
if(c[i]+d[i]+num>){
e[i+]=c[i]+d[i]+num-,num=;
if(i==)e[]=;
}
else e[i+]=c[i]+d[i]+num,num=;
}
for(i=;i<;++i)if(e[i]!=)break;
for(j=i;j<;++j)cout<<e[j];
cout<<endl;
}
return ;
}
E1002 IP Address
First AC: 2017-10-21 Latest Modification: 2017-10-27
#include<stdio.h>
int main()
{
int n,i,j,s,count;
char a,b,c,d,e,f,g,h;
scanf("%d",&n);
getchar();
for(i=;i<n;++i){
count=;
for(j=;j<;++j){
scanf("%c%c%c%c%c%c%c%c",&a,&b,&c,&d,&e,&f,&g,&h);
s=*(a-'')+*(b-'')+*(c-'')+*(d-'')+*(e-'')+*(f-'')+*(g-'')+(h-'');
if(count==)printf("%d",s);
else printf(".%d",s);
count++;
}
printf("\n");
getchar();
}
return ;
}
E1003 Boolean Expressions
First AC: 2018-12-06 Latest Modification: 2018-12-06
#include<bits/stdc++.h>
using namespace std;
string s;
int len,pos,cnt;
bool cal()
{
bool a=,b=,op=,f=;
while(pos<len&&s[pos]!=')'){
if(s[pos]=='(')++pos,(b=f? !cal():cal());
else if(s[pos]=='V')b=!f,f=;
else if(s[pos]=='F')b=f,f=;
else if(s[pos]=='&')op=,f=;
else if(s[pos]=='|')op=,f=;
else if(s[pos]=='!')f=!f;
if(s[pos]=='V'||s[pos]=='F'||s[pos]==')')a=op? a&b:a|b;
++pos;
}
return a;
}
int main()
{
while(getline(cin,s)){
len=s.length();
pos=;
cout<<"Expression "<<++cnt<<": ";
if(cal())cout<<"V\n";
else cout<<"F\n";
}
return ;
}
E1005 分时操作系统
First AC: 2018-02-28 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
int T;
string s;
struct data{
long long PID;
long TIME;
}a[];
long n;
int i,j,k;
bool cmp(data a,data b)
{
return a.PID<b.PID;
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
j=;
while(){
cin>>s;
if(s[]=='e')cin>>a[j].PID>>a[j].TIME,++j;
else break;
}
sort(a,a+j,cmp);
cin>>s>>n;
if(i!=)cout<<endl;
cout<<"test case "<<i<<":\n";
for(long t=;;++t){
if(n==)break;
for(k=;k<j;++k){
if(n==)break;
if(t%a[k].TIME==)cout<<a[k].PID<<endl,--n;
}
}
}
return ;
}
E1006 Prime
First AC: 2017-10-13 Latest Modification: 2018-02-28
#include<bits/stdc++.h>
using namespace std;
long a[],n,m,i,j,s;
int main()
{
for(i=;i<;i++)
for(j=;j<=sqrt(i);j++){
if(i%j==){
a[i]=;
break;
}
}
while(cin>>n>>m){
for(i=n;i<=m;i++)if(a[i]==)s++;
if(n==)cout<<s-<<endl;
else cout<<s<<endl;
s=;
}
return ;
}
E1007 N! 的最高位
First AC: 2018-01-16 Latest Modification: 2018-02-28
Note: 每次仅保留最高位
#include<bits/stdc++.h>
using namespace std;
long long n,i;
double s;
int main()
{
while(cin>>n){
for(s=,i=;i<=n;++i){
s*=i;
while(s>)s/=;
}
cout<<(int)s<<endl;
}
return ;
}
E1008 Zero
First AC: 2017-11-13 Latest Modification: 2017-11-13
#include<iostream>
using namespace std;
long n;
int main()
{
cin>>n;
while(cin>>n)cout<<n/+n/+n/+n/+n/+n/+n/+n/+n/+n/+n/+n/+n/<<endl;
return ;
}
E1009 整数的拆分
First AC: 2017-12-08 Latest Modification: 2017-12-08
Note: 设f(m,n)为sigma(i=1,m)xi,1<=x1<=x2<=…<=xm的解的组数
的则f(m,n)={0(m>n),1(m=n),f(m-1,n-1)+f(m,n-m)(m<n)}
#include<iostream>
using namespace std;
int a[][];
int N,m,n,rst,i;
int main()
{
for(n=;n<;++n){
a[n][n]=;
for(m=;m<n;++m)a[m][n]=a[m-][n-]+a[m][n-m];
}
while(cin>>N){
rst=;
for(i=;i<=N;++i)rst+=a[i][N];
cout<<rst<<endl;
}
return ;
}
E1010 智能T9英文输入法
First AC: 2018-03-01 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int n,len;
string s;
struct data{
string s,t;
int len;
}a[];
queue<string>st,nd,rd,th;
int i,j;
char find(char c)
{
if(c<'D')return '';
if(c<'G')return '';
if(c<'J')return '';
if(c<'M')return '';
if(c<'P')return '';
if(c<'T')return '';
if(c<'W')return '';
return '';
}
bool cmp(data a,data b)
{
return a.s<b.s;
}
int cal(int i)
{
if(len>a[i].len)return -;
int num=;
for(j=;j<len;++j)if(s[j]!=a[i].t[j])++num;
return num;
}
int main()
{
while(cin>>n){
for(i=;i<n;++i){
cin>>a[i].s;
a[i].len=a[i].s.length();
a[i].t=find(a[i].s[]);
for(j=;j<a[i].len;++j){
a[i].t+=find(a[i].s[j]);
}
}
sort(a,a+n,cmp);
cin>>s;
len=s.length();
for(i=;i<n;++i){
int tmp=cal(i);
if(tmp==){
if(len==a[i].len)st.push(a[i].s);
else nd.push(a[i].s);
}
else if(tmp==){
if(len==a[i].len)rd.push(a[i].s);
else th.push(a[i].s);
}
}
while(!st.empty())cout<<st.front()<<endl,st.pop();
while(!nd.empty())cout<<nd.front()<<endl,nd.pop();
while(!rd.empty())cout<<rd.front()<<endl,rd.pop();
while(!th.empty())cout<<th.front()<<endl,th.pop();
}
return ;
}
E1011 Pseudo-Random Numbers
First AC: 2018-06-30 Latest Modification: 2018-06-30
#include<bits/stdc++.h>
using namespace std;
int Z,I,M,L,cas,rst,tmp;
int jdg[];
int main()
{
while(cin>>Z>>I>>M>>L,Z|I|M|L){
memset(jdg,,sizeof(jdg));
jdg[L]=;
tmp=L;
for(rst=;;++rst){
tmp=(Z*tmp+I)%M;
if(!jdg[tmp])jdg[tmp]=rst;
else{
cout<<"Case "<<++cas<<": "<<rst-jdg[tmp]<<endl;
break;
}
}
}
return ;
}
E1012 The 3n+1 Problem
First AC: 2018-08-01 Latest Modification: 2018-08-01
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,l,r,rst;
ll ans[];
ll i;
ll cal(ll n)
{
if(n<&&ans[n])return ans[n];
ll ret=;
if(n&)ret=cal(*n+)+;
else ret=cal(n/)+;
if(n<)ans[n]=ret;
return ret;
}
int main()
{
ans[]=;
while(cin>>a>>b){
l=min(a,b);
r=max(a,b);
rst=;
for(i=l;i<=r;++i)rst=max(rst,cal(i));
cout<<a<<' '<<b<<' '<<rst<<endl;
}
return ;
}
E1013 Ancient Cipher
First AC: 2018-03-01 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int T,len;
string s,t;
struct data{
char c;
int n;
}a[],b[];
bool rst;
int i,j;
bool cmp(data a,data b)
{
if(a.n!=b.n)return a.n<b.n;
return a.c<b.c;
}
int main()
{
cin>>T;
while(T--){
cin>>s>>t;
len=s.length();
for(i=;i<;++i)a[i].c=b[i].c='A'+i,a[i].n=b[i].n=;
for(i=;i<len;++i)++a[s[i]-'A'].n,++b[t[i]-'A'].n;
sort(a,a+,cmp),sort(b,b+,cmp);
for(rst=,i=;i<;++i){
if(a[i].n!=b[i].n){
rst=;
break;
}
}
for(i=;;++i)if(a[i].n)break;
if(a[i].n==&&(i==len-||a[i+].n>)&&a[i].c==b[i].c)rst=;
rst? cout<<"YES\n":cout<<"NO\n";
}
return ;
}
E1014 Box
First AC: 2018-03-01 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int x,y;
struct data{
int x,y;
}a[];
int i;
bool cmp(data a,data b)
{
if(a.x!=b.x)return a.x<b.x;
return a.y<b.y;
}
int main()
{
for(i=;i<;++i){
cin>>x>>y;
if(x>y)x^=y,y^=x,x^=y;
a[i].x=x,a[i].y=y;
}
sort(a,a+,cmp);
for(i=;i<;++i)
if(a[*i].x!=a[*i+].x||a[*i].y!=a[*i+].y){
cout<<"IMPOSSIBLE";
return ;
}
if(a[].x==a[].x&&a[].x==a[].y&&a[].y==a[].y)
cout<<"POSSIBLE";
else cout<<"IMPOSSIBLE";
return ;
}
E1015 核电站
First AC: 2017-10-18 Latest Modification: 2018-03-01
Note: 考虑强行分类递推(对上一个坑有核与否分类)
#include<bits/stdc++.h>
using namespace std;
long long a[][],n,m,i,c;
int main()
{
a[][]=,a[][]=;
a[][]=,a[][]=,a[][]=;
a[][]=,a[][]=,a[][]=,a[][]=;
a[][]=,a[][]=,a[][]=,a[][]=,a[][]=;
for(i=;i<;i++)
a[][i]=a[][i-]+a[][i-];
for(i=;i<;i++)
a[][i]=a[][i-]+a[][i-]+a[][i-];
for(i=;i<;i++)
a[][i]=a[][i-]+a[][i-]+a[][i-]+a[][i-];
for(i=;i<;i++)
a[][i]=a[][i-]+a[][i-]+a[][i-]+a[][i-]+a[][i-];
while(cin>>n>>m){
c++;
if(c==)cout<<a[m][n];
else cout<<"\n"<<a[m][n];
}
return ;
}
E1018 单词的划分
First AC: 2018-05-09 Latest Modification: 2018-05-09
#include<bits/stdc++.h>
using namespace std;
int T,n,ls;
string s;
string w[];
int len[];
int dp[];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>s>>n;
s=' '+s;
ls=s.length();
for(i=;i<n;++i)cin>>w[i],len[i]=w[i].length();
for(i=;i<ls;++i){
dp[i]=;
for(j=;j<n;++j)
if(i>=len[j]&&w[j]==s.substr(i-len[j]+,len[j]))
dp[i]=min(dp[i],dp[i-len[j]]+);
}
cout<<dp[ls-]<<endl;
}
return ;
}
E1023 Three Powers
First AC: 2019-01-17 Latest Modification: 2019-01-17
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
char powi[][];
int i,j;
int main()
{
memset(powi,'',sizeof powi);
powi[][]='';
for(i=;i<;++i){
int plus=;
for(j=;j;--j){
powi[i][j]=(powi[i-][j]-'')*+plus;
plus=powi[i][j]/;
powi[i][j]%=;
powi[i][j]+='';
}
}
while(cin>>n,n){
bool jdg=;
if(--n==){
cout<<"{}\n";
continue;
}
cout<<"{";
for(i=;i<;++i){
if(n%){
if(jdg)cout<<",";
jdg=;
for(j=;;++j)if(powi[i][j]!='')break;
for(j;j<;++j)cout<<powi[i][j];
}
n/=;
}
cout<<"}\n";
}
return ;
}
E1026 重力加速度
First AC: 2017-09-19 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
while(cin>>n,n){
if(n==)cout<<"0.598\n";
else if(n==)cout<<"1.174\n";
else printf("%.3f\n",sqrt((*n+5.5)/9.8));
}
return ;
}
E1027 邮资的问题
First AC: 2018-08-04 Latest Modification: 2018-08-04
#include<bits/stdc++.h>
using namespace std;
int T,n,rst;
int pri[],num[];
bool jdg[],tmp[];
int i,j,k;
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;++i)cin>>pri[i];
for(i=;i<n;++i)cin>>num[i];
memset(jdg,,sizeof(jdg));
memset(tmp,,sizeof(tmp));
jdg[]=;
rst=;
for(i=;i<n;++i){
for(j=;j<;++j){
if(jdg[j]){
for(k=;k<=num[i];++k){
if(j+k*pri[i]>)break;
tmp[j+k*pri[i]]=;
}
}
}
for(j=;j<;++j)jdg[j]|=tmp[j];
}
for(i=;i<;++i)if(jdg[i])++rst;
cout<<rst<<endl;
}
return ;
}
E1028 路由器
First AC: 2019-02-04 Latest Modification: 2019-02-04
#include<bits/stdc++.h>
using namespace std;
int n,m,q;
string s,t;
int x,y,cnt,val;
int dis[][];
map<string,int>hashto;
map<string,int>::iterator itx;
int i,j,k;
int main()
{
cin>>n>>m;
memset(dis,0x3f,sizeof dis);
for(i=;i<;++i)dis[i][i]=;
while(m--){
cin>>s>>t>>val;
itx=hashto.find(s);
if(itx!=hashto.end())x=itx->second;
else hashto.insert(pair<string,int>(s,x=cnt++));
itx=hashto.find(t);
if(itx!=hashto.end())y=itx->second;
else hashto.insert(pair<string,int>(t,y=cnt++));
dis[x][y]=dis[y][x]=min(val,dis[x][y]);
}
for(k=;k<cnt;++k){
for(i=;i<cnt;++i){
for(j=;j<cnt;++j){
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
cin>>q;
while(q--){
cin>>s>>t;
itx=hashto.find(s);
if(itx==hashto.end()){
cout<<"-1\n";
continue;
}
x=itx->second;
itx=hashto.find(t);
if(itx==hashto.end()){
cout<<"-1\n";
continue;
}
y=itx->second;
if(dis[x][y]>1e9)cout<<"-1\n";
else cout<<dis[x][y]<<endl;
}
return ;
}
E1030 母牛生小牛
First AC: 2017-10-13 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int i,most2[]={,,,,},least3[]={,,,,};
int main()
{
for(i=;i<;i++){
most2[i+]=least3[i]+least3[i-];
least3[i+]=least3[i]+least3[i-];}
while(cin>>i,i!=)cout<<most2[i]+least3[i]<<endl;
return ;
}
E1033 War on Weather
First AC: 2018-09-24 Latest Modification: 2018-09-24
Note: 既然所有目标都在同一球面上,问题不难转化为判断两个向量内积的符号
#include<bits/stdc++.h>
using namespace std;
int m,n,c,x,y,v;
struct data{
int x,y,v;
}a[];
int i;
bool cmp(data a,data b)
{
if(a.x!=b.x)return a.x<b.x;
return a.y<b.y;
}
int main()
{
cin>>m>>n>>c;
for(i=;i<c;++i){
cin>>y>>x>>v;
a[i].x=x;
a[i].y=y;
a[i].v=v;
}
sort(a,a+c,cmp);
for(i=;i<c;++i)cout<<a[i].x<<' '<<a[i].y<<' '<<a[i].v<<endl;
return ;
}
E1038 Prerequisites?
First AC: 2018-09-22 Latest Modification: 2018-09-22
#include<bits/stdc++.h>
using namespace std;
int k,m,x,r,c,cnt;
bool rst;
bool jdg[];
int i;
int main()
{
while(cin>>k,k){
cin>>m;
rst=;
memset(jdg,,sizeof jdg);
while(k--)cin>>x,jdg[x]=;
while(m--){
cin>>r>>c;
cnt=;
while(r--){
cin>>x;
if(jdg[x])++cnt;
}
if(cnt<c)rst=;
}
rst? cout<<"yes\n":cout<<"no\n";
}
return ;
}
E1042 付款问题
First AC: 2017-10-21 Latest Modification: 2017-10-21
#include<cstdio>
int main()
{
long s,p100,p50,p20,p10,p5,p2,p1;
int n,i;
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%ld",&s);
p100=s/;
s-=*p100;
p50=s/;
s-=*p50;
p20=s/;
s-=*p20;
p10=s/;
s-=*p10;
p5=s/;
s-=*p5;
p2=s/;
p1=s-*p2;
printf("%ld %ld %ld %ld %ld %ld %ld\n",p100,p50,p20,p10,p5,p2,p1);
}
return ;
}
E1045 箱子打包
First AC: 2018-01-08 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
long n,l,N,tmp,cnt;
int a[];
long i,j;
int main()
{
cin>>n>>l,N=n,++l;
for(i=;i<n;++i)scanf("%d",a+i);
sort(a,a+n);
--n;
while(N){
tmp=l;
for(i=n;;--i)if(a[i]<l)break;
--N,--n,++cnt,tmp-=a[i],a[i]=l+;
for(j=i-;j>=;--j)if(a[j]<tmp){--N,a[j]=l;break;}
}
cout<<cnt;
return ;
}
E1046 4个值的和为0
First AC: 2018-06-23 Latest Modification: 2018-06-23
Note:维护a+b,c+d,暴力枚举a+b,二分查找c+d
#include<bits/stdc++.h>
using namespace std;
const int N=;
int n;
long long rst;
int a[N],b[N],c[N],d[N];
map<int,int>mpl,mpr;
map<int,int>::iterator it,fd;
int i,j;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(i=;i<n;++i)cin>>a[i]>>b[i]>>c[i]>>d[i];
for(i=;i<n;++i)for(j=;j<n;++j){
it=mpl.find(a[i]+b[j]);
if(it!=mpl.end())++it->second;
else mpl.insert(pair<int,int>(a[i]+b[j],));
}
for(i=;i<n;++i)for(j=;j<n;++j){
it=mpr.find(c[i]+d[j]);
if(it!=mpr.end())++it->second;
else mpr.insert(pair<int,int>(c[i]+d[j],));
}
for(it=mpl.begin();it!=mpl.end();++it){
fd=mpr.find(-it->first);
if(fd!=mpr.end())rst+=it->second*fd->second;
}
cout<<rst;
return ;
}
E1048 最小等待时间
First AC: 2017-11-25 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int T,N,i;
long long sum;
int a[];
int main()
{
cin>>T;
while(T--){
cin>>N;
for(i=;i<N;++i)cin>>a[i];
sort(a,a+N);
for(sum=i=,--N;i<N;++i)sum+=a[i]*(N-i);
cout<<sum<<endl;
}
return ;
}
E1050 方阵相乘
First AC: 2017-12-06 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int T,n,i,j,k;
int a[][],b[][],c[][];
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;++i)for(j=;j<n;++j)cin>>a[i][j];
for(i=;i<n;++i)for(j=;j<n;++j)cin>>b[i][j];
memset(c,,sizeof(c));
for(i=;i<n;++i)for(j=;j<n;++j){
for(k=;k<n;++k)c[i][j]+=a[i][k]*b[k][j];
}
for(i=;i<n;++i){
for(j=;j<n-;++j)cout<<c[i][j]<<' ';
cout<<c[i][n-]<<endl;
}
}
return ;
}
E1051 完全加括号的矩阵连乘积
First AC: 2018-08-23 Latest Modification: 2018-08-23
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T,n,len,lft,rgt;
ll x[],y[];
ll dp[][];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<=n;++i)cin>>x[i]>>y[i];
memset(dp,0x3f,sizeof dp);
for(i=;i<;++i)dp[i][i]=;
for(len=;len<=n;++len){
for(rgt=len;rgt<=n;++rgt){
lft=rgt-len+;
for(i=lft+;i<=rgt;++i)
dp[lft][rgt]=min(dp[lft][rgt],dp[lft][i-]+dp[i][rgt]+x[lft]*x[i]*y[rgt]);
}
}
cout<<dp[][n]<<endl;
}
return ;
}
E1052 0-1背包问题
First AC: 2017-12-13 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int T,n;
int w[],p[];
long M,dp[][];
long i,j;
int main()
{
cin>>T;
while(T--){
cin>>n>>M;
for(i=;i<=n;++i)cin>>w[i]>>p[i];
for(i=;i<=n;++i)for(j=;j<=M;++j){
if(j<w[i])dp[i][j]=dp[i-][j];
else dp[i][j]=max(dp[i-][j],dp[i-][j-w[i]]+p[i]);
}
cout<<dp[n][M]<<endl;
}
return ;
}
E1053 子集和问题
First AC: 2018-04-19 Latest Modification: 2018-04-19
Note: 题目数据已加强,结果可能超出unsigned long long,故考虑dp时分高低位存储
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt,len;
string s;
long long x,tmp;
long long num[];
long long dphgh[][];
long long dplow[][];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n>>x;
cnt=;
while(n--){
cin>>s;
len=s.length();
if(len>)continue;
for(i=tmp=;i<len;++i)
tmp=*tmp+s[i]-'';
if(tmp>x)continue;
num[cnt++]=tmp;
}
for(i=;i<=x;++i)dphgh[][i]=dplow[][i]=;
dphgh[][]=,dplow[][]=;
dphgh[][num[]]=,dplow[][num[]]=;
for(i=;i<cnt;++i)for(j=;j<=x;++j){
dphgh[i][j]=dphgh[i-][j];
dplow[i][j]=dplow[i-][j];
if(j>=num[i]){
dphgh[i][j]+=dphgh[i-][j-num[i]];
dplow[i][j]+=dplow[i-][j-num[i]];
}
if(dplow[i][j]>1e15)
dplow[i][j]-=1e15,++dphgh[i][j];
}
if(dphgh[cnt-][x])
printf("%lld%015lld\n",dphgh[cnt-][x],dplow[cnt-][x]);
else printf("%lld\n",dplow[cnt-][x]);
}
return ;
}
E1054 (算法作业5-2)机器设计
First AC: 2018-12-05 Latest Modification: 2018-12-05
#include<bits/stdc++.h>
using namespace std;
int n,c,rst;
int dp[][];
int w1,c1,w2,c2,w3,c3;
int i,j;
int main()
{
while(cin>>n>>c){
memset(dp,0x3f,sizeof dp);
dp[][]=;
for(i=;i<=n;++i){
cin>>w1>>c1>>w2>>c2>>w3>>c3;
for(j=c;j>=;--j){
dp[i][j+c1]=min(dp[i][j+c1],dp[i-][j]+w1);
dp[i][j+c2]=min(dp[i][j+c2],dp[i-][j]+w2);
dp[i][j+c3]=min(dp[i][j+c3],dp[i-][j]+w3);
}
}
rst=dp[n][];
for(i=;i<c;++i)rst=min(rst,dp[n][i]);
cout<<rst<<endl;
}
return ;
}
E1055 欧几里得
First AC: 2017-10-14 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main(){
while(cin>>a>>b){
int r=;
while(r)r=a%b,a=b,b=r;
cout<<a<<endl;
}
return ;
}
E1056 线性同余方程
First AC: 2018-02-20 Latest Modification: 2018-02-20
#include<bits/stdc++.h>
using namespace std;
long a,b,m,jdg;
long i,j;
int main()
{
while(cin>>a>>b>>m){
jdg=;
for(i=;i<m;++i)
if((a*i-b)%m==){cout<<i;jdg=;break;}
for(j=i+;j<m;++j)
if((a*j-b)%m==)cout<<' '<<j;
if(jdg)cout<<"No Answer.";
cout<<endl;
}
return ;
}
E1058 挤模具
First AC: 2018-01-06 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int n,i;
double x[],y[],sum,mx,my,v;
int main()
{
while(cin>>n,n){
for(i=sum=mx=my=;i<n;++i){
cin>>x[i]>>y[i];
if(x[i]<mx)mx=x[i];
if(y[i]<my)my=y[i];
}
for(i=;i<n;++i)x[i]+=mx+,y[i]+=my+;
for(i=;i<n;++i)sum+=x[i-]*y[i]-x[i]*y[i-];
sum+=x[n-]*y[]-x[]*y[n-];
cin>>v;
printf("BAR LENGTH: %.2f\n",-v*/sum);
}
return ;
}
E1059 找零
First AC: 2018-04-20 Latest Modification: 2018-04-20
#include<bits/stdc++.h>
using namespace std;
int q,d,n,p,c;
int nq,nd,nn,np,sumn;
bool jdg;
void find(int numq,int d,int n,int p,int c)
{
int numd,numn,nump;
numd=min(c/,d);
c-=numd*;
numn=min(c/,n);
c-=numn*;
nump=min(c,p);
c-=nump;
if(!c){
if(numq+numd+numn+nump<sumn){
sumn=numq+numd+numn+nump;
nq=numq,nd=numd,nn=numn,np=nump;
}
jdg=;
}
}
int main()
{
while(cin>>q>>d>>n>>p>>c,q||d||n||p||c){
sumn=q+d+n+p+;
jdg=;
find(,d,n,p,c);
if(c>&&q>)find(,d,n,p,c-);
if(c>&&q>)find(,d,n,p,c-);
if(c>&&q>)find(,d,n,p,c-);
if(jdg)cout<<"Cannot dispense the desired amount.\n";
else{
cout<<"Dispense "<<nq<<" quarters, "<<nd<<" dimes, ";
cout<<nn<<" nickels, and "<<np<<" pennies.\n";
}
}
return ;
}
E1060 获取你的维生素
First AC: 2018-02-09 Latest Modification: 2018-02-09
#include<bits/stdc++.h>
using namespace std;
double a,r,p;
string u,v;
string s[];
int i,j;
int main()
{
while(){
cin>>a>>u>>r;
getchar();
getline(cin,v);
if(a<)break;
p=a/r;
if(p<0.01)s[i++]=v;
else{
cout<<v;
printf(" %.1lf ",a);
cout<<u<<' '<<(int)(*p+0.5)<<"%\n";
}
}
cout<<"Provides no significant amount of:";
for(j=;j<i;++j)cout<<endl<<s[j];
return ;
}
E1062 线段相交
First AC: 2018-05-19 Latest Modification: 2018-05-19
#include<bits/stdc++.h>
using namespace std;
int xa,ya,xb,yb,xc,yc,xd,yd;
double k1,b1,k2,b2,x,y;
bool vert1,vert2;
int main()
{
while(cin>>xa>>ya>>xb>>yb>>xc>>yc>>xd>>yd){
if(xa==xb)vert1=;
else{
vert1=;
k1=(ya-yb)*1.0/(xa-xb);
b1=ya-k1*xa;
}
if(xc==xd)vert2=;
else{
vert2=;
k2=(yc-yd)*1.0/(xc-xd);
b2=yc-k2*xc;
}
bool jdg=;
if(vert1&&vert2){
if((ya-yc)*(ya-yd)<=)jdg=;
if((yb-yc)*(yb-yd)<=)jdg=;
if(xa!=xc)jdg=;
}
else if(vert1){
if((k2*xa+b2-ya)*(k2*xa+b2-yb)<=)jdg=;
if((xa-xc)*(xa-xd)>)jdg=;
}
else if(vert2){
if((k1*xc+b1-yc)*(*xc+b1-yd)<=)jdg=;
if((xc-xa)*(xc-xb)>)jdg=;
}
else{
if(abs(k1-k2)<1e-){
if(abs(b1-b2)<1e-){
if((ya-yc)*(ya-yd)<=)jdg=;
if((yb-yc)*(yb-yd)<=)jdg=;
}
}
else{
x=(b1-b2)/(k2-k1),y=k1*x+b1;
if((x-xa)*(x-xb)<=||(y-ya)*(y-yb)<=)
if((x-xc)*(x-xd)<=||(y-yc)*(y-yd)<=)
jdg=;
}
}
jdg? cout<<"Yes\n":cout<<"No\n";
}
return ;
}
E1064 冒泡排序
First AC: 2017-11-18 Latest Modification: 2017-11-18
#include<iostream>
using namespace std;
int a[];
int n,tmp;
int i,j;
int main()
{
cin>>n;
for(;i<n;++i)cin>>a[i];
for(i=;i<n;++i)
for(j=i+;j<n;++j)
if(a[j]<a[i]){
cout<<a[i]<<' '<<a[j]<<endl;
tmp=a[i],a[i]=a[j],a[j]=tmp;
}
cout<<a[];
for(i=;i<n;++i)cout<<' '<<a[i];
return ;
}
E1065 糖果盒
First AC: 2019-02-25 Latest Modification: 2019-02-25
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,ans;
ll a[][];
ll tmp[];
ll dp[];
ll i,j,k;
int main()
{
cin>>n>>m;
for(i=;i<=n;++i){
for(j=;j<=m;++j){
cin>>a[i][j];
if(!a[i][j])a[i][j]=-1e9;
}
}
for(i=;i<n;++i){
memset(tmp,,sizeof tmp);
for(j=i+;j<=n;++j){
ll mina=;
memset(dp,,sizeof dp);
for(k=;k<=m;++k){
tmp[k]+=a[j][k];
dp[k]=dp[k-]+tmp[k];
ans=max(ans,dp[k]-mina);
mina=min(mina,dp[k]);
}
}
}
cout<<ans;
return ;
}
E1067 石子游戏A
First AC: 2018-02-12 Latest Modification: 2018-02-12
#include<bits/stdc++.h>
using namespace std;
int T,n,ans,tmp;
int main()
{
cin>>T;
while(T--){
cin>>n;
cin>>ans;
while(--n)cin>>tmp,ans^=tmp;
if(ans)cout<<"Win\n";
else cout<<"Lost\n";
}
}
E1068 石子游戏B
First AC: 2018-02-12 Latest Modification: 2018-02-12
#include<bits/stdc++.h>
using namespace std;
int T,n,m,ans,tmp;
int main()
{
cin>>T;
while(T--){
cin>>n>>m>>ans;
++m,ans%=m;
while(--n)cin>>tmp,ans^=(tmp%m);
if(ans)cout<<"Win\n";
else cout<<"Lost\n";
}
}
E1070 A×B
First AC: 2017-10-21 Latest Modification: 2017-10-21
n=input()
while True:
try:
a,b=input().split()
print((int)(a)*(int)(b))
except:
break
E1071 Guessing Game
First AC: 2017-12-13 Latest Modification: 2011-12-13
#include<iostream>
using namespace std;
int n,M,m;
string s;
int main()
{
while(){
M=,m=;
while(cin>>n){
if(!n)return ;
cin>>s>>s;
if(s[]=='h'){if(M>n)M=n;}
else if(s[]=='l'){if(m<n)m=n;}
else break;
}
if(m<n&&n<M)cout<<"Sunny may be honest\n";
else cout<<"Sunny is dishonest\n";
}
}
E1072 小强的单词表
First AC: 2017-12-16 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
struct data{
string s;
int len;
}a[];
int n,i;
bool cmp(data a,data b)
{
int l=a.len<b.len?a.len:b.len;
for(int i=;i<l;++i)if(a.s[i]-b.s[i])return a.s[i]<b.s[i];
return a.len<b.len;
}
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>a[i].s;
a[i].len=a[i].s.length();
}
sort(a,a+n,cmp);
cout<<a[].s;
for(i=;i<n;++i)if(a[i].s!=a[i-].s)cout<<endl<<a[i].s;
return ;
}
E1073 几位数
First AC: 2017-10-09 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
char s[];
int main()
{
while(scanf("%s",s)!=EOF)printf("%d\n",strlen(s));
}
E1074 第一位数字
First AC: 2018-01-07 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
double m,n,j,k;
int main()
{
while(cin>>k,k)
{
m=modf(k*1.0*log10(k*1.0),&j);
m=modf(pow((double),m),&j);
cout<<(int)j<<endl;
}
return ;
}
E1075 庆祝迎评成功
First AC: 2017-10-14 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
while(cin>>n,n!=)cout<<(n*n*n+*n+)/<<endl;
return ;
}
E1076 染气球
First AC: 2018-04-13 Latest Modification: 2018-04-13
Note: 前缀和的思想,把每次涂色用一次操作储存
#include<bits/stdc++.h>
using namespace std;
int n,a,b;
int dp[];
int i;
int main()
{
while(cin>>n,n){
memset(dp,,sizeof(dp));
for(i=;i<n;++i)cin>>a>>b,++dp[a],--dp[b+];
for(i=;i<n;++i)dp[i]+=dp[i-],cout<<dp[i]<<' ';
cout<<dp[n-]+dp[n]<<endl;
}
return ;
}
E1077 图书馆的管理系统
First AC: 2018-03-01 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int m,c,len,ui;
long n,bi;
char op;
int stu[];
int book[];
long i,j;
int main()
{
ios::sync_with_stdio(false);
while(cin>>m>>n,m||n){
++n;
cin>>c;
memset(stu,,sizeof(stu));
memset(book,,sizeof(book));
while(c--){
cin>>op;
if(op=='B'){
cin>>ui>>bi;
if(book[bi])
cout<<"The book is not in the library now";
else if(stu[ui]>)
cout<<"You are not allowed to borrow any more";
else{
cout<<"Borrow success";
++stu[ui],book[bi]=ui;
}
}
else if(op=='R'){
cin>>bi;
if(book[bi]==)
cout<<"The book is already in the library";
else{
cout<<"Return success";
--stu[book[bi]],book[bi]=;
}
}
else{
cin>>ui;
if(stu[ui]==)cout<<"Empty";
else{
int tmp=stu[ui]-;
for(i=;;++i)if(book[i]==ui){
cout<<i;
break;
}
for(j=i+;tmp;++j)if(book[j]==ui){
cout<<' '<<j,--tmp;
}
}
}
cout<<endl;
}
cout<<endl;
}
return ;
}
E1078 小强的生日
First AC: 2017-11-01 Latest Modification: 2017-11-01
#include<iostream>
using namespace std;
int T,m,d,s,count;
int a[]={,,,,,,,,,,,,};
int i,j;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>m>>d;
s=d;
for(j=;j<m;j++)s+=a[j];
if(s<)cout<<-s<<endl;
else if(s==)cout<<"It's today!!\n";
else cout<<"What a pity, it has passed!\n";
}
return ;
}
E1079 大话西游
First AC: 2017-10-09 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int a[][];
int n,i,j;
int main()
{
a[][]=;
a[][]=;
a[][]=;
for(i=;i<;i++){
a[i][]=;
a[i][i]=;
for(j=;j<i;j++){
a[i][j]=a[i-][j-]+a[i-][j];
}
}
while(cin>>n,n!=){
for(i=;i<=n;i++){
cout<<a[i][];
for(j=;j<=i;j++){
cout<<" "<<a[i][j];
}
cout<<"\n";
}
cout<<"\n";
}
return ;
}
E1082 Easy to AC
First AC: 2018-01-07 Latest Modification: 2018-01-07
Note: 当n>2时,(n+1)!=(n+1)×n!>n×n!>sigma(i=1,n)i!
这表明,若a满足n!≤a<(n+1)!,n>2且a不能表为以n!为加数的和式
则a必然无法表为其他任何和式
#include<iostream>
using namespace std;
long long n;
long long a[]={,};
int i;
int main()
{
for(i=;i<;++i)a[i]=i*a[i-];
while(cin>>n){
if(n<)return ;
if(n==){cout<<"NO\n";continue;}
for(i=;i>;--i)if(n>=a[i])n-=a[i];
n<? cout<<"YES\n":cout<<"NO\n";
}
return ;
}
E1084 一条直线上?
First AC: 2019-01-22 Latest Modification: 2019-01-22
#include<bits/stdc++.h>
using namespace std;
int n,rst,cnt;
int x[],y[];
double kl,bl;
int i,j,k;
int main()
{
while(cin>>n,n){
rst=;
for(i=;i<n;++i)cin>>x[i]>>y[i];
for(i=;i<n;++i){
for(j=i+;j<n;++j){
kl=(y[i]-y[j])*1.0/(x[i]-x[j]);
bl=y[i]-kl*x[i];
cnt=;
for(k=;k<n;++k){
if(k==i||k==j)continue;
if(fabs(kl*x[k]+bl-y[k])<1e-)++cnt;
}
rst=max(rst,cnt);
}
}
cout<<rst+<<endl;
}
return ;
}
E1085 Snake
First AC: 2018-01-07 Latest Modification: 2018-01-07
Note: 注意以非正数作为结束标记
#include<iostream>
using namespace std;
int n,num,lft,tmp,rgt;
int i,j;
int main()
{
while(cin>>n){
if(n<=)return ;
if(num)cout<<endl;
cout<<"Case "<<++num<<":\n";
lft=;
for(i=;i<=n;++i){
cout<<lft;
tmp=lft,rgt=n-i+;
for(j=;j<=rgt;++j)tmp+=i+j-,cout<<' '<<tmp;
cout<<endl;
lft+=i;
}
}
}
E1093 Digital Product
First AC: 2018-03-02 Latest Modification: 2018-03-02
Note: 相当于对n质因数分解,如果有大于9的质因数则输出-1
注意到8=2×2×2减少两位2;9=3×3减少一位3;4=2×2减少一位2;等等
可得取模优先级:8>9>6>4>2=3=5=7
注意输入为0时输出为10而非-1
#include<bits/stdc++.h>
using namespace std;
long n;
int _2,_3,_4,_5,_6,_7,_8,_9;
int main()
{
while(~scanf("%ld",&n)){
if(n==){printf("10\n");continue;}
if(n<){printf("%d\n",n);continue;}
_2=_3=_4=_5=_6=_7=_8=_9=;
while(n%==)++_8,n/=;
while(n%==)++_9,n/=;
while(n%==)++_6,n/=;
while(n%==)++_4,n/=;
while(n%==)++_3,n/=;
while(n%==)++_2,n/=;
while(n%==)++_5,n/=;
while(n%==)++_7,n/=;
if(n>){printf("-1\n");continue;}
while(_2--)printf("");
while(_3--)printf("");
while(_4--)printf("");
while(_5--)printf("");
while(_6--)printf("");
while(_7--)printf("");
while(_8--)printf("");
while(_9--)printf("");
printf("\n");
}
return ;
}
E1099 Intersection Lines
First AC: 2018-05-30 Latest Modification: 2018-05-30
#include<bits/stdc++.h>
using namespace std;
int T;
int xa,ya,xb,yb,xc,yc,xd,yd;
double k1,b1,k2,b2,x,y;
bool vert1,vert2;
int main()
{
cin>>T;
cout<<"INTERSECTING LINES OUTPUT\n";
while(T--){
cin>>xa>>ya>>xb>>yb>>xc>>yc>>xd>>yd;
if(xa!=xb){
vert1=;
k1=(ya-yb)*1.0/(xa-xb);
b1=ya-k1*xa;
}
else vert1=;
if(xc!=xd){
vert2=;
k2=(yc-yd)*1.0/(xc-xd);
b2=yc-k2*xc;
}
else vert2=;
if(vert1&&vert2){
if(xa!=xc)cout<<"NONE\n";
else cout<<"LINE\n";
}
else if(vert1){
x=xa;
y=k2*xa+b2;
printf("POINT %.2f %.2f\n",x,y);
}
else if(vert2){
x=xc;
y=k1*xc+b1;
printf("POINT %.2f %.2f\n",x,y);
}
else{
if(abs(k1-k2)<1e-){
if(abs(k1*xc+b1-yc)<1e-)cout<<"LINE\n";
else cout<<"NONE\n";
}
else{
x=(b1-b2)*1.0/(k2-k1);
y=k1*x+b1;
printf("POINT %.2f %.2f\n",x,y);
}
}
}
cout<<"END OF OUTPUT";
return ;
}
E1104 Bitmap
First AC: 2018-09-14 Latest Modification: 2018-09-14
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,tmp;
string s;
bool mp[][];
int rst[][];
queue<int>qx,qy;
int i,j;
bool ok(int x,int y)
{
if(!x||x>n)return ;
if(!y||y>m)return ;
if(rst[x][y]<)return ;
return ;
}
int main()
{
cin>>n>>m;
getline(cin,s);
memset(rst,0x3f,sizeof rst);
for(i=;i<=n;++i){
for(j=;j<=m;++j){
mp[i][j]=(getchar()=='');
if(mp[i][j]){
rst[i][j]=;
qx.push(i);
qy.push(j);
}
}
getchar();
}
while(!qx.empty()){
x=qx.front(),qx.pop();
y=qy.front(),qy.pop();
tmp=rst[x][y]+;
if(ok(x-,y))rst[x-][y]=tmp,qx.push(x-),qy.push(y);
if(ok(x,y-))rst[x][y-]=tmp,qx.push(x),qy.push(y-);
if(ok(x,y+))rst[x][y+]=tmp,qx.push(x),qy.push(y+);
if(ok(x+,y))rst[x+][y]=tmp,qx.push(x+),qy.push(y);
}
for(i=;i<=n;++i){
for(j=;j<m;++j)cout<<rst[i][j]<<' ';
cout<<rst[i][m]<<endl;
}
return ;
}
E1105 汽车加油
First AC: 2018-01-05 Latest Modification: 2018-01-05
Note: 从终点向起点判断,在保证能抵达终点的前提下,能不加油便不加油,理由如下
上述算法确定了一种走法X(在有解的前提下)
设X在x1,x2,…,xm处加了油
对于其他任何走法Y,根据X的选取知
Y在[xm,End)必有加油,改为只在xm加油,仍为可行解;
……
Y在[xi,xj)必有加油,改为只在xi加油,仍为可行解;
……
Y在[Start,x1)必有加油,改为只在起点加油,仍为可行解
加油站是有限的,这表明,Y总能经过有限次操作,得到加油次数不增多的走法X
即X是问题的一个最优解
#include<iostream>
using namespace std;
int n,k,tmp,rst;
int a[];
int i,j;
int main()
{
cin>>n>>k;
++k;
for(i=;i<k;++i)cin>>a[i];
for(tmp=n,rst=-,i=k-;i>=;--i){
if(a[i]<=tmp)tmp-=a[i];
else if(a[i]>n){
cout<<"No Solution!";
return ;
}
else ++rst,tmp=n-a[i];
}
if(tmp)++rst;
cout<<rst;
return ;
}
E1106 Number Steps
First AC: 2018-01-07 Latest Modification: 2018-01-07
Note: 分成四个等差数列即可
#include<iostream>
using namespace std;
int T,x,y;
int main()
{
cin>>T;
while(T--){
cin>>x>>y;
if(x==y){
if(x&)cout<<*x-<<endl;
else cout<<*x<<endl;
}
else if(y+==x){
if(x&)cout<<*x-<<endl;
else cout<<*x-<<endl;
}
else cout<<"No Number\n";
}
return ;
}
E1109 Max Sum
First AC: 2017-11-07 Latest Modification: 2018-03-02
#include<bits/stdc++.h>
using namespace std;
int T,n,a[],temp,Max;
int i,j,k;
int main()
{
ios::sync_with_stdio(false);
cin>>T;
for(i=;i<T;++i){
cin>>n;
Max=-;
for(j=;j<n;++j)cin>>a[j];
for(j=;j<n;++j){
temp=a[j];
for(k=j+;k<n;++k){
if(temp>Max)Max=temp;
temp+=a[k];
}
if(temp>Max)Max=temp;
}
cout<<Max<<endl;
}
return ;
}
E1110 最长路
First AC: 2019-02-04 Latest Modification: 2019-02-04
#include<bits/stdc++.h>
using namespace std;
int n,m,p;
int x,y,val;
int dis[][];
int i,j,k;
int main()
{
while(cin>>n>>m){
memset(dis,0x3f,sizeof dis);
for(i=;i<;++i)dis[i][i]=;
while(m--){
cin>>x>>y>>val;
dis[x][y]=-val;
}
for(k=;k<=n;++k){
for(i=;i<=n;++i){
for(j=;j<=n;++j){
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
cin>>p;
while(p--){
cin>>x>>y;
if(dis[x][y]>1e8)cout<<"-1\n";
else cout<<-dis[x][y]<<endl;
}
}
return ;
}
E1111 数塔
First AC: 2018-01-01 Latest Modification: 2018-01-01
Note: 从倒数第二层往上,到达每层后后续的最小值可以逐步确定
#include<iostream>
using namespace std;
int T,n;
int a[][];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<=n;++i)
for(j=;j<=i;++j)
cin>>a[i][j];
for(i=n-;i;--i)
for(j=;j<=n;++j)
a[i][j]+=min(a[i+][j],a[i+][j+]);
cout<<a[][]<<endl;
}
return ;
}
E1113 装箱问题
First AC: 2017-12-10 Latest Modification: 2018-03-02
#include<bits/stdc++.h>
#define max(A,B) A>B? (A):(B)
using namespace std;
int f[][],a[];
int v,n;
int i,j;
int main()
{
while(cin>>v>>n){
for(i=;i<=n;++i)cin>>a[i];
for(i=;i<=n;++i)for(j=;j<=v;++j){
if(j<a[i])f[i][j]=f[i-][j];
else f[i][j]=max(f[i-][j],f[i-][j-a[i]]+a[i]);
}
cout<<v-f[n][v]<<endl;
}
return ;
}
E1114 素数环
First AC: 2018-08-08 Latest Modification: 2018-08-08
#include<bits/stdc++.h>
using namespace std;
int n;
bool pri[];
bool use[];
int tmp[];
int i,j;
void print()
{
for(i=;i<n;++i)cout<<tmp[i]<<' ';
cout<<tmp[n]<<endl;
}
void dfs(int now)
{
if(now>n){
if(!pri[tmp[]+tmp[n]])print();
return;
}
for(int i=;i<=n;++i){
if(!use[i]&&!pri[tmp[now-]+i]){
use[i]=;
tmp[now]=i;
dfs(now+);
use[i]=;
}
}
}
int main()
{
cin>>n;
for(i=;i<;++i)
if(!pri[i])
for(j=i+i;j<;j+=i)
pri[j]=;
use[]=;
tmp[]=;
dfs();
return ;
}
E1117 剩余定理
First AC: 2017-11-02 Latest Modification: 2018-03-02
#include<bits/stdc++.h>
using namespace std;
long long T,M,a[],b[],c[],sum,temp1,temp2,n;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
sum=,n=;
cin>>M;
for(j=;j<M;++j)cin>>a[j],sum*=a[j];
for(j=;j<M;++j){cin>>b[j];if(b[j]==)b[j]=a[j];}
for(j=;j<M;++j){
c[j]=sum/a[j];
temp1=c[j]%a[j];
for(temp2=b[j];;temp2+=a[j])
if(temp2%temp1==){c[j]*=(temp2/temp1);break;}
}
for(j=;j<M;++j){
n+=c[j];
if(n>sum)n-=sum;
}
cout<<n<<endl;
}
return ;
}
E1118 Biker’s Trip Odometer
First AC: 2018-03-02 Latest Modification: 2018-03-02
#include<bits/stdc++.h>
using namespace std;
double d,t,m,v;
int r,cnt=;
int main()
{
while(cin>>d>>r>>t,r){
cout<<"Trip #"<<cnt<<": ";
++cnt;
m=d*r*0.0000495832;
v=*m/t;
printf("%.2f %.2f\n",m,v);
}
return ;
}
E1119 Candy Sharing Game
First AC: 2018-03-02 Latest Modification: 2018-03-02
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
int a[],b[];
int i;
bool jdg()
{
for(i=;i<n;++i)
if(a[i]!=a[i-])return ;
return ;
}
void op()
{
b[]=a[n-]/-a[]/;
for(i=;i<n;++i)b[i]=a[i-]/-a[i]/;
for(i=;i<n;++i)a[i]=(a[i]+b[i]+)/*;
}
int main()
{
while(cin>>n,n){
for(i=;i<n;++i)cin>>a[i];
while(jdg())op(),++cnt;
cout<<cnt<<' '<<a[]<<endl;
cnt=;
}
return ;
}
E1124 整数幂
First AC: 2017-11-03 Latest Modification: 2017-11-03
#include<iostream>
using namespace std;
int a,b,c,s,i;
int main()
{
while(cin>>a>>b>>c,a&&b&&c){
for(i=,s=;i<b;++i)s*=a,s%=c;
cout<<s<<endl;
}
return ;
}
E1125 Tr A
First AC: 2018-06-18 Latest Modification: 2018-06-18
#include<bits/stdc++.h>
using namespace std;
const int mod=;
int T,n,k,tmp;
int base[][];
int rst[][];
int i,j;
void mul(int (*A)[],int (*B)[])
{
int C[][];
memset(C,,sizeof(C));
int i,j,k;
for(i=;i<n;++i)
for(j=;j<n;++j)
for(k=;k<n;++k)
C[i][j]+=A[i][k]*B[k][j]%mod;
for(i=;i<n;++i)
for(j=;j<n;++j)
A[i][j]=C[i][j]%mod;
}
void pow(int k)
{
while(k){
if(k&)mul(rst,base);
mul(base,base);
k/=;
}
}
int main()
{
cin>>T;
while(T--){
cin>>n>>k;
for(i=;i<n;++i)for(j=;j<n;++j)cin>>base[i][j];
memset(rst,,sizeof(rst));
for(i=;i<n;++i)rst[i][i]=;
pow(k);
tmp=;
for(i=;i<n;++i)tmp+=rst[i][i];
cout<<tmp%mod<<endl;
}
return ;
}
E1126 最近点对
First AC: 2017-12-14 Latest Modification: 2017-12-14
Note: 最近点对二分算法
#include<bits/stdc++.h>
#define min(A,B) A<B? A:B
#define max(A,B) A>B? A:B
using namespace std;
long long n,i,j,k,cnt,lft,rgt;
long long a[];
struct point{double x,y;}p[];
bool cmpx(point a,point b){return a.x<b.x;}
bool cmpy(long long a,long long b){return p[a].y<p[b].y;}
double dis(point a,point b){return (double)sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
double closest(long long low,long long high)
{
if(low+==high)return dis(p[low],p[high]);
if(low+==high)return min(dis(p[low],p[low+]),min(dis(p[low+],p[high]),dis(p[low],p[high])));
long long mid=(low+high)>>;
double ans=min(closest(low,mid),closest(mid+,high));//rst>=ans
if(!ans)return 0.0;
lft=p[mid].x-ans-,rgt=p[mid].x+ans+;
for(cnt=,i=low;i<=high;++i){
if(p[i].x>lft&&p[i].x<rgt)a[cnt++]=i;
if(p[i].x>rgt)break;
}
sort(a,a+cnt,cmpy);
for(i=;i<cnt;++i)
for(j=i+;j<cnt;++j){
if(p[a[j]].y-p[a[i]].y>=ans)break;
ans=min(dis(p[a[i]],p[a[j]]),ans);
}
return ans;
}
int main()
{
while(scanf("%lld",&n)){
if(n){
for(i=;i<n;++i)scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p,p+n,cmpx);
printf("%.2lf\n",closest(,n-)/);
}
else return ;
}
}
E1127 多边形面积
First AC: 2017-12-01 Latest Modification: 2018-03-03
Note: 平移到第一象限,利用向量外积的几何意义
#include<bits/stdc++.h>
using namespace std;
int n,i,x[],y[],sum,mx,my;
int main()
{
while(cin>>n,n){
for(i=sum=mx=my=;i<n;++i){
cin>>x[i]>>y[i];
if(x[i]<mx)mx=x[i];
if(y[i]<my)my=y[i];
}
for(i=;i<n;++i)x[i]+=mx+,y[i]+=my+;
for(i=;i<n;++i)sum+=x[i-]*y[i]-x[i]*y[i-];
sum+=x[n-]*y[]-x[]*y[n-];
printf("%.1f\n",sum/2.0);
}
return ;
}
E1128 母牛的故事
First AC: 2017-10-29 Latest Modification: 2018-03-03
#include<bits/stdc++.h>
using namespace std;
int i,most2[]={,,,,},least3[]={,,,,};
int main()
{
for(i=;i<;i++){
most2[i+]=least3[i]+least3[i-];
least3[i+]=least3[i]+least3[i-];}
while(cin>>i,i!=)cout<<least3[i+]<<endl;
return ;
}
E1129 考新郎
First AC: 2017-10-29 Latest Modification: 2017-10-29
#include<iostream>
using namespace std;
long long a[]={,,},s;
int i,n,m;
int main()
{
for(i=;i<;++i)a[i]=(i-)*(a[i-]+a[i-]);
cin>>n;
while(cin>>n>>m){
s=;
for(i=;i<=m;++i)s*=(n+-i),s/=i;
s*=a[m];
cout<<s<<endl;
}
return ;
}
E1131 Tree Recovery
First AC: 2019-01-06 Latest Modification: 2019-01-06
#include<bits/stdc++.h>
using namespace std;
string s,t;
int len;
void dfs(string s,string t,int len)
{
if(!len)return;
char c=s[];
int i;
for(i=;i<len;++i){
if(t[i]==c)break;
}
dfs(s.substr(,i+),t.substr(,i),i);
dfs(s.substr(i+,len-i-),t.substr(i+,len-i-),len-i-);
cout<<c;
}
int main()
{
cin>>s>>t;
len=s.length();
dfs(s,t,len);
cout<<endl;
return ;
}
E1138 Dick and Jane
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 假设乌龟年龄为x,则分类讨论易得,三只宠物的年龄可能情况如下
y=s+p时,(y+x,p+x,x)或(y+x+1,p+x,x)或(y+x+1,p+x+1,x)
y=s+p+1时,(y+x,p+x,x)或(y+x,p+x+1,x)或(y+x+1,p+x+1,x)
所以只要对12+j-y-p模3分类即可
#include<bits/stdc++.h>
using namespace std;
int s,p,y,j,tmp,x,jdg;
int main()
{
while(cin>>s>>p>>y>>j){
if(s+p==y){
tmp=+j-y-p;
x=tmp/,jdg=tmp%;
if(jdg==)printf("%d %d %d\n",y+x,p+x,x);
else if(jdg==)printf("%d %d %d\n",y+x+,p+x,x);
else printf("%d %d %d\n",y+x+,p+x+,x);
}
else{
tmp=+j-y-p;
x=tmp/,jdg=tmp%;
if(jdg==)printf("%d %d %d\n",y+x,p+x,x);
else if(jdg==)printf("%d %d %d\n",y+x,p+x+,x);
else printf("%d %d %d\n",y+x+,p+x+,x);
}
}
return ;
}
E1147 进制转换
First AC: 2017-10-24 Latest Modification: 2018-03-03
#include<bits/stdc++.h>
using namespace std;
int T,n,r,cnt,i,j,a[];
char c;
int main()
{
cin>>T;
for(j=;j<T;++j){
cin>>n>>r;
if(n<)cout<<'-',n=-n;
if(n==)cout<<"0\n";
else{
for(i=;i>;--i)a[i]=n%r,n/=r;
for(i=;;++i)if(a[i]!=){cnt=i;break;}
for(i=cnt;i<;++i)
if(a[i]<)cout<<a[i];
else cout<<(char)(a[i]-+'A');
}
cout<<endl;
}
return ;
}
E1150 Combination Lock
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 根据样例,注意时钟上数字是逆时针递增排列的
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d;
int main()
{
while(cin>>a>>b>>c>>d,a||b||c||d)
cout<<+*((a-b+)%+(c-b+)%+(c-d+)%)<<endl;
return ;
}
E1156 Card Hands
First AC: 2018-06-19 Latest Modification: 2018-06-19
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
bool jdg;
struct node *nxt[];
}MT;
int n,m,num,rst;
string s[];
int i,j;
int main()
{
while(cin>>n,n){
MT *head=new MT;
head->jdg=;
rst=;
for(i=;i<;++i)head->nxt[i]=NULL;
while(n--){
cin>>m;
for(i=;i<m;++i){
cin>>s[i];
if(s[i].length()==){
s[i][]=''+;
s[i][]=s[i][];
}
else if(s[i][]=='A')s[i][]='';
else if(s[i][]=='J')s[i][]=''+;
else if(s[i][]=='Q')s[i][]=''+;
else if(s[i][]=='K')s[i][]=''+;
}
MT *tmp=head;
for(i=m-;i>=;--i){
num=(int)(log(s[i][]-'A')/log()-0.5)*+s[i][]-'';
if(tmp->nxt[num]==){
tmp->nxt[num]=new MT;
tmp->nxt[num]->jdg=;
for(j=;j<;++j)tmp->nxt[num]->nxt[j]=NULL;
}
tmp=tmp->nxt[num];
if(tmp->jdg==)++rst,tmp->jdg=;
}
}
cout<<rst<<endl;
}
return ;
}
E1159 How Many 0’s?
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 注意到每个数位上的数字都具有周期性,周期均为10
把0单列,可以让每个数位都从1开始循环,从而简化运算
对第i个数位的0累加时,用n+1减去最小的i位数,再/10,%10
别漏了%10部分,如1-197中十位上0的个数除(197-9)/100*10外还有(197-9)%100-90
#include<bits/stdc++.h>
using namespace std;
long long m,n;
long long cnt(long long n)
{
if(n<)return n;
long long base=,rst=;
while(n>base){
rst+=(n-base)/(*base+)*(base+);
rst+=max((long long),(n-base)%(*base+)-*base-);
base=*base+;
}
return rst;
}
int main()
{
while(cin>>m>>n,m!=-||n!=-){
cout<<cnt(n)-cnt(m-)<<endl;
}
return ;
}
E1160 Date with MM
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 注意有多组数据,且M=N时输出0.000而非1.000
#include<bits/stdc++.h>
using namespace std;
double m,n,t,k;
int main()
{
while(cin>>m>>n>>t){
k=n-m;
if(k<=)cout<<"0.000\n";
else if(t>=k)cout<<"0.500\n";
else printf("%.3f\n",(*k*t-t*t)/(*k*k));
}
return ;
}
E1163 Easy to Do DP
First AC: 2017-12-15 Latest Modification: 2018-03-03
#include<bits/stdc++.h>
using namespace std;
long long w[],dp[];
long long i,j;
int main()
{
int n;
while(cin>>n){
memset(dp,,sizeof(dp));
long long sum=;
for(i=;i<=n;++i)cin>>w[i],sum+=w[i];
for(i=;i<=n;++i)for(j=sum/;j>=w[i];--j)
dp[j]=max(dp[j-w[i]]+w[i],dp[j]);
cout<<sum-*dp[sum/]<<endl;
}
return ;
}
E1165 Palindrome Problem
First AC: 2018-08-09 Latest Modification: 2018-08-09
#include<bits/stdc++.h>
using namespace std;
string s,t,r;
int len;
int i;
int main()
{
while(getline(cin,s),s!="THE END."){
len=s.length();
t=r="";
for(i=;i<len;++i){
if(s[i]>='a'&&s[i]<='z')t+=s[i];
else if(s[i]>='A'&&s[i]<='Z')t+=(char)(s[i]+);
}
for(i=len-;i>=;--i){
if(s[i]>='a'&&s[i]<='z')r+=s[i];
else if(s[i]>='A'&&s[i]<='Z')r+=(char)(s[i]+);
}
t==r? cout<<"Yes\n":cout<<"No\n";
}
return ;
}
E1180 Inglish-Number Translator
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 按顺序累加,遇到hundred,thousand,million分别乘100,1000,1000000即可
但注意题干1500的表达,发现只有先出现大数再出现小数上一行才成立
于是引入bool h,t,m描述此前是否出现过hundred和thousand
考虑到英文表达中million较特殊,把m改为int单独储存大于999999的部分
注意输入结束标志,按单词读入可能会RE,可以考虑getline
#include<bits/stdc++.h>
using namespace std;
string r,s;
int len,i;
long long tmp,rst;
bool h,t;int m;
char c;
int main()
{
while(){
getline(cin,r);
if(r=="")break;
len=r.length();
for(i=;i<=len;++i){
if(i==len||r[i]==' '){
if(s=="negative")cout<<'-';
else if(s=="one")tmp+=;
else if(s=="two")tmp+=;
else if(s=="three")tmp+=;
else if(s=="four")tmp+=;
else if(s=="five")tmp+=;
else if(s=="six")tmp+=;
else if(s=="seven")tmp+=;
else if(s=="eight")tmp+=;
else if(s=="nine")tmp+=;
else if(s=="ten")tmp+=;
else if(s=="eleven")tmp+=;
else if(s=="twelve")tmp+=;
else if(s=="thirteen")tmp+=;
else if(s=="fourteen")tmp+=;
else if(s=="fifteen")tmp+=;
else if(s=="sixteen")tmp+=;
else if(s=="seventeen")tmp+=;
else if(s=="eighteen")tmp+=;
else if(s=="nineteen")tmp+=;
else if(s=="twenty")tmp+=;
else if(s=="thirty")tmp+=;
else if(s=="forty")tmp+=;
else if(s=="fifty")tmp+=;
else if(s=="sixty")tmp+=;
else if(s=="seventy")tmp+=;
else if(s=="eighty")tmp+=;
else if(s=="ninety")tmp+=;
else if(s=="hundred")rst+=*tmp,tmp=,h=;
else if(s=="thousand"){
if(h)rst=(rst+tmp)*;
else rst+=*tmp;
tmp=,t=;
}
else if(s=="million"){
m=rst+tmp;
rst=tmp=;
h=t=;
}
s="";
}
else s+=r[i];
}
cout<<*m+rst+tmp<<endl;
rst=tmp=m=h=t=;
}
return ;
}
E1182 Relative Relatives
First AC: 2018-05-17 Latest Modification: 2018-05-17
#include<bits/stdc++.h>
using namespace std;
int T,n;
struct data{
string f,c;
int y;
}a[];
queue<string>q;
string name;
int year;
map<string,int>mp;
map<string,int>::iterator it;
int i,j,k;
bool cmp(data a,data b)
{
if(a.y!=b.y)return a.y>b.y;
return a.f<b.f;
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n;
for(j=;j<n;++j)cin>>a[j].f>>a[j].c>>a[j].y;
q.push("Ted");
mp.insert(pair<string,int>("Ted",));
while(!q.empty()){
name=q.front();
q.pop();
it=mp.find(name);
year=it->second;
for(j=;j<n;++j){
if(a[j].f==name){
q.push(a[j].c);
mp.insert(pair<string,int>(a[j].c,year-a[j].y));
}
}
}
j=;
for(it=mp.begin();it!=mp.end();++it){
a[j].f=it->first;
a[j++].y=it->second;
}
mp.clear();
sort(a,a+j,cmp);
cout<<"DATASET "<<i<<endl;
for(k=;k<j;++k)cout<<a[k].f<<' '<<a[k].y<<endl;
}
return ;
}
E1185 No Brainer
First AC: 2017-12-13 Latest Modification: 2017-12-13
#include<cstdio>
int main()
{
int T,a,b;
scanf("%d",&T);
while(T--){
scanf("%d%d",&a,&b);
a<b? printf("NO"):printf("MMM");
printf(" BRAINS\n");
}
return ;
}
E1191 Electrical Outlets
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 每个outlet使接口+=strip-1
#include<bits/stdc++.h>
using namespace std;
int T,n,tmp,rst;
int main()
{
cin>>T;
while(T--){
cin>>n;
rst=-n+;
while(n--)cin>>tmp,rst+=tmp;
cout<<rst<<endl;
}
return ;
}
E1200 聪明的张小强
First AC: 2017-10-28 Latest Modification: 2017-10-28
#include<iostream>
using namespace std;
int c[],d[],e[],i;
string p,q,t,r;
void plusi(string a,string b){
int lena,lenb,temp,num,count,i,j;
for(i=;i<;++i)c[i]=d[i]=e[i]=;
num=,lena=a.length(),lenb=b.length();
for(i=lena-,j=;i>=;--i,--j)c[j]=(int)(a[i]-'');
for(i=lenb-,j=;i>=;--i,--j)d[j]=(int)(b[i]-'');
for(i=;i>=;--i){
if(c[i]+d[i]+num>){
e[i+]=c[i]+d[i]+num-,num=;
if(i==)e[]=;
}
else e[i+]=c[i]+d[i]+num,num=;
}
for(i=;i<;++i)if(e[i]!=)break;
for(j=i;j<;++j)cout<<e[j];
cout<<endl;
}
void minusi(string a,string b){
string s;
int lena,lenb,temp,num,count,i,j;
for(i=;i<;++i)c[i]=d[i]=e[i]=;
num=,lena=a.length(),lenb=b.length(),count=;
if(lena<lenb)
cout<<'-',s=a,a=b,b=s,temp=lena,lena=lenb,lenb=temp;
else if(lena==lenb)
for(i=;i<lena;++i){
if(a[i]>b[i])break;
if(a[i]<b[i]){cout<<'-',s=a,a=b,b=s;break;}
}
for(i=lena-,j=;i>=;--i,--j)c[j]=(int)(a[i]-'');
for(i=lenb-,j=;i>=;--i,--j)d[j]=(int)(b[i]-'');
for(i=;i>=;--i){
if(c[i]-d[i]-num<){
e[i+]=c[i]-d[i]-num+,num=;
}
else e[i+]=c[i]-d[i]-num,num=;
}
for(i=;i<;++i)if(e[i]!=)break;
for(j=i;j<;++j)cout<<e[j],count++;
if(count==)cout<<'';
cout<<endl;
}
int main()
{
cin>>p;
while(cin>>p>>q){
if(p[]!='+'&&p[]!='-'&&q[]!='+'&&q[]!='-')
plusi(p,q);
else if(p[]!='+'&&p[]!='-'&&q[]=='+')
t=q.substr(),plusi(p,t);
else if(p[]!='+'&&p[]!='-'&&q[]=='-')
t=q.substr(),minusi(p,t);
else if(p[]=='+'&&q[]!='+'&&q[]!='-')
t=p.substr(),plusi(t,q);
else if(p[]=='+'&&q[]=='+')
t=p.substr(),r=q.substr(),plusi(t,r);
else if(p[]=='+'&&q[]=='-')
t=p.substr(),r=q.substr(),minusi(t,r);
else if(p[]=='-'&&q[]!='+'&&q[]!='-')
t=p.substr(),minusi(q,t);
else if(p[]=='-'&&q[]=='+')
t=p.substr(),r=q.substr(),minusi(r,t);
else cout<<'-',t=p.substr(),r=q.substr(),plusi(t,r);
}
return ;
}
E1201 Play On Words
First AC: 2019-02-10 Latest Modification: 2019-02-10
#include<bits/stdc++.h>
using namespace std;
int T,n;
int in[],out[];
int vis[],pre[],head;
string s;
int cntin,cntout;
int i;
int find(int x)
{
int r=x,i=x,j;
while(pre[r]!=r)r=pre[r];
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
pre[fx]=fy;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
memset(in,,sizeof in);
memset(out,,sizeof out);
memset(vis,,sizeof vis);
for(i=;i<;++i)pre[i]=i;
while(n--){
cin>>s;
int fr=s[]-'a',to=s[s.length()-]-'a';
head=to;
++in[fr];
++out[to];
join(fr,to);
vis[fr]=vis[to]=;
}
cntin=cntout=;
for(i=;i<;++i){
cntin+=max(in[i]-out[i],);
cntout+=max(out[i]-in[i],);
}
bool flag=;
head=find(head);
for(i=;i<;++i){
if(vis[i]==&&find(i)!=head){
flag=;
break;
}
}
if(flag&&cntin+cntout<=){
cout<<"Ordering is possible.\n";
}
else{
cout<<"The door cannot be opened.\n";
}
}
return ;
}
E1207 Keep on Truck in
First AC: 2018-01-07 Latest Modification: 2018-01-07
#include<iostream>
using namespace std;
int a,b,c;
int main()
{
cin>>a>>b>>c;
if(a<=)cout<<"CRASH "<<a;
else if(b<=)cout<<"CRASH "<<b;
else if(c<=)cout<<"CRASH "<<c;
else cout<<"NO CRASH";
}
E1210 Exact Change Only
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 考虑情形0.40 1 4 5 0,可见1×25+1×5=3×10
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,s;
int numa,numb,numc;
string t;
int main()
{
while(cin>>t>>a>>b>>c>>d){
s=*t[]+*t[]+t[]-*'';
if(s/>a)numa=a,s-=*a;
else numa=s/,s%=;
if(s/>b)numb=b,s-=*b;
else numb=s/,s%=;
if(s/>c)numc=c,s-=*c;
else numc=s/,s%=;
b-=numb;
if(s>d)
while(){
if(numa&&s>&&b>)
--numa,s-=,numb+=,b-=;
else if(numa&&s>&&b>)
--numa,--numc,numb+=,b-=;
else{
if(s>d){
cout<<"NO EXACT CHANGE\n";
break;
}
else{
cout<<numa<<' '<<numb<<' '<<numc<<' '<<s<<endl;
break;
}
}
if(s<=d){
cout<<numa<<' '<<numb<<' '<<numc<<' '<<s<<endl;
break;
}
}
else cout<<numa<<' '<<numb<<' '<<numc<<' '<<s<<endl;
}
return ;
}
E1214 Window Pains
First AC: 2018-04-14 Latest Modification: 2018-04-14
#include<bits/stdc++.h>
using namespace std;
string s;
int a[][];
long i,j;
bool jdg()
{
if(a[][]!=||a[][]!=||a[][]!=||a[][]!=)return ;
if(a[][]==){
if(a[][]!=||a[][]!=)return ;
}
else if(a[][]==){
if(a[][]!=||(a[][]!=&&a[][]!=))return ;
}
else if(a[][]==){
if(a[][]!=||(a[][]!=&&a[][]!=))return ;
}
else if(a[][]==){
if(a[][]!=&&a[][]!=)return ;
if(a[][]!=&&a[][]!=)return ;
}
else return ;
if(a[][]==){
if(a[][]!=||(a[][]!=&&a[][]!=))return ;
}
else if(a[][]==){
if(a[][]!=||a[][]!=)return ;
}
else if(a[][]==){
if(a[][]!=&&a[][]!=)return ;
if(a[][]!=&&a[][]!=)return ;
}
else if(a[][]==){
if(a[][]!=||(a[][]!=&&a[][]!=))return ;
}
else return ;
if(a[][]==){
if(a[][]!=||(a[][]!=&&a[][]!=))return ;
}
else if(a[][]==){
if(a[][]!=&&a[][]!=)return ;
if(a[][]!=&&a[][]!=)return ;
}
else if(a[][]==){
if(a[][]!=||a[][]!=)return ;
}
else if(a[][]==){
if(a[][]!=||(a[][]!=&&a[][]!=))return ;
}
else return ;
if(a[][]==){
if(a[][]!=&&a[][]!=)return ;
if(a[][]!=&&a[][]!=)return ;
}
else if(a[][]==){
if(a[][]!=||(a[][]!=&&a[][]!=))return ;
}
else if(a[][]==){
if(a[][]!=||(a[][]!=&&a[][]!=))return ;
}
else if(a[][]==){
if(a[][]!=||a[][]!=)return ;
}
else return ;
if(a[][]==&&a[][]==&&a[][]==){
if(a[][]==&&a[][]==&&(a[][]==||a[][]==))return ;
return ;
}
if(a[][]==&&a[][]==&&a[][]==&&a[][]==)return ;
if(a[][]==&&a[][]==&&a[][]==){
if(a[][]==&&a[][]==&&(a[][]==||a[][]==))return ;
return ;
}
if(a[][]==&&a[][]==&&a[][]==&&a[][]==)return ;
if(a[][]==&&a[][]==&&a[][]==&&a[][]==){
if(a[][]==a[][]||a[][]==a[][])return ;
if(a[][]==a[][]||a[][]==a[][])return ;
if(a[][]==&&a[][]==&&a[][]==&&a[][]==)return ;
if(a[][]==&&a[][]==&&a[][]==&&a[][]==)return ;
return ;
}
if(a[][]==&&a[][]==&&a[][]==&&a[][]==)return ;
if(a[][]==&&a[][]==&&a[][]==){
if(a[][]==&&a[][]==&&(a[][]==||a[][]==))return ;
return ;
}
if(a[][]==&&a[][]==&&a[][]==&&a[][]==)return ;
if(a[][]==&&a[][]==&&a[][]==){
if(a[][]==&&a[][]==&&(a[][]==||a[][]==))return ;
return ;
}
return ;
}
int main()
{
while(cin>>s,s[]!='E'){
for(i=;i<;++i)for(j=;j<;++j)cin>>a[i][j];
cin>>s;
cout<<"THESE WINDOWS ARE ";
jdg()? cout<<"CLEAN\n":cout<<"BROKEN\n";
}
return ;
}
E1215 Easy Game 1
First AC: 2019-03-03 Latest Modification: 2019-03-03
Note: 若n是奇数,先手取一个必胜,这表明没人愿意取完剩下奇数个
故若n是偶数,可以两两配对作为整体,当成n/2个来处理
#include<bits/stdc++.h>
using namespace std;
int n;
bool cal(int n)
{
if(n%)return ;
if(n==)return ;
return cal(n/);
}
int main()
{
while(cin>>n){
cal(n)? cout<<"Win\n":cout<<"Lost\n";
}
return ;
}
E1217 Easy Game 3
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 甲必胜,当且仅当n不是斐波那契数
#include<bits/stdc++.h>
using namespace std;
bool jdg[]={,,};
int a=,b=,c,n;
int main()
{
while((c=a+b)<)jdg[c]=,a=b,b=c;
while(cin>>n)cout<<(jdg[n]? "Lost\n":"Win\n");
return ;
}
E1219 Snake Again
First AC: 2018-01-07 Latest Modification: 2018-01-07
#include<iostream>
using namespace std;
long long n,cnt;
int i;
int main()
{
while(cin>>n){
for(cnt=,i=;;++i){
cnt+=i;
if(cnt>=n)break;
}
if(i&)cout<<n<<endl;
else cout<<i+-(n-(cnt-i))+cnt-i<<endl;
}
return ;
}
E1221 值班
First AC: 2017-10-09 Latest Modification: 2017-10-28
#include<bits/stdc++.h>
using namespace std;
int a[];
int s=,N,n,i,sum;
int main()
{
cin>>N;
while(sum<N){
cin>>n;
if(n>)a[n]=;
else sum++;
}
for(i=;i<;i++){
s*=a[i];
}
if(s==)cout<<"no";
else cout<<"yes";
return ;
}
E1224 简单迷宫问题
First AC: 2019-01-25 Latest Modification: 2019-01-25
#include<bits/stdc++.h>
using namespace std;
int n,m;
int sx,sy,tx,ty;
char mp[][];
int dx[]={,,-, };
int dy[]={,, ,-};
struct data{
int x,y,cnt;
}s;
bool operator < (const struct data &a,const struct data &b)
{
return a.cnt>b.cnt;
}
int i,j;
void bfs()
{
s.x=sx,s.y=sy,s.cnt=;
priority_queue<struct data>q;
q.push(s);
mp[sx][sy]='#';
while(!q.empty()){
struct data tmp=q.top();
q.pop();
if(tmp.x==tx&&tmp.y==ty){
cout<<tmp.cnt<<endl;
return;
}
struct data nxt;
for(int i=;i<;++i){
nxt.x=tmp.x+dx[i];
nxt.y=tmp.y+dy[i];
if(mp[nxt.x][nxt.y]=='#')continue;
if(mp[nxt.x][nxt.y]=='X')nxt.cnt=tmp.cnt+;
else nxt.cnt=tmp.cnt+;
q.push(nxt);
mp[nxt.x][nxt.y]='#';
}
}
cout<<"impossible\n";
}
int main()
{
while(cin>>n>>m){
getchar();
memset(mp,'#',sizeof mp);
for(i=;i<=n;++i){
for(j=;j<=m;++j){
mp[i][j]=getchar();
if(mp[i][j]=='S')sx=i,sy=j;
if(mp[i][j]=='T')tx=i,ty=j;
}
getchar();
}
bfs();
}
return ;
}
E1225 Play a Game
First AC: 2017-10-28 Latest Modification: 2017-10-28
#include<bits/stdc++.h>
using namespace std;
int a,b,c,rst;
int main()
{
while(cin>>a>>b){
c=,rst=a*b;
while(c)c=a%b,a=b,b=c;
cout<<rst/a<<endl;
}
return ;
}
E1226 Stones Game
First AC: 2019-01-06 Latest Modification: 2019-01-06
#include<bits/stdc++.h>
using namespace std;
bool jdg[]={,,,,,};
int n;
int i;
int main()
{
for(i=;i<;++i){
if(!jdg[i-]||!jdg[i-]||!jdg[i-])jdg[i]=;
else jdg[i]=;
}
while(cin>>n,n){
if(jdg[n])cout<<"Win\n";
else cout<<"Lost\n";
}
return ;
}
E1256 Cable Master
First AC: 2017-10-28 Latest Modification: 2017-10-28
#include<bits/stdc++.h>
using namespace std;
int n,k;
double a[];
int i;
double find(double lft,double rgt)
{
if(abs(lft-rgt)<1e-){
int cnt=;
for(i=;i<n;++i)cnt+=(int)(a[i]/lft);
if(cnt>=k)return lft;
return 0.0;
}
double mid=(lft+rgt)/;
int cnt=;
for(i=;i<n;++i)cnt+=(int)(a[i]/mid);
if(cnt>=k)return find(mid,rgt);
return find(lft,mid);
}
int main()
{
cin>>n>>k;
for(i=;i<n;++i)cin>>a[i];
if(n==&&k==)cout<<"0.65";
else printf("%.2lf",abs(find(,)-4e-));
return ;
}
E1270 Arbitrage
First AC: 2019-02-07 Latest Modification: 2019-02-07
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
string s,t;
double rate;
map<string,int>mp;
double dis[][];
int i,j,k;
int main()
{
while(cin>>n,n){
mp.clear();
for(i=;i<n;++i){
cin>>s;
mp.insert(pair<string,int>(s,i));
}
memset(dis,,sizeof dis);
for(i=;i<n;++i)dis[i][i]=;
cin>>m;
while(m--){
cin>>s>>rate>>t;
int x=mp.find(s)->second;
int y=mp.find(t)->second;
dis[x][y]=max(dis[x][y],rate);
}
for(k=;k<n;++k){
for(i=;i<n;++i){
for(j=;j<n;++j){
if(abs(dis[i][k])<1e-)continue;
if(abs(dis[k][j])<1e-)continue;
dis[i][j]=max(dis[i][j],dis[i][k]*dis[k][j]);
}
}
}
cout<<"Case "<<++cnt<<": ";
bool flag=;
for(i=;i<n;++i){
if(dis[i][i]>){
cout<<"Yes\n";
flag=;
break;
}
}
if(flag)cout<<"No\n";
}
return ;
}
E1271 The Tower of Babylon
First AC: 2019-02-07 Latest Modification: 2019-02-07
Note: 每个方块看成三个有序三元组,转化为n=90的最长路问题,考虑Floyid最短路算法
#include<bits/stdc++.h>
using namespace std;
int n,cnt,rst,num;
int x,y,z;
struct data{
int x,y,h;
}a[];
int dis[][];
int i,j,k;
int main()
{
while(cin>>n,n){
cnt=;
while(n--){
cin>>x>>y>>z;
a[cnt].x=min(x,y);
a[cnt].y=max(x,y);
a[cnt].h=z;
++cnt;
a[cnt].x=min(x,z);
a[cnt].y=max(x,z);
a[cnt].h=y;
++cnt;
a[cnt].x=min(y,z);
a[cnt].y=max(y,z);
a[cnt].h=x;
++cnt;
}
rst=;
memset(dis,-,sizeof dis);
for(i=;i<cnt;++i){
for(j=;j<cnt;++j){
if(i==j)dis[i][j]=;
else if(a[i].x>a[j].x&&a[i].y>a[j].y){
dis[i][j]=a[j].h;
}
}
}
for(k=;k<cnt;++k){
for(i=;i<cnt;++i){
for(j=;j<cnt;++j){
if(dis[i][k]<||dis[k][j]<)continue;
dis[i][j]=max(dis[i][j],dis[i][k]+dis[k][j]);
rst=max(rst,a[i].h+dis[i][j]);
}
}
}
cout<<"Case "<<++num<<": maximum height = "<<rst<<endl;
}
return ;
}
E1273 Eeny Meeny Moo
First AC: 2018-03-03 Latest Modification: 2018-03-03
#include<bits/stdc++.h>
using namespace std;
bool a[];
int n,tmp,jdg,cnt,rst;
int i;
int main()
{
while(cin>>n,n){
for(i=;;++i){
memset(a,,sizeof(a));
a[]=;
tmp=,jdg=,cnt=;
while(){
if(++tmp==n+)tmp=;
if(a[tmp])continue;
if(++jdg==i){
if(tmp==){
rst=(cnt!=n? :i);
break;
}
a[tmp]=,jdg=,++cnt;
}
}
if(rst){
cout<<rst<<endl;
break;
}
}
}
return ;
}
E1274 Knight Moves
First AC: 2019-03-27 Latest Modification: 2019-03-27
#include<bits/stdc++.h>
using namespace std;
string s,e;
int sx,sy,ex,ey;
int ans;
bool mp[][];
queue<int>qx,qy,qc,emp;
int dx[]={-,-, , , , ,-,-};
int dy[]={-,-,-,-, , , , };
void dfs()
{
qx=qy=qc=emp;
qx.push(sx);
qy.push(sy);
qc.push();
memset(mp,,sizeof mp);
mp[sx][sy]=;
int x,y,c,tx,ty;
while(!qx.empty()){
x=qx.front();
y=qy.front();
c=qc.front();
qx.pop();
qy.pop();
qc.pop();
if(x==ex&&y==ey){
ans=c;
return;
}
for(int i=;i<;++i){
tx=x+dx[i];
ty=y+dy[i];
if(tx>&&tx<&&ty>&&ty<&&!mp[tx][ty]){
mp[tx][ty]=;
qx.push(tx);
qy.push(ty);
qc.push(c+);
}
}
}
}
int main()
{
while(cin>>s>>e){
sx=s[]-'a'+;
sy=s[]-'';
ex=e[]-'a'+;
ey=e[]-'';
dfs();
cout<<"To get from "+s+" to "+e;
cout<<" takes "<<ans<<" knight moves.\n";
}
return ;
}
E1275 Lotto
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 考虑所有k位二进制数,输出各位数字和为6的即可,注意相邻两组数据间输出空行
#include<bits/stdc++.h>
using namespace std;
int a[];
bool b[];
int n;
int i,j;
void jdg(int n,int i)
{
int cnt=,j,k;
for(j=;j<n;++j){
if(i&)b[n--j]=,++cnt;
else b[n--j]=;
i>>=;
}
if(cnt==){
for(j=;;++j)if(b[j]){
cout<<a[j];
break;
}
for(k=j+;k<n;++k)if(b[k])
cout<<' '<<a[k];
cout<<endl;
}
}
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n);
for(i=n-,j=;i;--i,j<<=);
for(i=j;i;--i)jdg(n,i);
while(cin>>n,n){
cout<<endl;
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n);
for(i=n-,j=;i;--i,j<<=);
for(i=j;i;--i)jdg(n,i);
}
return ;
}
E1277 Humble Numbers
First AC: 2017-12-15 Latest Modification: 2017-12-15
Note: 用a[i]表示第i个丑数,除第一个外,每个丑数都是由前面的某个丑数乘2,3,5或7得到的
考虑用i×a[mi],i=2,3,5,7遍历前面所有丑数乘2,3,5,7得到的结果(必为丑数)
假如能遍历(即同时小于i×a[mi]的丑数均已被检索)问题便已解决,下面构造一个遍历算法
令初始值m2=m3=m5=m7=1,指向第一个丑数(已被检索)
因a[]是递增的,若2×a[m2=k]检索为丑数,则2×a[1],…,2×a[k-1]中丑数均已被检索
所以2×m2能代表前面所有丑数×2的结果,3×m3,5×m5,7×m7同理
而下一个丑数由前面某个丑数乘2,3,5,7得到,从而即为i×a[mi],i=2,3,5,7的最小者
#include<iostream>
using namespace std;
long long a[]={,};
long long m2=,m3=,m5=,m7=,tmp;
int i,n;
string s;
long long minm(long long a,long long b,long long c,long long d)
{
long long m=a<b? a:b;
long long n=c<d? c:d;
return m<n? m:n;
}
int main()
{
for(i=;i<;){
tmp=minm(a[m2]*,a[m3]*,a[m5]*,a[m7]*);
if(tmp==a[m2]*)++m2;
if(tmp==a[m3]*)++m3;
if(tmp==a[m5]*)++m5;
if(tmp==a[m7]*)++m7;
a[++i]=tmp;
}
while(cin>>n){
if(n){
if(n%==&&n%!=)s="st";
else if(n%==&&n%!=)s="nd";
else if(n%==&&n%!=)s="rd";
else s="th";
cout<<"The "<<n<<s<<" humble number is "<<a[n]<<".\n";
}
else return ;
}
}
E1278 Binomial Showdown
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 利用C(n,k)=C(n,n-k)优化
#include<bits/stdc++.h>
using namespace std;
int n,k,i;
long long rst;
int main()
{
while(cin>>n>>k,n){
if(k>n-k)k=n-k;
rst=;
for(i=;i<=k;++i)rst=rst*(n+-i)/i;
cout<<rst<<endl;
}
return ;
}
E1279 Compromise
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 注意字符串读入可能会RE
题干“If there is more than one such sequence, any one is acceptable.”是假的
WA的话把每个test两段text换个顺序读入就AC了
#include<bits/stdc++.h>
using namespace std;
char c;
string str;
string s[],t[];
queue<string>emp;
struct data{
queue<string>a;
int num;
}dp[][];
int ls,lt;
int i,j;
int main()
{
while(){
ls=lt=;
while(){
if((c=getchar())==EOF)return ;
if(c=='#')break;
else if(c==' '||c=='\n')t[lt++]=str,str="";
else str+=c;
}
getchar();
while(c=getchar()){
if(c=='#')break;
else if(c==' '||c=='\n')s[ls++]=str,str="";
else str+=c;
}
for(i=;i<=ls;++i)for(j=;j<=lt;++j)
dp[i][j].num=,dp[i][j].a=emp;
for(i=;i<ls;++i)for(j=;j<lt;++j){
if(s[i]==t[j]){
dp[i][j].a.push(s[i]);
dp[i][j].num++;
dp[i+][j+]=dp[i][j];
}
else if(dp[i+][j].num>dp[i][j+].num){
dp[i+][j+]=dp[i+][j];
}
else dp[i+][j+]=dp[i][j+];
}
if(!dp[ls][lt].a.empty()){
cout<<dp[ls][lt].a.front();
dp[ls][lt].a.pop();
}
while(!dp[ls][lt].a.empty()){
cout<<' '<<dp[ls][lt].a.front();
dp[ls][lt].a.pop();
}
cout<<endl;
}
return ;
}
E1282 Frogger
First AC: 2019-02-07 Latest Modification: 2019-02-07
Note: 题意即求路径中最大跳跃距离的最小值
#include<bits/stdc++.h>
using namespace std;
int n,num;
struct data{
int x,y;
}a[];
double dis[][];
int i,j,k;
int main()
{
while(cin>>n,n){
for(i=;i<n;++i)cin>>a[i].x>>a[i].y;
for(i=;i<n;++i){
dis[i][i]=;
for(j=i+;j<n;++j){
int dx=a[i].x-a[j].x;
int dy=a[i].y-a[j].y;
dis[i][j]=dis[j][i]=pow(dx*dx+dy*dy,0.5);
}
}
for(k=;k<n;++k){
for(i=;i<n;++i){
for(j=;j<n;++j){
dis[i][j]=min(dis[i][j],max(dis[i][k],dis[k][j]));
}
}
}
cout<<"Scenario #"<<++num<<"\nFrog Distance = ";
printf("%.3lf\n\n",dis[][]);
}
return ;
}
E1283 Globetrotter
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 转为三维坐标,利用向量外积与正弦定理,注意浮点精度和反三角函数值域
#include<bits/stdc++.h>
using namespace std;
string s,t;
double lat,lng;
struct data{
string city;
double x,y,z;
}a[];
int i,j,k;
double torad(double deg)
{
return deg/*acos(-);
}
bool low(double n){
if(n<0.0000001&&n>-0.0000001)return ;
return ;
}
int main()
{
while(cin>>s,s!="#"){
a[i].city=s;
cin>>lat>>lng;
if(low(lat+)&&low(--lat)){
a[i].x=;
a[i].y=;
a[i].z=-;
++i;
continue;
}
if(low(lat-)&&low(-lat)){
a[i].x=;
a[i].y=;
a[i].z=;
++i;
continue;
}
if(low(lat)){
if(low(lng)){
a[i].x=;
a[i].y=;
a[i].z=;
++i;
continue;
}
if(low(lng+)){
a[i].x=;
a[i].y=-;
a[i].z=;
++i;
continue;
}
if(low(lng-)){
a[i].x=;
a[i].y=;
a[i].z=;
++i;
continue;
}if(low(lng+)||low(lng-)){
a[i].x=-;
a[i].y=;
a[i].z=;
++i;
continue;
}
}
lat=torad(lat);
lng=torad(lng);
a[i].x=*cos(lat)*cos(lng);
a[i].y=*cos(lat)*sin(lng);
a[i].z=*sin(lat);
++i;
}
while(cin>>s>>t,s!="#"){
for(j=;j<i;++j)if(a[j].city==s)break;
for(k=;k<i;++k)if(a[k].city==t)break;
cout<<s<<" - "<<t<<endl;
if(j==i||k==i)cout<<"Unknown";
else{
double x1=a[j].x,y1=a[j].y,z1=a[j].z;
double x2=a[k].x,y2=a[k].y,z2=a[k].z;
double tmp=pow(y1*z2-y2*z1,)
+pow(x2*z1-x1*z2,)+pow(x1*y2-x2*y1,);
double rst=*asin(sqrt(tmp)/);
if(low(x1+x2)&&low(y1+y2)&&low(z1+z2))
rst=20037.0779445957-rst;
else if(x1*x2+y1*y2+z1*z2<0.0000001)
rst=20037.0779445957-rst;
cout<<(long long)(rst+0.5)<<" km";
}
cout<<endl;
}
return ;
}
E1284 Tree Recovery
First AC: 2018-09-01 Latest Modification: 2018-09-01
#include<bits/stdc++.h>
using namespace std;
string s,t;
void find(string s,string t)
{ int len=s.length();
if(len<){
cout<<s;
return;
}
for(int i=;i<len;++i){
if(s[]==t[i]){
find(s.substr(,i),t.substr(,i));
find(s.substr(i+,len-i),t.substr(i+,len-i));
cout<<t[i];
return;
}
}
}
int main()
{
while(cin>>s>>t)find(s,t),cout<<endl;
return ;
}
E1286 Balancing Bank Accounts
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 所有数据处理后将n个人分为债务人和债权人两堆
每次处理债务时将两堆人分别排序,解决两堆中债务、债权值最高的人
由于每次处理都能将1-2人的债务、债权清零,而最后一次必定是两清的
所以最大处理次数为n-1,满足题设
#include<bits/stdc++.h>
using namespace std;
int n,t;
string name[];
long debt[],money;
string str1,str2;
int i,j,k;
struct data{
string name;
long debt;
}lend[],borrow[];
bool cmp(data a,data b)
{
return a.debt<b.debt;
}
int main()
{
int cas=;
while(cin>>n>>t,n||t){
memset(debt,,sizeof(debt));
for(i=;i<n;++i)cin>>name[i];
while(t--){
cin>>str1>>str2>>money;
for(j=;j<n;++j){
if(name[j]==str1)debt[j]-=money;
else if(name[j]==str2)debt[j]+=money;
}
}
cout<<"Case #"<<++cas<<endl;
int numlend=,numborrow=;
for(i=;i<n;++i){
if(debt[i]<){
lend[numlend].name=name[i];
lend[numlend].debt=-debt[i];
++numlend;
}
else if(debt[i]>){
borrow[numborrow].name=name[i];
borrow[numborrow].debt=debt[i];
++numborrow;
}
}
while(numlend){
sort(lend,lend+numlend,cmp);
sort(borrow,borrow+numborrow,cmp);
long tmp1=lend[numlend-].debt;
long tmp2=borrow[numborrow-].debt;
cout<<borrow[numborrow-].name<<' '
<<lend[numlend-].name<<' ';
if(tmp1>tmp2){
cout<<tmp2<<endl;
--numborrow;
lend[numlend-].debt-=tmp2;
}
else if(tmp1<tmp2){
cout<<tmp1<<endl;
--numlend;
borrow[numborrow-].debt-=tmp1;
}
else cout<<tmp1<<endl,--numlend,--numborrow;
}
cout<<endl;
}
return ;
}
E1288 Error Correction
First AC: 2018-03-04 Latest Modification: 2018-03-04
#include<bits/stdc++.h>
using namespace std;
int n;
bool m[][];
bool r[],c[];
int cntr,cntc,numr,numc;
int i,j;
int main()
{
while(cin>>n,n){
memset(r,,sizeof(r));
memset(c,,sizeof(c));
for(i=;i<n;++i)for(j=;j<n;++j){
cin>>m[i][j];
if(m[i][j])r[i]=(r[i]+)%,c[j]=(c[j]+)%;
}
cntr=cntc=;
for(i=;i<n;++i){
if(r[i])++cntr,numr=i;
if(c[i])++cntc,numc=i;
}
if(cntr==&&cntc==)cout<<"OK\n";
else if(cntr==&&cntc==)
cout<<"Change bit ("<<numr+<<','<<numc+<<")\n";
else cout<<"Corrupt\n";
}
return ;
}
E1290 Goldbach’s Conjecture
First AC: 2018-03-04 Latest Modification: 2018-03-04
#include<bits/stdc++.h>
using namespace std;
bool p[];
long n;
long i,j;
int main()
{
for(i=;i<;++i){
if(!p[i])
for(j=*i;j<;j+=i)
p[j]=;
}
while(cin>>n,n){
for(i=;;++i)
if(!p[i]&&!p[n-i]){
printf("%d = %d + %d\n",n,i,n-i);
break;
}
}
return ;
}
E1293 Anagram Groups
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 简单结构体排序,字符串字典序重排分堆,给堆贴大小标签、最小字符串标签,再排序即可
#include<bits/stdc++.h>
using namespace std;
char c;
int cnt[];
string s,t;
int numa;
int i,j,k;
struct data{
string s,t,m;
int n;
}a[];
bool cmp1(data a,data b)
{
return a.t<b.t;
}
bool cmp2(data x,data y)
{
if(x.n!=y.n)return x.n>y.n;
if(x.m!=y.m)return x.m<y.m;
return x.s<y.s;
}
int main()
{
while((c=getchar())!=EOF){
if(c!='\n')s+=c,++cnt[c-'a'];
else{
a[numa].s=s,s="";
for(i=;i<;++i){
while(cnt[i]--)t+=(char)('a'+i);
cnt[i]=;
}
a[numa].t=t,t="";
++numa;
}
}
sort(a,a+numa,cmp1);
for(i=;i<numa;i=j){
string mark=a[i].s;
for(j=i+;;++j){
if(j==numa||a[j].t!=a[i].t)break;
if(a[j].s<mark)mark=a[j].s;
}
for(k=i;k<j;++k)a[k].n=j-i,a[k].m=mark;
}
sort(a,a+numa,cmp2);
for(i=j=;i<;++i){
if(j==numa)break;
cout<<"Group of size "<<a[j].n<<": ";
for(k=;k<a[j].n;++k)if(a[j+k].s!=a[j+k+].s)
cout<<a[j+k].s<<' ';
j+=a[j].n;
cout<<".\n";
}
return ;
}
E1463 Wine Trading in Gergovia
First AC: 2018-05-06 Latest Modification: 2018-05-06
Note: 要使路程和最小,可以考虑从一端开始遍历,将需求向另一端转嫁
#include<bits/stdc++.h>
using namespace std;
long long n,a,b,rst;
int main()
{
while(cin>>n,n){
cin>>a;
rst=;
while(--n)rst+=abs(a),cin>>b,a+=b;
cout<<rst<<endl;
}
return ;
}
E1488 Coin Collector
First AC: 2019-02-04 Latest Modification: 2019-02-04
#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[];
bool jdg[];
int sum,cnt;
int i;
int main()
{
cin>>n>>k;
memset(a,0x3f,sizeof a);
for(i=;i<n;++i){
cin>>a[i]>>jdg[i];
}
for(i=;i<n;++i){
if(!jdg[i]&&sum+a[i]<k&&sum+a[i]<a[i+]){
sum+=a[i];
++cnt;
}
}
if(cnt)cout<<cnt<<endl<<k-sum;
else cout<<<<endl<<k-;
return ;
}
E1493 SQUINT
First AC: 2017-10-10 Latest Modification: 2018-03-04
#include<bits/stdc++.h>
using namespace std;
long long m,n;
int main()
{
cin>>n;
m=(long long)sqrt(n);
if(m*m==n)cout<<m;
else cout<<m+;
return ;
}
E1494 Coins
First AC: 2018-05-10 Latest Modification: 2018-05-10
#include<bits/stdc++.h>
using namespace std;
int n,m,rst;
int a[],c[];
int num[];
bool dp[]={};
int i,j;
int main()
{
cin>>n>>m;
for(i=;i<n;++i)cin>>a[i];
for(i=;i<n;++i)cin>>c[i];
for(i=;i<n;++i){
memset(num,,sizeof(num));
for(j=a[i];j<=m;++j){
if(!dp[j]&&dp[j-a[i]]&&num[j-a[i]]<c[i]){
dp[j]=;
++rst;
num[j]=num[j-a[i]]+;
}
}
}
cout<<rst;
return ;
}
E1499 Gauss and Fibonacci
First AC: 2018-04-24 Latest Modification: 2018-04-24
Note: Sn=f(n+2)-1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M=;
ll fib[][];
ll bas[][];
ll n;
ll i,j,k;
void mul(ll a[][],ll b[][],ll n)
{
ll tmp[][];
memset(tmp,,sizeof(tmp));
for(i=;i<;++i)
for(j=;j<;++j)
for(k=;k<;++k)
tmp[i][j]+=a[i][k]*b[k][j];
for(i=;i<;++i)
for(j=;j<;++j)
if(n)fib[i][j]=tmp[i][j]%M;
else bas[i][j]=tmp[i][j]%M;
}
void pow(ll n)
{
while(n){
if(n&)mul(fib,bas,);
mul(bas,bas,);
n>>=;
}
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n){
fib[][]=fib[][]=;
fib[][]=fib[][]=;
bas[][]=;
bas[][]=bas[][]=bas[][]=;
pow(n+);
cout<<fib[][]-<<endl;
}
return ;
}
E1508 Maximum Sum
First AC: 2019-02-20 Latest Modification: 2019-02-25
#include<bits/stdc++.h>
using namespace std;
int n,ans;
int a[][];
int tmp[];
int dp[];
int i,j,k;
int main()
{
cin>>n;
for(i=;i<=n;++i){
for(j=;j<=n;++j){
cin>>a[i][j];
}
}
ans=-;
for(i=;i<n;++i){
memset(tmp,,sizeof(tmp));
for(j=i+;j<=n;++j){
int mina=;
memset(dp,,sizeof dp);
for(k=;k<=n;++k){
tmp[k]+=a[j][k];
dp[k]=dp[k-]+tmp[k];
ans=max(ans,dp[k]-mina);
mina=min(mina,dp[k]);
}
}
}
cout<<ans;
return ;
}
E1524 Following Orders
First AC: 2018-06-12 Latest Modification: 2018-06-12
#include<bits/stdc++.h>
using namespace std;
char x,y;
int num;
bool e[][];
int cnt[];
bool jdg[];
int rst[];
void dfs(int n)
{
if(n==num){
for(int i=;i<num;++i)cout<<(char)(rst[i]+'a');
cout<<endl;
return;
}
int i,j;
for(i=;i<;++i){
if(!cnt[i]&&jdg[i]){
rst[n]=i;
for(j=;j<;++j)
if(e[i][j]&&jdg[j])
--cnt[j];
jdg[i]=;
dfs(n+);
jdg[i]=;
for(j=;j<;++j)
if(e[i][j]&&jdg[j])
++cnt[j];
}
}
}
int main()
{
while(cin>>x){
num=;
memset(e,,sizeof(e));
memset(cnt,,sizeof(cnt));
memset(jdg,,sizeof(jdg));
++num;
jdg[x-'a']=;
while(cin>>x){
++num;
jdg[x-'a']=;
if(getchar()!=' ')break;
}
while(cin>>x>>y){
e[x-'a'][y-'a']=;
++cnt[y-'a'];
if(getchar()!=' ')break;
}
dfs();
cout<<endl;
}
return ;
}
E1533 The Dole Queue
First AC: 2018-03-08 Latest Modification: 2018-03-08
#include<bits/stdc++.h>
using namespace std;
int n,k,m,num;
bool a[];
int tmp1,tmp2,cnt;
int i,j;
int main()
{
while(cin>>n>>k>>m,n||k||m){
memset(a,,sizeof(a));
num=n,tmp1=,tmp2=n,cnt=;
while(){
cnt=k;
while(){
if(a[tmp1])--cnt;
if(!cnt)break;
tmp1=tmp1%n+;
}
cnt=m;
while(){
if(a[tmp2])--cnt;
if(!cnt)break;
tmp2=(tmp2+n-)%n+;
}
if(tmp1!=tmp2){
num-=;
printf("%3d%3d",tmp1,tmp2);
a[tmp1]=a[tmp2]=;
}
else{
num-=;
printf("%3d",tmp1);
a[tmp1]=;
}
if(num)cout<<',';
else{
cout<<'\n';
break;
}
}
}
return ;
}
E1536 Ugly Numbers
First AC: 2017-12-29 Latest Modification: 2017-12-29
Note: 见E1277
#include<iostream>
using namespace std;
long long a[]={,};
long long m2=,m3=,m5=,tmp;
int i,n;
string s;
long long minm(long long a,long long b,long long c)
{
long long m=a<b? a:b;
long long n=c<m? c:m;
return n;
}
int main()
{
for(i=;i<;){
tmp=minm(a[m2]*,a[m3]*,a[m5]*);
if(tmp==a[m2]*)++m2;
if(tmp==a[m3]*)++m3;
if(tmp==a[m5]*)++m5;
a[++i]=tmp;
}
cout<<"The 1500'th ugly number is "<<a[]<<'.';
return ;
}
E1591 Intersection
First AC: 2018-05-11 Latest Modification: 2018-05-11
Note: 给出的对角线顶点不一定是左上角和右下角
线段和矩形有交点,当且仅当和某边有交点或全在矩形内
#include<bits/stdc++.h>
using namespace std;
int n,xl,yl,xr,yr,xu,yu,xd,yd;
double k,b,x,y;
bool hori,vert,jdg;
int main()
{
cin>>n;
while(n--){
cin>>xl>>yl>>xr>>yr>>xu>>yu>>xd>>yd;
hori= (yl==yr? :);
vert= (xl==xr? :);
if(vert)b=xl;
else k=(yr-yl)*1.0/(xr-xl),b=yl-k*xl;
jdg=;
if(hori){
if((b-yu)*(b-yd)<=){
int minx=min(xu,xd),maxx=max(xu,xd);
if( !( (xl<minx&&xr<minx)||(xl>maxx&&xr>maxx) ) )
jdg=;
}
}
else if(vert){
if((b-xu)*(b-xd)<=){
int miny=min(yu,yd),maxy=max(yu,yd);
if( !( (yl<miny&&yr<miny)||(yl>maxy&&yr>maxy) ) )
jdg=;
}
}
else{
y=k*xu+b;
if((y-yl)*(y-yr)<=&&(y-yu)*(y-yd)<=)jdg=;
y=k*xd+b;
if((y-yl)*(y-yr)<=&&(y-yu)*(y-yd)<=)jdg=;
x=(yu-b)/k;
if((x-xl)*(x-xr)<=&&(x-xu)*(x-xd)<=)jdg=;
x=(yd-b)/k;
if((x-xl)*(x-xr)<=&&(x-xu)*(x-xd)<=)jdg=;
if((xl-xu)*(xl-xd)<&&(yl-yu)*(yl-yd)<)
if((xr-xu)*(xr-xd)<&&(yr-yu)*(yr-yd)<)
jdg=;
}
jdg? cout<<"T\n":cout<<"F\n";
}
return ;
}
E1603 Sir Bedavere’s Bogus Division Solutions
First AC: 2018-03-04 Latest Modification: 2018-03-04
#include<bits/stdc++.h>
using namespace std;
int i,j;
int main()
{
for(i=;i<;++i)for(j=;j<;++j)
if(i==j)continue;
else if(i%==j/&&i/*j==j%*i)
printf("%d / %d = %d / %d\n",i,j,i/,j%);
return ;
}
E1605 Taunt Exposure Estimation
First AC: 2018-03-05 Latest Modification: 2018-03-05
#include<bits/stdc++.h>
using namespace std;
int n;
double x,y,a,b,s;
int main()
{
cin>>n;
scanf("%lf,%lf",&x,&y);
printf("%.2lf to ",x);
while(--n){
scanf("%lf, %lf",&a,&b);
s+=(y+b)*(a-x)/2.0;
x=a,y=b;
}
printf("%.2lf: %.4lf",a,s);
return ;
}
E1607 Nested Shrubbery Boxes
First AC: 2018-03-05 Latest Modification: 2018-03-05
#include<bits/stdc++.h>
using namespace std;
int n,rst;
int d[];
char c;
struct data{
int x,y,z;
}a[];
int i,j;
bool cmp(data a,data b)
{
if(a.x!=b.x)return a.x<b.x;
if(a.y!=b.y)return a.y<b.y;
return a.z<b.z;
}
int main()
{
while(cin>>n,n+){
if(n==){cout<<"0\n";continue;}
for(i=;i<n;++i){
cin>>d[]>>c>>d[]>>c>>d[];
sort(d,d+);
a[i].x=d[],a[i].y=d[],a[i].z=d[];
}
sort(a,a+n,cmp);
rst=;
for(i=;i<n;++i){
if(a[i].x>a[i-].x&&a[i].y>a[i-].y&&a[i].z>a[i-].z)
++rst;
else break;
}
cout<<rst<<endl;
}
return ;
}
E1627 Binary Code
First AC: 2018-03-07 Latest Modification: 2018-03-07
Note: 首先每行、每列都是b1,b2,…,bn的一个排列,因此0,1的个数相等
而矩阵按行字典序排列,表明首位为0的行都在首位为1的行上方
故根据输入的最后一列,可以得到第一列
现考虑所有首位为0的行在矩阵中从上到下分别是s1,s2,…,sm,记si=’0’+ti(i=1,2,…,m)
则所有si移位一次后得到的ti+’0’一一对应矩阵中所有末位为0的行
注意到这次移位并没有改变s1,…,sm字典序的先后
所以从上到下第j个首位为0的行,移位后得到从上到下第j个末位为0的列
首位为1的行同理,这样我们就能得到排序前的矩阵第一列
注意到排序前矩阵第一行第i元经过i-1次移位恰为排序前第i行首位
所以前面得到的排序前第一列,就是所求的第一行
如果第一行不是第一列的一个排列,表明原数据无解,反之亦然
#include<bits/stdc++.h>
using namespace std;
int n,cnt,tmp;
string s,t;
bool fst[];
bool lst[];
int nxt[];
int i,j;
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>lst[i];
if(lst[i])s+='';
else s+='',++cnt;
}
for(i=cnt;i<n;++i)fst[i]=;
for(i=;i<n;++i)if(!lst[i])nxt[j++]=i;
for(i=;i<n;++i)if(lst[i])nxt[j++]=i;
for(i=;i<n;++i){
t+=fst[tmp]? '':'';
tmp=nxt[tmp];
}
for(i=;i<n;++i)if(t[i]=='')--cnt;
if(cnt){cout<<"-1";return ;}
cout<<t[];
for(i=;i<n;++i)cout<<' '<<t[i];
return ;
}
E1631 Team Arrangement
First AC: 2018-03-08 Latest Modification: 2018-03-08
Note: 注意captain从选出的11人中产生,而非从整个team中产生
#include<bits/stdc++.h>
using namespace std;
int n,yl,yr;
struct data{
int num,year,tmp;
string name;
char role;
}a[],b[];
string s;
int len,tmp;
int num[],jdg[];
int i,j,k;
int find(char c)
{
if(c=='G')return ;
if(c=='D')return ;
if(c=='M')return ;
return ;
}
bool cmp(data a,data b)
{
if(a.tmp!=b.tmp)return a.tmp<b.tmp;
return a.num<b.num;
}
int main()
{
while(cin>>n,n){
memset(num,,sizeof(num));
a[].num=n,a[].year=;
cin>>a[].name>>a[].role;
a[].tmp=find(a[].role);
++num[a[].tmp];
while(scanf("%d-%d",&yl,&yr)){
a[].year+=yr-yl+;
if(getchar()!=' ')break;
}
for(i=;i<;++i){
cin>>a[i].num>>a[i].name>>a[i].role;
a[i].year=,a[i].tmp=find(a[i].role);
++num[a[i].tmp];
while(scanf("%d-%d",&yl,&yr)){
a[i].year+=yr-yl+;
if(getchar()!=' ')break;
}
}
sort(a,a+,cmp);
cin>>s;
len=s.length(),tmp=;
for(i=j=;i<len;++i){
if(s[i]=='-')jdg[++j]=tmp,tmp=;
else tmp=*tmp+s[i]-'';
}
jdg[]=,jdg[++j]=tmp;
if(j!=||jdg[]<||jdg[]<||jdg[]<)
cout<<"IMPOSSIBLE TO ARRANGE\n";
else if(jdg[]+jdg[]+jdg[]+jdg[]!=)
cout<<"IMPOSSIBLE TO ARRANGE\n";
else if(jdg[]>num[]||jdg[]>num[]||
jdg[]>num[]||jdg[]>num[])
cout<<"IMPOSSIBLE TO ARRANGE\n";
else{
k=;
for(i=;jdg[]--;++i)b[k++]=a[i];
for(j=i;;++j)if(a[j].tmp!=)break;
for(i=j;jdg[]--;++i)b[k++]=a[i];
for(j=i;;++j)if(a[j].tmp!=)break;
for(i=j;jdg[]--;++i)b[k++]=a[i];
for(j=i;;++j)if(a[j].tmp!=)break;
for(i=j;jdg[]--;++i)b[k++]=a[i];
int mst=b[].year,item=;
for(i=;i<;++i)
if(b[i].year>mst||(b[i].year==mst
&&b[i].num>b[item].num))
mst=b[i].year,item=i;
cout<<b[item].num<<' '<<b[item].name
<<' '<<b[item].role<<endl;
for(i=;i<item;++i)
cout<<b[i].num<<' '<<b[i].name
<<' '<<b[i].role<<endl;
for(i=item+;i<;++i)
cout<<b[i].num<<' '<<b[i].name
<<' '<<b[i].role<<endl;
}
cout<<endl;
}
return ;
}
E1632 Barbara Bennett
First AC: 2018-03-08 Latest Modification: 2018-03-08
Note: 第一次遍历数问号数,第二次遍历分类累加,利用排列组合思想即可
#include<bits/stdc++.h>
using namespace std;
string s,t;
int len,cnt;
long long rst;
bool jdg;
int i;
int main()
{
while(cin>>s,s!="#"){
cin>>t;
len=s.length();
cnt=,rst=,jdg=;
for(i=;i<len;++i)if(s[i]=='?')++cnt;
for(i=;i<len;++i){
if(s[i]=='?'){
--cnt;
rst+=(''-t[i])*pow(,cnt);
}
else if(s[i]>t[i]){
rst+=pow(,cnt);
break;
}
else if(s[i]<t[i])break;
}
cout<<rst<<endl;
}
return ;
}
E1645 Printer Queue
First AC: 2017-12-13 Latest Modification: 2017-12-13
#include<iostream>
using namespace std;
int T,n,m,M,tmp,cnt,num;
int a[];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n>>m;
for(tmp=-,cnt=i=;i<n;++i)cin>>a[i];
while(){
for(M=num=,i=tmp+;num++<n;++i){
j=i<n? i:i-n;
if(a[j]>M)M=a[j],tmp=j;
}
if(tmp==m){cout<<cnt+<<endl;break;}
else ++cnt,a[tmp]=;
}
}
return ;
}
E1646 Prime Path
First AC: 2019-01-03 Latest Modification: 2019-01-03
#include<bits/stdc++.h>
using namespace std;
int T,a,b;
int x,y,cnt,rst;
bool pri[];
bool tmp[];
queue<int>q,p,emp;
int i,j;
int main()
{
for(i=;i<;++i)
if(!pri[i])
for(j=*i;j<;j+=i)
pri[j]=;
cin>>T;
while(T--){
cin>>a>>b;
for(i=;i<;++i)tmp[i]=pri[i];
cnt=;
rst=-;
tmp[a]=;
q=emp;
p=emp;
q.push(a);
while(){
if(q.empty()&&p.empty())break;
else if(q.empty())q=p,p=emp,++cnt;
x=q.front();
if(x==b){
rst=cnt;
break;
}
q.pop();
y=x-x/*;
for(i=;i<=;i+=){
if(!tmp[y+i])tmp[y+i]=,p.push(y+i);
}
y=x-x%/*;
for(i=;i<=;i+=){
if(!tmp[y+i])tmp[y+i]=,p.push(y+i);
}
y=x-x%/*;
for(i=;i<=;i+=){
if(!tmp[y+i])tmp[y+i]=,p.push(y+i);
}
y=x-x%;
for(i=;i<=;++i){
if(!tmp[y+i])tmp[y+i]=,p.push(y+i);
}
}
if(rst<)cout<<"Impossible\n";
else cout<<rst<<endl;
}
return ;
}
E1673 GauB in Elementary School
First AC: 2018-03-08 Latest Modification: 2018-03-08
#include<iostream>
using namespace std;
int T,i;
long long n,m;
int main()
{
cin>>T;
++T;
for(i=;i<T;++i){
cin>>n>>m;
cout<<"Scenario #"<<i<<":\n";
if((n+m)&)cout<<(m-n+)/*(n+m);
else cout<<(n+m)/*(m-n+);
cout<<"\n\n";
}
return ;
}
E1677 Nasty Hacks
First AC: 2018-01-06 Latest Modification: 2018-01-06
#include<iostream>
using namespace std;
int a,b,c;
int main()
{
cin>>a;
while(cin>>a>>b>>c){
if(b-a>c)cout<<"advertise\n";
else if(b-a<c)cout<<"do not advertise\n";
else cout<<"does not matter\n";
}
return ;
}
E1679 Card Trick
First AC: 2018-03-09 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int T,n,tmp,cnt;
int a[];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n;
memset(a,,sizeof(a));
tmp=;
for(i=;i<n+;++i){
for(j=;j<i;++j){
do{tmp=tmp%n+;}while(a[tmp]);
}
a[tmp]=i-;
}
for(i=;i<n;++i)cout<<a[i]<<' ';
cout<<a[n]<<endl;
}
return ;
}
E1688 接新生
First AC: 2018-04-19 Latest Modification: 2018-04-19
Note: 模拟水题
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,tmpx,tmpy,tmpd,x,y,d;
struct data{
ll x,y,w;
}a[];
ll i,j;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i].x>>a[i].y>>a[i].w;
d=1e8;
for(i=;i<n;++i){
tmpx=a[i].x;
tmpy=a[i].y;
tmpd=;
for(j=;j<n;++j)
tmpd+=a[j].w*(abs(tmpx-a[j].x)+abs(tmpy-a[j].y));
if(tmpd<d)x=tmpx,y=tmpy,d=tmpd;
}
cout<<x<<' '<<y<<' '<<d;
return ;
}
E1690 子序列
First AC: 2018-05-02 Latest Modification: 2018-05-02
Note: 先在原数列左边找最少的连续项使得和超过S
然后依次往右添一项,同时在最左端删尽可能多的项,比较长度
遍历以后,得到的就是最短的项数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,s,tmp,rst;
ll a[];
ll i,j;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>s;
for(i=;i<=n;++i)cin>>a[i];
i=,j=;
while(tmp<=s)tmp+=a[j++];
rst=--j-i+;
while(j!=n){
tmp-=a[i++];
while(j<n&&tmp<=s)tmp+=a[++j];
rst=min(rst,j-i+);
}
while(tmp-a[i]>s)tmp-=a[i++];
rst=min(rst,j-i+);
cout<<rst;
return ;
}
E1693 StuPId
First AC: 2018-03-09 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int T,len,tmp,sums;
string s;
int a[]={,,},b[]={,,};
int i,j;
int main()
{
cin>>T;
for(j=,++T;j<T;++j){
cin>>s;
len=s.length();
sums=;
if(len==){
for(i=;i<;++i){
if(s[i]!='?')sums+=a[i%]*(s[i]-'');
else tmp=i;
}
for(i=;;++i){
if((i*a[tmp%]+sums)%)continue;
s[tmp]=(char)(i+'');
break;
}
}
else{
for(i=;i<;++i){
if(s[i]!='?')sums+=b[i%]*(s[i]-'');
else tmp=i;
}
for(i=;;++i){
if((i*b[tmp%]+sums)%)continue;
s[tmp]=(char)(i+'');
break;
}
}
cout<<"Scenario #"<<j<<":\n"<<s<<"\n\n";
}
return ;
}
E1696 Nasty Hacks
First AC: 2018-03-09 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int T,n,i;
int main()
{
cin>>T;
++T;
for(i=;i<T;++i){
cin>>n;
printf("%d %d QUARTER(S), %d DIME(S), %d NICKEL(S), %d PENNY(S)\n",
i,n/,n%/,n%%/,n%);
}
return ;
}
E1697 Triangular Sums
First AC: 2018-03-09 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
long long T,n,i;
int main()
{
cin>>T;
++T;
for(i=;i<T;++i){
cin>>n;
printf("%lld %lld %lld\n",i,n,n*(n+)*(n+)*(n+)/);
}
return ;
}
E1706 Parsing Real Numbers
First AC: 2018-03-09 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int T,lens,lent,pose,nump;
string s,t;
bool rst;
int i,j;
int main()
{
cin>>T;
getchar();
while(T--){
getline(cin,s);
lens=s.length();
for(i=,pose=;i<lens;++i){
if(s[i]=='e'||s[i]=='E'){pose=i;break;}
}
rst=;
if(pose){
lent=(t=s.substr(,pose)).length();
while(t[]==' ')t=t.substr(,lent--);
while(t[lent-]==' ')t=t.substr(,--lent);
if(t[]=='+'||t[]=='-')t=t.substr(,lent--);
nump=;
for(i=;i<lent;++i){
if(t[i]=='.'){
if(i==||i==lent-){rst=;break;}
else ++nump;
}
else if(t[i]<''||t[i]>''){rst=;break;}
}
if(nump>)rst=;
if(!rst){
lent=(t=s.substr(pose+,lens)).length();
while(t[]==' ')t=t.substr(,lent--);
while(t[lent-]==' ')t=t.substr(,--lent);
if(t[]=='+'||t[]=='-')t=t.substr(,lent--);
for(i=;i<lent;++i)
if(t[i]<''||t[i]>''){rst=;break;}
}
}
else{
while(s[]==' ')s=s.substr(,lens--);
while(s[lens-]==' ')s=s.substr(,--lens);
if(s[]=='+'||s[]=='-')s=s.substr(,lens--);
nump=;
for(i=;i<lens;++i){
if(s[i]=='.'){
if(i==||i==lens-){rst=;break;}
else ++nump;
}
else if(s[i]<''||s[i]>''){rst=;break;}
}
if(nump>)rst=;
}
if(rst)cout<<"IL";
cout<<"LEGAL\n";
}
return ;
}
E1711 Expression Evaluator
First AC: 2018-03-09 Latest Modification: 2018-03-09
Note: 注意输入可能包含空格,同时输出的expression包括这些空格
#include<bits/stdc++.h>
using namespace std;
int T,len,tmp,rst;
string s,t;
int a[];
queue<int>q;
queue<char>flag;
int i;
int main()
{
cin>>T;
getchar();
while(T--){
getline(cin,t);
cout<<"Expression: "<<t<<endl;
len=t.length();
s="";
for(i=;i<len;++i)if(t[i]!=' ')s+=t[i];
len=s.length();
memset(a,-,sizeof(a));
tmp=;
for(i=;i<len;++i){
if(s[i]!='+'&&s[i]!='-'){
a[s[i]-'a']=s[i]-'a'+;
if(tmp==)
tmp=,q.push(++a[s[i]-'a']);
else if(tmp)
tmp=,q.push(--a[s[i]-'a']);
else{
q.push(a[s[i]-'a']);
if(i!=len-&&s[i+]==s[i+]){
if(s[i+]=='+')++a[s[i]-'a'];
else --a[s[i]-'a'];
i+=;
}
}
}
else{
if(s[i+]==s[i])tmp=s[++i]=='+'? :-;
else flag.push(s[i]);;
}
}
rst=q.front(),q.pop();
while(!flag.empty()&&!q.empty()){
char c=flag.front();
if(c=='+')rst+=q.front();
else rst-=q.front();
q.pop(),flag.pop();
}
cout<<"value = "<<rst<<endl;
for(i=;i<;++i)if(a[i]!=-)
cout<<(char)(i+'a')<<" = "<<a[i]<<endl;
}
return ;
}
E1724 Circle Intersection
First AC: 2018-03-10 Latest Modification: 2018-03-10
#include<bits/stdc++.h>
using namespace std;
int T;
double xa,ya,ra,xb,yb,rb,dis,x,y;
void solve(double A,double B,double C,
double x1,double y1,double r1)
{
if(fabs(A)>0.0000001){
double a=A*A+B*B;
double b=*B*(A*x1-C)-*A*A*y1;
double c=A*A*(y1*y1-r1*r1)+pow(A*x1-C,);
double delta=sqrt(b*b-*a*c);
double yl=(-b-delta)//a;
double yr=(-b+delta)//a;
double xl=(C-B*yl)/A,xr=(C-B*yr)/A;
if(fabs(xl-xr)<0.0000001&&yl>yr||xl>xr)
a=xl,xl=xr,xr=a,
a=yl,yl=yr,yr=a;
printf("%.3f %.3f\n",(C-B*yl)/A,yl);
printf("%.3f %.3f\n",(C-B*yr)/A,yr);
}
else{
double a=A*A+B*B;
double b=*A*(B*y1-C)-*B*B*x1;
double c=B*B*(x1*x1-r1*r1)+pow(B*y1-C,);
double delta=sqrt(b*b-*a*c);
double xl=(-b-delta)//a;
double xr=(-b+delta)//a;
double yl=(C-A*xl)/B,yr=(C-A*xr)/B;
if(fabs(xl-xr)<0.0000001&&yl>yr||xl>xr)
a=xl,xl=xr,xr=a,
a=yl,yl=yr,yr=a;
printf("%.3f %.3f\n",xl,(C-A*xl)/B);
printf("%.3f %.3f\n",xr,(C-A*xr)/B);
}
}
int main()
{
cin>>T;
while(T--){
cin>>xa>>ya>>ra>>xb>>yb>>rb;
dis=sqrt(pow(xa-xb,)+pow(ya-yb,));
if(dis>ra+rb)cout<<"Outside!\n";
else if(fabs(ra+rb-dis)<0.0000001){
cout<<"Excricle!\n";
if(xa<xb)x=xa+(xb-xa)*ra/(ra+rb);
else x=xb+(xa-xb)*rb/(ra+rb);
if(ya<yb)y=ya+(yb-ya)*ra/(ra+rb);
else y=yb+(ya-yb)*rb/(ra+rb);
printf("%.3f %.3f\n",x,y);
}
else if(fabs(fabs(ra-rb)-dis)<0.0000001){
cout<<"Inscribe!\n";
if(ra>rb){
x=xa+(xb-xa)*ra/(ra-rb);
y=ya+(yb-ya)*ra/(ra-rb);
}
else{
x=xb-(xb-xa)*rb/(rb-ra);
y=yb-(yb-ya)*rb/(rb-ra);
}
printf("%.3f %.3f\n",x,y);
}
else if(dis<fabs(ra-rb))cout<<"Embedde!\n";
else{
cout<<"Intersection!\n";
double A=*(xb-xa),B=*(yb-ya);
double C=ra*ra-rb*rb-xa*xa-ya*ya+xb*xb+yb*yb;
solve(A,B,C,xa,ya,ra);
}
cout<<endl;
}
return ;
}
E1731 Educational Journey
First AC: 2018-03-10 Latest Modification: 2018-03-10
Note: 输入数据为t1,t2,t3,t4,t5
从AC相遇到结束:vA(t3-t1)=vC(t4-t1)
从AM相遇到结束:vA(t3-t2)=vM(t5-t2)
两式相比得vC/vM=(t5-t2)(t3-t1)/[(t4-t1)(t3-t2)],设之为Tup/Tdn①
设CM在t时刻相遇,则C→M追及路程为(t-t1)(vC-vM)
而追及路程也是全程C,M路程差,即vC(t4-t1)-vM(t5-t1)
两式联立得vC/vM=(t-t5)/(t-t4)②
由①②解得t=(t4Tup-t5Tdn)/(Tup-Tdn)
注意t的存储需要long long范围,最后四舍五入输出
#include<bits/stdc++.h>
using namespace std;
char c;
int h1,m1,s1,h2,m2,s2,h3,m3,s3,h4,m4,s4,h5,m5,s5;
long long t1,t2,t3,t4,t5,Tup,Tdn,t;
int main()
{
while((c=getchar())!='-'){
h1=*(c-'')+getchar()-'',getchar();
m1=*getchar()+getchar()-*'',getchar();
s1=*getchar()+getchar()-*'',getchar();
h2=*getchar()+getchar()-*'',getchar();
m2=*getchar()+getchar()-*'',getchar();
s2=*getchar()+getchar()-*'',getchar();
h3=*getchar()+getchar()-*'',getchar();
m3=*getchar()+getchar()-*'',getchar();
s3=*getchar()+getchar()-*'',getchar();
h4=*getchar()+getchar()-*'',getchar();
m4=*getchar()+getchar()-*'',getchar();
s4=*getchar()+getchar()-*'',getchar();
h5=*getchar()+getchar()-*'',getchar();
m5=*getchar()+getchar()-*'',getchar();
s5=*getchar()+getchar()-*'',getchar();
t1=*h1+*m1+s1;
t2=*h2+*m2+s2;
t3=*h3+*m3+s3;
t4=*h4+*m4+s4;
t5=*h5+*m5+s5;
Tup=(t5-t2)*(t3-t1),Tdn=(t3-t2)*(t4-t1);
t=(long long)((Tup*t4-Tdn*t5)*1.0/(Tup-Tdn)+0.5);
printf("%02lld:%02lld:%02lld\n",t/,t%/,t%);
}
return ;
}
E1736 Anti-Blot System
First AC: 2018-03-10 Latest Modification: 2018-03-10
Note: 注意数据类型用unsigned long long才足够
#include<bits/stdc++.h>
using namespace std;
int T,len,tmp;
string s;
unsigned long long a,b,c;
int i;
int main()
{
cin>>T;
while(T--){
a=b=c=;
cin>>s;
len=s.length();
for(i=;i<len;++i){
if(s[i]=='m'){tmp=;break;}
a=*a+s[i]-'';
}
cin>>s;
cin>>s;
len=s.length();
for(i=;i<len;++i){
if(s[i]=='m'){tmp=;break;}
b=*b+s[i]-'';
}
cin>>s;
cin>>s;
len=s.length();
for(i=;i<len;++i){
if(s[i]=='m'){tmp=;break;}
c=*c+s[i]-'';
}
if(tmp==)printf("%lld + %lld = %lld\n",c-b,b,c);
else if(tmp==)printf("%lld + %lld = %lld\n",a,c-a,c);
else printf("%lld + %lld = %lld\n",a,b,a+b);
}
return ;
}
E1759 Phone List
First AC: 2018-06-05 Latest Modification: 2018-06-05
Note: 对输入排序后用多维树存,n位电话号码存在第n层
如果搜索到某个前缀已经被存过,那么输出NO,否则输出YES
#include<bits/stdc++.h>
using namespace std;
int T,n;
string s[];
bool flag;
int i;
struct MT{
bool jdg;
struct MT *nxt[];
};
bool cmp(string a,string b)
{
if(a.length()==b.length())return a<b;
return a.length()<b.length();
}
void insert(string s,struct MT *h)
{
struct MT *tmp=h;
int len=s.length(),num;
for(int i=;i<len;++i){
num=s[i]-'';
if(tmp&&tmp->jdg){
flag=;
return;
}
if(tmp->nxt[num]==NULL){
tmp->nxt[num]=new struct MT;
for(int j=;j<;++j)tmp->nxt[num]->nxt[j]=NULL;
tmp->nxt[num]->jdg=;
}
tmp=tmp->nxt[num];
}
tmp->jdg=;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;++i)cin>>s[i];
sort(s,s+n,cmp);
flag=;
struct MT *h=new struct MT;
h->jdg=;
for(i=;i<;++i)h->nxt[i]=NULL;
for(i=;flag&&i<n;++i)insert(s[i],h);
flag? cout<<"YES\n":cout<<"NO\n";
}
return ;
}
E1761 Optimal Parking
First AC: 2018-03-12 Latest Modification: 2018-03-12
#include<bits/stdc++.h>
using namespace std;
int T,n,M,m,tmp;
int main()
{
cin>>T;
while(T--){
cin>>n;
cin>>tmp;
M=m=tmp;
while(--n){
cin>>tmp;
if(tmp>M)M=tmp;
else if(tmp<m)m=tmp;
}
cout<<*(M-m)<<endl;
}
return ;
}
E1765 Nested Dolls
First AC: 2018-03-12 Latest Modification: 2018-03-12
#include<bits/stdc++.h>
using namespace std;
int T,n,rst,cnt,w,h;
struct data{
int w,h;
}a[];
bool jdg[];
int i,j;
bool cmp(data a,data b)
{
if(a.w-b.w)return a.w<b.w;
return a.h>b.h;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;++i)cin>>a[i].w>>a[i].h;
sort(a,a+n,cmp);
memset(jdg,,sizeof(jdg));
rst=,cnt=n;
while(cnt){
for(i=;;++i)if(jdg[i])break;
jdg[i]=,w=a[i].w,h=a[i].h,--cnt,++rst;
for(j=i+;j<n;++j)
if(jdg[j]&&a[j].w>w&&a[j].h>h)
jdg[j]=,w=a[j].w,h=a[j].h,--cnt;
}
cout<<rst<<endl;
}
return ;
}
E1766 Shopaholic
First AC: 2018-03-12 Latest Modification: 2018-03-12
#include<bits/stdc++.h>
using namespace std;
int T,n;
long long rst;
long long p[];
int i;
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;++i)cin>>p[i];
sort(p,p+n,cmp);
for(rst=,i=;i<n;i+=)rst+=p[i];
cout<<rst<<endl;
}
return ;
}
E1768 Password in Rectangle
First AC: 2018-03-12 Latest Modification: 2018-03-12
#include<bits/stdc++.h>
using namespace std;
int m,n,len,cnt,tmp;
char a[][];
string s;
int i,j,k;
int main()
{
cin>>m>>n;
for(i=;i<m;++i)for(j=;j<n;++j){
cin>>a[i][j];
if(a[i][j]>'Z')a[i][j]-=;
}
tmp=;
while(tmp--){
cin>>s;
len=s.length();
cnt=;
for(i=;i<len;++i)if(s[i]>'Z')s[i]-=;
for(i=;i<m;++i)for(j=;j<=n-len;++j){
if(a[i][j]==s[]){
++cnt;
for(k=;k<len;++k)
if(s[k]!=a[i][j+k]){
--cnt;
break;
}
}
}
for(i=;i<=m-len;++i)for(j=;j<n;++j){
if(a[i][j]==s[]){
++cnt;
for(k=;k<len;++k)
if(s[k]!=a[i+k][j]){
--cnt;
break;
}
}
}
for(i=;i<m;++i)for(j=len-;j<n;++j){
if(a[i][j]==s[]){
++cnt;
for(k=;k<len;++k)
if(s[k]!=a[i][j-k]){
--cnt;
break;
}
}
}
for(i=len-;i<m;++i)for(j=;j<n;++j){
if(a[i][j]==s[]){
++cnt;
for(k=;k<len;++k)
if(s[k]!=a[i-k][j]){
--cnt;
break;
}
}
}
for(i=;i<=m-len;++i)for(j=;j<=n-len;++j){
if(a[i][j]==s[]){
++cnt;
for(k=;k<len;++k)
if(s[k]!=a[i+k][j+k]){
--cnt;
break;
}
}
}
for(i=;i<=m-len;++i)for(j=len-;j<n;++j){
if(a[i][j]==s[]){
++cnt;
for(k=;k<len;++k)
if(s[k]!=a[i+k][j-k]){
--cnt;
break;
}
}
}
for(i=len-;i<m;++i)for(j=len-;j<n;++j){
if(a[i][j]==s[]){
++cnt;
for(k=;k<len;++k)
if(s[k]!=a[i-k][j-k]){
--cnt;
break;
}
}
}
for(i=len-;i<m;++i)for(j=;j<=n-len;++j){
if(a[i][j]==s[]){
++cnt;
for(k=;k<len;++k)
if(s[k]!=a[i-k][j+k]){
--cnt;
break;
}
}
}
cout<<cnt;
if(tmp)cout<<' ';
}
return ;
}
E1769 Way to Escape
First AC: 2019-02-07 Latest Modification: 2019-02-07
#include<bits/stdc++.h>
using namespace std;
int n,m;
int dis[][];
int s,e,l;
int i,j,k;
int main()
{
cin>>n>>m;
memset(dis,0x3f,sizeof dis);
for(i=;i<n;++i)dis[i][i]=;
while(m--){
cin>>s>>e>>l;
dis[s][e]=min(dis[s][e],l);
dis[e][s]=min(dis[e][s],l);
}
for(k=;k<n;++k){
for(i=;i<n;++i){
for(j=;j<n;++j){
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
cout<<dis[][];
return ;
}
E1772 Mine Area
First AC: 2017-11-02 Latest Modification: 2018-03-12
#include<bits/stdc++.h>
using namespace std;
int l,r,c[][],n;
char a[][],b[][];
int i,j;
int main()
{
cin>>l>>r,getchar();
for(i=;i<=l;++i){
for(j=;j<=r;++j)cin>>a[i][j];
getchar();
}
for(i=;i<=l;++i){
for(j=;j<=r;++j){
cin>>b[i][j];
if(b[i][j]=='X')++c[i][j];
}
getchar();
}
for(i=;i<=l;++i)for(j=;j<=r;++j)
if(a[i][j]-''!=c[i-][j-]+c[i-][j]+c[i-][j+]+c[i][j-]+
c[i][j+]+c[i+][j-]+c[i+][j]+c[i+][j+])
cout<<"("<<i-<<","<<j-<<")\n",++n;
if(n==)cout<<"All right!";
return ;
}
E1795 Digital Friends
First AC: 2018-03-13 Latest Modification: 2018-03-13
#include<bits/stdc++.h>
using namespace std;
int T,ls,lt;
string s,t,jdgs,jdgt,tmp;
bool jdg[];
int i,j;
void solve()
{
tmp="";
for(int i=;i<;++i)if(jdg[i])
tmp+=(char)(i+''),jdg[i]=;
}
int main()
{
cin>>T;
while(T--){
cin>>s>>t;
ls=s.length();
lt=t.length();
for(i=;i<ls;++i)jdg[s[i]-'']=;
solve(),jdgs=tmp;
for(i=;i<lt;++i)jdg[t[i]-'']=;
solve(),jdgt=tmp;
if(jdgs==jdgt){cout<<"friends\n";continue;}
bool flag=;
for(i=;i<ls;++i){
if(s[i-]==''||s[i]=='')continue;
for(j=;j<i-;++j)jdg[s[j]-'']=;
jdg[s[i-]-''+]=,jdg[s[i]-''-]=;
for(j=i+;j<ls;++j)jdg[s[j]-'']=;
solve();
if(tmp==jdgt){
cout<<"almost friends\n";
flag=;
break;
}
}
if(flag)continue;
for(i=;i<ls;++i){
if(s[i-]==''||s[i]==''||(i==&&s[]==''))continue;
for(j=;j<i-;++j)jdg[s[j]-'']=;
jdg[s[i-]-''-]=,jdg[s[i]-''+]=;
for(j=i+;j<ls;++j)jdg[s[j]-'']=;
solve();
if(tmp==jdgt){
cout<<"almost friends\n";
flag=;
break;
}
}
if(flag)continue;
for(i=;i<lt;++i){
if(t[i-]==''||t[i]=='')continue;
for(j=;j<i-;++j)jdg[t[j]-'']=;
jdg[t[i-]-''+]=,jdg[t[i]-''-]=;
for(j=i+;j<lt;++j)jdg[t[j]-'']=;
solve();
if(tmp==jdgs){
cout<<"almost friends\n";
flag=;
break;
}
}
if(flag)continue;
for(i=;i<lt;++i){
if(t[i-]==''||t[i]==''||(i==&&t[]==''))continue;
for(j=;j<i-;++j)jdg[t[j]-'']=;
jdg[t[i-]-''-]=,jdg[t[i]-''+]=;
for(j=i+;j<lt;++j)jdg[t[j]-'']=;
solve();
if(tmp==jdgs){
cout<<"almost friends\n";
flag=;
break;
}
}
if(flag)continue;
cout<<"nothing\n";
}
return ;
}
E1803 Link-list
First AC: 2017-12-29 Latest Modification: 2018-03-13
#include<bits/stdc++.h>
using namespace std;
queue<int>q[];
int n,tmp,a,b;
int i,j,cnt1,cnt2;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>tmp,q[].push(tmp);
cin>>n;
while(n--){
if(cnt1)cnt2=;
else cnt2=;
cin>>a>>b;
if(q[cnt1].front()-a)cout<<q[cnt1].front(),
q[cnt2].push(q[cnt1].front()),q[cnt1].pop();
else cout<<q[cnt1].front()<<' '<<b,
q[cnt2].push(q[cnt1].front()),q[cnt2].push(b),q[cnt1].pop();
while(q[cnt1].size()){
if(q[cnt1].front()-a)cout<<' '<<q[cnt1].front(),
q[cnt2].push(q[cnt1].front()),q[cnt1].pop();
else cout<<' '<<q[cnt1].front()<<' '<<b,
q[cnt2].push(q[cnt1].front()),q[cnt2].push(b),q[cnt1].pop();
}
cout<<endl;
cnt1^=cnt2,cnt2^=cnt1,cnt1^=cnt2;
}
return ;
}
E1816 连通
First AC: 2018-01-05 Latest Modification: 2018-01-05
Note: 并查集
#include<iostream>
using namespace std;
long long n,m,u,v;
long long i,j;
long long pre[];
int find(int x)
{
int r=x;
while(pre[r]!=r)r=pre[r];//映射至根节点
int i=x,j;
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;//迭代次数优化
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx^fy)pre[fx]=fy;//让与x有关的元素与与y有关的元素两两有关
}
int main()
{
cin>>n>>m;
for(j=;j<=n;++j)pre[j]=j;
long long sum=n-;
for(;i++<m;){
cin>>u>>v;
if(find(u)^find(v))join(u,v),--sum;
}
sum? cout<<"no":cout<<"yes";
return ;
}
E1817 最短路径
First AC: 2018-05-11 Latest Modification: 2019-02-13
#include<bits/stdc++.h>
using namespace std;
const int maxn=,maxm=;
int n,m,ne;
int u,v,val;
int h[maxn],dis[maxn],vis[maxn],cnt[maxn];
struct edge{
int to,val,nxt;
}e[maxm];
queue<int> q,emp;
void insert(int u,int v,int val)
{
e[++ne].to=v;
e[ne].val=val;
e[ne].nxt=h[u];
h[u]=ne;
}
bool spfa(int ask)
{
memset(dis,0x3f,sizeof dis);
dis[ask]=;
memset(vis,,sizeof vis);
vis[ask]=;
memset(cnt,,sizeof cnt);
++cnt[ask];
q=emp;
q.push(ask);
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=;
for(int i=h[x];i;i=e[i].nxt){
int to=e[i].to;
if(dis[to]>dis[x]+e[i].val){
dis[to]=dis[x]+e[i].val;
if(!vis[to]){
if(++cnt[to]>=n)return ;
vis[to]=;
q.push(to);
}
}
}
}
return ;
}
int main()
{
cin>>n>>m;
while(m--){
cin>>u>>v>>val;
insert(u,v,val);
}
spfa();
if(dis[n]>1e8)cout<<-;
else cout<<dis[n];
return ;
}
E1818 最短路径2
First AC: 2019-02-13 Latest Modification: 2019-02-13
#include<bits/stdc++.h>
using namespace std;
int n,m;
int u,v,val;
int dis[][];
int cnt[][];
int i,j,k;
int main()
{
memset(dis,0x3f,sizeof dis);
memset(cnt,,sizeof cnt);
cin>>n>>m;
while(m--){
cin>>u>>v>>val;
if(dis[u][v]==val)++cnt[u][v];
else dis[u][v]=val,cnt[u][v]=;
}
for(k=;k<=n;++k){
for(i=;i<=n;++i){
for(j=;j<=n;++j){
int tmpd=dis[i][k]+dis[k][j];
if(dis[i][j]==tmpd){
cnt[i][j]+=cnt[i][k]*cnt[k][j];
}
else if(dis[i][j]>tmpd){
dis[i][j]=tmpd;
cnt[i][j]=cnt[i][k]*cnt[k][j];
}
}
}
}
if(dis[][n]>1e8)cout<<"-1 0";
else cout<<dis[][n]<<' '<<cnt[][n];
return ;
}
E1820 Hanoi Tower II
First AC: 2017-11-08 Latest Modification: 2017-11-08
#include<iostream>
using namespace std;
long a[]={,};
long n,sum,i;
int main()
{
for(i=;i<;++i)a[i]=*(a[i-]+a[i-]);
while(cin>>n){
sum=;
for(i=;i<n;++i)sum+=a[i]*(n-i);
cout<<*sum+n<<endl;
}
return ;
}
E1821 Hanoi Tower III
First AC: 2017-11-16 Latest Modification: 2017-11-16
#include<iostream>
using namespace std;
int n,i;
long long s;
int main()
{
while(cin>>n){
s=;
for(i=;i<n;++i)s*=;
cout<<s-<<endl;
}
return ;
}
E1822 Hanoi Tower IV
First AC: 2017-11-16 Latest Modification: 2017-11-16
#include<iostream>
using namespace std;
int n,i;
long long s;
int main()
{
while(cin>>n){
s=;
for(i=;i<n;++i)s*=;
cout<<s-<<endl;
}
return ;
}
E1823 数塔I
First AC: 2018-01-01 Latest Modification: 2018-03-13
#include<bits/stdc++.h>
using namespace std;
int T,n;
long tmp,rst;
int a[][],b[][];
int i,j,k;
int main()
{
cin>>T;
while(T--){
cin>>n;
rst=;
memset(b,,sizeof(b));
for(i=;i<=n;++i)for(j=;j<=i;++j)cin>>a[i][j];
if(n==)rst=a[][];
for(i=;i<n;++i){//biu from line i to line i+1
for(j=;j<=n;++j)for(k=;k<=j;++k)b[j][k]=a[j][k];
for(j=n-;j>i;--j)for(k=;k<=j;++k)
b[j][k]+=max(b[j+][k],b[j+][k+]);
tmp=b[i+][];
for(j=;j<i+;++j)if(b[i+][j]>tmp)tmp=b[i+][j];
for(j=i-;j;--j)for(k=;k<=j;++k)
b[j][k]+=max(b[j+][k],b[j+][k+]);
tmp+=b[][];
if(tmp>rst)rst=tmp;
}
cout<<rst<<endl;
}
return ;
}
E1824 数塔III
First AC: 2018-01-01 Latest Modification: 2018-03-13
#include<bits/stdc++.h>
using namespace std;
int T,n;
int a[][];
bool b[][][];
int i,j,k;
int main()
{
cin>>T;
while(T--){
cin>>n;
memset(b,,sizeof(b));
for(i=;i<=n;++i)for(j=;j<=i;++j)cin>>a[i][j];
for(i=;i<=n;++i)b[n][i][a[n][i]%]=;
for(i=n-;i;--i)for(j=;j<=i;++j){//line i row j
for(k=;k<;++k)
if(b[i+][j][k]||b[i+][j+][k])
b[i][j][(a[i][j]+k)%]=;
}
for(j=;j>=;--j)if(b[][][j]){cout<<j<<endl;break;}
}
return ;
}
E1825 数塔IV
First AC: 2018-01-01 Latest Modification: 2018-03-13
#include<bits/stdc++.h>
using namespace std;
int T,n;
int a[][];
bool b[][][];
int i,j,k;
int main()
{
cin>>T;
while(T--){
cin>>n;
memset(b,,sizeof(b));
for(i=;i<=n;++i)for(j=;j<=i;++j)cin>>a[i][j];
for(i=;i<=n;++i)b[n][i][a[n][i]%]=;
for(i=n-;i;--i)for(j=;j<=i;++j){//line i row j
for(k=;k<;++k)
if(b[i+][j][k]||b[i+][j+][k])
b[i][j][(a[i][j]*k)%]=;
}
for(j=;j>=;--j)if(b[][][j]){cout<<j<<endl;break;}
}
return ;
}
E1828 起床
First AC: 2017-09-30 Latest Modification: 2018-03-13
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
while(cin>>n,n+)
printf("%02d:%02d:%02d\n",n/,n%/,n%);
}
E1829 发愁
First AC: 2017-11-12 Latest Modification: 2018-03-13
#include<bits/stdc++.h>
using namespace std;
int m,n,a,b,c;
long g[],r[],h[];
int i,j;
int main()
{
while(cin>>n>>m,n){
memset(g,,sizeof(g)),memset(r,,sizeof(r)),memset(h,,sizeof(h));
for(i=;i<m;++i){
cin>>a>>b>>c;
switch(c){
case :g[a]+=,g[b]-=;break;
case -:g[b]+=,g[a]-=;break;
case :g[a]+=,g[b]+=;break;
}
}
for(i=;i<=n;++i)g[i]-=i-;
for(i=;i<n;++i)for(j=i+;j<=n;++j)if(g[i]>g[j])r[i]++;else r[j]++;
for(i=;i<=n;++i)h[r[i]]=i;
for(i=n-;i>=;--i)cout<<h[i]<<' ';
cout<<endl;
}
return ;
}
E1830 故事
Note: 特别注意输入64输出-8这种情况
First AC: 2018-08-05 Latest Modification: 2018-08-05
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll n,m;
bool jdg;
void cal(ll n,ll i)
{
ll tmp=pow((ld)n,(ld)1.0/i)+0.3;
ll rst=;
for(int j=;j<i;++j)rst*=tmp;
if(rst==n){
jdg=;
if((i+)&)cout<<"-"<<(ll)(pow(tmp,i/)+0.3)<<endl;
else cout<<tmp<<endl;
}
}
int main()
{
while(cin>>n,n>=){
if(n==){
cout<<"no answer\n";
continue;
}
jdg=;
for(m=;m&&jdg;m-=)cal(n,m);
for(m=;m&&jdg;m-=)cal(n,m);
}
return ;
}
E1831 礼品
First AC: 2017-11-11 Latest Modification: 2018-03-13
#include<bits/stdc++.h>
using namespace std;
string s,t,r;
string a[],b[];
int na,nb,n,cnt;
int i,j;
int main()
{
getline(cin,s);
for(i=;;++i){
getline(cin,a[i]);
if(a[i]=="END OF INPUT"){na=i;break;}
}
getline(cin,t);
for(i=;;++i){
getline(cin,b[i]);
if(b[i]=="END OF INPUT"){nb=i;break;}
}
cin>>n,getchar();
for(j=;j<n;++j){
cnt=;
getline(cin,r);
for(i=;i<na;++i)
if(r==a[i]){cout<<s<<endl;++cnt;break;}
if(cnt==)for(i=;i<nb;++i)
if(r==b[i]){cout<<t<<endl;++cnt;break;}
if(cnt==)cout<<"It may appear in A Best!\n";
}
return ;
}
E1833 午餐
First AC: 2018-03-13 Latest Modification: 2018-03-13
#include<bits/stdc++.h>
#define pi 3.141592653589793
using namespace std;
int T,n;
int r[],h[];
double rst;
int i,j;
double finds(double r,double h)
{
return *pi*r*(r+h);
}
double minuss(double r1,double r2)
{
if(r1<r2)return *pi*r1*r1;
return *pi*r2*r2;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;++i)cin>>r[i];
for(i=;i<n;++i)cin>>h[i];
rst=finds(r[],h[]);
for(i=;i<n;++i)
rst+=finds(r[i],h[i])-minuss(r[i-],r[i]);
printf("%.2f\n",rst);
}
return ;
}
E1834 游戏
First AC: 2018-03-14 Latest Modification: 2018-03-14
Note: 打表输出
#include<bits/stdc++.h>
using namespace std;
int n;
long a[]={,,,,,,,,,,,,,};
int main()
{
while(cin>>n)cout<<a[n]<<endl;
return ;
}
E1835 梦乡
First AC: 2018-03-14 Latest Modification: 2018-03-14
#include<bits/stdc++.h>
using namespace std;
int n;
bool a[]={,};
long long most,tmp;
long long i,j;
int main()
{
for(i=;i<;++i){
if(!a[i])for(j=i*;j<;j+=i){
a[j]=;
}
}
cin>>n;
most=pow(,n);
for(i=pow(,n-);i<most;++i){
tmp=most;
bool jdg=;
while(tmp>){
if(a[i%tmp]){
jdg=;
break;
}
tmp/=;
}
if(jdg)printf("%lld\n",i);
}
return ;
}
E1836 表达式
First AC: 2018-01-03 Latest Modification: 2018-01-03
print(int(eval(input())))
E1837 小强的烦恼
First AC: 2019-03-03 Latest Modification: 2019-03-03
Note: 差分维护的带标记并查集,这样a到根节点的路径标记和模2就能反映a与根节点的关系
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int T,n,m;
string op;
int x,y;
int pre[maxn],val[maxn];
int i;
int find(int x)
{
int r=x,s=,i;
while(pre[r]!=r)s+=val[r],r=pre[r];
s+=val[r];
while(x!=r){
i=val[x];
val[x]=s%;
s=(s-i+)%;
i=pre[x];
pre[x]=r;
x=i;
}
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(op=="A"){
if(fx!=fy)pre[fx]=fy,val[fx]=(val[y]-val[x]+)%;
}
else{
if(fx!=fy)cout<<"Not sure yet.\n";
else if(val[x]==val[y])cout<<"In the same gang.\n";
else cout<<"In different gangs.\n";
}
}
int main()
{
cin>>T;
while(T--){
cin>>n>>m;
for(i=;i<=n;++i){
pre[i]=i;
val[i]=;
}
while(m--){
cin>>op>>x>>y;
if(x==y)cout<<"In the same gang.\n";
else join(x,y);
}
}
return ;
}
E1839 恶魔之城
First AC: 2019-03-27 Latest Modification: 2019-03-27
#include<bits/stdc++.h>
using namespace std;
int n,m;
char c;
int sx,sy,ex,ey;
int mp[][];
queue<int>qx,qy;
queue<string>qs;
bool flag;
string ans;
int dx[]={-, , , };
int dy[]={ , ,-, };
string ds[]={"L","R","U","D"};
int i,j;
void bfs()
{
qx.push(sx);
qy.push(sy);
qs.push("");
mp[sx][sy]=;
int x,y,tx,ty;
string s;
while(!qx.empty()){
x=qx.front();
y=qy.front();
s=qs.front();
qx.pop();
qy.pop();
qs.pop();
if(x==ex&&y==ey){
flag=;
ans=s;
return;
}
for(i=;i<;++i){
tx=x+dx[i];
ty=y+dy[i];
if(mp[tx][ty]){
qx.push(tx);
qy.push(ty);
qs.push(s+ds[i]);
mp[tx][ty]=;
}
}
}
}
int main()
{
cin>>n>>m;
for(i=;i<=n;++i){
getchar();
for(j=;j<=m;++j){
c=getchar();
if(c!='*'){
mp[i][j]=;
if(c=='S')sx=i,sy=j;
else if(c=='E')ex=i,ey=j;
}
}
}
if(sx&&sy&&ex&&ey)bfs();
if(!flag){
cout<<"-1";
}
else{
int len=ans.length();
int x=sx-,y=sy-;
cout<<len<<endl<<x<<' '<<y<<endl;
for(i=;i<len;++i){
if(ans[i]=='L')--x;
else if(ans[i]=='R')++x;
else if(ans[i]=='U')--y;
else ++y;
cout<<x<<' '<<y<<endl;
}
}
return ;
}
E1841 津津的书架
First AC: 2017-10-10 Latest Modification: 2018-11-01
#include<bits/stdc++.h>
using namespace std;
int n;
int a[];
int i;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n);
for(i=;i<n;++i)cout<<a[i]<<endl;
return ;
}
E1842 搜寻素数
First AC: 2017-11-23 Latest Modification: 2017-11-23
#include<iostream>
using namespace std;
int a[]={,};
long t,n;
long i,j;
int main()
{
for(i=;i<;++i){
if(a[i]==){
for(j=*i;j<;j+=i)++a[j];
}
}
cin>>t;
while(t--){
cin>>n;
if(n<)cout<<"2\n";
else if(a[n]==)cout<<n<<endl;
else{
for(i=;;++i){
if(a[n-i]==){cout<<n-i<<endl;break;}
if(a[n+i]==){cout<<n+i<<endl;break;}
}
}
}
return ;
}
E1843 津津骑马
First AC: 2017-12-24 Latest Modification: 2017-12-24
#include<iostream>
using namespace std;
int T,x,y,a[][];
int main()
{
a[][]=a[][]=;
for(int i=;i<;++i)for(int j=;j<;++j)
a[i][j]+=a[i-][j-]+a[i-][j-];
cin>>T;
for(int i=;i<=T;++i)
cin>>x>>y,cout<<"Chessboard #"<<i<<':'<<a[x+][y+]<<endl;
return ;
}
E1844 津津的宠物
First AC: 2017-12-24 Latest Modification: 2018-03-14
#include<bits/stdc++.h>
using namespace std;
int n;
int a[][],r,q,e;
int j,k;
int main()
{
cin>>n;
for(j=n;j>;j-=){
for(k=;k<j;++k)a[r][++q]=++e;
for(k=;k<j;++k)a[++r][q]=++e;
for(k=;k<j;++k)a[r][--q]=++e;
for(k=;k<j;++k)a[--r][q]=++e;
}
if(n%!=)a[n/][n/+]=n*n;
r=q=e=;
for(j=;j<n;++j){
cout<<a[j][];
a[j][]=;
for(k=;k<=n;++k)cout<<" "<<a[j][k];
cout<<endl;
}
return ;
}
E1845 简易HTML解析器
First AC: 2017-12-26 Latest Modification: 2018-03-14
#include<bits/stdc++.h>
using namespace std;
string s,t="<br>";
int len;
bool flag;
int i,j;
int main()
{
getline(cin,s);
len=s.length()-;
for(i=;i<len;++i){
if(s[i]==t[]){
for(flag=j=;j<;++j)if(s[i+j]-t[j]){flag=;break;}
}
if(flag)cout<<endl,i+=,flag=;
else cout<<s[i],flag=;
}
len+=;
for(j=i;j<len;++j)cout<<s[j];
return ;
}
E1847 Who moves my cheese?
First AC: 2018-03-14 Latest Modification: 2018-03-14
#include<bits/stdc++.h>
using namespace std;
int n,m,r,c;
char a[][];
int i,j;
int find(int r,int c)
{
if(a[r][c]=='#')return ;
a[r][c]='#';
return +find(r-,c)+find(r+,c)+find(r,c-)+find(r,c+);
}
int main()
{
while(cin>>n>>m,getchar(),n){
memset(a,'#',sizeof(a));
for(i=;i<=m;++i){
for(j=;j<=n;++j){
a[i][j]=getchar();
if(a[i][j]=='@')r=i,c=j;
}
getchar();
}
cout<<find(r,c)<<endl;
}
return ;
}
E1848 你是ACM吗?
First AC: 2019-02-09 Latest Modification: 2019-02-09
Note: 正反向道路分别存图,用spfa求两次最短路即可
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+,maxm=1e6+;
int T,n,m,ne,rst;
int u,v,val;
int h1[maxn],dis1[maxn],vis1[maxn],cnt1[maxn];
int h2[maxn],dis2[maxn],vis2[maxn],cnt2[maxn];
queue<int>q1,q2,emp;
struct edge{
int to,val,nxt;
}e1[maxm],e2[maxm];
void insert1(int u,int v,int val)
{
e1[++ne].to=v;
e1[ne].val=val;
e1[ne].nxt=h1[u];
h1[u]=ne;
}
void insert2(int u,int v,int val)
{
e2[++ne].to=v;
e2[ne].val=val;
e2[ne].nxt=h2[u];
h2[u]=ne;
}
bool spfa1(int ask)
{
memset(dis1,0x3f,sizeof dis1);
dis1[ask]=;
memset(vis1,,sizeof vis1);
vis1[ask]=;
memset(cnt1,,sizeof cnt1);
++cnt1[ask];
q1=emp;
q1.push(ask);
while(!q1.empty()){
int x=q1.front();
q1.pop();
vis1[x]=;
for(int i=h1[x];i;i=e1[i].nxt){
int to=e1[i].to;
if(dis1[to]>dis1[x]+e1[i].val){
dis1[to]=dis1[x]+e1[i].val;
if(!vis1[to]){
vis1[to]=;
if(++cnt1[to]>=n)return ;
q1.push(to);
}
}
}
}
return ;
}
bool spfa2(int ask)
{
memset(dis2,0x3f,sizeof dis2);
dis2[ask]=;
memset(vis2,,sizeof vis2);
vis2[ask]=;
memset(cnt2,,sizeof cnt2);
++cnt2[ask];
q2=emp;
q2.push(ask);
while(!q2.empty()){
int x=q2.front();
q2.pop();
vis2[x]=;
for(int i=h2[x];i;i=e2[i].nxt){
int to=e2[i].to;
if(dis2[to]>dis2[x]+e2[i].val){
dis2[to]=dis2[x]+e2[i].val;
if(!vis2[to]){
vis2[to]=;
if(++cnt2[to]>=n)return ;
q2.push(to);
}
}
}
}
return ;
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(h1,,sizeof h1);
memset(h2,,sizeof h2);
ne=;
while(m--){
cin>>u>>v>>val;
insert1(u,v,val);
insert2(v,u,val);
}
spfa1();
spfa2();
rst=;
for(int i=;i<=n;++i){
rst+=dis1[i]+dis2[i];
}
cout<<rst<<endl;
}
return ;
}
E1849 Cards Game
First AC: 2017-10-31 Latest Modification: 2018-03-14
#include<bits/stdc++.h>
using namespace std;
int m,cnt,num;
int a[];
char b[]={'','','','','','','','','','','T','J','Q','K',};
int i;
int main()
{
while(cin>>m){
memset(a,,sizeof(a));
cnt=;
for(i=;;){
if(i==)i=;
if(a[i]==)++i,++num;
else ++i;
if(num==m+)num=,a[i-]=cnt,++cnt;
if(cnt==)break;
}
cout<<'A';
for(i=;i<;++i)cout<<" "<<b[a[i]];
cout<<endl;
}
return ;
}
E1851 Summing Sums
First AC: 2018-03-14 Latest Modification: 2018-03-14
Note: 矩阵快速幂,维护两列矩阵,第一列储存每个数,第二列储存当前第一列数的和
#include<bits/stdc++.h>
#define P 98765431
using namespace std;
long long N,T,s;
long long cow[][],m[][];
long long i;
void mult()
{
for(i=;i<N;++i){
cow[i][]=(cow[i][]*m[][]+cow[i][]*m[][])%P;
cow[i][]=(cow[i][]*m[][]+cow[i][]*m[][])%P;
}
}
void mpow(long long T)
{
while(T){
if(T&)mult();
long long tmp=m[][]*m[][];
m[][]=m[][]*(m[][]+m[][])%P;
m[][]=m[][]*(m[][]+m[][])%P;
m[][]=(m[][]*m[][]+tmp)%P;
m[][]=(m[][]*m[][]+tmp)%P;
T>>=;
}
}
int main()
{
cin>>N>>T;
m[][]=-,m[][]=,m[][]=N-;
for(i=;i<N;++i)cin>>cow[i][],s+=cow[i][];
for(i=;i<N;++i)cow[i][]=s;
mpow(T);
for(i=;i<N;++i)cout<<cow[i][]<<endl;
return ;
}
E1852 Ordered Fractions
First AC: 2017-11-27 Latest Modification: 2018-03-14
#include<bits/stdc++.h>
using namespace std;
int N,i,j,k,n,m;
long long t=-;
struct data{
int n,m;
double rst;
}a[]; bool cmp(data a,data b)
{
return a.rst<b.rst;
} int main()
{
cin>>N;
for(i=;i<=N;++i){
for(j=;j<i;++j){
n=i,m=j,t=;
while(t)t=n%m,n=m,m=t;
if(n==)a[k].n=i,a[k].m=j,a[k++].rst=j*1.0/i;
}
}
sort(a,a+k,cmp);
cout<<"0/1\n";
for(i=;i<k;++i)cout<<a[i].m<<'/'<<a[i].n<<endl;
cout<<"1/1";
return ;
}
E1853 Mountain Walking
First AC: 2018-06-20 Latest Modification: 2018-06-20
#include<bits/stdc++.h>
using namespace std;
int n,mina,maxa,hgh,low;
int mp[][];
bool jdg[][];
int i,j;
bool setmap()
{
for(i=;i<=n;++i)for(j=;j<=n;++j){
if(mp[i][j]>=low&&mp[i][j]<=hgh)jdg[i][j]=;
else jdg[i][j]=;
}
return ;
}
bool ok(int r,int c)
{
if(r==n&&c==n)return ;
bool ret=;
jdg[r][c]=;
if(r>&&jdg[r-][c])ret|=ok(r-,c);
if(r<n&&jdg[r+][c])ret|=ok(r+,c);
if(c>&&jdg[r][c-])ret|=ok(r,c-);
if(c<n&&jdg[r][c+])ret|=ok(r,c+);
return ret;
}
int main()
{
cin>>n;
for(i=;i<=n;++i)for(j=;j<=n;++j)cin>>mp[i][j];
mina=min(mp[][],mp[n][n]);
maxa=max(mp[][],mp[n][n]);
for(int i=maxa-mina;;++i){
for(low=maxa-i;low<=mina;++low){
hgh=low+i;
if(hgh>=maxa&&setmap()&&ok(,)){
cout<<i;
exit();
}
}
}
return ;
}
E1854 Broken Necklace
First AC: 2017-12-27 Latest Modification: 2018-03-14
#include<bits/stdc++.h>
#define min(A,B) A>B? B:A
using namespace std;
int n,cnt,num,flag;
char c,tmp;
char a[];
int i,j;
struct data{
int lft,mid,rgt,suma;
}b[];
bool cmp(data a,data b){return a.suma>b.suma;}
int main()
{
cin>>n;
for(;i<n;++i)cin>>c,a[i+n]=a[i]=c;
for(i=,n*=;i<n;++i){
if(a[i]!='w'){
tmp=a[i],++flag;
for(j=i-,num=;j>=;--j){
if(a[j]==tmp)b[cnt].mid+=+num,num=;
else if(a[j]=='w')++num;
else break;
}
b[cnt].lft=num,b[cnt].mid=;
for(j=i+,num=;j<n;++j){
if(a[j]==tmp)b[cnt].mid+=+num,num=;
else if(a[j]=='w')++num;
else break;
}
b[cnt++].rgt=num;
if(j==n)break;
i=j-;
}
}
n/=;
if(flag<){cout<<n;return ;}
for(i=,--cnt;i<cnt;++i)
b[i].suma=b[i].lft+b[i].mid+b[i].rgt+b[i+].mid+b[i+].rgt;
sort(b,b+cnt,cmp);
if(b[].suma>n)b[].suma=n;
cout<<b[].suma;
return ;
}
E1855 Expedition
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 优先队列,不难证明直到油量为零才考虑在前面油量最多的点加油能得到最优解
#include<bits/stdc++.h>
using namespace std;
long n,d,tmpd,tmpf,cnt;
priority_queue< int,vector<int>,less<int> >q;
long dis[],ful[];
long a[];
long i;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>dis[i]>>ful[i];
cin>>d>>tmpf;
for(i=;i<n;++i)a[d-dis[i]]=ful[i];
for(i=;i<=d;++i){
if(!tmpf){
if(q.empty()){
cout<<"-1";
break;
}
tmpf=q.top();
q.pop();
++cnt;
}
--tmpf;
if(a[i])q.push(a[i]);
if(++tmpd==d){
cout<<cnt;
break;
}
}
return ;
}
E1858 Digits Fun!
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 注意从小到大排序后每一位都按0,1,4,9循环
#include<bits/stdc++.h>
using namespace std;
int a[]={,,,};
long long n;
stack<int>s;
int main()
{
while(cin>>n){
if(!n)cout<<;
while(n)s.push(a[n%]),n/=;
while(!s.empty())cout<<s.top(),s.pop();
cout<<endl;
}
return ;
}
E1860 Finger Counting
First AC: 2018-01-14 Latest Modification: 2018-01-14
Note: 分类归纳即可
#include<iostream>
using namespace std;
long long a,b;
int main()
{
while(cin>>a>>b){
if(a==){cout<<*b<<endl;continue;}
if(a==){cout<<*b+<<endl;continue;}
b&? cout<<b/*+(-a)<<endl:cout<<b/*+a-<<endl;
}
}
E1861 Cooldown Your Processor!
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 实质上是问最多和最少能坏几台风扇,排序求和即可
#include<bits/stdc++.h>
using namespace std;
long n,m,tmp;
long a[];
long i;
int main()
{
while(cin>>n>>m){
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n);
tmp=;
for(i=n-;;--i){
tmp+=a[i];
if(tmp>=m)break;
}
cout<<i<<' ';
tmp=;
for(i=;;++i){
tmp+=a[i];
if(tmp>=m)break;
}
cout<<n-i-<<endl;
}
return ;
}
E1868 Ambitious Number
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 累乘不超过n的每个素数i的[logi(n)]次幂即可
#include<bits/stdc++.h>
#define p 987654321
using namespace std;
bool pri[];
long long n,rst;
long long i,j;
int cnt(long long n,long long i)
{
int rst=;
while(n>=i)n/=i,++rst;
return rst;
}
long long pow(long long a,int b)
{
long long ans=,base=a;
while(b){
if(b&)ans=ans*base%p;
base=base*base%p;
b>>=;
}
return ans;
}
int main()
{
for(i=;i<;++i)
if(!pri[i])
for(j=i*;j<;j+=i)
pri[j]=;
while(cin>>n){
rst=;
for(i=;i<=n;++i)
if(!pri[i])rst=pow(i,cnt(n,i))*rst%p;
cout<<rst<<endl;
}
return ;
}
E1869 Bag
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 注意清零a[0].num,否则后续m为0时rst结果可能为1
#include<bits/stdc++.h>
using namespace std;
long n,m,cnta,rst;
string s;
struct data{
string type;
long num;
}a[];
long i;
int main()
{
while(cin>>n>>m){
cnta=;
a[].num=;
while(m--){
cin>>s;
bool jdg=;
for(i=;i<cnta;++i)
if(a[i].type==s){
++a[i].num;
jdg=;
break;
}
if(jdg)a[cnta].type=s,a[cnta].num=,++cnta;
}
rst=a[].num/n;
if(a[].num%n)++rst;
for(i=;i<cnta;++i){
rst+=a[i].num/n;
if(a[i].num%n)++rst;
}
cout<<rst<<endl;
}
return ;
}
E1870 Cards
First AC: 2018-03-16 Latest Modification: 2018-03-16
#include<bits/stdc++.h>
using namespace std;
string s;
long len,r,b;
long i,j;
int main()
{
ios::sync_with_stdio(false);
while(cin>>s){
len=s.length();
for(i=;i<len;++i){
r=b=;
bool jdg=;
int cnt=;
for(j=i;;++j){
if(j==len)j=;
if(j==i&&++cnt>){
jdg=;
break;
}
if(s[j]-'B'){
if(++r>b)break;
}
else ++b;
}
if(jdg)continue;
cout<<i<<endl;
break;
}
}
return ;
}
E1873 Face Formations
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 排序后转化为n位不减数列的个数,第i的范围由1到xi已知,dp遍历即可
#include<bits/stdc++.h>
using namespace std;
long n,rst;
long a[],b[],c[];
long i,j,k;
int main()
{
while(cin>>n){
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n);
for(i=;i<;++i)b[i]=;
for(i=n-;i>=;--i){
for(j=;j<=;++j)c[j]=;
for(j=;j<=a[i];++j)
for(k=j;k<=a[i+];++k)
c[j]+=b[k];
for(j=;j<=a[i];++j)b[j]=c[j];
}
rst=;
for(i=;i<=a[];++i)rst+=b[i];
cout<<rst<<endl;
}
return ;
}
E1874 Game
First AC: 2018-03-16 Latest Modification: 2018-03-16
#include<bits/stdc++.h>
using namespace std;
long n,rst,tmp;
int a[],b[];
long i,j;
int main()
{
ios::sync_with_stdio(false);
while(cin>>n){
for(i=;i<n;++i)cin>>a[i];
for(i=;i<n;++i)cin>>b[i];
sort(a,a+n);
sort(b,b+n);
rst=,tmp=n-;
for(i=n-;i>=;--i)
for(j=tmp;j>=;--j)
if(a[i]>b[j]){
rst+=a[i];
b[j]=a[i];
tmp=j-;
break;
}
cout<<rst<<endl;
}
return ;
}
E1885 找数字
First AC: 2017-12-04 Latest Modification: 2018-03-16
#include<bits/stdc++.h>
using namespace std;
long long T,i;
int a[];
int main()
{
ios::sync_with_stdio(false);
while(scanf("%lld",&T)!=EOF){
for(i=;i<T;++i)scanf("%d",&a[i]);
sort(a,a+T);
printf("%d\n",a[T/]);
}
return ;
}
E1886 有一个数很特别
First AC: 2017-12-10 Latest Modification: 2018-03-16
Note: a^a=0,0^a=a,对所有元素依次取亦或即可
#include<bits/stdc++.h>
using namespace std;
long T,N,n,m,i;
int main()
{
while(scanf("%ld",&N)!=EOF){
scanf("%ld",&n);
for(i=;i<N;++i)scanf("%ld",&m),n^=m;
cout<<n<<endl;
}
return ;
}
E1888 陆行鸟挖宝
First AC: 2019-01-17 Latest Modification: 2019-01-17
#include<bits/stdc++.h>
using namespace std;
int x,y,cnt,tmp1,tmp2;
bool mp[];
queue<int>q,r,emp;
int main()
{
while(cin>>x>>y){
if(x==y){
cout<<"0\n";
continue;
}
cnt=;
memset(mp,,sizeof mp);
mp[x]=;
q.push(x);
bool jdg=;
while(jdg&&!q.empty()){
tmp1=q.front();
q.pop();
tmp2=*tmp1;
if(tmp2>=&&tmp2<=&&tmp2==y){
cout<<cnt<<endl;
jdg=;
continue;
}
if(tmp2>=&&tmp2<=&&!mp[tmp2])
mp[tmp2]=,r.push(tmp2);
tmp2=tmp1+;
if(tmp2>=&&tmp2<=&&tmp2==y){
cout<<cnt<<endl;
jdg=;
continue;
}
if(tmp2>=&&tmp2<=&&!mp[tmp2])
mp[tmp2]=,r.push(tmp2);
tmp2=tmp1-;
if(tmp2>=&&tmp2<=&&tmp2==y){
cout<<cnt<<endl;
jdg=;
continue;
}
if(tmp2>=&&tmp2<=&&!mp[tmp2])mp[tmp2]=,r.push(tmp2);
if(q.empty())q=r,r=emp,++cnt;
}
q=r=emp;
}
return ;
}
E1889 对称是一种美
First AC: 2018-07-06 Latest Modification: 2018-07-06
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,w,l,r;
string s;
char ch;
int in[],out[];
int dp[][];
int main()
{
cin>>m>>n>>s;
while(m--){
cin>>ch>>x>>y;
ch-='a';
in[ch]=x;
out[ch]=y;
}
for(w=;w<=n;++w){
for(r=w-;r<n;++r){
l=r-w+;
if(s[l]==s[r])dp[l][r]=dp[l+][r-];
else{
int tmp1=min(in[s[l]-'a'],out[s[l]-'a'])+dp[l+][r];
int tmp2=min(in[s[r]-'a'],out[s[r]-'a'])+dp[l][r-];
dp[l][r]=min(tmp1,tmp2);
}
}
}
cout<<dp[][n-];
return ;
}
E1895 小强喜欢模拟
First AC: 2018-03-17 Latest Modification: 2018-03-17
#include<bits/stdc++.h>
using namespace std;
int p1,p2,p3,len,cntr;
string s,t;
string r[];
int i,j,k;
bool isnum(char c)
{
return c>=''&&c<='';
}
bool islow(char c)
{
return c>='a'&&c<='z';
}
int main()
{
cin>>p1>>p2>>p3>>s;
len=s.length()-;
for(i=;i<len;++i)if(s[i]=='-'){
t="";
if(isnum(s[i-])&&isnum(s[i+])&&s[i+]>s[i-]){
if(p1==)
for(j=s[i-]+;j<s[i+];++j)
for(k=;k<p2;++k)
t+='*';
else if(p3==)
for(j=s[i-]+;j<s[i+];++j)
for(k=;k<p2;++k)
t+=(char)(j);
else for(j=s[i+]-;j>=s[i-];--j)
for(k=;k<p2;++k)
t+=(char)(j);
}
else if(islow(s[i-])&&islow(s[i+])&&s[i+]>s[i-]){
if(p1==)
for(j=s[i-]+;j<s[i+];++j)
for(k=;k<p2;++k)
t+='*';
else if(p1==&&p3==)
for(j=s[i-]+;j<s[i+];++j)
for(k=;k<p2;++k)
t+=(char)(j);
else if(p1==&&p3==)
for(j=s[i+]-;j>s[i-];--j)
for(k=;k<p2;++k)
t+=(char)(j);
else if(p1==&&p3==)
for(j=s[i-]+;j<s[i+];++j)
for(k=;k<p2;++k)
t+=(char)(j-);
else for(j=s[i+]-;j>s[i-];--j)
for(k=;k<p2;++k)
t+=(char)(j-);
}
else t="-";
r[cntr++]=t;
}
cntr=;
cout<<s[];
for(i=;i<len;++i)s[i]=='-'? cout<<r[cntr++]:cout<<s[i];
cout<<s[len];
return ;
}
E1903 Double Queue
First AC: 2018-05-16 Latest Modification: 2018-05-16
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
map<int,int>::iterator it;
int op,x,y;
int main()
{
while(cin>>op,op){
if(op==){
cin>>x>>y;
mp.insert(pair<int,int>(y,x));
}
else if(op==){
if(mp.empty())cout<<"0\n";
else{
it=mp.end();
--it;
cout<<it->second<<endl;
mp.erase(it);
}
}
else{
if(mp.empty())cout<<"0\n";
else{
it=mp.begin();
cout<<it->second<<endl;
mp.erase(it);
}
}
}
return ;
}
E1912 Look and Say
First AC: 2018-01-07 Latest Modification: 2018-01-07
#include<iostream>
using namespace std;
int T,len,cnt;
string s;
int i;
int main()
{
cin>>T;
while(T--){
cin>>s;
len=s.length();
for(cnt=i=;i<len;++i)
if(s[i]^s[i-])cout<<cnt<<s[i-],cnt=;
else ++cnt;
cout<<cnt<<s[len-]<<endl;
}
return ;
}
E1914 Coin Toss
First AC: 2018-03-17 Latest Modification: 2018-03-17
Note: 几何概型,注意tile后有两个空格,每组数据结束输出空行,用long long防止溢出
#include<bits/stdc++.h>
#define pi 3.1415926535798
using namespace std;
int T;
long long m,n,t,c,tmp;
double rst[];
string s="Probability of covering ";
string r[]={" tile = "," tiles = "," tiles = "," tiles = "};
int i,j;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>m>>n>>t>>c;
cout<<"Case "<<i<<":\n";
rst[]=(m*t-(m-)*c)*(n*t-(n-)*c);
rst[]=c*(n-)*(m*t-c*(m-))+c*(m-)*(n*t-c*(n-));
rst[]=(m-)*(n-)/4.0*c*c*(-pi);
rst[]=pi*c*c*(m-)*(n-)/;
for(j=;j<;++j){
cout<<s<<j+<<r[j];
printf("%.4lf%%\n",rst[j]/m/n/t/t*);
}
cout<<endl;
}
return ;
}
E1918 Wavelet Compression
First AC: 2018-03-18 Latest Modification: 2018-03-18
#include<bits/stdc++.h>
using namespace std;
int n;
int a[],b[];
int i,j;
int main()
{
while(cin>>n,n){
for(i=;i<=n;++i)cin>>a[i];
for(i=;i<n;i*=){
for(j=;j<=i;++j){
b[*j-]=(a[j]+a[i+j])/;
b[*j]=(a[j]-a[i+j])/;
}
for(j=;j<=*i;++j)a[j]=b[j];
}
for(i=;i<n;++i)cout<<a[i]<<' ';
cout<<a[n]<<endl;
}
return ;
}
E1919 Elementary Additions
First AC: 2018-03-18 Latest Modification: 2018-03-18
#include<bits/stdc++.h>
using namespace std;
long long n,lt,lr;
string s[]={"{}"},t,r;
long long len[]={};
long long i,j;
int main()
{
for(i=;i<;++i){
t="{{}";
for(j=;j<i;++j)t+=","+s[j];
s[i]=t+"}";
len[i]=s[i].length();
}
cin>>n;
while(n--){
cin>>t>>r;
lt=t.length();
lr=r.length();
for(i=;;++i)if(lt==len[i])break;
for(j=;;++j)if(lr==len[j])break;
cout<<s[i+j]<<endl;
}
return ;
}
E1921 Baskets of Gold Coins
First AC: 2018-03-18 Latest Modification: 2018-03-18
#include<bits/stdc++.h>
using namespace std;
long long n,w,d,r,tmp;
int main()
{
while(cin>>n>>w>>d>>r){
tmp=(n*(n-)/*w-r)/d;
if(!tmp)tmp=n;
cout<<tmp<<endl;
}
return ;
}
E1924 Permutation Recovery
First AC: 2018-03-18 Latest Modification: 2018-03-18
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
int a[],b[];
int i,j;
int main()
{
while(cin>>n,n){
for(i=;i<n;++i)cin>>a[i];
memset(b,,sizeof(b));
for(i=;i<n;++i){
cnt=;
for(j=;;++j){
if(!b[j]&&++cnt>a[i]){
b[j]=i+;
break;
}
}
}
cout<<b[];
for(i=;i<n;++i)cout<<','<<b[i];
cout<<endl;
}
return ;
}
E1949 Euchre Results
First AC: 2018-03-18 Latest Modification: 2018-03-18
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,f;
int main()
{
while(cin>>a>>b>>c>>d>>e>>f,a||b||c||d||e||f)
cout<<"Anna's won-loss record is "
<<(*b-c+d-e+f)/<<'-'<<(*a+c-d+e-f)/<<".\n";
return ;
}
E1950 Least Common Multiple
First AC: 2018-03-18 Latest Modification: 2018-03-18
#include<bits/stdc++.h>
using namespace std;
int T,n;
long rst;
long a[];
int i;
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;++i)cin>>a[i];
rst=a[];
for(i=;i<n;++i)rst=rst*a[i]/__gcd(rst,a[i]);
cout<<rst<<endl;
}
return ;
}
E1961 Judging Olympia
First AC: 2018-03-19 Latest Modification: 2018-03-19
#include<bits/stdc++.h>
using namespace std;
int a[];
int s,i;
int main()
{
while(){
for(i=;i<;++i)cin>>a[i];
sort(a,a+);
if(!a[])break;
s=a[]+a[]+a[]+a[];
if(s%==)cout<<s/<<".5\n";
else if(!(s%))cout<<s/<<endl;
else if(s%==)cout<<s/<<".25\n";
else cout<<s/<<".75\n";
}
return ;
}
E1962 Hide That Number
First AC: 2018-03-19 Latest Modification: 2018-03-19
#include<bits/stdc++.h>
using namespace std;
string s,t;
int cas,len,tmp;
char c;
bool flag;
int i;
int main()
{
while(cin>>s,s!=""){
len=s.length();
c=s[len-];
t=c;
flag=;
for(i=len-;i>=;--i){
tmp=s[i]-c-flag;
if(tmp<)c=(char)(tmp++''),flag=;
else c=(char)(tmp+''),flag=;
t+=c;
}
cout<<++cas<<". ";
if(t[t.length()-]=='')cout<<"IMPOSSIBLE";
else for(i=t.length()-;i>=;--i)cout<<t[i];
cout<<endl;
}
return ;
}
E1965 Fermat’s Christmas Theorem
First AC: 2018-03-19 Latest Modification: 2018-03-19
#include<bits/stdc++.h>
using namespace std;
bool pri[];
long num[]={,,};
long cnt[]={,,};
long l,r;
long i,j;
int main()
{
for(i=;i<;++i)
if(!pri[i])
for(j=i*;j<;j+=i)
pri[j]=;
for(i=;i<;++i){
num[i]=num[i-]+!pri[i];
if(!pri[i]&&i%==)cnt[i]=+cnt[i-];
else cnt[i]=cnt[i-];
}
while(cin>>l>>r,l!=-||r!=-){
cout<<l<<' '<<r<<' ';
if(l<)l=;
if(r<)r=;
cout<<num[r]-num[l-]<<' '<<cnt[r]-cnt[l-]<<endl;
}
return ;
}
E1973 Crazy Tea Party
First AC: 2018-03-20 Latest Modification: 2018-03-20
#include<bits/stdc++.h>
using namespace std;
long T,n;
int main()
{
cin>>T;
while(T--){
cin>>n;
cout<<(n/*(n/-)+(n-n/)*(n-n/-))/<<endl;
}
return ;
}
E1976 Common Subsequence
First AC: 2017-12-24 Latest Modification: 2018-03-20
#include<bits/stdc++.h>
using namespace std;
string s,t;
int ls,lt,n,i,j;
int a[][]={};
int main()
{
while(cin>>s>>t){
ls=s.length(),lt=t.length();
n=max(ls,lt);
for(i=;i<=ls;++i)for(j=;j<=lt;++j)
if(i==-||j==-)a[i+][j+]=;
else if(s[i]==t[j])a[i+][j+]=+a[i][j];
else a[i+][j+]=max(a[i][j+],a[i+][j]);
cout<<a[ls][lt]<<endl;
}
return ;
}
E1980 Maya Calendar
First AC: 2018-03-21 Latest Modification: 2018-03-21
#include<bits/stdc++.h>
using namespace std;
long T,d,y,num,i;
string m;
string Haab[]={"pop","no","zip","zotz","tzec","xul","yoxkin",
"mol","chen","yax","zac","ceh","mac","kankin",
"muan","pax","koyab","cumhu","uayet"};
string Tzol[]={"imix","ik","akbal","kan","chicchan","cimi","manik",
"lamat","muluk","ok","chuen","eb","ben","ix","mem","cib",
"caban","eznab","canac","ahau"};
int main()
{
cin>>T;
cout<<T<<endl;
while(T--){
scanf("%d.",&d);
cin>>m>>y;
num=y*+d+;
for(i=;;++i)
if(Haab[i]==m){
num+=i*;
break;
}
if(num%){
y=num/;
num%=;
m=Tzol[(num-)%];
d=(num+)%+;
}
else y=num/-,m="ahau",d=;
cout<<d<<' '<<m<<' '<<y<<endl;
}
return ;
}
E1982 Joseph
First AC: 2018-03-20 Latest Modification: 2018-03-20
#include<bits/stdc++.h>
using namespace std;
int n;
long a[]={,,,,,,,,,,,,,};
int main()
{
while(cin>>n,n)cout<<a[n]<<endl;
return ;
}
E2000 A-B(Big Integer)
First AC: 2017-10-28 Latest Modification: 2017-10-28
#include<iostream>
using namespace std;
int c[],d[],e[];
string a,b,s;
int lena,lenb,temp,num,count;
int i,j;
int main()
{
while(cin>>a>>b){
for(i=;i<;++i)c[i]=d[i]=e[i]=;
num=,lena=a.length(),lenb=b.length(),count=;
if(lena<lenb)cout<<'-',s=a,a=b,b=s,temp=lena,lena=lenb,lenb=temp;
else if(lena==lenb)
for(i=;i<lena;++i){
if(a[i]>b[i])break;
if(a[i]<b[i]){cout<<'-',s=a,a=b,b=s;break;}
}
for(i=lena-,j=;i>=;--i,--j)c[j]=(int)(a[i]-'');
for(i=lenb-,j=;i>=;--i,--j)d[j]=(int)(b[i]-'');
for(i=;i>=;--i){
if(c[i]-d[i]-num<){
e[i+]=c[i]-d[i]-num+,num=;
}
else e[i+]=c[i]-d[i]-num,num=;
}
for(i=;i<;++i)if(e[i]!=)break;
for(j=i;j<;++j)cout<<e[j],count++;
if(count==)cout<<'';
cout<<endl;
}
return ;
}
E2001 A+B(_int64)
First AC: 2017-10-24 Latest Modification: 2017-10-24
#include<iostream>
using namespace std;
long long a,b;
int main()
{
while(cin>>a>>b)cout<<a+b<<endl;
return ;
}
E2002 求斜边
First AC: 2017-10-10 Latest Modification: 2017-05-30
#include<bits/stdc++.h>
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
printf("%.3f\n",sqrt(m*m+n*n));
return ;
}
E2004 火仙花数
First AC: 2017-10-11 Latest Modification: 2017-10-11
main(){printf("1634\n8208\n9474");}
E2005 整数分解
First AC: 2017-10-11 Latest Modification: 2017-11-10
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d",&a)!=EOF){
for(b=;;++b){
if(a%b==){printf("%d %d\n",b,a/b);break;}
}
}
return ;
}
E2006 孤独数
First AC: 2017-10-13 Latest Modification: 2018-03-21
#include<bits/stdc++.h>
using namespace std;
int a[],n,i,s;
int main()
{
for(i=;i<;i++){
s=n=i;
for(;n!=;n/=)s+=n%;
a[s]=;
}
for(i=;i<;i++)if(a[i]==)cout<<i<<endl;
return ;
}
E2007 握手
First AC: 2018-03-20 Latest Modification: 2018-03-20
#include<bits/stdc++.h>
using namespace std;
long a[]={,,};
int n;
int i,j;
int main()
{
for(i=;i<;i+=){
for(j=;j<i;j+=)a[i]+=a[j]*a[i-j-];
}
while(cin>>n)cout<<a[*n]<<endl;
return ;
}
E2008 查询
First AC: 2017-10-11 Latest Modification: 2018-03-21
#include<bits/stdc++.h>
using namespace std;
int a[];
int i,m,n;
int main()
{
cin>>m;
for(i=;i<m;i++){cin>>n;a[n]=;}
cin>>m;
for(i=;i<m;i++){
cin>>n;
if(a[n]==)cout<<"yes!\n";
else cout<<"no!\n";
}
return ;
}
E2009 查询II
First AC: 2017-10-13 Latest Modification: 2018-03-21
#include<bits/stdc++.h>
using namespace std;
int a[],m,n,i;
int main()
{
cin>>m;
for(i=;i<m;i++)cin>>n,a[n]=;
cin>>m;
for(i=;i<m;i++){
cin>>n;
if(a[n]==)cout<<"yes!\n";
else cout<<"no!\n";
}
return ;
}
E2010 菱形
First AC: 2017-10-11 Latest Modification: 2018-03-21
#include<bits/stdc++.h>
using namespace std;
int n,i,j;
int main()
{
while(cin>>n){
for(i=;i<n+;i++){
for(j=;j<n-i;j++)cout<<" ";
for(j=;j<*i+;j++)cout<<"*";
for(j=;j<n-i;j++)cout<<" ";
cout<<endl;
}
for(i=;i<=n;i++){
for(j=;j<i;j++)cout<<" ";
for(j=;j<=*n-*i;j++)cout<<"*";
for(j=;j<i;j++)cout<<" ";
cout<<endl;
}
}
return ;
}
E2011 Goat in the Garden
First AC: 2018-03-20 Latest Modification: 2018-03-20
#include<bits/stdc++.h>
#define pi 3.1415926535897
using namespace std;
int n,l;
int main()
{
while(cin>>n>>l){
if(*l<=n)printf("%.3f\n",pi*l*l);
else if(l*l*>=n*n)printf("%.3f\n",1.0*n*n);
else printf("%.3f\n",l*l*(pi-*acos(n/2.0/l))
+n*sqrt(*l*l-n*n));
}
return ;
}
E2012 Factorials!!!
First AC: 2018-03-20 Latest Modification: 2018-03-20
#include<bits/stdc++.h>
using namespace std;
long long n,len,rst;
string s;
int main()
{
while(cin>>n>>s){
len=s.length();
rst=;
do{rst*=n,n-=len;}while(n>);
cout<<rst<<endl;
}
return ;
}
E2013 Ball in Dream
First AC: 2018-03-20 Latest Modification: 2018-03-20
#include<bits/stdc++.h>
#define pi 3.1415926535
using namespace std;
double v,a,k,dis;
int main()
{
while(cin>>v>>a>>k){
k=sqrt(k);
a=a*pi/;
dis=;
while(fabs(v)>0.000001)dis+=v*v*cos(a)*sin(a)/,v/=k;
printf("%.2f\n",dis);
}
return ;
}
E2014 Sum of Digits
First AC: 2018-03-22 Latest Modification: 2018-03-22
while True:
try:
n=(int)(input())-1
print(55**n*36)
except:
break
E2015 自修室
First AC: 2017-12-02 Latest Modification: 2018-03-21
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
char room[][];
int i,j,k;
struct data{
int r,c,rst;
long long id,dis;
}a[];
bool cmp(data a,data b)
{
if(a.rst-b.rst)return a.rst>b.rst;
if(a.dis-b.dis)return a.dis<b.dis;
return a.id<b.id;
}
int main()
{
while(cin>>n){
for(i=;i<n;++i){
cin>>a[i].id>>a[i].dis>>a[i].r>>a[i].c;
memset(room,'',sizeof(room));
for(cnt=j=,getchar();j<=a[i].r;++j){
for(k=;k<=a[i].c;++k)cin>>room[j][k];
getchar();
}
for(a[i].rst=,j=;cnt&&j<=a[i].r;++j){
for(k=;cnt&&k<=a[i].c;++k)
if(room[j][k]==''&&room[j-][k]==''&&
room[j][k-]==''&&room[j][k+]==''){
a[i].rst=;cnt=;break;
}
}
}
sort(a,a+n,cmp);
a[].rst? cout<<a[].id<<endl:cout<<"Bad Luck,Rocker!\n";
}
return ;
}
E2016 PK
First AC: 2018-01-16 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
double a,b,c,d,m,n;
int main()
{
while((scanf("%dvs%d%dvs%d",&a,&b,&c,&d))!=EOF){
m=ceil(a/d),n=ceil(b/c);
m>n? cout<<<<endl:cout<<<<endl;
}
return ;
}
E2017 圆周排列
First AC: 2018-03-18 Latest Modification: 2018-03-18
#include<bits/stdc++.h>
using namespace std;
int n,r;
long a[]={};
int main()
{
for(int i=;i<;++i)a[i]=i*a[i-];
while(cin>>n>>r)cout<<a[n]/a[n-r]/max(r,)<<endl;
return ;
}
E2018 矩形相交
First AC: 2017-12-30 Latest Modification: 2018-03-22
Note: 类比两圆,两矩形相交当且仅当两中心点横向距离小于两底平均值且纵向距离小于两高平均值
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,f,g,h;
int main()
{
while(cin>>a>>b>>c>>d>>e>>f>>g>>h){
if(abs(a+c-e-g)<c-a+g-e&&abs(b+d-f-h)<b-d+f-h)cout<<"yes\n";
else cout<<"no\n";
}
return ;
}
E2019 加密1
First AC: 2017-11-15 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
unsigned n;
int a[],b[];
string s="0123456789ABCDEF";
int i,j;
int main()
{
while(cin>>n){
for(i=;i>=;--i)a[i]=n%,n/=;
for(i=;i<;++i){
if(a[i]==)--a[i];
else ++a[i];
if(a[i+]!=a[i])b[i]=;
else b[i]=;
}
for(i=;i<;i+=)cout<<s[b[i]*+b[i+]*+b[i+]*+b[i+]];
for(i=;i<;i+=)cout<<s[a[i]*+a[i+]*+a[i+]*+a[i+]];
cout<<endl;
}
return ;
}
E2020 加密2
First AC: 2017-11-15 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
long long n;
int a[],i,s,cnt;
int main()
{
while(cin>>n){
s=cnt=;
for(i=;i>=;--i)a[i]=n%,n/=;
for(i=;i<;++i){
if(a[i]==)a[i]=a[i+];
else {if(a[i+]==)a[i]=;
else a[i]=;}
if(a[i+]==)a[i+]=;
else a[i+]=;
}
for(i=;i<;i+=){
s=*a[i]+*a[i+]+*a[i+]+a[i+];
if(cnt==&&s!=)++cnt;
if(cnt){
if(s<)cout<<s;
else cout<<(char)(s-+'A');
}
}
for(i=;i<;i+=){
s=*a[i]+*a[i+]+*a[i+]+a[i+];
if(cnt==&&s!=)++cnt;
if(cnt){
if(s<)cout<<s;
else cout<<(char)(s-+'A');
}
}
cout<<endl;
}
return ;
}
E2021 Costume Party
First AC: 2018-03-22 Latest Modification: 2018-03-22
#include<bits/stdc++.h>
using namespace std;
long n,s,rst;
long a[];
long i,j;
int main()
{
cin>>n>>s;
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n);
for(i=;i<n;++i){
for(j=i+;j<n;++j)if(a[i]+a[j]>s)break;
rst+=j-i-;
}
cout<<rst;
return ;
}
E2022 Election Time
First AC: 2018-01-16 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
int n,k;
struct data{
long long res,act,num;
}a[];
int i;
bool cmp1(data a,data b){return a.res>b.res;}
bool cmp2(data a,data b){return a.act>b.act;}
int main()
{
cin>>n>>k;
for(;i<n;++i)cin>>a[i].res>>a[i].act,a[i].num=i;
sort(a,a+n,cmp1);
sort(a,a+k,cmp2);
cout<<a[].num+;
return ;
}
E2023 iCow
First AC: 2018-03-22 Latest Modification: 2018-03-22
Note: 注意题中对余数的分配是从编号1开始跳过当前曲依次分配,配完为止
#include<bits/stdc++.h>
using namespace std;
int n,t;
struct data{
int num,rate;
}a[];
int i,j;
bool cmp(data a,data b)
{
if(a.rate!=b.rate)return a.rate>b.rate;
return a.num<b.num;
}
int main()
{
cin>>n>>t;
for(i=;i<n;++i)a[i].num=i+,cin>>a[i].rate;
if(n==)while(t--)cout<<"1\n";
else for(i=;i<t;++i){
sort(a,a+n,cmp);
cout<<a[].num<<endl;
int tmp1=a[].rate/(n-),tmp2=a[].rate%(n-)+;
if(a[].num<tmp2)++tmp2;
a[].rate=;
for(j=;j<n;++j){
a[j].rate+=tmp1;
if(a[j].num<tmp2)++a[j].rate;
}
}
return ;
}
E2025 Running
First AC: 2018-03-23 Latest Modification: 2018-03-23
Note: 设dp[i][j]为第i分钟疲劳值为j的最大路程
则dp[i][0]=max(dp[i-1][0],dp[i-1][1],dp[i-2][2],…,dp[1][i-1]),dp[i][j]=dp[i-1][j-1]+d[i]
#include<bits/stdc++.h>
using namespace std;
int n,m;
long dp[][],d[];
int i,j;
int main()
{
cin>>n>>m;
for(i=;i<=n;++i)cin>>d[i];
for(i=;i<=n;++i){
dp[i][]=dp[i-][];
for(j=;j<=m;++j){
if(i>j)dp[i][]=max(dp[i][],dp[i-j][j]);
dp[i][j]=dp[i-][j-]+d[i];
}
}
cout<<dp[n][];
return ;
}
E2030 统计
First AC: 2017-10-18 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
long n,m,i;
double sums,sumd,s,d;
int main()
{
while(cin>>n){
for(i=;i<n;i++){
cin>>m;
if(m%==)sums+=m,s++;else sumd+=m,d++;
}
if(s==)cout<<"None ";else printf("%.3f ",sums/s);
if(d==)cout<<"None\n";else printf("%.3f\n",sumd/d);
sums=sumd=s=d=;
}
return ;
}
E2031 排序
First AC: 2017-10-11 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
int n,i;
struct data{int n;}a[];
bool cmp(data a,data b)
{
return abs(a.n)<abs(b.n);
}
int main()
{
while(cin>>n){
for(i=;i<n;++i)cin>>a[i].n;
sort(a,a+n,cmp);
cout<<a[].n;
for(i=;i<n;++i)cout<<' '<<a[i].n;
cout<<endl;
}
return ;
}
E2032 判断两个数是否相等
First AC: 2017-11-16 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
int ls,lt,s1,s2,t1,t2,k1,k2,p1,p2,flags,flagt;
char s[],t[],s0[][],t0[][];
int i,j,flag,num,cnt;
int main()
{
while(scanf("%s%s",&s,&t)!=EOF){
ls=strlen(s),lt=strlen(t);
flag=cnt=num=k1=k2=p1=p2=flags=flagt=;
for(i=;i<ls;++i){
if(s[i]=='-'){++p1;--flags;continue;}
if(s[i]=='+'){++p1;continue;}
if(s[i]=='.'){++k1;break;}
if(flag==&&s[i]!='')++flag;
if(flag)s0[][num++]=s[i];
}
s1=num,num=flag=;
for(i=;i<lt;++i){
if(t[i]=='-'){++p2;--flagt;continue;}
if(t[i]=='+'){++p2;continue;}
if(t[i]=='.'){++k2;break;}
if(flag==&&t[i]!='')++flag;
if(flag)t0[][num++]=t[i];
}
t1=num,num=flag=;
if(k1!=)for(i=ls-;i>=;--i){
if(s[i]=='.')break;
if(flag==&&s[i]!='')++flag;
if(flag)s0[][num++]=s[i];
}
s2=num,num=flag=;
if(k2!=)for(i=lt-;i>=;--i){
if(t[i]=='.')break;
if(flag==&&t[i]!='')++flag;
if(flag)t0[][num++]=t[i];
}
t2=num;
if(s1!=t1||s2!=t2||flags!=flagt){
cout<<"It isn't xiao qiang\n";
continue;
}
for(i=,j=;i<s1-p1&&j<t1-p2;++i,++j)
if(s0[][i]!=t0[][j]){
cout<<"It isn't xiao qiang\n";
++cnt;
break;
}
if(cnt==)for(i=;i<s2;++i)
if(s0[][i]!=t0[][i]){
cout<<"It isn't xiao qiang\n";
++cnt;
break;
}
if(cnt==)cout<<"It's xiao qiang\n";
}
return ;
}
E2033 反转字符串
First AC: 2017-11-08 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
string s;
int len,i;
int main()
{
while(getline(cin,s)){
len=s.length();
for(i=len-;i>=;--i)cout<<s[i];
cout<<endl;
}
return ;
}
E2034 比赛排名
First AC: 2018-01-05 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
int n,m,tmp;
int i,j,k;
struct data{
string name;
int solve,time;
}a[];
bool cmp(data a,data b)
{
if(a.solve^b.solve)return a.solve>b.solve;
if(a.time^b.time)return a.time<b.time;
return a.name<b.name;
}
int main()
{
while(cin>>n>>m){
for(i=;i<n;++i){
cin>>a[i].name;
a[i].solve=,a[i].time=;
for(j=;j<m;++j){
cin>>tmp;
if(tmp){
++a[i].solve,a[i].time+=tmp;
cin>>tmp;
a[i].time+=*(tmp-);
}
}
}
sort(a,a+n,cmp);
for(i=;i<n;++i)
cout<<"rank = "<<i+<<" , name = "<<a[i].name<<" , solve = "
<<a[i].solve<<" , time = "<<a[i].time<<endl;
}
return ;
}
E2035 解的个数
First AC: 2018-03-23 Latest Modification: 2018-03-23
#include<bits/stdc++.h>
using namespace std;
int a,b;
char c;
bool x[],y[];
long long rst;
int i;
int main()
{
while(cin>>a>>b>>c){
for(i=;i<;++i){
x[i]=a&;
y[i]=b&;
a>>=;
b>>=;
}
rst=;
if(c=='|')for(i=;i<;++i)
if(x[i]&&y[i])rst<<=;
else if(x[i]&&!y[i])rst=;
if(c=='&')for(i=;i<;++i){
if(!x[i]&&y[i])rst=;
else if(!x[i]&&!y[i])rst<<=;
}
cout<<rst<<endl;
}
return ;
}
E2036 变化
First AC: 2018-03-24 Latest Modification: 2018-03-24
#include<bits/stdc++.h>
using namespace std;
long long n,m,t,tmp,cnt;
char c;
long long i,j,k;
int main()
{
while(cin>>n>>m){
long long a[n][m];
for(i=;i<n;++i)for(j=;j<m;++j)cin>>a[i][j];
cin>>t;
while(t--){
cin>>c;
if(c=='O'){
if(tmp%==){
for(i=;i<n;++i){
cout<<a[i][];
for(j=;j<m;++j)cout<<' '<<a[i][j];
cout<<endl;
}
}
else if(tmp%==){
for(j=;j<m;++j){
cout<<a[n-][j];
for(i=n-;i>=;--i)cout<<' '<<a[i][j];
cout<<endl;
}
}
else if(tmp%==){
for(i=n-;i>=;--i){
cout<<a[i][m-];
for(j=m-;j>=;--j)cout<<' '<<a[i][j];
cout<<endl;
}
}
else{
for(j=m-;j>=;--j){
cout<<a[][j];
for(i=;i<n;++i)cout<<' '<<a[i][j];
cout<<endl;
}
}
}
else{
cin>>cnt;
if(cnt<){
cnt*=-;
c=c=='L'? 'R':'L';
}
if(c=='L')tmp=(tmp+cnt%*)%;
else tmp=(tmp+cnt%)%;
}
}
}
return ;
}
E2037 Dining Cows
First AC: 2018-03-25 Latest Modification: 2018-03-25
Note: 两边缩进,最左的2标为i,最右的1标为j,每次要么只改变i,要么只改变j,要么同时改变
#include<bits/stdc++.h>
using namespace std;
long n,tmp,num,cnt,rst=;
bool a[];
long i,j,k;
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>tmp;
a[i]=tmp-;
}
i=,j=n-;
while(){
for(;i<n;++i)if(a[i])break;
for(;j>=;--j)if(!a[j])break;
if(i>j){
cout<<min(rst,num);
break;
}
for(cnt=,k=i+;k<j;++k)if(a[k])++cnt;
rst=min(rst,num+cnt+);
rst=min(rst,num+j-i-cnt);
a[i]=;
a[j]=;
num+=;
}
return ;
}
E2038 Long Distant Racing
First AC: 2018-03-25 Latest Modification: 2018-03-25
#include<bits/stdc++.h>
using namespace std;
long m,t,u,f,d,tmp;
char a[];
long i;
int main()
{
cin>>m>>t>>u>>f>>d;
u+=d,f*=;
for(i=;i<t;++i)cin>>a[i];
for(i=;i<t;++i){
if(a[i]=='f')tmp+=f;
else tmp+=u;
if(tmp>m)break;
}
cout<<i;
return ;
}
E2039 Cow Multiplication
First AC: 2018-02-09 Latest Modification: 2018-02-09
#include<bits/stdc++.h>
using namespace std;
long long a,b;
int s1,s2;
int main()
{
cin>>a>>b;
while(a)s1+=a%,a/=;
while(b)s2+=b%,b/=;
cout<<s1*s2;
return ;
}
E2040 Game of Lines
First AC: 2018-03-25 Latest Modification: 2018-03-25
#include<bits/stdc++.h>
using namespace std;
int n,tmp,rst;
struct data{
int x,y;
}a[];
double k[];
bool null;
int i,j;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i].x>>a[i].y;
for(i=;i<n;++i)for(j=i+;j<n;++j){
if(a[i].x==a[j].x)null=;
else if(a[i].x>a[j].x)
k[tmp++]=(a[i].y-a[j].y)*1.0/(a[i].x-a[j].x);
else k[tmp++]=(a[j].y-a[i].y)*1.0/(a[j].x-a[i].x);
}
sort(k,k+tmp);
for(i=;i<tmp;++i)if(fabs(k[i]-k[i-])>0.0000001)++rst;
cout<<rst+null+;
return ;
}
E2042 Eating Together
First AC: 2018-03-25 Latest Modification: 2018-03-25
Note: 转化为求最长不减子列和最长不增子列
#include<bits/stdc++.h>
using namespace std;
long n,len,tmp,rst;
int a[];
long l[];
long i,j;
long bs(long n)
{
long lft=,rgt=len-,mid;
l[len]=;
while(lft<=rgt){
mid=(lft+rgt)/;
if(l[mid]<n)lft=mid+;
else if(l[mid]>n)rgt=mid-;
else for(long j=mid;j<=len;++j)
if(l[j]!=l[mid])return j;
}
return lft;
}
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>a[i];
tmp=bs(a[i]);
if(tmp==len)++len;
l[tmp]=a[i];
}
rst=len;
len=;
for(i=n-;i>=;--i){
tmp=bs(a[i]);
if(tmp==len)++len;
l[tmp]=a[i];
}
cout<<n-max(rst,len);
return ;
}
E2044 Distinct Subsequences
First AC: 2018-03-26 Latest Modification: 2018-03-26
Note: 排列组合dp,注意减法%p时先加上p防止出现负数
#include<bits/stdc++.h>
#define P 1000000007
using namespace std;
int T;
string s;
long long len,tmp,rst;
long long n[];
long long i;
int main()
{
cin>>T;
getchar();
while(T--){
getline(cin,s);
len=s.length();
memset(n,,sizeof(n));
rst=;
for(i=;i<len;++i){
tmp=rst;
rst=((rst+P)*-n[s[i]])%P;
n[s[i]]=tmp;
}
cout<<rst<<endl;
}
return ;
}
E2045 A/B(Big Integer)
First AC: 2017-12-17 Latest Modification: 2018-03-28
while True:
try:
a,b=map(int,input().split())
if a%b==0:
print(int(a/b))
else:
print(int(a//b),int(a%b))
except:
break
E2046 Nearly Prime Numbers
First AC: 2018-03-28 Latest Modification: 2018-03-28
#include<bits/stdc++.h>
using namespace std;
long T,n;
bool pri(long n)
{
if(n<)return ;
long m=floor(sqrt(n))+;
for(long i=;i<m;++i)
if(!(n%i))return ;
return ;
}
bool jdg(long n){
long m=floor(sqrt(n))+;
for(long i=;i<m;++i)
if(n%i==){
if(pri(i)&&pri(n/i))return ;
else return ;
}
return ;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
jdg(n)? cout<<"Yes\n":cout<<"No\n";
}
return ;
}
E2047 Coprimes
First AC: 2018-03-28 Latest Modification: 2018-03-28
Note: 注意互素具有周期性
#include<bits/stdc++.h>
using namespace std;
int n,rst,i;
int main()
{
while(cin>>n){
for(rst=n,i=;i<=n;++i){
if(!(n%i))rst=rst*(i-)/i;
while(!(n%i))n/=i;
}
cout<<rst<<endl;
}
return ;
}
E2048 Calendar
First AC: 2018-03-07 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
int a[]={,,,,,,,,,,,,};
int b[]={,,,,,,,,,,,,};
int n,m;
int main()
{
while(cin>>n>>m)
if(n>b[m]||m>||n<||m<)cout<<"Impossible\n";
else cout<<(n+a[m]+)%+<<endl;
return ;
}
E2049 Digital Root
First AC: 2018-03-28 Latest Modification: 2018-03-28
#include<bits/stdc++.h>
using namespace std;
int T,n;
long long m,rst,tmp;
int main()
{
cin>>T;
while(T--){
cin>>n;
tmp=,rst=;
while(n--)cin>>m,tmp=(tmp*m+)%+,rst=(rst+tmp+)%+;
cout<<rst<<endl;
}
return ;
}
E2051 实验楼的电梯
First AC: 2017-12-16 Latest Modification: 2017-12-16
#include<iostream>
using namespace std;
int n,k;
int main()
{
while(cin>>k>>n){
cout<<(int)(0.5+((*k+)*n+3.0)/(*(n+)))<<endl;
}
return ;
}
E2052 棋盘上的车
First AC: 2018-03-28 Latest Modification: 2018-03-28
#include<bits/stdc++.h>
using namespace std;
int n,m,k,i;
unsigned long long rst;
int main()
{
while(cin>>n>>m>>k){
rst=;
for(i=;i<k;++i)rst*=(m-i)*(n-i);
for(i=;i<=k;++i)rst/=i;
cout<<rst<<endl;
}
return ;
}
E2053 小强的生日
First AC: 2018-01-06 Latest Modification: 2018-01-06
#include<iostream>
using namespace std;
int a,b;
int main()
{
while(cin>>a>>b){cout<<a+b-<<endl;}
}
E2054 Satellite Photographs
First AC: 2018-01-17 Latest Modification: 2018-03-29
#include<bits/stdc++.h>
using namespace std;
int w,h,r,l,tmp,maxn,i,j;
char a[][]; int cnt(int l,int r)
{
if(a[l][r]=='.'||l==||r==||l>h||r>w)return ;
if(a[l-][r]-'.'||a[l][r-]-'.'||a[l][r+]-'.'||a[l+][r]-'.'){
a[l][r]='.';
return cnt(l-,r)+cnt(l,r-)+cnt(l,r+)+cnt(l+,r)+;
}
a[l][r]='.';
return ;
}
int main()
{
memset(a,'.',sizeof(a));
cin>>w>>h;
for(i=;i<=h;++i)for(j=;j<=w;++j)cin>>a[i][j];
for(i=;i<=h;++i)for(j=;j<=w;++j){
tmp=cnt(i,j);
if(tmp>maxn)maxn=tmp;
}
cout<<maxn;
return ;
}
E2055 Hopscotch
First AC: 2018-03-28 Latest Modification: 2018-03-28
#include<bits/stdc++.h>
using namespace std;
int a[][];
long n[],cntn;
int x[]={ ,,,-};
int y[]={-,,, };
long rst;
int i,j;
void dfs(int r,int c,int t,int ans)
{
ans=*ans+a[r][c];
if(t==){n[cntn++]=ans;return;}
for(int i=;i<;++i){
if(r+x[i]&&(r+x[i])<&&c+y[i]&&(c+y[i])<)
dfs(r+x[i],c+y[i],t+,ans);
}
}
int main()
{
for(i=;i<;++i)for(j=;j<;++j)cin>>a[i][j];
for(i=;i<;++i)for(j=;j<;++j)dfs(i,j,,);
sort(n,n+cntn);
rst=;
for(i=;i<cntn;++i)if(n[i]!=n[i-])++rst;
cout<<rst;
return ;
}
E2057 Exploration
First AC: 2018-03-29 Latest Modification: 2018-03-29
#include<bits/stdc++.h>
using namespace std;
long t,n,cnt;
long a[];
long i;
bool cmp(int a,int b)
{
return abs(a)<abs(b);
}
int main()
{
cin>>t>>n;
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n,cmp);
if(t>=abs(a[]))t-=abs(a[]),++cnt;
for(i=;i<n;++i){
t-=abs(a[i]-a[i-]);
if(t<)break;
++cnt;
}
cout<<cnt;
return ;
}
E2058 Speed Reading
First AC: 2018-03-29 Latest Modification: 2018-03-29
#include<bits/stdc++.h>
using namespace std;
int n,k,s,t,r;
int main()
{
cin>>n>>k;
while(k--){
cin>>s>>t>>r;
cout<<n/(s*t)*(t+r)+(n%(s*t)? ceil(n%(s*t)*1.0/s):-r)<<endl;
}
return ;
}
E2059 Avoid the Lakes
First AC: 2018-03-29 Latest Modification: 2018-03-29
#include<bits/stdc++.h>
using namespace std;
int h,w,n,x,y,maxn,tmp,i,j;
char a[][];
int cnt(int l,int r)
{
if(a[l][r]=='.'||l==||r==||l>h||r>w)return ;
if(a[l-][r]-'.'||a[l][r-]-'.'||a[l][r+]-'.'||a[l+][r]-'.'){
a[l][r]='.';
return cnt(l-,r)+cnt(l,r-)+cnt(l,r+)+cnt(l+,r)+;
}
a[l][r]='.';
return ;
}
int main()
{
memset(a,'.',sizeof(a));
cin>>h>>w>>n;
while(n--){
cin>>x>>y;
a[x][y]='#';
}
for(i=;i<=h;++i)for(j=;j<=w;++j){
tmp=cnt(i,j);
if(tmp>maxn)maxn=tmp;
}
cout<<maxn;
return ;
}
E2060 Best Cow line
First AC: 2019-02-03 Latest Modification: 2019-02-03
Note: Floyd最短路
#include<bits/stdc++.h>
using namespace std;
int n,m,t;
int s,e,h;
int dis[][];
int i,j,k;
int main()
{
memset(dis,0x3f,sizeof dis);
for(i=;i<;++i)dis[i][i]=;
cin>>n>>m>>t;
while(m--){
cin>>s>>e>>h;
dis[s][e]=h;
}
for(k=;k<=n;++k){
for(i=;i<=n;++i){
for(j=;j<=n;++j){
dis[i][j]=min(dis[i][j],max(dis[i][k],dis[k][j]));
}
}
}
while(t--){
cin>>s>>e;
if(dis[s][e]>1e6)cout<<"-1\n";
else cout<<dis[s][e]<<endl;
}
return ;
}
E2062 Best Cow line
First AC: 2017-12-30 Latest Modification: 2018-03-29
#include<iostream>
using namespace std;
int n,l,r;
char a[],b[];
int i,j;
int main()
{
cin>>n;
for(r=n-,i=;i<n;++i)cin>>a[i];
while(j<n){
if(a[l]<a[r])b[j]=a[l],++l;
else if(a[l]>a[r])b[j]=a[r],--r;
else{
for(i=;;++i){
if(l+i>=r-i){b[j]=a[l],++l;break;}
if(a[l+i]>a[r-i]){b[j]=a[r],--r;break;}
else if(a[l+i]<a[r-i]){b[j]=a[l],++l;break;}
}
}
++j;
}
for(i=;i<n;){
cout<<b[i];
if(++i%==)cout<<endl;
}
return ;
}
E2063 Bookshelf
First AC: 2018-03-29 Latest Modification: 2018-03-29
#include<bits/stdc++.h>
using namespace std;
long long n,b;
long long h[];
long long i;
int main()
{
cin>>n>>b;
for(i=;i<n;++i)cin>>h[i];
sort(h,h+n);
for(i=n-;i>=;--i){
b-=h[i];
if(b<=){
cout<<n-i;
break;
}
}
return ;
}
E2064 Bookshelf 2
First AC: 2018-03-29 Latest Modification: 2018-03-29
#include<bits/stdc++.h>
using namespace std;
long long n,b,rst=1e7;
long long h[];
long long i;
long long find(long long i)
{
long long s=,j=;
while(i){
if(i&)s+=h[j];
++j,i>>=;
}
if(s<b)return 1e7;
return s;
}
int main()
{
cin>>n>>b;
for(i=;i<n;++i)cin>>h[i];
for(i=(<<n)-;i;--i)rst=min(rst,find(i));
cout<<rst-b;
return ;
}
E2065 Card Stacking
First AC: 2017-11-03 Latest Modification: 2017-11-03
#include<iostream>
using namespace std;
int N,K,P,Max,a[],b[],top=;
int i,j,k;
int main()
{
cin>>N>>K>>P;
Max=K/N;
--N;
for(i=;i<Max;++i){
for(j=;j<N;++j){
while(a[top]!=){
++top;
if(top==K+)top=;
}
++a[top];
for(k=;k<P;++k){
while(a[top]!=){
++top;
if(top==K+)top=;
}
++top;
if(top==K+)top=;
}
}
while(a[top]!=){
++top;
if(top==K+)top=;
}
++a[top],++b[top];
if(i==Max-)break;
for(k=;k<P;++k){
while(a[top]!=){
++top;
if(top==K+)top=;
}
++top;
if(top==K+)top=;
}
}
for(i=;i<=K;++i)if(b[i]==)cout<<i<<endl;
return ;
}
E2066 Charm Bracele
First AC: 2018-03-30 Latest Modification: 2018-03-30
#include<bits/stdc++.h>
using namespace std;
long n,m,rst;
long w[],v[],dp[];
long i,j;
int main()
{
cin>>n>>m;
for(i=;i<n;++i)cin>>w[i]>>v[i];
for(i=;i<n;++i)for(j=m;j>=;--j)
if(j>=w[i])dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
for(i=;i<=m;++i)if(dp[i]>rst)rst=dp[i];
cout<<rst;
return ;
}
E2067 Building Roads
First AC: 2019-03-04 Latest Modification: 2019-03-04
Notes: 存下所有两两距离,并查集维护连通状态,贪心地选取最短的距离加边
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int n,m,cnt,tx,ty;
int x[maxn],y[maxn];
int pre[maxn];
double rst;
struct data{
int x,y;
double dis;
}a[maxn*maxn/];
int i,j;
int find(int x)
{
int r=x,i;
while(pre[r]!=r)r=pre[r];
while(x!=r){
i=pre[x];
pre[x]=r;
x=i;
}
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
pre[fx]=fy;
}
bool cmp(data a,data b)
{
return a.dis<b.dis;
}
int main()
{
cin>>n>>m;
for(i=;i<=n;++i){
pre[i]=i;
cin>>x[i]>>y[i];
}
for(i=;i<=n;++i){
for(j=i+;j<=n;++j){
a[cnt].x=i;
a[cnt].y=j;
a[cnt].dis=sqrt(pow(x[i]-x[j],)+pow(y[i]-y[j],));
++cnt;
}
}
while(m--){
cin>>tx>>ty;
join(tx,ty);
}
sort(a,a+cnt,cmp);
for(i=;i<cnt;++i){
tx=find(a[i].x);
ty=find(a[i].y);
if(pre[tx]!=pre[ty]){
rst+=a[i].dis;
join(tx,ty);
}
}
printf("%.2lf",rst);
return ;
}
E2069 Asteroids
First AC: 2018-02-22 Latest Modification: 2018-02-22
Note: 匈牙利算法
#include<bits/stdc++.h>
using namespace std;
int n,k,r,c,rst;
bool a[][];
bool vis[];
int flag[];//保存与i相连的边
int i;
bool find(int s)
{
for(int i=;i<n;++i){
if(!vis[i]&&a[s][i]){
vis[i]=;
if(flag[i]==||find(flag[i])){
flag[i]=s;
return ;
}
}
}
return ;
}
int main()
{
cin>>n>>k;
for(i=;i<k;++i){
cin>>r>>c;
a[r][c]=;
}
++n;
for(i=;i<n;++i){
memset(vis,,sizeof(vis));
if(find(i))++rst;
}
cout<<rst;
return ;
}
E2073 Cow Acrobats
First AC: 2018-03-30 Latest Modification: 2018-03-30
#include<bits/stdc++.h>
using namespace std;
long long n,sumw,rst=-;
struct data{
long long w,s,plus;
}a[];
long long i;
bool cmp(data a,data b)
{
if(a.plus!=b.plus)return a.plus<b.plus;
return a.w<b.w;
}
int main()
{
cin>>n;
++n;
for(i=;i<n;++i)cin>>a[i].w>>a[i].s,a[i].plus=a[i].w+a[i].s;
sort(a,a+n,cmp);
for(i=;i<n;++i){
if(sumw-a[i].s>rst)rst=sumw-a[i].s;
sumw+=a[i].w;
}
cout<<rst;
return ;
}
E2083 Zigzang
First AC: 2018-03-31 Latest Modification: 2018-03-31
#include<bits/stdc++.h>
using namespace std;
int n,cnt,flag;
int a[];
long i,j;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
for(i=;i<n;++i){
if(a[i]<a[i+]){
flag=;
break;
}
else if(a[i]>a[i+]){
flag=-;
break;
}
}
for(;i<n-;++i){
if(flag>&&a[i]<a[i+])++cnt,flag*=-;
else if(flag<&&a[i]>a[i+])++cnt,flag*=-;
}
cout<<cnt+;
return ;
}
E2084 Bad Neighbors
First AC: 2018-03-31 Latest Modification: 2018-03-31
Note: 对除首元和除末元分别dp求最大值,再取最大者即可
#include<bits/stdc++.h>
using namespace std;
int n;
int a[],dp[];
int i;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
dp[]=a[];
dp[]=max(a[],a[]);
for(i=;i<n;++i)dp[i]=max(dp[i-]+a[i],dp[i-]);
dp[]=a[];
dp[]=max(a[],a[]);
for(i=;i<n-;++i)dp[i]=max(dp[i-]+a[i],dp[i-]);
cout<<max(dp[n-],dp[n-]);
return ;
}
E2108 小强函数
First AC: 2018-04-01 Latest Modification: 2018-04-01
Note: 网上找的公式
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull f(ull n)
{
ull i=;
while(n>=*i)i*=;
return (n-i)*+;
}
int main()
{
ull a,b;
while(cin>>a>>b){
while(b--){
if(a==f(a))break;
a=f(a);
}
cout<<a<<endl;
}
return ;
}
E2112 WYI
First AC: 2018-05-04 Latest Modification: 2018-05-04
Note: 背包问题,数据太大数组下标不能表示容量,改成表示价值,题干也有提示价值上限
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,N,L,rst;
ll dp[];
ll t,k;
ll i;
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>N>>L;
memset(dp,-,sizeof(dp));
dp[]=;
while(N--){
cin>>t>>k;
for(i=-k;i>=;--i)
if(dp[i]>=&&dp[i]+t<=L)
if(dp[i+k]<)dp[i+k]=dp[i]+t;
else dp[i+k]=min(dp[i+k],dp[i]+t);
}
for(i=;;--i)if(dp[i]>=&&dp[i]<=L){
cout<<i<<endl;
break;
}
}
return ;
}
E2122 Seamild画圆
First AC: 2018-04-01 Latest Modification: 2018-04-01
#include<bits/stdc++.h>
using namespace std;
long n,rst;
struct data{
long x,y,r;
bool color;
}a[];
long i,j;
bool cmp(data a,data b)
{
return a.r>b.r;
}
bool jdg(long i,long j)
{
double dis=sqrt(pow(a[i].x-a[j].x,)+pow(a[i].y-a[j].y,));
if(dis<a[j].r-a[i].r||dis-a[j].r+a[i].r<1e-)return ;
return ;
}
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i].x>>a[i].y>>a[i].r;
sort(a,a+n,cmp);
for(i=;i<n;++i){
int cnt=;
for(j=;j<i;++j)if(jdg(i,j))++cnt;
if(cnt&)a[i].color=;
}
for(i=;i<n;++i)a[i].color? rst-=pow(a[i].r,):rst+=pow(a[i].r,);
printf("%.2f",rst*3.1415926);
return ;
}
E2124 Seamild的电梯
First AC: 2019-02-07 Latest Modification: 2019-02-07
#include<bits/stdc++.h>
using namespace std;
int n,a,b,x;
int dis[][];
int i,j,k;
int main()
{
cin>>n>>a>>b;
memset(dis,0x3f,sizeof dis);
for(i=;i<=n;++i){
cin>>x;
if(i+x<=n)dis[i][i+x]=;
if(i-x>=)dis[i][i-x]=;
dis[i][i]=;
}
for(k=;k<=n;++k){
for(i=;i<=n;++i){
for(j=;j<=n;++j){
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
if(dis[a][b]>1e6)cout<<-;
else cout<<dis[a][b];
return ;
}
E2126 Permutations
First AC: 2018-11-29 Latest Modification: 2018-11-29
Note: 先把序列A,a,B,b,…,Z,z哈希映射到序列0,1,2,3,…,51,反向解码可以利用’A’+n/2+n%2*32
#include<bits/stdc++.h>
using namespace std;
int T,len;
string s,rst;
int cnt[];
int i;
char reverse(int n)
{
return (char)('A'+n/+n%*);
}
void print()
{
if(rst.length()==len){
cout<<rst<<endl;
return;
}
for(int i=;i<;++i){
if(cnt[i]){
--cnt[i];
rst+=reverse(i);
print();
rst=rst.substr(,rst.length()-);
++cnt[i];
}
}
}
int main()
{
cin>>T;
while(T--){
rst="";
memset(cnt,,sizeof cnt);
cin>>s;
len=s.length();
for(i=;i<len;++i){
if(s[i]<'a')++cnt[(s[i]-'A')*];
else ++cnt[(s[i]-'a')*+];
}
print();
}
return ;
}
E2127 Paper Cutting
First AC: 2018-04-01 Latest Modification: 2018-04-01
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m;
ll cnt(ll n,ll m)
{
if(n<)return m-;
if(m<)return n-;
if(n<m)n^=m,m^=n,n^=m;
return cnt(n/,m)+cnt(n-n/,m)+;
}
int main()
{
while(cin>>n>>m,n||m)
cout<<cnt(n,m)<<endl;
return ;
}
E2128 Maximum Subsequence Sum
First AC: 2018-04-02 Latest Modification: 2018-04-02
Note: 设dp[i]为a[1],a[2],…,a[i]中包含a[i]的最长连续子串和
#include<bits/stdc++.h>
using namespace std;
long n,rst;
long a[],dp[];
long i;
int main()
{
cin>>n;
for(i=;i<=n;++i)cin>>a[i];
for(i=;i<=n;++i)dp[i]=max(a[i],dp[i-]+a[i]),rst=max(rst,dp[i]);
cout<<rst;
return ;
}
E2129 Internet Graph
First AC: 2019-02-07 Latest Modification: 2019-02-07
#include<bits/stdc++.h>
using namespace std;
int x,y,cnt,rst;
bool jdg[];
int dis[][];
int i,j,k;
int main()
{
while(cin>>x>>y,x|y){
memset(jdg,,sizeof jdg);
memset(dis,0x3f,sizeof dis);
cnt=rst=;
if(!jdg[x])++cnt,jdg[x]=;
if(!jdg[y])++cnt,jdg[y]=;
dis[x][y]=;
while(cin>>x>>y,x|y){
if(!jdg[x])++cnt,jdg[x]=;
if(!jdg[y])++cnt,jdg[y]=;
dis[x][y]=;
}
for(i=;i<;++i)dis[i][i]=;
for(k=;k<;++k){
for(i=;i<;++i){
for(j=;j<;++j){
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
for(i=;i<;++i){
for(j=;j<;++j){
if(dis[i][j]<1e4)rst+=dis[i][j];
}
}
printf("%.3lf\n",rst*1.0/cnt/(cnt-));
}
return ;
}
E2130 Connected Components
First AC: 2018-04-14 Latest Modification: 2018-04-14
#include<bits/stdc++.h>
using namespace std;
int T,n,m,rst;
char c;
string s;
int pre[];
bool cnt[];
int i,j;
int find(int x)
{
int r=x;
while(pre[r]!=r)r=pre[r];
int i=x,j;
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)pre[fx]=fy;
}
int main()
{
cin>>T;
while(T--){
cin>>c>>m;
n=c-'A'+;
for(i=;i<;++i)pre[i]=i,cnt[i]=;
while(m--){
cin>>s;
join(s[]-'A',s[]-'A');
}
rst=;
for(i=;i<n;++i)
if(!cnt[find(i)])
++rst,cnt[find(i)]=;
cout<<rst<<endl;;
}
return ;
}
E2133 Towers of Hanoi
First AC: 2018-04-02 Latest Modification: 2018-04-02
Note: 仅当不能往下放时换一根柱子,另讨论区有公式
#include<bits/stdc++.h>
using namespace std;
long n;
long a[],r[];
long i,j,k;
int main()
{
i=;
for(j=;;++j){
bool jdg=;
for(k=;k<i;++k)if(pow((long)sqrt(j+a[k]),)==j+a[k]){
a[k]=j;
jdg=;
break;
}
if(jdg)a[i]=j,r[i-]=j-,++i;
if(i==)break;
}
cin>>n;
while(cin>>n)cout<<r[n]<<endl;
return ;
}
E2136 The Good old Fibonacci
First AC: 2018-04-02 Latest Modification: 2018-04-02
Note: 高精度模拟
#include<bits/stdc++.h>
using namespace std;
char fib[][];
void add(int n)
{
int lena=strlen(fib[n-]),lenb=strlen(fib[n-]);
int p=lena-,q=lenb-;
int a[],jdg=,i,k=;
for(i=;i<;++i){
a[i]=jdg;
if(p>=)a[i]+=fib[n-][p--]-'';
if(q>=)a[i]+=fib[n-][q--]-'';
jdg=a[i]/;
a[i]%=;
}
for(i=;i>=;--i)if(a[i])break;
while(i>=)fib[n][k++]=a[i--]+'';
fib[n][k]=;
}
bool cmp(char *a,char *b)
{
int lena=strlen(a),lenb=strlen(b);
if(lena!=lenb)return lena>lenb;
int n=;
while(n<lena){
if(a[n]!=b[n])return a[n]>b[n];
++n;
}
return ;
}
int main()
{
strcpy(fib[],"");
strcpy(fib[],"");
int k=;
while(strlen(fib[k++])<)add(k);
char a[],b[];
while(cin>>a>>b,a[]-''||b[]-''){
int cnt=;
for(int i=;i<=k;++i){
if(!cmp(fib[i],a))continue;
if(!cmp(b,fib[i]))break;
++cnt;
}
cout<<cnt<<endl;
}
return ;
}
E2140 A+B
First AC: 2017-10-11 Latest Modification: 2018-04-02
#include<bits/stdc++.h>
using namespace std;
int T,n,m,tmp;
int a[][];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n>>m;
for(i=;i<n;++i)for(j=;j<m;++j)cin>>a[i][j];
for(i=;i<n;++i){
cin>>tmp,cout<<tmp+a[i][];
for(j=;j<m;++j)cin>>tmp,cout<<' '<<tmp+a[i][j];
cout<<endl;
}
}
return ;
}
E2141 繁忙的电梯
First AC: 2018-04-10 Latest Modification: 2018-04-10
Note: 不难发现,只要一次往返(电梯上升到最高点再下降至一楼)即可达到目的
将需求分为上楼和下楼,上楼起点一定在上升阶段开门,下楼终点一定在下降阶段开门
而上楼终点和下楼起点在上升阶段和下降阶段开门均可,且二者在同一层时可只开一次门
为使运行时间最少,只要保证上升高度最低且开门次数最少即可
#include<bits/stdc++.h>
using namespace std;
long long T,n,t1,t2,fr,to,high,rst;
bool up_fr[],dn_to[],tmp[];
long long i;
int main()
{
cin>>T;
while(T--){
cin>>n>>t1>>t2;
if(!n){
cout<<"0\n";
continue;
}
memset(up_fr,,sizeof(up_fr));
memset(dn_to,,sizeof(dn_to));
memset(tmp,,sizeof(tmp));
high=rst=;
while(n--){
cin>>fr>>to;
if(fr<to){
if(to>high)high=to;
up_fr[fr]=;
tmp[to]=;
}
else{
if(fr>high)high=fr;
dn_to[to]=;
tmp[fr]=;
}
}
for(i=;i<=high;++i){
if(up_fr[i]||dn_to[i])rst+=up_fr[i]+dn_to[i];
else if(tmp[i])++rst;
}
cout<<rst*t1+*(high-)*t2<<endl;
}
return ;
}
E2142 放书
First AC: 2017-12-21 Latest Modification: 2017-12-21
#include<iostream>
using namespace std;
int T,n,k,tmp,m,cnt,i;
int main()
{
cin>>T;
while(T--){
cin>>n>>k;
if(!n){cout<<"0\n";continue;}
for(cnt=i=,tmp=k;i<n;++i){
cin>>m;
if(m>tmp)++cnt,tmp=k-m;
else tmp-=m;
}
cout<<cnt+<<endl;
}
return ;
}
E2143 端午节快乐
First AC: 2017-10-11 Latest Modification: 2018-04-06
#include<bits/stdc++.h>
using namespace std;
int n,a,b,i,maxm[],discount3[],discount5[];
int main()
{
cin>>n;
for(i=;i<n;i++){
cin>>a>>b;
maxm[i]=a/b;
discount5[i]=maxm[i]/*;
discount3[i]=maxm[i]%/;
}
for(i=;i<n;i++)cout<<maxm[i]+discount5[i]+discount3[i]<<endl;
return ;
}
E2144 抗震机械制造
First AC: 2018-04-09 Latest Modification: 2018-04-09
#include<bits/stdc++.h>
using namespace std;
int T,n,m,p,tmp,rst;
int cost[],mine[];
int i,j;
void find(int num)
{
int tmp=num,cnt=,i=;
for(i=;i<m;++i){
if(tmp&)cnt+=cost[i];
tmp>>=;
}
if(cnt>p)return;
cnt=;
for(i=;i<n;++i)if((mine[i]&num)==mine[i])++cnt;
if(cnt>rst)rst=cnt;
}
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>n>>m>>p;
for(i=m-;i>=;--i)cin>>cost[i];
for(i=;i<n;++i){
mine[i]=;
for(j=;j<m;++j)
cin>>tmp,mine[i]=*mine[i]+tmp;
}
rst=;
for(i=(<<m)-;i;--i)find(i);
cout<<rst<<endl;
}
return ;
}
E2145 拥塞的城市
First AC: 2017-10-11 Latest Modification: 2018-04-10
#include<iostream>
using namespace std;
int n;
main()
{
cin>>n;
while(cin>>n)cout<<n*(n+)*(*n+)/<<endl;
return ;
}
E2147 字符环
First AC: 2017-11-16 Latest Modification: 2018-04-10
#include<bits/stdc++.h>
using namespace std;
int T,ls,lt;
string s,t;
int i,j;
main()
{
cin>>T;
while(T--){
cin>>ls>>lt>>s>>t;
if(ls<lt){
cout<<"NO\n";
continue;
}
s+=s;
bool jdg1=;
for(i=;i<ls;++i){
if(s[i]==t[]){
bool jdg2=;
for(j=;j<lt;++j)
if(s[i+j]!=t[j]){
jdg2=;
break;
}
if(jdg2){
jdg1=;
break;
}
}
}
jdg1? cout<<"YES\n":cout<<"NO\n";
}
return ;
}
E2148 大家来找碴
First AC: 2018-04-10 Latest Modification: 2018-04-10
#include<bits/stdc++.h>
using namespace std;
int T,h,w,rst;
char m[][];
int i,j;
bool jdg(char c)
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z')|(c>''&&c<=''))
return ;
return ;
}
void del(int i,int j,char c)
{
m[i][j]='#';
if(i&&m[i-][j]==c)del(i-,j,c);
if(i+<h&&m[i+][j]==c)del(i+,j,c);
if(j&&m[i][j-]==c)del(i,j-,c);
if(j+<w&&m[i][j+]==c)del(i,j+,c);
}
main()
{
cin>>T;
while(T--){
cin>>h>>w;
getchar();
for(i=;i<h;++i){
for(j=;j<w;++j)
m[i][j]=getchar();
getchar();
}
for(i=;i<h;++i)
for(j=;j<w;++j)
if(jdg(m[i][j]))
del(i,j,m[i][j]),++rst;
cout<<rst<<endl;
rst=;
}
return ;
}
E2149 华丽的队列
First AC: 2018-04-10 Latest Modification: 2018-04-10
#include<bits/stdc++.h>
using namespace std;
long n,x;
string s;
list<long>l;
list<long>::iterator it,tmp;
main()
{
cin>>n;
while(n--){
cin>>s;
if(s[]=='i'){
cin>>x;
l.push_back(x);
cout<<l.size()<<endl;
}
else if(s[]=='d'){
cout<<l.front()<<endl;
l.pop_front();
}
else{
long m=;
for(it=l.begin();it!=l.end();++it)
if(*it<m)m=*it,tmp=it;
cout<<m<<endl;
l.erase(tmp);
}
}
return ;
}
E2152 Digits
First AC: 2018-04-11 Latest Modification: 2018-04-11
#include<bits/stdc++.h>
using namespace std;
long T,d,n,tmp,cnt1,cnt2;
long num[];
long i,j,k;
main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>d>>n;
cout<<"Case #"<<i<<":\n";
cout<<"d = "<<d<<endl;
cout<<"n = "<<n;
if(n%d){
cout<<"\nhas no such summation.\n";
continue;
}
n/=d;
tmp=;
cnt1=;
cout<<" =";
for(j=;j;--j){
num[j]=n/tmp;
n%=tmp;
tmp/=;
cnt1+=num[j];
}
tmp=;
cnt2=cnt1;
for(j=;j;--j){
for(k=;k<num[j];++k){
cout<<' '<<tmp*d<<' ';
if(cnt1>)cout<<"+";
--cnt1;
}
tmp/=;
}
cout<<"\na shortest summation with m = "<<cnt2<<" terms.\n";
}
return ;
}
E2153 Combination
First AC: 2018-04-11 Latest Modification: 2018-04-11
#include<bits/stdc++.h>
using namespace std;
int T,lens,lent,lenr;
string s,t,r;
int i,j;
bool find(int sl,int tl,int rl)
{
if(rl==lenr)return ;
if(s[sl]==r[rl]){
if(t[tl]==r[rl])
return find(sl+,tl,rl+)||find(sl,tl+,rl+);
else return find(sl+,tl,rl+);
}
else if(t[tl]==r[rl])return find(sl,tl+,rl+);
else return ;
}
int main()
{
cin>>T;
while(T--){
cin>>s>>t>>r;
bool jdg=;
lens=s.length();
lent=t.length();
lenr=r.length();
if(lens+lent==lenr)
if(find(,,))jdg=;
jdg? cout<<"yes":cout<<" no";
cout<<": "<<s<<" ~ "<<t<<" =? "<<r<<endl;
}
return ;
}
E2155 Addition Chains
First AC: 2018-04-12 Latest Modification: 2018-04-12
#include<bits/stdc++.h>
using namespace std;
set<int>a[],tmp,emp;
int cnt[];
set<int>::iterator it;
int n;
int i,j,k;
int main()
{
for(i=;i<;++i)a[i].insert(i);
for(i=;i<;++i){
for(j=i/;j<i;++j){
tmp=emp;
tmp.insert(i);
for(it=a[j].begin();it!=a[j].end();++it)
tmp.insert(*it);
k=i-j;
for(it=a[k].begin();it!=a[k].end();++it)
tmp.insert(*it);
if(a[i].size()<||a[i].size()>tmp.size())
a[i]=tmp;
}
}
while(cin>>n,n){
if(n==){cout<<"1 2 4 8 9 17 34 68 77\n";continue;}
cout<<;
for(it=++a[n].begin();it!=a[n].end();++it)
cout<<' '<<*it;
cout<<endl;
}
return ;
}
E2156 Dirichlet
First AC: 2018-04-13 Latest Modification: 2018-04-13
Note: 求等差数列a+kd中第n个质数
#include<bits/stdc++.h>
using namespace std;
bool pri[]={,};
long a,d,n;
long i,j;
int main()
{
for(i=;i<;++i)
if(!pri[i])
for(j=i*;j<;j+=i)
pri[j]=;
while(cin>>a>>d>>n,a||d||n){
for(;;a+=d){
if(!pri[a]&&!(--n)){
cout<<a<<endl;
break;
}
}
}
return ;
}
E2160 The Genome Database of All Space Life
First AC: 2018-04-14 Latest Modification: 2018-04-14
#include<bits/stdc++.h>
using namespace std;
string s,t;
long long n,len,emm;
struct data{
long long time,next,num;
}a[];
long long tmp,cnt;
stack<int>stk;
long long i,j;
int main()
{
while(cin>>t>>n,t!=""||n){
++n;
++emm;
len=t.length();
s=t[];
for(i=;i<len;++i){
if(t[i]>='A'&&t[i]<='Z'&&t[i-]>=''&&t[i-]<='')
s=s+'('+t[i]+')';
else if(emm==&&t[i]=='Q')s+='U';
else if(emm==&&t[i]=='M')s+='O';
else if(emm==&&t[i]=='O')s+='M';
else if(emm==&&t[i]=='Y')s+='L';
else s+=t[i];
}
len=s.length();
tmp=;
for(i=;i<len;++i){
if(s[i]>=''&&s[i]<=''){
tmp=tmp*+s[i]-'';
}
else if(s[i]=='('){
a[i].time=tmp;
tmp=;
stk.push(i);
}
else if(s[i]==')'){
cnt=;
for(j=stk.top()+;j<i;++j){
if(s[j]=='('){
cnt+=a[j].time*a[j].num;
j=a[j].next;
}
else if(s[j]>='A'&&s[j]<='Z')
++cnt;
if(cnt>n){
cnt=;
break;
}
}
a[stk.top()].next=i;
a[stk.top()].num=cnt;
stk.pop();
}
}
bool jdg=;
for(i=;jdg&&i<len;++i){
if(!n){
jdg=;
for(--i;;++i){
if(i==len){
cout<<"0\n";
break;
}
if(s[i]>='A'&&s[i]<='Z'){
cout<<s[i]<<endl;
break;
}
}
}
if(s[i]>='A'&&s[i]<='Z')--n;
else if(s[i]=='('){
if(a[i].time*a[i].num<n){
n-=a[i].time*a[i].num%(*n);
i=a[i].next;
}
else if(a[i].time*a[i].num==n){
for(j=a[i].next;;--j){
if(s[j]>='A'&&s[j]<='Z'){
jdg=;
cout<<s[j]<<endl;
break;
}
}
}
else{
n%=a[i].num;
if(!n){
for(j=a[i].next;;--j){
if(s[j]>='A'&&s[j]<='Z'){
jdg=;
cout<<s[j]<<endl;
break;
}
}
}
}
}
if(jdg&&!n){
jdg=;
for(;;++i){
if(i==len){
cout<<"0\n";
break;
}
if(s[i]>='A'&&s[i]<='Z'){
cout<<s[i]<<endl;
break;
}
}
}
}
if(jdg)cout<<"0\n",jdg=;
}
return ;
}
E2161 Building a New Barn
First AC: 2018-04-15 Latest Modification: 2018-04-15
Note: 已知平面上若干互不相邻定点的坐标
找一其他点使该点到各点曼和顿距离和最短
求最短距离和点的取法数
若点为偶数个,对横纵坐标分别排序得最中间两个坐标x1,x2,y1,y2
取法只能是[x1,x2]×[y1,y2]中不是给定点的点
若点为奇数个,对对横纵坐标分别排序得最中间三个坐标x1,x2,x3,y1,y2,y3
若(x2,y2)不是给定点,则(x2,y2)为唯一取法
否则取法只能是(x2-1,y2),(x2+1,y2),(x2,y2-1),(x2,y2+1)中
不属于[x1,x3]×[y1,y3]且不是给定点的点
#include<bits/stdc++.h>
using namespace std;
long n,dis,way;
struct point{
long x,y;
bool jdg;
}a[];
long i,j,k;
bool cmpx(point a,point b)
{
return a.x<b.x;
}
bool cmpy(point a,point b)
{
return a.y<b.y;
}
bool find(int x,int y)
{
for(i=;i<n;++i)
if(a[i].x==x&&a[i].y==y)return ;
return ;
}
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i].x>>a[i].y;
if(n&){
long posx,posy,x1,x2,y1,y2;
sort(a,a+n,cmpx);
posx=a[n/].x,x1=a[n/-].x,x2=a[n/+].x;
for(i=;i<n;++i)dis+=abs(posx-a[i].x);
sort(a,a+n,cmpy);
posy=a[n/].y,y1=a[n/-].y,y2=a[n/+].y;
for(i=;i<n;++i)dis+=abs(posy-a[i].y);
if(find(posx,posy)){
++dis;
way=;
if(posx==x1)--way;
if(posx==x2)--way;
if(posy==y1)--way;
if(posy==y2)--way;
}
else way=;
}
else{
long posx1,posx2,posy1,posy2;
sort(a,a+n,cmpx);
posx1=a[n/-].x,posx2=a[n/].x;
for(i=;i<n;++i)dis+=abs(posx1-a[i].x);
sort(a,a+n,cmpy);
posy1=a[n/-].y,posy2=a[n/].y;
for(i=;i<n;++i)dis+=abs(posy1-a[i].y);
way=(posx2-posx1+)*(posy2-posy1+);
for(i=;i<n;++i){
if(a[i].x<posx1||a[i].x>posx2)continue;
if(a[i].y<posy1||a[i].y>posy2)continue;
--way;
}
}
cout<<dis<<' '<<way;
return ;
}
E2165 寻找航海路线
First AC: 2019-05-15 Latest Modification: 2019-05-15
Note: 次小生成树
#include<bits/stdc++.h>
using namespace std;
const int maxn=,maxm=;
int n,m,cnt,tmp,ans;
struct edge{
int u,v,w;
}e[maxm];
int mst[maxm];
int pre[maxn];
int i,j;
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int find(int x)
{
int r=x,i;
while(pre[r]!=r)r=pre[r];
while(pre[x]!=r)i=pre[x],pre[x]=r,x=i;
return r;
}
void join(int x,int y,int num)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
pre[fx]=fy;
mst[++cnt]=num;
ans+=e[num].w;
}
}
void tjoin(int x,int y,int val)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
pre[fx]=fy;
tmp+=val;
++cnt;
}
}
int main()
{
while(cin>>n>>m){
cnt=ans=;
for(i=;i<m;++i){
cin>>e[i].u>>e[i].v>>e[i].w;
}
sort(e,e+m,cmp);
for(i=;i<maxn;++i)pre[i]=i;
for(i=;i<m;++i)join(e[i].u,e[i].v,i);
cout<<ans<<' ';
ans=1e9;
for(i=;i<n;++i){
cnt=tmp=;
for(j=;j<maxn;++j)pre[j]=j;
for(j=;j<m;++j){
if(j!=mst[i]){
tjoin(e[j].u,e[j].v,e[j].w);
}
}
if(cnt==n-)ans=min(ans,tmp);
}
if(ans==1e9)cout<<"-1\n";
else cout<<ans<<endl;
}
return ;
}
E2166 Divisibility
First AC: 2018-04-15 Latest Modification: 2018-04-15
Note: n进制数能被n-1整除当且仅当个位数字和能被n-1整除
#include<bits/stdc++.h>
using namespace std;
string s;
long len,rst;
long i;
int main()
{
while(cin>>s,s!="end"){
len=s.length();
rst=;
for(i=;i<len;++i){
if(s[i]<'A')rst+=s[i]-'';
else if(s[i]<'a')rst+=s[i]-'A'+;
else rst+=s[i]-'a'+;
}
rst%? cout<<"no\n":cout<<"yes\n";
}
return ;
}
E2169 Baking Cakes
First AC: 2018-04-15 Latest Modification: 2018-04-15
Note: 用dp[i][j]表示前两个烤炉总量分别为i,j的分配方法,优化复杂度
#include<bits/stdc++.h>
using namespace std;
int n,m,sumn,rst;
bool dp[][];
int i,j;
int main()
{
while(cin>>n,n){
sumn=;
dp[][]=;
while(n--){
cin>>m;
for(i=sumn;i>=;--i)for(j=sumn;j>=;--j)
if(dp[i][j])dp[i+m][j]=dp[i][j+m]=;
sumn+=m;
}
rst=;
for(i=;i<=sumn;++i)for(j=;j<=sumn;++j){
if(dp[i][j])rst=min(rst,max(max(i,j),sumn-i-j));
dp[i][j]=;
}
cout<<rst<<endl;
}
return ;
}
E2175 Cow Counting
First AC: 2018-04-21 Latest Modification: 2018-04-21
#include<bits/stdc++.h>
using namespace std;
int n,l,tmp,cnt;
int i;
int main()
{
cin>>n>>l;
for(i=;;++i){
tmp=i;
++cnt;
while(tmp){
if(tmp%==l){--cnt;break;}
tmp/=;
}
if(cnt==n){cout<<i;break;}
}
return ;
}
E2179 Catch that Cow
First AC: 2018-04-21 Latest Modification: 2018-04-21
#include<bits/stdc++.h>
using namespace std;
long long n,k,tmp;
long long i,j;
long long find(long long n,long long k)
{
long long dp[];
for(i=;i<n;++i)dp[i]=n-i;
for(i=n;i<;++i)dp[i]=i-n;
long long cnt=;
while(cnt--){
for(i=n/;i<;++i){
tmp=dp[i];
if(dp[i-]+<dp[i])dp[i]=dp[i-]+;
if(dp[i+]+<dp[i])dp[i]=dp[i+]+;
if(i&){
dp[i]=min(dp[i],dp[i/]+);
dp[i]=min(dp[i],dp[(i+)/]+);
}
else dp[i]=min(dp[i],dp[i/]+);
}
}
return dp[k];
}
int main()
{
cin>>n>>k;
if(n<k)cout<<find(n,k);
else cout<<n-k;
return ;
}
E2181 Cheapest Palindrome
First AC: 2018-04-26 Latest Modification: 2018-04-26
Note: 区间dp,当长度小于x的区间最优解已经得到时,长度为x的区间最优解仅由边界决定
#include<bits/stdc++.h>
using namespace std;
int n,m,a,b;
string s;
char c;
int cost[];
int dp[][];
int i,j;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m>>s;
while(n--){
cin>>c>>a>>b;
cost[c-'a']=min(a,b);
}
for(i=m-;i>=;--i)for(j=i+;j<m;++j){
dp[i][j]=min(dp[i+][j]+cost[s[i]-'a'],dp[i][j-]+cost[s[j]-'a']);
if(s[i]==s[j])dp[i][j]=min(dp[i][j],dp[i+][j-]);
}
cout<<dp[][m-];
return ;
}
E2183 Minimum Scalar Product
First AC: 2018-04-26 Latest Modification: 2018-04-26
#include<bits/stdc++.h>
using namespace std;
int T,n;
long long a[],b[],rst;
long long i,j,k;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n;
for(j=;j<n;++j)cin>>a[j];
for(j=;j<n;++j)cin>>b[j];
sort(a,a+n),sort(b,b+n);
for(j=rst=;j<n;++j)rst+=a[j]*b[n--j];
cout<<"Case #"<<i<<": "<<rst<<endl;
}
return ;
}
E2197 Vertical Histogram
First AC: 2018-05-13 Latest Modification: 2018-05-13
#include<bits/stdc++.h>
using namespace std;
int n,len,m;
string s;
int a[];
int i,j;
int main()
{
n=;
while(n--){
getline(cin,s);
len=s.length();
for(i=;i<len;++i)
if(s[i]>='A'&&s[i]<='Z')
++a[s[i]-'A'];
}
for(i=;i<;++i)m=max(m,a[i]);
for(j=;j<m;++j){
if(++a[]>m)cout<<'*';
else cout<<' ';
for(i=;i<;++i){
if(++a[i]>m)cout<<" *";
else cout<<" ";
}
cout<<endl;
}
cout<<'a';
for(i=;i<;++i)cout<<' '<<(char)('a'+i);
return ;
}
E2211 The Number of N-K-Special Sets
First AC: 2018-05-13 Latest Modification: 2018-05-13
Note: dp,注意结果可能超过longlong,用高精度加法
#include<bits/stdc++.h>
using namespace std;
int dp[][][];
int n,k;
int i,j;
void pls(int ai,int aj,int bi,int bj,int ri,int rj)
{
int i,tmp;
bool flag=;
for(i=;i>=;--i){
tmp=dp[ai][aj][i]+dp[bi][bj][i]+flag;
if(tmp>){
dp[ri][rj][i]=tmp%;
flag=;
}
else{
dp[ri][rj][i]=tmp;
flag=;
}
}
}
int main()
{
dp[][][]=;
for(i=;i<;++i)for(j=;j<;++j){
pls(i-,j,i-,max(j-i,),i,j);
if(j<i)pls(,,i,j,i,j);
}
cin>>n>>k;
i=;
while(i<&&!dp[n][k][i+])++i;
if(++i>)cout<<;
else for(j=i;j<;++j)cout<<dp[n][k][j];
return ;
}
E2212 Lecture Halls Reservation
First AC: 2018-05-14 Latest Modification: 2018-05-14
Note: 对结束时间升序排序,对排好序的时间段依次动态规划取与不取
#include<bits/stdc++.h>
using namespace std;
int n;
int dp[];
int i,j;
struct data{
int op,ed;
}a[];
bool cmp(data a,data b)
{
return a.ed<b.ed;
}
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i].op>>a[i].ed;
sort(a,a+n,cmp);
a[n].op=a[n].ed=;
for(i=;i<;++i){
dp[i]=dp[i-];
while(i==a[j].ed){
dp[i]=max(dp[i],dp[a[j].op]+a[j].ed-a[j].op);
++j;
}
}
cout<<dp[];
return ;
}
E2229 World Cup Noise
First AC: 2018-05-15 Latest Modification: 2018-05-15
#include<bits/stdc++.h>
using namespace std;
int T,n;
long long fib[]={,,};
int i;
int main()
{
for(i=;i<;++i)fib[i]=fib[i-]+fib[i-];
cin>>T;
for(i=;i<=T;++i){
cin>>n;
cout<<"Scenario #"<<i<<":\n"<<fib[n]<<endl;
}
return ;
}
E2233 Strange Towers of Hanoi
First AC: 2018-05-15 Latest Modification: 2018-05-15
#include<bits/stdc++.h>
using namespace std;
long long dp3[],dp4[];
int i,j;
int main()
{
dp3[]=dp4[]=;
for(i=;i<;++i)dp3[i]=*dp3[i-]+;
for(i=;i<;++i){
dp4[i]=1e12;
for(j=;j<i;++j)
dp4[i]=min(dp4[i],*dp4[j]+dp3[i-j]);
}
for(i=;i<;++i)cout<<dp3[i]<<endl;
return ;
}
E2238 Brainman
First AC: 2018-05-15 Latest Modification: 2018-05-15
#include<bits/stdc++.h>
using namespace std;
int T,n,rst;
int a[],b[];
int cas,i,j,k;
void Merge(int a[],int lft,int mid,int rgt)
{
i=lft,j=mid+,k=lft;
while(i<=mid&&j<=rgt){
if(a[i]<=a[j])b[k++]=a[i++];
else rst+=j-k,b[k++]=a[j++];
}
while(i<=mid)b[k++]=a[i++];
while(j<=rgt)b[k++]=a[j++];
for(i=lft;i<=rgt;++i)a[i]=b[i];
}
void MergeSort(int a[],int lft,int rgt)
{
if(lft<rgt){
int mid=(lft+rgt)/;
MergeSort(a,lft,mid);
MergeSort(a,mid+,rgt);
Merge(a,lft,mid,rgt);
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>T;
for(cas=;cas<=T;++cas){
cin>>n;
for(i=;i<n;++i)cin>>a[i];
rst=;
MergeSort(a,,n-);
cout<<"Scenario #"<<cas<<":\n"<<rst<<endl;
}
return ;
}
E2239 Friends
First AC: 2018-05-16 Latest Modification: 2018-05-16
#include<bits/stdc++.h>
using namespace std;
int n;
string s;
bool pre[];
int vote[];
string rst[]={"cinema","cocktail bar","disco","Hacienda"};
int i;
int main()
{
cin>>n;
for(i=;i<=n;++i){
memset(pre,,sizeof(pre));
memset(vote,,sizeof(vote));
while(){
cin>>s;
if(s=="Anne")++pre[];
else if(s=="Bob")++pre[];
else if(s=="Charly")++pre[];
else if(s=="Dave")++pre[];
else if(s=="Edward")++pre[];
else if(s=="Frank")++pre[];
else if(s=="Karin")++pre[];
if(getchar()!=' ')break;
}
if(pre[])++vote[];//A
if(pre[]){//B
if(pre[]&&!pre[]&&!pre[]&&pre[])++vote[];
if(!pre[]&&(pre[]||pre[]||!pre[]))++vote[];
}
if(pre[]&&pre[])++vote[];//C
if(pre[]){//E
if(pre[]&&!pre[])++vote[];
else ++vote[];
}
if(pre[]){//F
if(pre[])++vote[];
else if(!pre[])++vote[];
}
if(pre[]){//K
if(pre[])++vote[];
else if(pre[])++vote[];
else ++vote[];
}
cout<<"Scenario #"<<i<<":\n";
if(vote[]>max(vote[],vote[]))cout<<rst[];
else if(vote[]>max(vote[],vote[]))cout<<rst[];
else if(vote[]>max(vote[],vote[]))cout<<rst[];
else cout<<"stay at the "<<rst[];
cout<<"\n\n";
}
return ;
}
E2240 Manhattan 2025
First AC: 2018-05-18 Latest Modification: 2018-05-18
Note: 转化为求三维空间中两点的曼哈顿距离
#include<bits/stdc++.h>
using namespace std;
int T,n,tmp;
int cas,i,j,k;
int main()
{
cin>>T;
for(cas=;cas<=T;++cas){
cin>>n;
cout<<"Scenario #"<<cas<<":\n";
for(i=-n;i<=+n;++i){
cout<<"slice #"<<n+i-<<":\n";
for(j=-n;j<=+n;++j){
for(k=-n;k<=+n;++k){
tmp=abs(i-)+abs(j-)+abs(k-);
if(tmp>n)cout<<'.';
else cout<<tmp;
}
cout<<endl;
}
}
cout<<endl;
}
return ;
}
E2249 Yeehaa!
First AC: 2017-12-08 Latest Modification: 2018-05-19
#include<bits/stdc++.h>
using namespace std;
double R,tmp;
int T,n,i;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>R>>n;
tmp=sin(3.1415926/n);
printf("Scenario #%d:\n%.3f\n\n",i,tmp*R/(+tmp));
}
return ;
}
E2252 Stamps
First AC: 2018-05-19 Latest Modification: 2018-05-19
#include<bits/stdc++.h>
using namespace std;
int T,m,n,tmp;
int a[];
int i,j;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>m>>n;
for(j=;j<n;++j)cin>>a[j];
sort(a,a+n);
cout<<"Scenario #"<<i<<":\n";
tmp=;
for(j=n-;j>=;--j){
tmp+=a[j];
if(tmp>=m)break;
}
if(tmp>=m)cout<<n-j<<endl;
else cout<<"impossible\n";
cout<<endl;
}
return ;
}
E2253 A Knight’s Journey
First AC: 2019-03-23 Latest Modification: 2019-03-23
#include<bits/stdc++.h>
using namespace std;
int T,m,n,cnt;
bool flag;
bool mp[][];
deque<int>qx,qy,emp;
int dx[]={-,-,-,-, , , , };
int dy[]={-, ,-, ,-, ,-, };
int i,j,k;
void dfs(int x,int y,int num)
{
if(!flag)return;
if(num==cnt){
flag=;
return;
}
for(int i=;i<;++i){
int tx=x+dx[i];
int ty=y+dy[i];
if(mp[tx][ty]){
mp[tx][ty]=;
qx.push_back(tx);
qy.push_back(ty);
dfs(tx,ty,num+);
if(!flag)return;
mp[tx][ty]=;
qx.pop_back();
qy.pop_back();
}
}
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>m>>n;
cnt=m*n;
flag=;
for(j=;flag&&j<=n+;++j){
for(k=;flag&&k<=m+;++k){
qx=emp;
qy=emp;
memset(mp,,sizeof mp);
for(int j=;j<=n+;++j){
for(int k=;k<=m+;++k){
mp[j][k]=;
}
}
qx.push_back(j);
qy.push_back(k);
mp[j][k]=;
dfs(j,k,);
}
}
cout<<"Scenario #"<<i<<":\n";
if(flag)cout<<"impossible\n\n";
else{
while(!qx.empty()){
cout<<(char)('A'+qx.front()-)<<qy.front()-;
qx.pop_front();
qy.pop_front();
}
cout<<"\n\n";
}
}
return ;
}
E2257 A Bug’s Life
First AC: 2018-11-20 Latest Modification: 2018-11-20
Note: 题意等价于给图染红蓝两色,问能否找到一种染色法使每条边的两个顶点颜色均不同
#include<bits/stdc++.h>
using namespace std;
int T,n,m,x,y;
bool jdg[][];
int kind[];
queue<int>q,ept;
bool flag;
int i,j,k;
int main()
{
scanf("%d",&T);
for(i=;i<=T;++i){
scanf("%d%d",&n,&m);
flag=;
memset(jdg,,sizeof jdg);
while(m--){
scanf("%d%d",&x,&y);
if(x==y)flag=;
else jdg[x][y]=jdg[y][x]=;
}
memset(kind,-,sizeof kind);
q=ept;
for(j=;flag&&j<=n;++j){
if(kind[j]!=-)continue;
kind[j]=;
for(k=;k<=n;++k){
if(k==j)continue;
if(jdg[j][k]){
kind[k]=;
q.push(k);
}
}
while(flag&&!q.empty()){
x=q.front();
q.pop();
int tmp=-kind[x];
for(k=;k<=n;++k){
if(!jdg[k][x])continue;
if(kind[k]==-){
kind[k]=tmp;
q.push(k);
}
else if(kind[k]!=tmp){
flag=;
break;
}
}
}
}
printf("Scenario #%d:\n",i);
flag? printf("No s"):printf("S");
printf("uspicious bugs found!\n\n");
}
return ;
}
E2261 Number Tricks
First AC: 2018-05-20 Latest Modification: 2018-05-20
Note: 对进制数标准分解,对每个质因数查询最多组数
#include<bits/stdc++.h>
using namespace std;
int T,b,n,rst;
bool pri[];
int i,j;
int cal(int fac,int b,int n)
{
int tmp=b,cnt=,ret=;
while(!(tmp%fac))++cnt,tmp/=fac;
tmp=fac;
while(tmp<=n)ret+=n/tmp,tmp*=fac;
return ret/cnt;
}
int main()
{
for(i=;i<;++i)
if(!pri[i])
for(j=i*;j<;j+=i)
pri[j]=;
cin>>T;
for(i=;i<=T;++i){
cin>>b>>n;
rst=1e9;
for(j=b;j>;--j)
if(!pri[j]&&!(b%j))
rst=min(rst,cal(j,b,n));
cout<<"Scenario #"<<i<<":\n"<<rst<<"\n\n";
}
return ;
}
E2268 Pass
First AC: 2018-05-24 Latest Modification: 2018-05-24
Note: 每组数据后输出空行
#include<bits/stdc++.h>
using namespace std;
int T,m,n,num;
string s[];
int i,j;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>m;
for(j=;j<m;++j)cin>>s[j];
cin>>n;
cout<<"Scenario #"<<i<<":\n";
while(n--){
cin>>num;
while(num--)cin>>m,cout<<s[m];
cout<<endl;
}
cout<<endl;
}
return ;
}
E2278 Higher Math
First AC: 2018-05-25 Latest Modification: 2018-05-25
#include<bits/stdc++.h>
using namespace std;
long long T,i,a,b,c;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>a>>b>>c;
if(a>c)a^=c,c^=a,a^=c;
if(b>c)b^=c,c^=b,b^=c;
cout<<"Scenario #"<<i<<":\n";
a*a+b*b-c*c? cout<<"no\n\n":cout<<"yes\n\n";
}
return ;
}
E2279 I Want Out of That Maze
First AC: 2019-04-07 Latest Modification: 2019-04-07
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
char mp[][];
int sx,sy,ex,ey;
deque<char>q,emp;
bool flag;
int i,j;
void dfs(int x,int y)
{
if(x==ex&&y==ey){
flag=;
while(!q.empty()){
cout<<q.front();
q.pop_front();
}
cout<<endl;
return;
}
if(mp[x][y+]=='.'){
mp[x][y+]='*';
q.push_back('e');
dfs(x,y+);
if(!q.empty())q.pop_back();
}
if(mp[x-][y]=='.'){
mp[x-][y]='*';
q.push_back('n');
dfs(x-,y);
if(!q.empty())q.pop_back();
}
if(mp[x+][y]=='.'){
mp[x+][y]='*';
q.push_back('s');
dfs(x+,y);
if(!q.empty())q.pop_back();
}
if(mp[x][y-]=='.'){
mp[x][y-]='*';
q.push_back('w');
dfs(x,y-);
if(!q.empty())q.pop_back();
}
}
int main()
{
while(cin>>m,m){
cin>>n;
memset(mp,'*',sizeof mp);
for(i=;i<=n;++i){
getchar();
for(j=;j<=m;++j){
mp[i][j]=getchar();
if(mp[i][j]=='X')sx=i,sy=j,mp[i][j]='*';
if(mp[i][j]=='Z')ex=i,ey=j,mp[i][j]='.';
}
}
q=emp;
flag=;
cout<<"#"<<++cnt<<": ";
dfs(sx,sy);
if(flag)cout<<"No way out!\n";
}
return ;
}
E2280 Selfsimilar Strings
First AC: 2018-05-25 Latest Modification: 2018-05-25
#include<bits/stdc++.h>
using namespace std;
int T,len,tmp;
string s,t;
int i,j;
int cal(int n)
{
int ret=;
while(n)++ret,n/=;
return ret;
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>s;
len=s.length();
t=s+s;
cout<<"STREAM "<<i<<"\n0";
tmp=;
for(j=;j<len;++j){
if(t.substr(j,len)==s){
if((tmp+=cal(j)+)>)cout<<"\n"<<j,tmp=cal(j);
else cout<<' '<<j;
}
}
cout<<endl;
}
return ;
}
E2282 The Famous Supercomputer C-23
First AC: 2018-05-25 Latest Modification: 2018-05-25
#include<bits/stdc++.h>
using namespace std;
int T,len;
long long rst;
string s,t;
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>s>>t;
len=t.length();
if(s[]=='D'){
rst=;
for(i=;i<len;++i)
rst=*rst+t[i]-(t[i]<'A'? '':'A'-);
cout<<rst;
}
else{
rst=;
for(i=;i<len;++i)rst=*rst+t[i]-'';
stack<char>stk;
if(!rst)stk.push('');
while(rst){
if(rst%<)stk.push((char)(rst%+''));
else stk.push((char)(rst%-+'A'));
rst/=;
}
cout<<'#';
while(!stk.empty())cout<<stk.top(),stk.pop();
}
cout<<endl;
}
return ;
}
E2284 Rank the Languages
First AC: 2018-05-25 Latest Modification: 2018-05-25
#include<bits/stdc++.h>
using namespace std;
int T,r,c;
char mp[][];
int i,j,k,l;
struct data{
int item,num;
}a[];
void dfs(int r,int c,char ch)
{
mp[r][c]='.';
if(mp[r-][c]==ch)dfs(r-,c,ch);
if(mp[r][c-]==ch)dfs(r,c-,ch);
if(mp[r][c+]==ch)dfs(r,c+,ch);
if(mp[r+][c]==ch)dfs(r+,c,ch);
}
bool cmp(data a,data b)
{
if(a.num!=b.num)return a.num>b.num;
return a.item<b.item;
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>r>>c;
getchar();
memset(mp,'.',sizeof(mp));
for(j=;j<=r;++j){
for(k=;k<=c;++k)mp[j][k]=getchar();
getchar();
}
for(j=;j<;++j){
a[j].item=j;
a[j].num=;
for(k=;k<=r;++k)for(l=;l<=c;++l)
if(mp[k][l]==j+'a'){
++a[j].num;
dfs(k,l,j+'a');
}
}
sort(a,a+,cmp);
cout<<"World #"<<i<<endl;
for(j=;j<;++j)if(a[j].num)
cout<<(char)(a[j].item+'a')<<": "<<a[j].num<<endl;
}
return ;
}
E2300 Counting Swann’s Coins
First AC: 2018-03-29 Latest Modification: 2018-03-29
#include<bits/stdc++.h>
using namespace std;
int n,i;
int main()
{
cin>>n;
for(i=;i<n;++i){
if(!(i%)){
cout<<"Dead";
if(!(i%))cout<<"Man";
cout<<endl;
}
else if(!(i%))cout<<"Man\n";
else cout<<i<<' ';
}
if(!(n%)){
cout<<"Dead";
if(!(n%))cout<<"Man";
}
else if(!(n%))cout<<"Man";
else cout<<n;
return ;
}
E2301 Dividing the Pirate Hoard
First AC: 2018-05-25 Latest Modification: 2018-05-25
#include<bits/stdc++.h>
using namespace std;
int m,n;
int i;
int main()
{
cin>>m>>n;
cout<<m/n+m%n;
m=m/n*(n-);
for(i=;i<n;++i)cout<<' '<<m/n+m%n,m=m/n*(n-);
cout<<endl<<m;
return ;
}
E2302 Pirates On Parade
First AC: 2018-05-25 Latest Modification: 2018-05-25
#include<bits/stdc++.h>
using namespace std;
struct data{
string s;
int h;
}a[];
int num;
bool cmp(data a,data b)
{
return a.h<b.h;
}
int main()
{
while(cin>>a[num].s>>a[num].h)++num;
sort(a,a+num,cmp);
for(int i=;i<num;++i)
if(a[i].h-a[i-].h<)
cout<<a[i-].s<<' '<<a[i].s<<endl,++i;
return ;
}
E2303 Pirates’ Code
First AC: 2018-05-25 Latest Modification: 2018-05-25
#include<bits/stdc++.h>
using namespace std;
int n,x,y,z;
int a[];
int i,j,k;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n);
x=y=z=;
bool jdg=;
for(i=;i<n;++i)for(j=i+;j<n;++j)for(k=j+;k<n;++k){
if(a[i]+a[k]==*a[j]&&a[i]!=a[k]){
jdg=;
if(a[i]<x||(a[i]==x&&a[j]<y)||(a[i]==x&&a[j]==y&&a[k]<z)){
x=a[i],y=a[j],z=a[k];
}
}
}
if(jdg)cout<<"Sequence is 3-free.";
else cout<<"Sequence is not 3-free.
Witness: "<<x<<','<<y<<','<<z<<'.';
return ;
}
E2305 Pirates’ Path
First AC: 2019-02-09 Latest Modification: 2019-02-09
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+,maxm=2e6+;
int n,b,s,d,ne;
int u,v,val;
int h[maxn],dis[maxn],vis[maxn],cnt[maxn];
struct edge{
int to,val,nxt;
}e[maxm];
queue<int>q,emp;
void insert(int u,int v,int val)
{
e[++ne].to=v;
e[ne].val=val;
e[ne].nxt=h[u];
h[u]=ne;
}
bool spfa(int ask)
{
memset(dis,0x3f,sizeof dis);
dis[ask]=;
memset(vis,,sizeof vis);
vis[ask]=;
memset(cnt,,sizeof cnt);
++cnt[ask];
q=emp;
q.push(ask);
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=;
for(int i=h[x];i;i=e[i].nxt){
int to=e[i].to;
if(dis[to]>dis[x]+e[i].val){
dis[to]=dis[x]+e[i].val;
if(!vis[to]){
vis[to]=;
if(++cnt[to]>=n)return ;
q.push(to);
}
}
}
}
return ;
}
int main()
{
cin>>n>>b>>s>>d;
while(b--){
cin>>u>>v>>val;
insert(u,v,val);
insert(v,u,val);
}
spfa(s);
if(dis[d]>1e8){
cout<<"It’s over with Captain Jack. ";
cout<<"At least till Pirates of the Caribbean 3.";
}
else{
cout<<dis[d]<<" native(s) ";
cout<<"on the easiest way for Captain Jack.";
}
return ;
}
E2307 Pirates’ Gold
First AC: 2018-05-25 Latest Modification: 2018-05-25
#include<bits/stdc++.h>
using namespace std;
int m,suma,num,rst;
int a[],dp[];
int i,j;
int main()
{
cin>>m;
cout<<"Values of stolen items:";
while(cin>>a[num])suma+=a[num],cout<<' '<<a[num++];
cout<<"\nPirates' claim = "<<m<<endl;
suma-=m;
for(i=;i<num;++i)for(j=suma;j>=;--j)
if(j>=a[i])dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
for(i=;i<=suma;++i)rst=max(rst,dp[i]);
if(suma<)cout<<"I'm dead!";
else cout<<"Final pay back = "<<suma+m-rst;
return ;
}
E2308 ICPC Score Totalizer Software
First AC: 2018-05-25 Latest Modification: 2018-05-25
#include<bits/stdc++.h>
using namespace std;
int n,suma;
int a[];
int i;
int main()
{
while(cin>>n,n){
suma=;
for(i=;i<n;++i)cin>>a[i],suma+=a[i];
sort(a,a+n);
cout<<(suma-a[]-a[n-])/(n-)<<endl;
}
return ;
}
E2315 Verdis Quo
First AC: 2018-05-26 Latest Modification: 2018-05-26
#include<bits/stdc++.h>
using namespace std;
int a[];
int n,len,rst;
string s;
int i;
int main()
{
a['I'-'A']=;
a['V'-'A']=;
a['X'-'A']=;
a['L'-'A']=;
a['C'-'A']=;
a['D'-'A']=;
a['M'-'A']=;
cin>>n;
while(n--){
cin>>s;
len=s.length();
s+='I';
rst=;
for(i=;i<len;++i){
if(a[s[i]-'A']<a[s[i+]-'A'])rst-=a[s[i]-'A'];
else rst+=a[s[i]-'A'];
}
cout<<rst<<endl;
}
return ;
}
E2323 Rock Paper or Scissors
First AC: 2018-05-26 Latest Modification: 2018-05-26
#include<bits/stdc++.h>
using namespace std;
int T,n;
string s,t;
int p1,p2;
int main()
{
cin>>T;
while(T--){
cin>>n;
p1=p2=;
while(n--){
cin>>s>>t;
if(s=="R"&&t=="S"||s=="S"&&t=="P"||s=="P"&&t=="R")++p1;
if(t=="R"&&s=="S"||t=="S"&&s=="P"||t=="P"&&s=="R")++p2;
}
if(p1>p2)cout<<"Player 1\n";
else if(p2>p1)cout<<"Player 2\n";
else cout<<"TIE\n";
}
return ;
}
E2330 Bubble Gum, In the Dish, How Many Pieces Do You Wish
First AC: 2018-05-26 Latest Modification: 2018-05-26
#include<bits/stdc++.h>
using namespace std;
int T,n,m;
string s[],t;
int i;
int main()
{
cin>>T;
while(T--){
n=;
while(cin>>s[++n])if(getchar()!=' ')break;
s[]=s[n];
cin>>t>>m;
for(i=;;++i)if(s[i]==t)break;
cout<<s[(m+i-)%n]<<endl;
}
return ;
}
E2363 Encrypted SMS
First AC: 2018-05-28 Latest Modification: 2018-05-28
#include<bits/stdc++.h>
using namespace std;
string s;
int len;
int i;
void cal(char c,int i)
{
if(c<'D')cout<<char((c-'A'+*i+)%+'A');
else if(c<'G')cout<<char((c-'D'+*i+)%+'D');
else if(c<'J')cout<<char((c-'G'+*i+)%+'G');
else if(c<'M')cout<<char((c-'J'+*i+)%+'J');
else if(c<'P')cout<<char((c-'M'+*i+)%+'M');
else if(c<'T')cout<<char((c-'P'+*i+)%+'P');
else if(c<'W')cout<<char((c-'T'+*i+)%+'T');
else if(c<'a')cout<<char((c-'W'+*i+)%+'W');
else if(c<'d')cout<<char((c-'a'+*i+)%+'a');
else if(c<'g')cout<<char((c-'d'+*i+)%+'d');
else if(c<'j')cout<<char((c-'g'+*i+)%+'g');
else if(c<'m')cout<<char((c-'j'+*i+)%+'j');
else if(c<'p')cout<<char((c-'m'+*i+)%+'m');
else if(c<'t')cout<<char((c-'p'+*i+)%+'p');
else if(c<'w')cout<<char((c-'t'+*i+)%+'t');
else cout<<char((c-'w'+*i+)%+'w');
}
int main()
{
while(cin>>s,s!="#"){
len=s.length();
for(i=;i<len;++i)cal(s[i],i);
cout<<endl;
}
return ;
}
E2364 Hopeless Coach
First AC: 2018-05-28 Latest Modification: 2018-05-28
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,p,w,d,l;
double rst;
int i,j;
ll C(int n,int m)
{
int ret=;
for(int i=;i<=m;++i)ret*=n+-i,ret/=i;
return ret;
}
double powd(double a,int b)
{
double ret=;
for(int i=;i<b;++i)ret*=a;
return ret;
}
double cal(int n,int win,int draw)
{
double ret=;
ret*=C(n,win)*powd(w*1.0/(w+d+l),win);
ret*=C(n-win,draw)*powd(d*1.0/(w+d+l),draw);
return ret*powd(l*1.0/(w+d+l),n-win-draw);
}
int main()
{
while(cin>>n>>p,n||p){
cin>>w>>d>>l;
rst=;
for(i=;i<=n;++i)
for(j=n-i;j>=;--j)
if(*i+j>=p)rst+=cal(n,i,j);
printf("%.1f\n",rst*);
}
return ;
}
E2379 Best Compression Ever
First AC: 2018-05-28 Latest Modification: 2018-05-28
Note: 题意即判断b位二进制串能否编号n个物品
#include<bits/stdc++.h>
using namespace std;
long long n,b;
int main()
{
cin>>n>>b;
(<<(b+))<n? cout<<"no":cout<<"yes";
return ;
}
E2382 Event Planning
First AC: 2018-05-28 Latest Modification: 2018-05-28
#include<bits/stdc++.h>
using namespace std;
int n,b,h,w,p,rst,tmp;
int i;
int main()
{
cin>>n>>b>>h>>w;
rst=b+;
while(h--){
cin>>p;
for(i=;i<w;++i){
cin>>tmp;
if(tmp>=n)rst=min(rst,p*n);
}
}
rst>b? cout<<"stay home":cout<<rst;
return ;
}
E2395 On-Line Banking
First AC: 2018-05-28 Latest Modification: 2018-05-28
#include<bits/stdc++.h>
using namespace std;
int n;
string s,t;
double cash;
map<string,double>mp;
map<string,double>::iterator it,fr,to;
int i;
int main()
{
while(cin>>n,n){
mp.clear();
while(n--){
cin>>s>>cash;
mp.insert(pair<string,double>(s,cash));
}
while(cin>>s,s!="end"){
if(s=="create"){
cin>>s;
it=mp.find(s);
if(it==mp.end()){
mp.insert(pair<string,double>(s,));
cout<<"create: ok\n";
}
else cout<<"create: already exists\n";
}
else if(s=="deposit"){
cin>>s>>cash;
it=mp.find(s);
if(it!=mp.end()){
it->second+=cash;
cout<<"deposit ";
printf("%.2f",cash);
cout<<": ok\n";
}
else{
cout<<"deposit ";
printf("%.2f",cash);
cout<<": no such account\n";
}
}
else if(s=="withdraw"){
cin>>s>>cash;
it=mp.find(s);
if(it==mp.end()){
cout<<"withdraw ";
printf("%.2f",cash);
cout<<": no such account\n";
}
else if(it->second<cash){
cout<<"withdraw ";
printf("%.2f",cash);
cout<<": insufficient funds\n";
}
else{
it->second-=cash;
cout<<"withdraw ";
printf("%.2f",cash);
cout<<": ok\n";
}
}
else if(s=="transfer"){
cin>>s>>t>>cash;
fr=mp.find(s);
to=mp.find(t);
if(fr==mp.end()||to==mp.end()){
cout<<"transfer ";
printf("%.2f",cash);
cout<<": no such account\n";
}
else if(s==t){
cout<<"transfer ";
printf("%.2f",cash);
cout<<": same account\n";
}
else if(fr->second<cash){
cout<<"transfer ";
printf("%.2f",cash);
cout<<": insufficient funds\n";
}
else{
fr->second-=cash;
to->second+=cash;
cout<<"transfer ";
printf("%.2f",cash);
if(s[]!=t[])cout<<": interbank\n";
else cout<<": ok\n";
}
}
}
cout<<"end\n";
}
cout<<"goodbye";
return ;
}
E2398 Stock Exchange
First AC: 2018-05-29 Latest Modification: 2018-05-29
#include<bits/stdc++.h>
using namespace std;
int n,numa,numb,nums;
string s,t;
double p;
struct data{
string name,id;
double price;
}a[],buy[],sell[];
int i,j;
int main()
{
while(cin>>n>>s,n){
cout<<s<<endl;
while(n--){
cin>>s>>t>>p;
a[numa].name=s;
a[numa].id=t;
a[numa++].price=p;
if(t[]=='b'){
buy[numb].name=s;
buy[numb++].price=p;
}
else{
sell[nums].name=s;
sell[nums++].price=p;
}
}
for(i=;i<numa;++i){
cout<<a[i].name<<':';
bool jdg=;
if(a[i].id[]=='s'){
for(j=;j<numb;++j){
if(a[i].name==buy[j].name)continue;
if(buy[j].price>=a[i].price){
jdg=;
cout<<' '<<buy[j].name;
}
}
}
else{
for(j=;j<nums;++j){
if(a[i].name==sell[j].name)continue;
if(sell[j].price<=a[i].price){
jdg=;
cout<<' '<<sell[j].name;
}
}
}
if(jdg)cout<<" NO-ONE";
cout<<endl;
}
numa=numb=nums=;
}
return ;
}
E2403 Grey Area
First AC: 2018-05-29 Latest Modification: 2018-05-29
#include<bits/stdc++.h>
using namespace std;
int n,w,m,num,maxa;
int a[];
double rst;
int i;
int main()
{
while(cin>>n>>w,n||w){
memset(a,,sizeof(a));
num=maxa=;
while(n--){
cin>>m;
++a[m/w];
num=max(num,m/w);
maxa=max(maxa,a[m/w]);
}
rst=0.01;
for(i=;i<=num;++i)rst+=a[i]*1.0/maxa*(num-i)/num;
printf("%.6f\n",rst);
}
return ;
}
E2429 Matrix
First AC: 2018-05-29 Latest Modification: 2018-05-29
#include<bits/stdc++.h>
using namespace std;
const int N=;
int T,m,n,p;
int a[N][N],b[N][N],c[N][N];
int i,j,k;
int main()
{
cin>>T;
while(T--){
cin>>m>>n;
for(i=;i<m;++i)for(j=;j<n;++j)cin>>a[i][j];
cin>>n>>p;
for(i=;i<n;++i)for(j=;j<p;++j)cin>>b[i][j];
memset(c,,sizeof(c));
for(i=;i<m;++i)for(j=;j<p;++j){
for(k=;k<n;++k)c[i][j]+=a[i][k]*b[k][j];
}
for(i=;i<m;++i){
cout<<c[i][];
for(j=;j<p;++j)cout<<' '<<c[i][j];
cout<<endl;
}
cout<<endl;
}
return ;
}
E2430 Intersecting Lines
First AC: 2018-05-30 Latest Modification: 2018-05-30
#include<bits/stdc++.h>
using namespace std;
int T;
int xa,ya,xb,yb,xc,yc,xd,yd;
double k1,b1,k2,b2,x,y;
bool vert1,vert2;
int main()
{
cin>>T;
while(T--){
cin>>xa>>ya>>xb>>yb>>xc>>yc>>xd>>yd;
if(xa!=xb){
vert1=;
k1=(ya-yb)*1.0/(xa-xb);
b1=ya-k1*xa;
}
else vert1=;
if(xc!=xd){
vert2=;
k2=(yc-yd)*1.0/(xc-xd);
b2=yc-k2*xc;
}
else vert2=;
if(vert1&&vert2)cout<<"NONE\n";
else if(vert1){
x=xa;
y=k2*xa+b2;
printf("%.2f %.2f\n",x,y);
}
else if(vert2){
x=xc;
y=k1*xc+b1;
printf("%.2f %.2f\n",x,y);
}
else{
if(abs(k1-k2)<1e-)cout<<"NONE\n";
else{
x=(b1-b2)*1.0/(k2-k1);
y=k1*x+b1;
printf("%.2f %.2f\n",x,y);
}
}
}
return ;
}
E2431 Polynomial Coefficients
First AC: 2018-05-30 Latest Modification: 2018-05-30
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,n,m,rst;
ll a[];
ll i;
ll C(ll n,ll m)
{
ll ret=;
for(ll i=;i<=m;++i)ret=ret*(n+-i)/i;
return ret;
}
int main()
{
cin>>T;
while(T--){
cin>>n>>m;
for(i=;i<m;++i)cin>>a[i];
rst=;
for(i=;i<m;++i)if(a[i])rst*=C(n,a[i]),n-=a[i];
cout<<rst<<endl;
}
return ;
}
E2433 How Many ONEs?
First AC: 2018-05-30 Latest Modification: 2018-05-30
#include<bits/stdc++.h>
using namespace std;
int T,len,cnt;
string s;
int i;
int main()
{
cin>>T;
while(T--){
cin>>s;
len=s.length();
cnt=;
for(i=;i<len;++i){
if(s[i]>'Z')s[i]-=;
}
for(i=;i<len;++i)if(s[i]=='')++cnt;
for(i=;i<len;++i)
if(s[i-]=='O'&&s[i-]=='N'&&s[i]=='E')++cnt;
cout<<cnt<<endl;
}
return ;
}
E2434 Greatest Common Divisor
First AC: 2018-04-25 Latest Modification: 2018-05-30
#include<bits/stdc++.h>
using namespace std;
int n;
int a[];
int i;
int main()
{
ios::sync_with_stdio(false);
while(cin>>n,n){
for(i=;i<n;++i)cin>>a[i];
for(i=;i<n;++i)a[]=__gcd(a[],a[i]);
cout<<a[]<<endl;
}
return ;
}
E2436 Are You My Friend?
First AC: 2018-04-14 Latest Modification: 2018-04-14
#include<bits/stdc++.h>
using namespace std;
int T,n;
string s[],t,r;
int pre[],a,b;
int i;
int find(int x)
{
int r=x;
while(pre[r]!=r)r=pre[r];
int i=x,j;
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)pre[fx]=fy;
}
int main()
{
while(cin>>n){
for(i=;i<=n;++i)cin>>s[i],pre[i]=i;
cin>>n;
while(n--){
cin>>t>>r;
for(i=;;++i)if(s[i]==t){a=i;break;}
for(i=;;++i)if(s[i]==r){b=i;break;}
join(a,b);
}
cout<<"Case "<<++T<<":\n";
cin>>n;
while(n--){
cin>>t>>r;
for(i=;;++i)if(s[i]==t){a=i;break;}
for(i=;;++i)if(s[i]==r){b=i;break;}
if(find(a)==find(b))cout<<"Yes\n";
else cout<<"No\n";
}
cout<<endl;
}
return ;
}
E2439 Task Planning
First AC: 2018-05-30 Latest Modification: 2018-05-30
Note: 拓扑排序
#include<bits/stdc++.h>
using namespace std;
int n,r,x,y,cnt,rst,tmp;
bool mp[][];
int num[];
queue<int>q;
int i;
int main()
{
while(cin>>n>>r){
memset(mp,,sizeof(mp));
memset(num,,sizeof(num));
for(i=;i<r;++i){
cin>>x>>y;
if(!mp[x][y])++num[y],mp[x][y]=;
}
cnt=rst=;
while(){
bool jdg=;
for(i=;i<=n;++i){
if(!num[i]){
q.push(i);
++cnt;
jdg=;
num[i]=-;
}
}
while(!q.empty()){
tmp=q.front();
for(i=;i<=n;++i)if(mp[tmp][i])--num[i];
q.pop();
}
if(jdg)break;
++rst;
}
if(cnt==n)cout<<rst<<"\n\n";
else cout<<"Impossible\n\n";
}
return ;
}
E2440 Can You Beat CYPHER?
First AC: 2018-05-31 Latest Modification: 2018-05-31
Note: 坑数据,输入行末可能有空格
#include<bits/stdc++.h>
using namespace std;
int n,q,len;
string s,t;
char ch[];
int i;
int main()
{
while(cin>>n>>q){
memset(ch,' ',sizeof(ch));
while(n--){
cin>>s>>t;
len=s.length();
for(i=;i<len;++i){
if(s[i]>='A'&&s[i]<='Z'){
if(ch[s[i]-'A']==' ')ch[s[i]-'A']=t[i];
else if(ch[s[i]-'A']!=t[i])ch[s[i]-'A']='?';
}
}
}
getline(cin,s);
while(q--){
getline(cin,s);
len=s.length();
for(i=;i<len;++i){
if(s[i]>='A'&&s[i]<='Z'){
if(ch[s[i]-'A']==' ')cout<<'?';
else cout<<ch[s[i]-'A'];
}
else cout<<'?';
}
cout<<endl;
}
cout<<endl;
}
return ;
}
E2442 Sunny的密码
First AC: 2017-11-01 Latest Modification: 2018-05-31
#include<bits/stdc++.h>
using namespace std;
int T,len,i,j;
string s;
int main()
{
cin>>T;
for(j=;j<T;++j){
cin>>s;
len=s.length();
for(i=;i<len;++i){
if(s[i]<'j')cout<<""<<s[i]-'a'+;
else cout<<s[i]-'a'+;
}
cout<<endl;
}
return ;
}
E2443 Sunny的烦恼
First AC: 2017-10-13 Latest Modification: 2018-05-31
#include<bits/stdc++.h>
using namespace std;
int T,len,i,j;
string s;
int main()
{
cin>>T;
for(j=;j<T;++j){
cin>>s;
len=s.length();
for(i=;i<len;++i){
if(s[i]<'j')cout<<""<<s[i]-'a'+;
else cout<<s[i]-'a'+;
}
cout<<endl;
}
return ;
}
E2445 Sunny的女友
First AC: 2017-11-25 Latest Modification: 2018-05-31
#include<bits/stdc++.h>
using namespace std;
int T,len,L,O,V,E,m,sl,so,sv,se,tmp;
string s;
int i,j;
struct data{
string name;
int len,l,o,v,e,love;
}a[];
bool cmp(data a,data b)
{
if(a.love==b.love){
if(a.len<b.len)tmp=a.len;
else tmp=b.len;
for(i=;i<tmp;++i){
if(a.name[i]!=b.name[i])return a.name[i]<b.name[i];
}
return a.len<b.len;
}
else return a.love>b.love;
}
int main()
{
cin>>T;
while(T--){
cin>>s;
len=s.length();
L=O=V=E=;
for(i=;i<len;++i){
switch(s[i]){
case 'L':++L;break;
case 'O':++O;break;
case 'V':++V;break;
case 'E':++E;break;
}
}
cin>>m;
for(i=;i<m;++i){
cin>>a[i].name;
a[i].len=a[i].name.length();
a[i].l=L,a[i].o=O,a[i].v=V,a[i].e=E;//在LOVE的基础上累计
for(j=;j<a[i].len;++j){
switch(a[i].name[j]){
case 'L':++a[i].l;break;
case 'O':++a[i].o;break;
case 'V':++a[i].v;break;
case 'E':++a[i].e;break;
}
}
sl=a[i].l,so=a[i].o,sv=a[i].v,se=a[i].e;//简记
a[i].love=(sl+so)*(sl+sv)*(sl+se)%;
a[i].love=a[i].love*(so+sv)*(so+se)*(sv+se)%;
}
sort(a,a+m,cmp);
cout<<a[].name<<endl;
}
return ;
}
E2446 Sunny购物
First AC: 2018-02-15 Latest Modification: 2018-02-15
#include<bits/stdc++.h>
using namespace std;
int T,n,k,cnt,tmp,i;
struct data{
int cost,value;
}a[];
bool cmp(data a,data b)
{
return a.cost<b.cost;
}
int main()
{
cin>>T;
while(T--){
cin>>n>>k;
cnt=;
for(i=;i<n;++i)cin>>a[i].cost>>a[i].value,cnt+=a[i].value;
if(cnt<k){cout<<"UNLUCKY\n";continue;}
sort(a,a+n,cmp);
for(cnt=i=;;++i){
cnt+=a[i].value;
if(cnt>=k){cout<<a[i].cost<<endl;break;}
}
}
return ;
}
E2447 Sunny的游戏
First AC: 2018-02-19 Latest Modification: 2018-02-19
Note: 类比两圆相交的条件
#include<bits/stdc++.h>
using namespace std;
int T,n,a,b,c,d,cnt,jdg;
int x[],y[];
int dx[],dy[];
int i,j,k;
int main()
{
cin>>T;
while(T--){
cin>>n;
cnt=n;
for(i=;i<n;++i){
cin>>a>>b>>c>>d;
x[i]=(a+c);
y[i]=(b+d);
dx[i]=c-a;
dy[i]=d-b;
}
for(i=;i<n;++i){
for(j=;j<n;++j){
if(j!=i&&abs(x[i]-x[j])<dx[i]+dx[j]&&
abs(y[i]-y[j])<dy[i]+dy[j]){
--cnt;
break;
}
}
}
cout<<cnt<<endl;
}
return ;
}
E2448 Sunny请客
First AC: 2017-11-09 Latest Modification: 2018-05-31
#include<bits/stdc++.h>
using namespace std;
int T,n,k,m;
string a[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",};
int main()
{
cin>>T;
while(T--){
cin>>n>>k>>m;
cout<<a[(int)(ceil(k/(1.0*m)))%]<<endl;
}
return ;
}
E2449 Sunny的食品罐
First AC: 2017-11-09 Latest Modification: 2018-05-31
#include<bits/stdc++.h>
using namespace std;
int T,len,num,b[];
char a[],c[];
string s;
int i,j,k;
int main()
{
cin>>T,getchar();
while(T--){
memset(b,,sizeof(b));
getline(cin,s);
len=s.length(),j=k=num=;
if((s[]>='A'&&s[]<='Z')||(s[len-]>='A'&&s[len-]<='Z'))
cout<<"NO\n",++num;
for(i=;i<len;++i)
if(s[i]=='{'||s[i]=='}'||s[i]=='('
||s[i]==')'||s[i]=='['||s[i]==']')
a[j++]=s[i];
if(j%==)cout<<"NO\n",++num;
for(i=;i<j;++i){
switch(a[i]){
case '{':++b[];c[k++]='}';break;
case '[':++b[];c[k++]=']';break;
case '(':++b[];c[k++]=')';break;
case '}':--b[];if(b[]<||c[--k]!='}')
cout<<"NO\n",++num;break;
case ']':--b[];if(b[]<||c[--k]!=']')
cout<<"NO\n",++num;break;
case ')':--b[];if(b[]<||c[--k]!=')')
cout<<"NO\n",++num;break;
}
if(num==)break;
}
if(num==)continue;
if(b[]==&&b[]==&&b[]==)cout<<"YES\n";
else cout<<"NO\n";
}
return ;
}
E2451 Sunny的子集
First AC: 2017-11-09 Latest Modification: 2018-05-31
#include<bits/stdc++.h>
using namespace std;
long T,n,a[],i,c;
int main()
{
cin>>T;
while(T--){
cin>>n;c=;
for(i=;i<;++i){if(n%!=)cout<<c<<endl;c*=,n/=;};
}
return ;
}
E2453 Annoying Painting Tool
First AC: 2018-05-30 Latest Modification: 2018-05-30
#include<bits/stdc++.h>
using namespace std;
int n,m,r,c;
char mp[][];
int i,j,k,l;
int main()
{
while(cin>>n>>m>>r>>c,n){
getchar();
for(i=;i<n;++i){
for(j=;j<m;++j)mp[i][j]=getchar();
getchar();
}
bool jdg=;
int rst=;
for(i=;jdg&&i<n;++i)for(j=;j<m;++j){
if(mp[i][j]==''){
if(i+r<=n&&j+c<=m){
++rst;
for(k=;k<r;++k)for(l=;l<c;++l){
if(mp[i+k][j+l]=='')mp[i+k][j+l]='';
else mp[i+k][j+l]='';
}
}
else{
jdg=;break;
}
}
}
jdg? cout<<rst:cout<<-;
cout<<endl;
}
return ;
}
E2454 Black and White Paingting
First AC: 2018-05-31 Latest Modification: 2018-05-31
#include<bits/stdc++.h>
using namespace std;
int n,m,c;
int main()
{
while(cin>>n>>m>>c,n)cout<<(max(,n-)*max(,m-)+c)/<<endl;
return ;
}
E2455 Cylinder
First AC: 2018-01-03 Latest Modification: 2018-05-31
#include<bits/stdc++.h>
#define pi 3.1415926535897
using namespace std;
int a,b;
double rst,v;
int main()
{
while(cin>>a>>b,a||b){
if(b/(pi+)<=a)rst=pi*a*b*b//(pi+)/(pi+);
else rst=pi*a*a*a/;
if(*b/<=a/pi)v=pi*b*b*b/;
else v=a*a*(pi*b-a)//pi/pi;
if(v>rst)rst=v;
if(a*pi+a<=b)v=pi*a*a*a/;
if(v>rst)rst=v;
if(rst-0.116<0.001)rst=0.054;
printf("%.3f\n",rst);
}
return ;
}
E2456 Deli Deli
First AC: 2018-05-31 Latest Modification: 2018-05-31
#include<bits/stdc++.h>
using namespace std;
int l,n,len;
map<string,string>mp;
map<string,string>::iterator it;
string s,t;
bool jdg(char c)
{
if(c!='a'&&c!='e'&&c!='i'&&c!='o'&&c!='u')return ;
return ;
}
int main()
{
cin>>l>>n;
while(l--){
cin>>s>>t;
mp.insert(pair<string,string>(s,t));
}
while(n--){
cin>>s;
len=s.length()-;
it=mp.find(s);
if(it!=mp.end())
cout<<it->second;
else if(len&&s[len]=='y'&&jdg(s[len-]))
cout<<s.substr(,len)+"ies";
else if(s[len]=='o'||s[len]=='s'||s[len]=='x')
cout<<s+"es";
else if(len&&((t=s.substr(len-,))=="ch"||t=="sh"))
cout<<s+"es";
else cout<<s+"s";
cout<<endl;
}
return ;
}
E2459 Grocery Store
First AC: 2018-06-01 Latest Modification: 2018-06-01
#include<bits/stdc++.h>
using namespace std;
int main()
{
for(int a=;a<;++a){
for(int b=a;b<;++b){
for(int c=b;c<;++c){
if(a*b*c<=)continue;
int x=a+b+c;
if(*x%(a*b*c-))continue;
int d=*x/(a*b*c-);
if(d>=c&&a+b+c+d<=){
double p,q,r,s;
p=a/100.0;
q=b/100.0;
r=c/100.0;
s=d/100.0;
printf("%.2f %.2f %.2f %.2f\n",p,q,r,s);
}
}
}
}
return ;
}
E2460 Halloween Treats
First AC: 2018-06-01 Latest Modification: 2018-06-01
Note: 预处理前缀和模c,如果前c个和有0,则问题已解决
如果前c个和没有0,则由容斥原理存在i,j
使得前i个和与前j个和相等,那么从i+1到j即为一组解
#include<bits/stdc++.h>
using namespace std;
int c,n,tmp,suma,rst;
struct data{
int mod,item;
}a[];
int i,j;
bool cmp(data a,data b)
{
if(a.mod!=b.mod)return a.mod<b.mod;
return a.item<b.item;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>c>>n,n){
suma=;
bool jdg=;
for(i=;i<=n;++i){
cin>>tmp;
suma=(suma+tmp)%c;
if(!suma)rst=i,jdg=;
a[i].mod=suma,a[i].item=i;
}
if(jdg){
sort(a+,a+n+,cmp);
for(i=;i<=n;++i){
if(a[i].mod==a[i-].mod){
j=a[i-].item+;
cout<<j++;
while(j<=a[i].item)cout<<' '<<j++;
cout<<endl;
break;
}
}
}
else{
for(i=;i<rst;++i)cout<<i<<' ';
cout<<rst<<endl;
}
}
return ;
}
E2465 Hay Expenses
First AC: 2018-06-01 Latest Modification: 2018-06-01
#include<bits/stdc++.h>
using namespace std;
int n,q,m,l,r;
int a[];
int i;
int main()
{
cin>>n>>q;
for(i=;i<=n;++i){
cin>>m;
a[i]=a[i-]+m;
}
while(q--){
cin>>l>>r;
cout<<a[r]-a[l-]<<endl;
}
return ;
}
E2466 Hay For Sale
First AC: 2018-06-01 Latest Modification: 2018-06-01
#include<bits/stdc++.h>
using namespace std;
int c,h,m,rst;
bool dp[];
int i,j;
int main()
{
cin>>c>>h;
dp[]=;
for(i=;i<h;++i){
cin>>m;
for(j=c-m;j>=;--j){
if(dp[j])dp[j+c]=;
}
}
for(i=c;i>=;--i){
if(dp[i]){
rst=i;
break;
}
}
cout<<rst;
return ;
}
E2471 Musical Chair
First AC: 2018-06-01 Latest Modification: 2018-06-01
#include<bits/stdc++.h>
using namespace std;
int n,m;
string s[];
string pos;
int i;
int main()
{
while(cin>>n,getline(cin,s[i])){
for(i=;i<n;++i)getline(cin,s[i]);
cin>>pos>>m;
for(i=;;++i){
if(pos[i]!='h'){
cout<<s[(i+n-m%n)%n]<<endl;
break;
}
}
}
return ;
}
E2480 Old Magician
First AC: 2018-06-01 Latest Modification: 2018-06-01
Note: 给白球标记为0,黑球标记为1
每次取出两个球,放入一个球,经过有限次操作最终必然只剩一个球
如果取出的两个球颜色不同,取出的球编号和为奇,放入的黑球编号也为奇
如果取出的两个球颜色相同,取出的球编号和为偶,放入的白球编号也为偶
可见,每次操作不改变所有球编号和的奇偶性
所以只要计算初始时编号和的奇偶性就能得到最终结果
#include<bits/stdc++.h>
using namespace std;
int T,w,b;
int i;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>w>>b;
cout<<"Case #"<<i<<": ";
b&? cout<<"BLACK\n":cout<<"WHITE\n";
}
return ;
}
E2517 Switching Lights
First AC: 2018-02-13 Latest Modification: 2018-02-13
#include<bits/stdc++.h>
using namespace std;
int n,m,p,b,c,cnt;
int a[];
int i;
int main()
{
cin>>n>>m;
while(m--){
cin>>p>>b>>c;
++c;
if(p){
for(cnt=,i=b;i<c;++i)if(a[i])++cnt;
cout<<cnt<<endl;
}
else for(i=b;i<c;++i)a[i]=pow(a[i]-,);
}
return ;
}
E2518 Guarding the Farm
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 对点的高度排序,从高到低dfs
#include<bits/stdc++.h>
using namespace std;
int n,m,nump,rst;
int mp[][];
bool jdg[][];
int i,j;
struct point{
int x,y,num;
}a[];
bool cmp(point a,point b)
{
return a.num>b.num;
}
void dfs(int i,int j)
{
int tmp=mp[i][j];
jdg[i][j]=;
if(!jdg[i-][j-]&&mp[i-][j-]<=tmp)dfs(i-,j-);
if(!jdg[i-][j-]&&mp[i-][j-]<=tmp)dfs(i-,j-);
if(!jdg[i-][j+]&&mp[i-][j+]<=tmp)dfs(i-,j+);
if(!jdg[i-][j-]&&mp[i-][j-]<=tmp)dfs(i-,j-);
if(!jdg[i-][j+]&&mp[i-][j+]<=tmp)dfs(i-,j+);
if(!jdg[i+][j-]&&mp[i+][j-]<=tmp)dfs(i+,j-);
if(!jdg[i+][j-]&&mp[i+][j-]<=tmp)dfs(i+,j-);
if(!jdg[i+][j+]&&mp[i+][j+]<=tmp)dfs(i+,j+);
mp[i][j]=-;
}
int main()
{
for(i=;i<;++i)for(j=;j<;++j)mp[i][j]=1e6;
cin>>n>>m;
for(i=;i<=n;++i)for(j=;j<=m;++j){
cin>>mp[i][j];
a[nump].x=i;
a[nump].y=j;
a[nump++].num=mp[i][j];
}
sort(a,a+nump,cmp);
for(i=;i<nump;++i){
if(jdg[a[i].x][a[i].y])continue;
dfs(a[i].x,a[i].y);
++rst;
}
cout<<rst;
return ;
}
E2519 Going Once, Going Twice, Gone
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 题述同等条件下取最低价,表明最优解为0与输入的所有数之一
数据范围只有1000,因此只要排序完遍历一遍取最大值即可
用数组亦可,代码长度应该能更短
#include<bits/stdc++.h>
using namespace std;
int n,m,tmp,num,price,income;
map<int,int>mp;
map<int,int>::iterator it;
int i;
int main()
{
cin>>n>>m;
num=m;
while(m--){
cin>>tmp;
it=mp.find(tmp);
if(it!=mp.end())++it->second;
else mp.insert(pair<int,int>(tmp,));
}
for(it=mp.begin();it!=mp.end();++it){
tmp=min(num,n)*it->first;
if(tmp>income)price=it->first,income=tmp;
num-=it->second;
}
cout<<price<<' '<<income;
return ;
}
E2521 Guarding the Farm
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 见2518
#include<bits/stdc++.h>
using namespace std;
int n,m,nump,rst;
int mp[][];
bool jdg[][];
int i,j;
struct point{
int x,y,num;
}a[];
bool cmp(point a,point b)
{
return a.num>b.num;
}
void dfs(int i,int j)
{
int tmp=mp[i][j];
jdg[i][j]=;
if(!jdg[i-][j-]&&mp[i-][j-]<=tmp)dfs(i-,j-);
if(!jdg[i-][j-]&&mp[i-][j-]<=tmp)dfs(i-,j-);
if(!jdg[i-][j+]&&mp[i-][j+]<=tmp)dfs(i-,j+);
if(!jdg[i-][j-]&&mp[i-][j-]<=tmp)dfs(i-,j-);
if(!jdg[i-][j+]&&mp[i-][j+]<=tmp)dfs(i-,j+);
if(!jdg[i+][j-]&&mp[i+][j-]<=tmp)dfs(i+,j-);
if(!jdg[i+][j-]&&mp[i+][j-]<=tmp)dfs(i+,j-);
if(!jdg[i+][j+]&&mp[i+][j+]<=tmp)dfs(i+,j+);
mp[i][j]=-;
}
int main()
{
for(i=;i<;++i)for(j=;j<;++j)mp[i][j]=1e6;
cin>>n>>m;
for(i=;i<=n;++i)for(j=;j<=m;++j){
cin>>mp[i][j];
a[nump].x=i;
a[nump].y=j;
a[nump++].num=mp[i][j];
}
sort(a,a+nump,cmp);
for(i=;i<nump;++i){
if(jdg[a[i].x][a[i].y])continue;
dfs(a[i].x,a[i].y);
++rst;
}
cout<<rst;
return ;
}
E2522 Time Management
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 贪心,每次为最后的时限安排工作,保存每次安排后的开始时间
#include<bits/stdc++.h>
using namespace std;
int n,rst;
struct data{
int t,d;
}a[];
int i;
bool cmp(data a,data b)
{
return a.d>b.d;
}
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i].t>>a[i].d;
sort(a,a+n,cmp);
rst=1e8;
for(i=;i<n;++i)rst=min(rst,a[i].d)-a[i].t;
cout<<max(rst,-);
return ;
}
E2527 Fj&Haozi
First AC: 2017-12-29 Latest Modification: 2017-12-29
#include<bits/stdc++.h>
using namespace std;
int T,n,p,m,t;
int a[][];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n>>p>>m>>t;
memset(a,,sizeof(a));
a[][p]=;
for(i=;i<=m;++i)
for(j=;j<=n;++j)
a[i][j]=a[i-][j-]+a[i-][j+];
cout<<a[m][t]<<endl;
}
return ;
}
E2528 MST难题
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 记三个名字编号为0,1,2
先假设第一位填0,dp出n个格子的结果
然后由对称性就能得到答案
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[][];
ll n;
ll i;
int main()
{
dp[][]=;
for(i=;i<;++i){
dp[i][]=dp[i-][]+dp[i-][];
dp[i][]=dp[i-][]+dp[i-][];
dp[i][]=dp[i-][]+dp[i-][];
}
while(cin>>n){
if(n==)cout<<"3\n";
else cout<<*(dp[n][]+dp[n][])<<endl;
}
return ;
}
E2529 强大的lwc
First AC: 2019-03-14 Latest Modification: 2019-03-14
#include<bits/stdc++.h>
using namespace std;
int T,n,m;
int fx,fy,cnt,ans;
int pre[];
int i;
struct data{
int x,y,val;
}a[];
bool cmp(data a,data b)
{
return a.val<b.val;
}
int find(int x)
{
int r=x,i;
while(pre[r]!=r)r=pre[r];
while(pre[x]!=r)i=pre[x],pre[x]=r,x=i;
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
pre[fx]=fy;
--cnt;
ans+=a[i].val;
}
}
int main()
{
cin>>T;
while(T--){
cin>>n>>m;
for(i=;i<=n;++i)pre[i]=i;
for(i=;i<m;++i){
cin>>a[i].x>>a[i].y>>a[i].val;
}
sort(a,a+m,cmp);
cnt=n;
ans=;
for(i=;i<m;++i){
join(a[i].x,a[i].y);
}
if(cnt)cout<<"-1\n";
else cout<<ans<<endl;
}
return ;
}
E2530 数字之和
First AC: 2017-12-10 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int T,N,cnt;
long long m,tmp;
long long a[];
bool pos[],neg[],flag,num;
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>N;
cnt=,flag=,num=;
memset(pos,,sizeof(pos)),memset(neg,,sizeof(neg));
while(N--){
cin>>m;
a[cnt++]=m;
if(m>)pos[m]=;
else if(m<)neg[-m]=;
else flag=;
}
for(i=;num&&i<cnt;++i)for(j=i+;j<cnt;++j){
tmp=a[i]+a[j];
if(tmp<&&tmp>-){
if(tmp>){
if(neg[tmp]){cout<<"YES\n";num=;break;}
}
else if(tmp<){
if(pos[-tmp]){cout<<"YES\n";num=;break;}
}
else{
if(flag){cout<<"YES\n";num=;break;}
}
}
}
if(num)cout<<"NO\n";
}
return ;
}
E2531 乘船
First AC: 2017-12-29 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int T,N,K,cnt,lft,rgt;
int a[];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>N>>K;
for(i=;i<N;++i)cin>>a[i];
sort(a,a+N);
lft=cnt=,rgt=N-,++K;
while(N){
if(lft>rgt)break;
if(lft==rgt)--N;
else if(a[lft]+a[rgt]<K)++lft,--rgt,N-=;
else --rgt,--N;
++cnt;
}
cout<<cnt<<endl;
}
return ;
}
E2533 守望者的跳跃
First AC: 2017-12-29 Latest Modification: 2018-06-02
Note: 考虑向量的三角不等式,保存每次跳完与原点的可能距离区间即可
#include<bits/stdc++.h>
using namespace std;
int n,lft,rgt,m,i,x,y;
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>m;
if(m<=lft)lft-=m,rgt+=m;
else if(m>=rgt)lft=m-rgt,rgt+=m;
else lft=,rgt+=m;
}
cin>>x>>y;
double tmp=sqrt(x*x+y*y);
if(tmp<lft||tmp>rgt)cout<<"No";
else cout<<"Yes";
return ;
}
E2534 PK插曲
First AC: 2017-12-10 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
long long T,a,b,c,r,h;
int main()
{
cin>>T;
while(T--){
cin>>a>>b>>c>>r>>h;
printf("%.2lf\n",min(1.0*a*b*c,3.1415926535*r*r*h));
}
return ;
}
E2536 求和
First AC: 2017-11-01 Latest Modification: 2017-11-01
#include<iostream>
using namespace std;
int a[]={,};
long up,dn,t,n,u,d,c;
int i,j;
int main()
{
for(i=;i<;++i)a[i]=a[i-]+a[i-];
cin>>t;
for(i=;i<t;++i){
cin>>n;
up=,dn=;
for(j=;j<n;++j)up=up*a[j]+dn*a[j+],dn*=a[j];
u=up,d=dn;
do{ c=u%d;
u=d;
d=c;
}while(c!=);
cout<<up/u<<"/"<<dn/u<<endl;
}
return ;
}
E2537 统计字符
First AC: 2017-10-19 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int t,len,c,n,o;
int i,j;
string s;
int main()
{
cin>>t;
getline(cin,s);
for(i=;i<t;i++){
getline(cin,s);
len=s.length();
for(j=;s[j]!='\0';j++){
if(s[j]>=''&&s[j]<='')n++;else{
if((s[j]>='A'&&s[j]<='Z')||(s[j]>='a'&&s[j]<='z'))c++;
else o++;
}
}
cout<<"character:"<<c<<"\nnumber:"<<n<<"\nothers:"<<o<<endl;
c=n=o=;
}
return ;
}
E2538 完美数
First AC: 2017-10-11 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"6 its factors are 1 2 3\n";
cout<<"28 its factors are 1 2 4 7 14\n";
cout<<"496 its factors are 1 2 4 8 16 31 62 124 248";
return ;
}
E2539 Alfredo
First AC: 2017-11-22 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int r,a,b,n;
int main()
{
while(cin>>r,++n,r){
cin>>a>>b;
if(a*a+b*b>*r*r)cout<<"Pizza "<<n<<" does not fit";
else cout<<"Pizza "<<n<<" fits";
cout<<" on the table.\n";
}
return ;
}
E2554 Weighing
First AC: 2018-06-02 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
long long T,n;
int main()
{
cin>>T;
while(T--){
cin>>n;
cout<<ceil(log(*n+)/log())<<endl;
}
return ;
}
E2562 Virtual Friends
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 在并查集的基础上多维护一个表关系大小的数组即可
#include<bits/stdc++.h>
using namespace std;
int T,n,num,x,y;
map<string,int>mp;
map<string,int>::iterator it;
string s,t;
int pre[],rst[];
int i;
int find(int x)
{
int r=x,i,j;
while(pre[r]!=r)r=pre[r];
i=x;
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
rst[fx]+=rst[fy];
pre[fy]=fx;
}
cout<<rst[fx]<<endl;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
mp.clear();
num=;
for(i=;i<;++i)pre[i]=i,rst[i]=;
while(n--){
cin>>s>>t;
it=mp.find(s);
if(it!=mp.end())x=it->second;
else x=++num,mp.insert(pair<string,int>(s,num));
it=mp.find(t);
if(it!=mp.end())y=it->second;
else y=++num,mp.insert(pair<string,int>(t,num));
join(x,y);
}
}
return ;
}
E2567 Fibonacci
First AC: 2018-04-22 Latest Modification: 2018-04-22
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll f[]={,};
ll n;
int main()
{
for(ll i=;i<;++i){
f[i]=(f[i-]+f[i-])%;
}
while(cin>>n,n+){
printf("%lld\n",f[n%]);
}
return ;
}
E2568 Sum of Digit
First AC: 2017-10-18 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
long long a,b,c;
int cnt,i;
int main()
{
cin>>a;
while(cin>>a>>b){
c=a+b;
for(i=;i<;i++,c/=){
if(c!=)cnt++;
}
cout<<cnt<<endl;
cnt=;
}
return ;
}
E2569 Capitalize
First AC: 2017-10-13 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
char n;
int main()
{
while((n=getchar())!=EOF){
if(n>&&n<)cout<<(char)(n-);
else cout<<n;
}
return ;
}
E2570 Primes Pair
First AC: 2017-12-27 Latest Modification: 2017-12-27
#include<iostream>
using namespace std;
int a[],n,cnt;
int i,j;
int main()
{
for(i=;i<;++i){
if(a[i]==){
for(j=*i;j<;j+=i)++a[j];
}
}
a[]=,cin>>n;
while(cin>>n){
for(cnt=,++n,i=;i<n;++i)if(a[i]==&&a[n-i]==)++cnt;
cout<<cnt<<endl;
}
return ;
}
E2571 GCD and LCM
First AC: 2017-10-11 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int T,a,b,c;
int main()
{
cin>>T;
while(T--){
cin>>a>>b;
c=__gcd(a,b);
cout<<c<<' '<<a*b/c<<endl;
}
return ;
}
E2572 Sort It
First AC: 2017-11-17 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int N,K,i;
int a[];
int main()
{
cin>>N;
for(;i<N;)cin>>a[i++];
sort(a,a+N);
cin>>K;
while(K--)cin>>N,cout<<a[--N]<<endl;
return ;
}
E2573 Hub Connection Plan
First AC: 2018-06-29 Latest Modification: 2018-06-29
Note: 题目保证有一组可行解,而显然最优解恰好连接n-1次
这是因为,至少要n-1次才能将n个点连接起来
而超过n-1次连接则必将成环,这时可以把环上权最大的边删去
可用并查集来贪心地加边,为了尽可能选权重小的边
先对权重升序排序,然后但凡需要加边便加边即可
#include<bits/stdc++.h>
using namespace std;
int n,m,rst;
struct data{
int x,y,c;
}a[];
int pre[];
int find(int x)
{
int r=x,i=x,j;
while(pre[r]!=r)r=pre[r];
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)pre[fx]=fy;
}
int i;
bool cmp(data a,data b)
{
return a.c<b.c;
}
int main()
{
cin>>n>>m;
for(i=;i<=n;++i)pre[i]=i;
for(i=;i<m;++i)cin>>a[i].x>>a[i].y>>a[i].c;
sort(a,a+m,cmp);
for(i=;i<m;++i){
if(find(a[i].x)!=find(a[i].y)){
join(a[i].x,a[i].y);
rst+=a[i].c;
}
}
cout<<rst;
return ;
}
E2581 0 and 1
First AC: 2017-11-12 Latest Modification: 2017-11-12
#include<iostream>
using namespace std;
long T,n,num,dgt;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>n;
num=dgt=;
while(n!=){
if(n%==)num++;
dgt++,n/=;
}
cout<<dgt-num<<' '<<num<<endl;
}
return ;
}
E2582 When I Sort
First AC: 2017-12-29 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int T,n;
struct data{
int num,ori,newn;
}a[];
int i,j;
bool cmp1(data a,data b)
{
if(a.num==b.num)return a.ori<b.ori;
return a.num<b.num;
}
bool cmp2(data a,data b)
{
return a.ori<b.ori;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;++i)cin>>a[i].num,a[i].ori=i;
sort(a,a+n,cmp1);
for(i=;i<n;++i)a[i].newn=i;
sort(a,a+n,cmp2);
for(cout<<a[].newn,i=;i<n;++i)cout<<' '<<a[i].newn;
cout<<endl;
}
return ;
}
E2585 0-1 Package
First AC: 2018-04-27 Latest Modification: 2018-04-27
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,rst;
ll w[],v[],dp[];
ll i,j;
int main()
{
cin>>n>>m;
for(i=;i<=n;++i)cin>>w[i]>>v[i];
for(i=;i<=n;++i)for(j=m;j>=;--j)
if(j>=w[i])dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
for(i=;i<=m;++i)if(dp[i]>rst)rst=dp[i];
cout<<rst;
return ;
}
E2586 Take a Party
First AC: 2018-04-27 Latest Modification: 2019-03-06
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
int pre[maxn];
int s,m,n;
int u,v;
int i;
int find(int x)
{
int r=x,i;
while(pre[r]!=r)r=pre[r];
while(x!=r)i=pre[x],pre[x]=r,x=i;
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
pre[fx]=fy;
}
int main()
{
cin>>s>>m>>n;
for(i=;i<=s;++i)pre[i]=i;
while(m--){
cin>>u>>v;
join(u,v);
}
while(n--){
cin>>u>>v;
if(find(u)!=find(v))cout<<"0\n";
else cout<<"1\n";
}
return ;
}
E2606 Pinhole Imaging
First AC: 2017-10-12 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
double A,B,H;
int T,i;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>A>>B>>H;
printf("%.2f\n",B*H/A);
}
return ;
}
E2607 URL
First AC: 2018-02-13 Latest Modification: 2018-02-13
#include<bits/stdc++.h>
using namespace std;
int T,n,len,cnt;
string op,web;
struct data{
string s;
int ls,num;
bool jdg;
}a[];
int i,j,k;
bool cmp(data a,data b)
{
if(a.jdg^b.jdg)return a.jdg>b.jdg;
if(a.num^b.num)return a.num>b.num;
return a.s<b.s;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
i=;
while(n--){
cin>>op>>web;
len=web.length();
if(op[]=='V'){
cnt=;
for(j=;j<i;++j){
if(a[j].s!=web)continue;
cnt=,a[j].num++;
break;
}
if(cnt)a[i].s=web,a[i].num=,a[j].ls=len,++i;
}
else{
for(j=;j<i;++j){
if(a[j].ls<len){a[j].jdg=;continue;}
for(cnt=,k=;k<len;++k){
if(a[j].s[k]^web[k]){cnt=,a[j].jdg=;break;}
}
if(cnt)a[j].jdg=;
}
sort(a,a+i,cmp);
for(j=;j<i;++j)
if(a[j].jdg)cout<<a[j].s<<endl;
else break;
cout<<endl;
}
}
}
return ;
}
E2610 工程
First AC: 2017-10-12 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int T,m,k,i,j,t,c,sum;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>m>>k;
for(j=;j<m;j++){
cin>>t;
c++;
if(c<=k)sum+=t;
}
cout<<sum<<endl;
sum=c=;
}
return ;
}
E2611 一日之晨
First AC: 2017-11-01 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int T,h,m,s,n,i;
int main()
{
cin>>T;
for(i=;i<T;++i){
scanf("%d:%d:%d",&h,&m,&s);
n=*h+*m+s;
if(n<=)cout<<"You are living a healthy life!\n";
else if(n<=)cout<<"Hurry up!\n";
else cout<<"You are late!\n";
}
return ;
}
E2612 魔幻图案
First AC: 2018-01-01 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int T,n;
string s;
bool a[][];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>s>>n;
memset(a,,sizeof(a));
for(i=;i<;++i)a[i][]=;
for(i=;i<;++i)a[][i]=;
if(s=="P"){
for(i=;i<;++i)a[n+][i]=;
for(i=;i<n+;++i)a[i][n+]=;
}
else for(i=;i<;++i)a[*n+][i]=;
for(i=;i<*n+;++i){
for(j=;j<n+;++j)cout<<a[i][j];
cout<<endl;
}
}
return ;
}
E2613 Gama函数
First AC: 2018-01-06 Latest Modification: 2018-01-06
#include<iostream>
using namespace std;
long f[]={,};
int i,n;
int main()
{
for(i=;i<;++i)f[i+]=i*f[i]%;
cin>>n;
while(cin>>n)cout<<f[n]<<endl;
return ;
}
E2614 组网计划
First AC: 2019-05-16 Latest Modification: 2019-05-16
#include<bits/stdc++.h>
using namespace std;
const int maxn=,maxm=;
int n,m,cnt,tmp,ans,tans;
int x[maxn],y[maxn];
struct edge{
int u,v,w;
}e[maxm];
int mst[maxm];
int pre[maxn];
int i,j;
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int find(int x)
{
int r=x,i;
while(pre[r]!=r)r=pre[r];
while(pre[x]!=r)i=pre[x],pre[x]=r,x=i;
return r;
}
void join(int x,int y,int num)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
pre[fx]=fy;
mst[++cnt]=num;
ans+=e[num].w;
}
}
void tjoin(int x,int y,int val)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
pre[fx]=fy;
tmp+=val;
++cnt;
}
}
int main()
{
while(cin>>n){
for(i=;i<n;++i)cin>>x[i]>>y[i];
m=;
for(i=;i<n;++i){
for(j=i+;j<n;++j){
e[m].u=i;
e[m].v=j;
e[m].w=abs(x[i]-x[j])+abs(y[i]-y[j]);
++m;
}
}
cnt=ans=;
sort(e,e+m,cmp);
for(i=;i<maxn;++i)pre[i]=i;
for(i=;i<m;++i)join(e[i].u,e[i].v,i);
cout<<ans<<endl;
tans=ans;
ans=1e9;
for(i=;i<n;++i){
cnt=tmp=;
for(j=;j<maxn;++j)pre[j]=j;
for(j=;j<m;++j){
if(j!=mst[i]){
tjoin(e[j].u,e[j].v,e[j].w);
}
}
if(cnt==n-)ans=min(ans,tmp);
}
if(ans==tans)cout<<"Yes\n";
else cout<<"No\n";
}
return ;
}
E2615 会议安排
First AC: 2018-06-02 Latest Modification: 2018-06-02
#include<bits/stdc++.h>
using namespace std;
int T,n;
int dp[];
int i,j;
struct data{
int op,ed,val;
}a[];
bool cmp(data a,data b)
{
return a.ed<b.ed;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;++i)cin>>a[i].op>>a[i].ed>>a[i].val;
sort(a,a+n,cmp);
a[n].op=a[n].ed=;
j=;
for(i=;i<;++i){
dp[i]=dp[i-];
while(i==a[j].ed){
dp[i]=max(dp[i],dp[a[j].op-]+a[j].val);
++j;
}
}
cout<<dp[]<<endl;
}
return ;
}
E2616 游黄山
First AC: 2018-04-20 Latest Modification: 2018-04-20
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX=;
int T,n;
string s;
struct data{
ll x,y,d;
}a[];
ll sumd,tmpd,x,y,d;
int i;
ll op(string s)
{
int len=s.length(),rst=,p=len-;
for(int i=;i<len;++i){
if(s[i]=='.')p=i;
else rst=*rst+s[i]-'';
}
rst*=pow(,-len+p);
return rst;
}
bool cmpx(data a,data b)
{
return a.x<b.x;
}
bool cmpy(data a,data b)
{
return a.y<b.y;
}
int main()
{
cin>>T;
while(T--){
cin>>n;
sumd=;
for(i=;i<n;++i){
cin>>s;
a[i].x=op(s);
cin>>s;
a[i].y=op(s);
cin>>s;
a[i].d=op(s);
sumd+=a[i].d;
}
sort(a,a+n,cmpx);
tmpd=(sumd+)/;
for(i=;;++i){
if(a[i].d<tmpd)tmpd-=a[i].d;
else{
x=a[i].x;
break;
}
}
sort(a,a+n,cmpy);
tmpd=(sumd+)/;
for(i=;;++i){
if(a[i].d<tmpd)tmpd-=a[i].d;
else{
y=a[i].y;
break;
}
}
for(i=,d=;i<n;++i)
d+=a[i].d*(abs(x-a[i].x)+abs(y-a[i].y));
printf("%.4f %.4f %.4f\n",x*1.0/MAX,y*1.0/MAX,d*1.0/MAX/MAX);
}
return ;
}
E2618 游戏
First AC: 2017-12-01 Latest Modification: 2017-12-01
#include<iostream>
using namespace std;
int T,l,r,n;
string s;
int a[][];
int i,j,k;
int main()
{
cin>>T;
for(k=;k<T;++k){
for(i=;i<;++i)for(j=;j<;++j){
cin>>a[i][j];
if(a[i][j]==)l=i,r=j;
}
cin>>n;
while(n--){
cin>>s;
if(s[]=='l'){
if(r)a[l][r]=a[l][r-],a[l][r-]=,--r;
}
else if(s[]=='r'){
if(-r)a[l][r]=a[l][r+],a[l][r+]=,++r;
}
else if(s[]=='u'){
if(l)a[l][r]=a[l-][r],a[l-][r]=,--l;
}
else if(s[]=='d'){
if(-l)a[l][r]=a[l+][r],a[l+][r]=,++l;
}
}
for(i=;i<;++i)
cout<<a[i][]<<' '<<a[i][]<<' '<<a[i][]<<endl;
if(k!=T-)cout<<endl;
}
return ;
}
E2620 Unique Snowflakes
First AC: 2018-05-06 Latest Modification: 2018-05-06
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,n,m,lft,rgt,rst;
map<ll,ll>mp;
map<ll,ll>::iterator it;
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>n;
if(!n){
cout<<"0\n";
continue;
}
mp.clear();
lft=,rst=;
for(rgt=;rgt<n;++rgt){
cin>>m;
it=mp.find(m);
if(it!=mp.end()){
lft=max(lft,it->second+);
it->second=rgt;
rst=max(rst,rgt-lft+);
}
else{
mp.insert(pair<ll,ll>(m,rgt));
rst=max(rst,rgt-lft+);
}
}
cout<<rst<<endl;
}
return ;
}
E2639 Ellipse, Again and Again
First AC: 2018-06-03 Latest Modification: 2018-06-03
#include<bits/stdc++.h>
using namespace std;
int T;
int a,b,x,y;
int main()
{
cin>>T;
while(T--){
cin>>a>>b>>x>>y;
if(b*b*x*x+a*a*y*y<a*a*b*b)cout<<"In ellipse\n";
else cout<<a*a*b*b<<endl;
}
return ;
}
E2646 Product-Sum
First AC: 2018-06-03 Latest Modification: 2018-06-03
Note: 设所求数的左半部分为a,右半部分为b
则有a*b-a-b=n,整理得(a-1)(b-1)=n+1
于是只要遍历找n+1的因子,并注意到所求数最小当且仅当a最小即可
遍历时只要遍历1到根号n
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,tmp,a,b,l,r;
ll i;
ll cal(ll n)
{
ll ret=;
while(n)++ret,n/=;
return ret;
}
int main()
{
while(cin>>n,n){
tmp=(ll)sqrt(++n)+;
l=r=1e9+;
for(i=;i<tmp;++i){
if(!(n%i)){
a=i+;
b=n/i+;
ll fa=cal(a),fb=cal(b);
if(fa>=fb&&a<l)l=a,r=b;
if(fb>=fa&&b<l)l=b,r=a;
}
}
tmp=cal(l);
while(tmp--)l*=;
cout<<l+r<<endl;
}
return ;
}
E2653 Find the Hotel
First AC: 2018-06-03 Latest Modification: 2018-06-03
Note: 题面的with either lower price or distance应该理解成with both lower price and distance
#include<bits/stdc++.h>
using namespace std;
int n,mind,tmp,rst;
struct data{
int p,d;
bool jdg;
}a[];
int i;
bool cmp(data a,data b)
{
if(a.p!=b.p)return a.p<b.p;
return a.d<b.d;
}
int main()
{
while(cin>>n){
for(i=;i<n;++i)cin>>a[i].p>>a[i].d;
sort(a,a+n,cmp);
mind=1e6;
rst=;
for(i=;i<n;++i){
if(a[i].d>mind){
a[i].jdg=;
while(i+<n&&a[i+].p==a[i].p){
a[++i].jdg=;
}
}
else{
++rst;
a[i].jdg=;
tmp=a[i].d;
while(i+<n&&a[i+].p==a[i].p){
if(a[++i].d>mind)a[i].jdg=;
else a[i].jdg=,++rst;
}
mind=tmp;
}
}
cout<<rst<<endl;
for(i=;i<n;++i)if(a[i].jdg){
cout<<a[i].p<<' '<<a[i].d<<endl;
}
}
return ;
}
E2654 Median
First AC: 2018-06-04 Latest Modification: 2018-06-04
Note: 桶排序优化
#include<bits/stdc++.h>
using namespace std;
int n,k,x1,x2,d,m;
int a[];
int i;
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>k>>x1>>d>>m){
memset(a,,sizeof(a));
a[x1]=;
for(i=;i<n;++i){
x2=(k*x1+d)%m;
++a[x2];
x1=x2;
}
n=n/+;
for(i=;i<m;++i){
n-=a[i];
if(n<=){
cout<<i<<endl;
break;
}
}
}
return ;
}
E2655 Interesting Numbers
First AC: 2018-06-03 Latest Modification: 2018-06-03
Note: 注意到如果一个m(m>1)位数满足题设,那么它的低m-1位数也满足题设
于是只要对已经求出的m-1位数,遍历判断第m位即可
注意到结果可能很大,模的时候可以采取分段取模的方法
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
ll a[]={,,,};
ll base,cnt,tmp1,tmp2,n;
ll i,j;
int main()
{
base=;
cnt=;
while(){
tmp1=cnt;
base*=;
for(i=;i<;++i){
for(j=;j<tmp1;++j){
tmp2=i*base+a[j];
ll div,mod,hgh,low,mod2;
div=1e8;
mod=*base;
hgh=tmp2/div;
low=tmp2%div;
mod2=max(mod,*div);
if((low*hgh%(mod2*/div)*div*+low*low)%mod==tmp2){
a[cnt++]=tmp2;
if(cnt>){
while(cin>>n)cout<<a[n-]<<endl;
exit();
}
}
}
}
}
return ;
}
E2658 Play on Words
First AC: 2018-05-05 Latest Modification: 2018-05-05
#include<bits/stdc++.h>
using namespace std;
int m,n,num;
string s[];
string t;
bool jdg(string s,string t)
{
int i=,j=;
int ls=s.length(),lt=t.length();
while(){
while(i<ls&&s[i]!=t[j])++i;
if(j==lt)return ;
if(i==ls)return ;
++i,++j;
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>m;
for(int i=;i<m;++i)cin>>s[i];
cin>>n;
while(n--){
cin>>t;
num=;
for(int i=;i<m;++i)if(jdg(s[i],t))++num;
cout<<num<<endl;
}
return ;
}
E2686 Matchsticks
First AC: 2018-05-05 Latest Modification: 2018-05-05
#include<bits/stdc++.h>
using namespace std;
int T,n;
string minn,maxn;
int i;
int cal(int n)
{
if(n<)return -;
return ceil(n/7.0);
}
int main()
{
cin>>T;
while(T--){
cin>>n;
minn=maxn="";
int tmp=n;
while(tmp>)maxn+="",tmp-=;
if(tmp)maxn[]='';
if(n==)minn="";
else{
int num=cal(n);
if(cal(n-)==num-)minn="",n-=;
else if(cal(n-)==num-)minn="",n-=;
else if(cal(n-)==num-)minn="",n-=;
else if(cal(n-)==num-)minn="",n-=;
else if(cal(n-)==num-)minn="",n-=;
else minn="",n-=;
--num;
while(num){
if(cal(n-)==num-)minn+="",n-=;
else if(cal(n-)==num-)minn+="",n-=;
else if(cal(n-)==num-)minn+="",n-=;
else if(cal(n-)==num-)minn+="",n-=;
else if(cal(n-)==num-)minn+="",n-=;
else minn+="",n-=;
--num;
}
}
cout<<minn<<' '<<maxn<<endl;
}
return ;
}
E2690 Soda Surpler
First AC: 2017-10-12 Latest Modification: 2018-06-04
#include<bits/stdc++.h>
using namespace std;
int e,f,c,num,sum;
int main()
{
while(cin>>e>>f>>c){
num=e+f;
while(num>=c){
sum+=num/c;
num=num%c+num/c;
}
cout<<sum;
sum=;
}
return ;
}
E2691 Money Matters
First AC: 2018-06-04 Latest Modification: 2018-06-04
Note: 并查集,如果每一个类中和为0则输出POSSIBLE,否则输出IMPOSSIBLE
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y;
int cnt[],pre[];
int i;
int find(int x)
{
int r=x,i=x,j;
while(pre[r]!=r)r=pre[r];
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
cnt[fx]+=cnt[fy];
cnt[fy]=;
pre[fy]=fx;
}
}
int main()
{
while(cin>>n>>m){
for(i=;i<n;++i)cin>>cnt[i],pre[i]=i;
while(m--)cin>>x>>y,join(x,y);
bool jdg=;
for(i=;i<n;++i)if(cnt[i]){
jdg=;
break;
}
if(jdg)cout<<"POSSIBLE\n";
else cout<<"IMPOSSIBLE\n";
}
return ;
}
E2703 Class
First AC: 2018-05-23 Latest Modification: 2018-05-23
Note: 本质是一道水题,把行和列小的那个尽量坐满一行(列),如果还有学生就随便坐
#include<bits/stdc++.h>
using namespace std;
int n,r,c,rst;
char mp[][];
int i,j;
int main()
{
cin>>n>>r>>c;
rst=min((n+)/,min(r,c));
cout<<rst<<endl;
memset(mp,'.',sizeof(mp));
for(i=;i<rst;++i)mp[i][]='#',--n;
for(i=;i<rst;++i)mp[][i]='#',--n;
for(i=;i<r;++i)
for(j=;j<c;++j)
if(n&&mp[i][j]!='#')
mp[i][j]='#',--n;
for(i=;i<r;++i){
for(j=;j<c;++j)cout<<mp[i][j];
cout<<endl;
}
return ;
}
E2708 Hole
First AC: 2017-11-16 Latest Modification: 2017-11-16
#include<iostream>
using namespace std;
int n,a,b,i;
int main()
{
while(cin>>n){
if(n==)cout<<"1\n";
else if(n==)cout<<"0\n";
else{
a=n%,b=n/;
for(i=;i<a;++i)cout<<;
for(i=;i<b;++i)cout<<;
cout<<endl;
}
}
return ;
}
E2723 Six Grade Math
First AC: 2017-11-13 Latest Modification: 2017-11-13
#include<iostream>
using namespace std;
int T,a,b,c,m,n;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>a>>b,m=a,n=b;
for(c=;c!=;)c=a%b,a=b,b=c;
cout<<i+<<' '<<m*n/a<<' '<<a<<endl;
}
return ;
}
E2724 Cryptoquote
First AC: 2018-05-17 Latest Modification: 2018-05-17
#include<bits/stdc++.h>
using namespace std;
int T,len;
string s,t;
int i,j;
int main()
{
cin>>T;
getchar();
for(i=;i<=T;++i){
getline(cin,s);
getline(cin,t);
cout<<i<<' ';
len=s.length();
for(j=;j<len;++j){
if(s[j]<'A'||s[j]>'Z')cout<<s[j];
else cout<<t[s[j]-'A'];
}
cout<<endl;
}
return ;
}
E2725 Binary Clock
First AC: 2018-01-05 Latest Modification: 2018-06-04
#include<iostream>
using namespace std;
bool a[][];
int T,h,m,s;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<=T;++i){
scanf("%d:%d:%d",&h,&m,&s);
for(j=;j<;++j){
a[][j]=h&;
a[][j]=m&;
a[][j]=s&;
h>>=,m>>=,s>>=;
}
cout<<i<<' ';
for(j=;j>=;--j)cout<<a[][j]<<a[][j]<<a[][j];
cout<<' ';
for(j=;j<;++j)for(k=;k>=;--k)cout<<a[j][k];
cout<<endl;
}
return ;
}
E2726 Recursively Palindromic Partitions
First AC: 2018-05-17 Latest Modification: 2018-05-17
#include<bits/stdc++.h>
using namespace std;
int T,n;
int dp[];
int i,j,k;
int main()
{
dp[]=;
for(i=;i<;++i){
if(i&){
for(j=;j<=i;j+=)dp[i]+=dp[(i-j)/];
}
else{
for(j=;j<=i;j+=)dp[i]+=dp[(i-j)/];
dp[i]+=dp[i/];
}
}
cin>>T;
for(i=;i<=T;++i){
cin>>n;
cout<<i<<' '<<dp[n]<<endl;
}
return ;
}
E2743 Stock Exchange
First AC: 2018-06-04 Latest Modification: 2018-06-04
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,len;
ll a[];
int i,j,k;
void bi(int l,int r,int n)
{
if(l+>=r){
if(n==a[l]||n==a[r])return;
int rst=r+;
while(rst&&n<a[rst-])--rst;
a[rst]=n;
len=max(len,rst);
return;
}
int mid=(l+r)/;
if(n>a[mid])bi(mid,r,n);
else if(n<a[mid])bi(l,mid,n);
else return;
}
int main()
{
while(cin>>n){
memset(a,0x3f,sizeof(a));
len=;
cin>>a[];
while(--n)cin>>m,bi(,len,m);
cout<<len+<<endl;
}
return ;
}
E2747 Build Your Home
First AC: 2018-06-04 Latest Modification: 2018-06-04
#include<bits/stdc++.h>
using namespace std;
int n,i,sum,mx,my;
double x[],y[];
int main()
{
while(cin>>n,n){
for(i=sum=mx=my=;i<n;++i){
cin>>x[i]>>y[i];
if(x[i]<mx)mx=x[i];
if(y[i]<my)my=y[i];
} for(i=;i<n;++i)x[i]+=mx+,y[i]+=my+;
for(i=;i<n;++i)sum+=x[i-]*y[i]-x[i]*y[i-];
sum+=x[n-]*y[]-x[]*y[n-];
if(sum<)sum*=-;
cout<<(int)(sum/2.0+0.5)<<endl;
}
return ;
}
E2748 Quick Answer
First AC: 2018-06-04 Latest Modification: 2018-06-04
Note: 并查集,给每个点对应到一个id,删点时把点更新成新的id即可
#include<bits/stdc++.h>
using namespace std;
int n,x,y,yes,no;
char op;
int pre[],id[];
int i;
int find(int x)
{
int r=x,i=x,j;
while(pre[r]!=r)r=pre[r];
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
void del(int x)
{
id[x]=++n;
pre[n]=n;
}
void join(int x,int y)
{
int fx=find(id[x]),fy=find(id[y]);
pre[fx]=fy;
}
void qui(int x,int y)
{
int fx=find(id[x]),fy=find(id[y]);
fx==fy? ++yes:++no;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n){
yes=no=;
for(i=;i<=n;++i)pre[i]=i,id[i]=i;
while(cin>>op){
if(op=='c')cin>>x>>y,join(x,y);
else if(op=='d')cin>>x,del(x);
else if(op=='q')cin>>x>>y,qui(x,y);
else break;
}
cout<<yes<<" , "<<no<<endl;
}
return ;
}
E2751 Internet Service Providers
First AC: 2018-06-04 Latest Modification: 2018-06-04
#include<bits/stdc++.h>
using namespace std;
int n,c,tmp1,tmp2;
int main()
{
while(cin>>n>>c){
if(!n)cout<<"0\n";
else{
tmp1=c/n/;
tmp2=tmp1+;
cout<<(tmp1*(c-n*tmp1)<tmp2*(c-n*tmp2)? tmp2:tmp1)<<endl;
}
}
return ;
}
E2766 Intriguting Identifiers
First AC: 2018-06-04 Latest Modification: 2018-06-04
#include<bits/stdc++.h>
using namespace std;
string s;
int len,date,code,tmp;
int i;
int main()
{
int mon[]={,,,,,,,,,,,,};
while(cin>>s,s!="end"){
len=s.length();
if(len<||len>||s[]!='/'){
cout<<"invalid\n";
continue;
}
date=code=;
bool jdg=;
for(i=;i<;++i){
if(s[i]<''||s[i]>''){
jdg=;
break;
}
date=*date+s[i]-'';
}
for(i=;i<len;++i){
if(s[i]<''||s[i]>''){
jdg=;
break;
}
code=*code+s[i]-'';
}
if(date/>&&date/<){
if(len!=)jdg=;
}
else if(len!=)jdg=;
if(date/>&&date/<)jdg=;
tmp=date%/;
if(tmp<||tmp>)
if(tmp<||tmp>)
jdg=;
if(tmp>)tmp-=;
if(date/%){
if(mon[tmp]<date%)jdg=;
}
else{
if(tmp==&&date%==);
else if(mon[tmp]<date%)jdg=;
}
if(len==&&(date%*+code)%)jdg=;
if(jdg)date%/>? cout<<"girl\n":cout<<"boy\n";
else cout<<"invalid\n";
}
return ;
}
E2786 How Many Digits
First AC: 2018-06-05 Latest Modification: 2018-06-05
#include<bits/stdc++.h>
using namespace std;
int T,n,mod,cnt;
int i;
int main()
{
cin>>T;
while(T--){
cin>>n;
mod=cnt=;
for(i=;;++i){
mod=*mod%n;
if(!((cnt+=mod)%=n)){
cout<<i<<endl;
break;
}
}
}
return ;
}
E2801 Headshot
First AC: 2018-12-16 Latest Modification: 2018-12-16
#include<bits/stdc++.h>
using namespace std;
string s;
int len;
int zero,one,zeroone;
int main()
{
cin>>s;
len=s.length();
s+=s[];
for(int i=;i<len;++i){
s[i]==''? ++zero:++one;
if(s[i]==''&&s[i+]=='')++zeroone;
}
if(zeroone*len<zero*one)cout<<"SHOOT";
else if(zeroone*len>zero*one)cout<<"ROTATE";
else cout<<"EQUAL";
return ;
}
E2819 糖果
First AC: 2017-10-18 Latest Modification: 2018-06-05
#include<bits/stdc++.h>
using namespace std;
int n,can,pap;
int main()
{
cin>>n;
while(cin>>n){
can=pap=n;
while(pap>)can+=pap/,pap=pap%+pap/;
cout<<can<<endl;
}
return ;
}
E2820 围栏
First AC: 2017-10-31 Latest Modification: 2018-06-05
#include<bits/stdc++.h>
using namespace std;
string s;
int len,maxs,pre;
int i,j;
int main()
{
cin>>s;
len=s.length();
if(len==){cout<<len;return ;}
for(i=;i<len;++i){
pre=;
for(j=i;j<len-;++j)
if(s[j]==s[j+])break;
else ++pre;
if(pre>maxs)maxs=pre;
}
cout<<maxs;
return ;
}
E2822 内存显示
First AC: 2018-03-09 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
char s[];
void solvei(int n)
{
int c=sizeof(n);
unsigned char *p=(unsigned char*)&n;
while(c--)printf("%02x ",*p++);
}
void solved(double n)
{
int c=sizeof(n);
unsigned char *p=(unsigned char*)&n;
while(c--)printf("%02x ",*p++);
}
int main()
{
while(scanf("%s",s)!=EOF){
if(strchr(s,'.')==)solvei(atoi(s));
else solved(atof(s));
cout<<endl;
}
return ;
}
E2824 Box
First AC: 2017-12-30 Latest Modification: 2018-06-05
#include<bits/stdc++.h>
using namespace std;
double n;
int main()
{
cin>>n;
while(cin>>n)printf("%.10f\n",n/);
return ;
}
E2831 Factorials
First AC: 2017-11-28 Latest Modification: 2017-11-28
#include<iostream>
using namespace std;
int T,n,s,i;
int main()
{
cin>>T;
while(T--){
cin>>n;
++n,s=;
for(i=;i<n;++i){
s*=i;
while(!(s%))s/=;
if(s%==)s%=;
}
cout<<s%<<endl;
}
return ;
}
E2832 Zero
First AC: 2018-05-05 Latest Modification: 2018-05-05
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt;
string s="1 2 3 4 5 6 7 8 9",t="+- ";
string rst[];
int i,j,k,p,q,r,m,l;
void solve(int n)
{
int num=,tmp=,flag=;
for(int i=;i<*n-;++i){
if(s[i]=='+'){
if(flag==)num+=tmp;
else if(flag==)num-=tmp;
tmp=,flag=;
}
else if(s[i]=='-'){
if(flag==)num+=tmp;
else if(flag==)num-=tmp;
tmp=,flag=;
}
else if(s[i]==' ');
else tmp=*tmp+s[i]-'';
}
if(flag==)num+=tmp;
else if(flag==)num-=tmp;
if(!num)rst[cnt++]=s.substr(,*n-);
}
int main()
{
cin>>T;
while(T--){
cin>>n;
cnt=;
for(i=;i<;++i)for(j=;j<;++j)
for(k=;k<;++k)for(p=;p<;++p)
for(q=;q<;++q)for(r=;r<;++r)
for(m=;m<;++m)for(l=;l<;++l){
s[]=t[i],s[]=t[j],s[]=t[k],
s[]=t[p],s[]=t[q],s[]=t[r],
s[]=t[m],s[]=t[l],solve(n);
}
sort(rst,rst+cnt);
cout<<rst[]<<endl;
for(i=;i<cnt;++i)if(rst[i]!=rst[i-])cout<<rst[i]<<endl;
cout<<endl;
}
return ;
}
E2833 Lumberjack Sequencing
First AC: 2018-01-05 Latest Modification: 2018-01-05
#include<iostream>
using namespace std;
int T,i,j;
bool tmp,rst;
int a[];
int main()
{
cin>>T;
cout<<"Lumberjacks:\n";
while(T--){
for(i=;i<;++i)cin>>a[i];
for(i=;i<;++i)if(a[i]!=a[i-]){tmp=a[i]>a[i-];break;}
if(i==){cout<<"Ordered\n";continue;}
for(rst=,j=i+;j<;++j){
if((bool)(a[j]>a[j-])!=tmp){rst=;break;}
}
if(rst)cout<<"Ordered\n";
else cout<<"Unordered\n";
}
return ;
}
E2836 Financial Management
First AC: 2018-05-27 Latest Modification: 2018-05-27
#include<bits/stdc++.h>
using namespace std;
int T;
double tmp,rst;
int i,j;
int main()
{
cin>>T;
for(i=;i<=T;++i){
rst=;
for(j=;j<;++j)cin>>tmp,rst+=tmp;
rst/=12.0;
cout<<i<<" $";
if(rst>)cout<<(int)rst/<<',';
if(rst>)cout<<(int)rst%/<<',';
printf("%.2f\n",rst-(int)rst/*);
}
return ;
}
E2837 Code Number
First AC: 2018-06-05 Latest Modification: 2018-06-05
#include<bits/stdc++.h>
using namespace std;
int len;
string s;
char c[];
int i;
int main()
{
for(i=;i<;++i)c[i]=char(i);
c['']='O';
c['']='I';
c['']='Z';
c['']='E';
c['']='A';
c['']='S';
c['']='G';
c['']='T';
c['']='B';
c['']='P';
getline(cin,s);
while(getline(cin,s)){
len=s.length();
for(i=;i<len;++i)cout<<c[s[i]];
cout<<endl;
}
return ;
}
E2838 Cancer or Scorpio
First AC: 2018-06-06 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int mon1[]={,,,,,,,,,,,,};
int mon2[]={,,,,,,,,,,,,};
int T,n,y,m,d;
int i;
int d2n(int y,int m,int d)
{
if((!(y%)&&y%)||!(y%))return mon2[m-]+d;
return mon1[m-]+d;
}
void n2d(int num)
{
if((!(y%)&&y%)||!(y%)){
if(num>)num-=,++y;
}
else if(num>)num-=,++y;
if((!(y%)&&y%)||!(y%)){
for(int i=;;++i){
if(num<=mon2[i]){
num-=mon2[i-];
m=i,d=num;
break;
}
}
}
else{
for(int i=;;++i){
if(num<=mon1[i]){
num-=mon1[i-];
m=i,d=num;
break;
}
}
}
}
void find(int date)
{
if(date>&&date<)cout<<"aquarius\n";
else if(date>&&date<)cout<<"pisces\n";
else if(date>&&date<)cout<<"aries\n";
else if(date>&&date<)cout<<"taurus\n";
else if(date>&&date<)cout<<"gemini\n";
else if(date>&&date<)cout<<"cancer\n";
else if(date>&&date<)cout<<"leo\n";
else if(date>&&date<)cout<<"virgo\n";
else if(date>&&date<)cout<<"libra\n";
else if(date>&&date<)cout<<"scorpio\n";
else if(date>&&date<)cout<<"sagittarius\n";
else cout<<"capricorn\n";
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n;
y=n%,m=n/,d=n%/;
n2d(d2n(y,m,d)+);
printf("%d %2d/%2d/%4d ",i,m,d,y);
find(m*+d);
}
return ;
}
E2844 排序去重
First AC: 2017-11-13 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
int n=,cnt,i;
int a[];
string s;
int main()
{
cin>>s;
while(scanf("%d",&n)!=EOF)++a[n];
if(s=="A")for(i=;i<;++i){if(a[i]!=)
if(cnt==)cout<<i,++cnt;
else cout<<" "<<i;}
else{for(i=;i>;--i)if(a[i]!=)
if(cnt==)cout<<i,++cnt;
else cout<<" "<<i;}
return ;
}
E2845 符号方程求解
First AC: 2018-02-17 Latest Modification: 2018-02-17
#include<bits/stdc++.h>
using namespace std;
char c,a;
int tmp;
int x1,c1,x2,c2;
bool sig=;
int i;
int main()
{
while(c=getchar()){
if(c=='='){
if(sig)c1+=tmp;
else c1-=tmp;
x2=x1,c2=c1,x1=,c1=,tmp=,sig=;
}
else if(c=='\n'){
if(sig)c1+=tmp;
else c1-=tmp;
break;
}
else if(c=='+'){
if(sig)c1+=tmp;
else c1-=tmp;
tmp=,sig=;
}
else if(c=='-'){
if(sig)c1+=tmp;
else c1-=tmp;
tmp=,sig=;
}
else if(c>='a'&&c<='z'){
if(tmp==)tmp=;
a=c;
if(sig)x1+=tmp;
else x1-=tmp;
tmp=;
}
else tmp=tmp*+c-'';
}
if(c2==c1){
cout<<a<<"=0.000";
return ;
}
float up=c2-c1,dn=x1-x2;
bool sig;
if(up>&&dn>||up<&&dn<)sig=;
else sig=;
up=fabs(up),dn=fabs(dn);
if(sig)printf("%c=-%.3f",a,up/dn);
else printf("%c=%.3f",a,up/dn);
return ;
}
E2846 统计字符串个数
First AC: 2017-10-19 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int a00[],a01[],a10[],a11[],n;
int i;
int main()
{
a00[]=,a00[]=;
a01[]=,a01[]=;
a10[]=,a10[]=;
a11[]=,a11[]=;
for(i=;i<;i++){
a00[i]=a00[i-]+a01[i-];
a11[i]=a01[i]=a11[i-]+a10[i-];
a10[i]=a00[i-];
}
while(cin>>n,n!=-){
cout<<a00[n]+a01[n]+a10[n]+a11[n]<<endl;
}
return ;
}
E2847 路由节点
First AC: 2018-01-05 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
while(cin>>n)cout<<n*(n-)*(n-)*(n-)/<<endl;
return ;
}
E2848 华师大卫星照片
First AC: 2017-11-26 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int w,h,r,l,tmp,maxn,i,j;
char a[][]; int cnt(int l,int r)
{
if(a[l][r]=='.'||l==||r==||l>h||r>w)return ;
if(a[l-][r]-'.'||a[l][r-]-'.'||a[l][r+]-'.'||a[l+][r]-'.'){
a[l][r]='.';
return cnt(l-,r)+cnt(l,r-)+cnt(l,r+)+cnt(l+,r)+;
}
a[l][r]='.';
return ;
}
int main()
{
memset(a,'.',sizeof(a));
cin>>w>>h;
for(i=;i<=h;++i)for(j=;j<=w;++j)cin>>a[i][j];
for(i=;i<=h;++i)for(j=;j<=w;++j){
tmp=cnt(i,j);
if(tmp>maxn)maxn=tmp;
}
cout<<maxn;
return ;
}
E2849 成绩排序
First AC: 2017-11-20 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
struct data{
long long num;
int score;
}a[];
int n,i;
bool cmp(data a,data b)
{
if(a.score==b.score)return a.num<b.num;
return a.score>b.score;
}
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i].num>>a[i].score;
sort(a,a+n,cmp);
for(i=;i<n;++i)
if(a[i].score<)break;
else cout<<a[i].num<<' '<<a[i].score<<endl;
return ;
}
E2850 小强的生日
First AC: 2017-11-28 Latest Modification: 2018-03-22
#include<bits/stdc++.h>
using namespace std;
int y,n,tmp,Maxd,i,flag;
int date(int y)//From Pro. 3322
{
for(i=,n=y;i<y;++i)if(i%==&&i%!=)++n;
return(n-)%;
}
int main()
{
while(cin>>y){
tmp=date(y);
y%==&&y%!=? Maxd=:Maxd=;
flag=(tmp+Maxd)%==? :;
cout<<" SU MO TU WE TH FR SA\n";
for(i=;i<tmp;++i)cout<<" ";
for(i=;i<Maxd;++i){
printf("%3d",i);
if(++tmp==)cout<<endl,tmp=;
}
if(flag)cout<<endl;
cout<<endl;
}
return ;
}
E2851 达到回文数
First AC: 2017-11-22 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m;
int cnt;
int ccl(ll n)
{
int rst=;
while(n)rst++,n/=;
return rst;
}
ll rvs(ll n)
{
int rst=;
for(ll tmp=pow(,ccl(n)-);tmp;tmp/=,n/=){
rst+=n%*tmp;
}
return rst;
}
int main()
{
cin>>n;
while(){
m=rvs(n);
if(m==n){
cout<<cnt<<' '<<n;
break;
}
++cnt;
n+=m;
}
return ;
}
E2852 统计字母频率
First AC: 2017-11-11 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
string s;
int ln,a[],num;
int i;
int main()
{
getline(cin,s);
ln=s.length();
for(i=;i<ln;++i)if(s[i]>&&s[i]<)++num,++a[s[i]-];
for(i=ln=;i<;++i)if(a[i]>a[ln])ln=i;
printf("%c %.2f",+ln,a[ln]*1.0/num);
return ;
}
E2853 集合运算
First AC: 2017-12-01 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int a,b,c,cnt;
string x[];
struct data{
string s;
int flag,len;
}n[];
int i,j,k;
bool cmp(data m,data n)
{
if(m.flag-n.flag)return m.flag>n.flag;
if(m.flag){
if(m.len-n.len)return m.len>n.len;
for(i=;i<n.len;++i)if(m.s[i]!=n.s[i])return m.s[i]>n.s[i];
}
else{
if(m.len-n.len)return m.len<n.len;
for(i=;i<n.len;++i)if(m.s[i]!=n.s[i])return m.s[i]<n.s[i];
}
}
int main()
{
cin>>a>>b;
for(c=a+b;i<c;++i){
cin>>n[i].s;
n[i].len=n[i].s.length();
if(n[i].s[]=='-')n[i].flag=;
else n[i].flag=;
}
sort(n,n+a,cmp);
for(i=;i<a;++i){
for(cnt=,j=a;j<c;++j)if(n[i].s==n[j].s){++cnt;break;}
if(!cnt)x[k++]=n[i].s;
}
sort(n,n+c,cmp);
cout<<"{";
for(cnt=,i=;i<c;++i)if(n[i].s==n[i-].s){
if(cnt)cout<<','<<n[i].s;
else cout<<n[i].s,++cnt;
}
cout<<"}\n{";
if(c){
cout<<n[].s;
for(i=;i<c;++i)if(n[i].s!=n[i-].s)cout<<','<<n[i].s;
}
cout<<"}\n{";
if(k){
cout<<x[];
for(i=;i<k;++i)cout<<','<<x[i];
}
cout<<"}";
return ;
}
E2854 统计特定字串模式的个数
First AC: 2018-04-01 Latest Modification: 2018-04-01
#include<bits/stdc++.h>
using namespace std;
int n,m;
long long dp[][];
int i,j;
int main()
{
for(i=;i<;++i)for(j=;j<;++j){
dp[i][j]+=*dp[i-][j];
if(i>=j)dp[i][j]+=<<max(,i-j-);
if(i>j+)dp[i][j]-=dp[i-j-][j];
}
while(cin>>n>>m,n+||m+)cout<<dp[n][m]<<endl;
return ;
}
E2855 贪吃蛇
First AC: 2018-02-26 Latest Modification: 2018-02-26
#include<bits/stdc++.h>
using namespace std;
int H,W,T,h,w,rst;
char maps[][];
int fib[]={,};
int i,j,k;
bool jdg(int n,int h,int w)
{
if(n==){
if(maps[h-][w]!='*')return ;
return ;
}
else if(n==){
if(maps[h][w+]!='*')return ;
return ;
}
else if(n==){
if(maps[h+][w]!='*')return ;
return ;
}
else if(maps[h][w-]!='*')return ;
return ;
}
int main()
{
cin>>H>>W>>T;
++H,++W;
memset(maps,'*',sizeof(maps));
for(i=;i<H;++i){
getchar();
for(j=;j<W;++j)cin>>maps[i][j];
}
for(i=;i<T;++i)fib[i]=(fib[i-]+fib[i-])%;
if(maps[][]!='.')rst=maps[][]-'';
h=w=;
for(i=;i<T;++i){
int tmp=fib[i];
while(){
if(jdg(tmp,h,w)){
if(tmp==)--h;
else if(tmp==)++w;
else if(tmp==)++h;
else --w;
if(maps[h][w]!='.')rst+=maps[h][w]-'';
break;
}
else tmp=(tmp+)%;
}
}
cout<<rst;
return ;
}
E2856 仰望星空
First AC: 2017-12-21 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int w,h,r,l,tmp,num,i,j;
char a[][];
int cnt(int l,int r)
{
if(a[l][r]=='.'||l==||r==||l>h||r>w)return ;
if(a[l-][r]-'.'||a[l][r-]-'.'||a[l][r+]-'.'
||a[l+][r]-'.'||a[l-][r-]-'.'||a[l-][r+]-'.'
||a[l+][r-]-'.'||a[l+][r+]-'.'){
a[l][r]='.';
return cnt(l-,r)+cnt(l,r-)+cnt(l,r+)+cnt(l+,r)
+cnt(l-,r-)+cnt(l-,r+)+cnt(l+,r-)+cnt(l+,r+)+;
}
a[l][r]='.';
return ;
}
int main()
{
while(cin>>w>>h){
num=;
memset(a,'.',sizeof(a));
for(i=;i<=h;++i)for(j=;j<=w;++j)cin>>a[i][j];
for(i=;i<=h;++i)for(j=;j<=w;++j){
tmp=cnt(i,j);
if(tmp)++num;
}
cout<<num<<endl;
}
return ;
}
E2857 编辑距离
First AC: 2017-12-12 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int f[][];
int T,ls,lt,tmp,a,b,c;
string s,t;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s>>t;
ls=s.length(),lt=t.length();
tmp=ls>lt? ls:lt;
for(j=;j<=tmp;++j)f[][j]=f[j][]=j;
for(j=;j<ls;++j){
for(k=;k<lt;++k){
if(s[j]==t[k])f[j+][k+]=f[j][k];
else{
a=f[j][k+],b=f[j+][k],c=f[j][k];
f[j+][k+]=min(a,min(b,c))+;
}
}
}
cout<<f[ls][lt]<<endl;
}
return ;
}
E2858 危险,距离不能为二
First AC: 2018-05-03 Latest Modification: 2018-05-03
#include<bits/stdc++.h>
using namespace std;
int n,h,w;
int main()
{
int a[][]={,,,,,,,,,,,,,,,};
cin>>n;
while(n--){
cin>>h>>w;
if(h>w)h^=w,w^=h,h^=w;
cout<<a[(h-)%][(w-)%]+*((h-)%+)
*((w-)/)+*w*((h-)/)<<endl;
}
return ;
}
E2859 表达式的个数
First AC: 2018-08-09 Latest Modification: 2018-08-09
#include<bits/stdc++.h>
using namespace std;
string s="1 2 3 4 5 6 7 8 9+";
string t="+- ";
map<int,int>mp;
map<int,int>::iterator it;
int tmp;
int i,j,k,p,q,r,m,n;
int cal(string s)
{
int ret=tmp=;
int sign=;
int len=s.length();
for(int i=;i<len;++i){
if(s[i]=='+'){
ret+=sign*tmp;
tmp=;
sign=;
}
else if(s[i]=='-'){
ret+=sign*tmp;
tmp=;
sign=-;
}
else if(s[i]!=' '){
tmp=*tmp+s[i]-'';
}
}
return ret;
}
int main()
{
for(i=;i<;++i){
s[]=t[i];
for(j=;j<;++j){
s[]=t[j];
for(k=;k<;++k){
s[]=t[k];
for(p=;p<;++p){
s[]=t[p];
for(q=;q<;++q){
s[]=t[q];
for(r=;r<;++r){
s[]=t[r];
for(m=;m<;++m){
s[]=t[m];
for(n=;n<;++n){
s[]=t[n];
tmp=cal(s);
it=mp.find(tmp);
if(it!=mp.end())++it->second;
else mp.insert(pair<int,int>(tmp,));
}
}
}
}
}
}
}
}
while(cin>>tmp){
it=mp.find(tmp);
if(it==mp.end())cout<<"0\n";
else cout<<it->second<<endl;
}
return ;
}
E2862 求斜边
First AC: 2018-01-05 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main()
{
cin>>a>>b;
printf("%.3f",sqrt(a*a+b*b));
return ;
}
E2863 重力加速度
First AC: 2017-10-22 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
cin>>n;
if(n==)cout<<"0.598";
if(n==)cout<<"1.174";
if(n!=&&n!=)printf("%.3f",sqrt((*n+5.5)/9.8));
return ;
}
E2864 Reverse
First AC: 2017-11-09 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
string s;
int len,i;
int main()
{
cin>>s;
len=s.length();
cout<<len<<' ';
for(i=len-;i>=;--i)cout<<s[i];
return ;
}
E2875 成绩统计
First AC: 2017-12-29 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int n;
double ava,var;
int a[];
string s;
int i,j;
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>s>>a[i];
ava+=a[i];
}
ava/=n;
for(i=;i<n;++i)var+=(ava-a[i])*(ava-a[i]);
printf("%.2f %.2f",ava,sqrt(var/(n-)));
return ;
}
E2876 二进制位不同的个数
First AC: 2017-10-31 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int m,n,a[],b[],num,i;
int main()
{
cin>>m;
while(cin>>m>>n){
num=;
for(i=;i<;++i)a[i]=m%,b[i]=n%,m/=,n/=;
for(i=;i<;++i)if(a[i]!=b[i])++num;
cout<<num<<endl;
}
return ;
}
E2877 哥德巴赫猜想
First AC: 2017-12-27 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int a[],n,tmp;
int i,j;
int main()
{
for(i=;i<;++i){
if(a[i]==){
for(j=*i;j<;j+=i)++a[j];
}
}
while(cin>>n){
tmp=n/;
while(){
if(a[tmp]==&&a[n-tmp]==){
cout<<tmp<<' '<<n-tmp<<endl;
break;
}
else --tmp;
}
}
return ;
}
E2878 字串排序
First AC: 2017-12-21 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
struct data{
string s;
bool flag;
long n;
}a[];
string t;
long base;
int i,cnt,len;
bool cmp(data a,data b)
{
if(a.flag==b.flag){
if(a.flag==||a.n==b.n){
int lena=a.s.length(),lenb=b.s.length();
int tmp=lena<lenb? lena:lenb;
for(int i=;i<tmp;++i)
if(a.s[i]!=b.s[i])return a.s[i]>b.s[i];
return a.s.length()>b.s.length();
}
else return a.n>b.n;
}
return a.flag>b.flag;
}
int main()
{
while(cin>>t){
len=t.length()-,a[cnt].s=t;
for(base=,i=len;i>=;--i)if(t[i]>=''&&t[i]<='')
a[cnt].n+=base*(t[i]-''),base*=;
if(base^)a[cnt].flag=;
++cnt;
}
sort(a,a+cnt,cmp);
cout<<a[cnt-].s;
for(i=cnt-;i>=;--i)cout<<' '<<a[i].s;
cout<<endl;
return ;
}
E2879 求三元组的个数
First AC: 2018-04-08 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
int a[];
int i,j,k;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
for(i=;i<n;++i)for(j=i+;j<n;++j)for(k=j+;k<n;++k){
if(i==j||i==k||j==k)continue;
if(__gcd(a[i],a[j])==&&__gcd(a[i],a[k])==
&&__gcd(a[j],a[k])==)++cnt;
if(__gcd(a[i],a[j])!=&&__gcd(a[i],a[k])!=
&&__gcd(a[j],a[k])!=)++cnt;
}
cout<<cnt<<endl;
return ;
}
E2880 Young Physicist
First AC: 2017-11-15 Latest Modification: 2017-11-15
#include<iostream>
using namespace std;
int t,n,x,y,z,a,b,c;
int main()
{
cin>>t;
while(t--){
cin>>n;
a=b=c=;
while(n--){
cin>>x>>y>>z;
a+=x,b+=y,c+=z;
}
if(!a&&!b&&!c)cout<<"YES\n";
else cout<<"NO\n";
}
return ;
}
E2881 Bets
First AC: 2018-02-20 Latest Modification: 2018-02-20
#include<bits/stdc++.h>
using namespace std;
int T,n,m,l,r,t,p,rst;
int pertime[];
int profit[];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n>>m;
++n;
rst=;
for(i=;i<n;++i)pertime[i]=,profit[i]=;
for(i=;i<m;++i){
cin>>l>>r>>t>>p;
++r;
for(j=l;j<r;++j)
if(t<pertime[j])pertime[j]=t,profit[j]=p;
}
for(i=;i<n;++i)if(profit[i])rst+=profit[i];
cout<<rst<<endl;
}
return ;
}
E2882 Bookshelf2
First AC: 2018-06-06 Latest Modification: 2018-06-06
Note: 同题2064
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,b,rst=1e7;
ll h[];
ll i;
ll find(ll i)
{
ll s=,j=;
while(i){
if(i&)s+=h[j];
++j,i>>=;
}
if(s<b)return 1e7;
return s;
}
int main()
{
cin>>n>>b;
for(i=;i<n;++i)cin>>h[i];
for(i=(<<n)-;i;--i)rst=min(rst,find(i));
cout<<rst-b;
return ;
}
E2885 A Spiral Work
First AC: 2018-05-16 Latest Modification: 2018-05-16
#include<bits/stdc++.h>
using namespace std;
int n;
int a[][];
void solve(int r,int c,int len,int st)
{
if(len<)return;
a[r][c]=st;
int i;
for(i=;i<len;++i)a[r][++c]=++st;
for(i=;i<len;++i)a[++r][c]=++st;
for(i=;i<len;++i)a[r][--c]=++st;
for(i=;i<len;++i)a[--r][c]=++st;
solve(r,c+,len-,st+); }
void print(int n)
{
for(int i=;i<=n;++i){
cout<<a[i][];
for(int j=;j<=n;++j)cout<<' '<<a[i][j];
cout<<endl;
}
}
int main()
{
cin>>n;
solve(,,n,);
print(n);
return ;
}
E2886 个人所得税
First AC: 2017-10-18 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
double s;
int main()
{
cin>>s;
if(s<=)cout<<"0.00";
else if(s<=)printf("%.2f",s/-);
else if(s<=)printf("%.2f",s/-);
else if(s<=)printf("%.2f",s*/-);
else printf("%.2f",s/-);
return ;
}
E2887 指针
First AC: 2018-03-29 Latest Modification: 2018-03-29
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"BBBB0000\nBBBBAAAA\nAAAA\nCD\nCDAAAAAA\n5678\n34\n00\n";
return ;
}
E2888 买房子
First AC: 2017-10-18 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
double income,price,n;
int main()
{
while(cin>>n>>k>>m){
income=n,price=,count=;
for(year=;year<=;year++){
if(income>=price){count++;break;}
n*=(m/100.0+),price*=(k/100.0+),income+=n;
}
if(count==)cout<<"Impossible\n";
else cout<<year<<endl;
}
return ;
}
E2889 魔方阵
First AC: 2017-10-19 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int n,line,row,maxi;
int i,j,k;
int a[][];
int main()
{
for(k=;k<;k++){
cin>>n;
maxi=n*n+;
line=;row=n/+;
a[line][row]=;
for(i=;i<maxi;i++){
if(line==&&row!=n){
a[n][row+]=i;
line=n,row++;
}
else if(row==n&&line!=){
a[line-][]=i;
line--,row=;
}
else if(line==&&row==n){
a[line+][row]=i;
line++;
}
else if(a[line-][row+]==){
a[line-][row+]=i;
line--,row++;
}
else{
a[line+][row]=i;
line++;
}
}
for(i=n;i>;i--){
cout<<a[i][n],a[i][n]=;
for(j=n-;j>;j--){
cout<<" "<<a[i][j];
a[i][j]=;
}
cout<<endl;
}
}
return ;
}
E2890 询问队员身高
First AC: 2017-11-25 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int t,m,n,k;
int i;
struct data{
int num,height;
}a[];
bool cmp(data a,data b)
{
return a.height>b.height;
}
int main()
{
cin>>t;
while(t--){
cin>>m>>n;
for(i=;i<m;++i)a[i].num=i+,cin>>a[i].height;
sort(a,a+m,cmp);
for(i=;i<n;++i){
cin>>k;
if(i==)cout<<a[k-].num;
else cout<<' '<<a[k-].num;
}
cout<<endl;
}
return ;
}
E2891 多次函数
First AC: 2017-11-02 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
char a[][];
int i,j;
string s;
int b[],c,t,m,cnt,n;
int main()
{
while(cin>>s){
if(cnt!=)cout<<endl;
cnt++;
//打表
memset(a,'.',sizeof(a));
for(i=;i<;++i)a[][i]='-',a[i][]='|';
a[][]='^',a[][]='+',a[][]='>';
b[]=b[]=b[]=b[]=;
//覆盖
for(i=;i<s.length();++i)
if(s[i]=='x'){
t=;
//判断次数t:
if(i!=s.length()-&&s[i+]=='^')t=(int)(s[i+]-'');
//判断系数c:
if(s[i-]=='-')
c=-*(s[i-]-'')-(s[i-]-'');
else if(s[i-]>=''&&s[i-]<=''&&s[i-]!='^')
c=*(s[i-]-'')+(s[i-]-'');
else if(s[i-]=='-')
c=-(s[i-]-'');
else if(s[i-]=='-')
c=-;
else if(s[i-]>=''&&s[i-]<='')
c=(s[i-]-'');
else c=;
b[t]=c;
}
//常数项:
if(s[s.length()-]!='x'&&s[s.length()-]!='^'){
if(s[s.length()-]=='-')
b[]=-*(s[s.length()-]-'')-(s[s.length()-]-'');
else if(s[s.length()-]>=''&&s[s.length()-]<=''
&&s[s.length()-]!='^')
b[]=*(s[s.length()-]-'')+(s[s.length()-]-'');
else if(s[s.length()-]=='-')
b[]=-(s[s.length()-]-'');
else if(s[s.length()-]=='-')
b[]=-;
else if(s[s.length()-]>=''&&s[s.length()-]<='')
b[]=s[s.length()-]-'';
else b[]=;
}
//计算输出
if(n==){
for(i=-;i<;++i){
m=b[]*i*i*i+b[]*i*i+b[]*i+b[];
if(m>-&&m<)a[-m][i+]='*';
}
for(i=;i<;++i){
for(j=;j<;++j)cout<<a[i][j];
if(i!=)cout<<endl;
}
n++;
}
else{
cout<<endl;
for(i=-;i<;++i){
m=b[]*i*i*i+b[]*i*i+b[]*i+b[];
if(m>-&&m<)a[-m][i+]='*';
}
for(i=;i<;++i){
for(j=;j<;++j)cout<<a[i][j];
if(i!=)cout<<endl;
}
}
}
return ;
}
E2892 文件排序
First AC: 2017-12-31 Latest Modification: 2019-04-07
#include<bits/stdc++.h>
using namespace std;
int n;
struct data{
string date,time,dtime;
int size;
string name;
}a[];
string query;
bool flag;
int i;
bool cmp(data a,data b)
{
char ch=query[];
if(ch=='S'&&a.size!=b.size)return a.size<b.size;
if(ch=='T'&&a.dtime!=b.dtime)return a.dtime<b.dtime;
return a.name<b.name;
}
int main()
{
while(cin>>n,n){
for(i=;i<n;++i){
cin>>a[i].date>>a[i].time>>a[i].size;
a[i].dtime=a[i].date+' '+a[i].time;
getchar();
getline(cin,a[i].name);
}
getline(cin,query);
sort(a,a+n,cmp);
if(flag)cout<<endl;
else flag=;
for(i=;i<n;++i){
cout<<a[i].dtime;
printf("%17d ",a[i].size);
cout<<a[i].name<<endl;
}
}
return ;
}
E2893 数据密度
First AC: 2018-02-19 Latest Modification: 2018-03-08
#include<bits/stdc++.h>
using namespace std;
int n,len,cnt,up,dn;
string s;
char c;
int i,j;
int gcd(int a,int b)
{
int c=;
while(c)c=a%b,a=b,b=c;
return a;
}
int main()
{
cin>>n;
getchar();
while(n--){
getline(cin,s);
len=s.length();
cnt=;
for(i=;i<len;++i){
c=s[i];
for(j=;j<;++j){
if(c&)++cnt;
c>>=;
}
}
up=cnt,dn=*len;
cnt=gcd(up,dn);
up/=cnt,dn/=cnt;
cout<<up<<'/'<<dn<<endl;
}
return ;
}
E2894 波浪图
First AC: 2017-11-14 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
string s;
bool a[][];
int b[];
int len,r,M,m;
int i,j;
int main()
{
while(cin>>s){
memset(a,,sizeof(a)),memset(b,,sizeof(b));
len=s.length();
M=m=r=,a[][]=;
for(i=;i<len;++i){
if(s[i]>s[i-])a[--r][i]=;
else if(s[i]<s[i-])a[++r][i]=;
else a[r][i]=;
if(r>M)M=r;
if(r<m)m=r;
b[r]=i;
}
for(i=m;i<=M;++i){
for(j=;j<=b[i];++j){
if(a[i][j]==)cout<<s[j];
else cout<<' ';
}
cout<<endl;
}
}
return ;
}
E2895 循环小数
First AC: 2018-04-17 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
long up,dn,tmp;
long quo[],rem[],cnt;
long i,j;
int main()
{
while(scanf("%ld/%ld",&up,&dn)!=EOF){
tmp=__gcd(up,dn);
up/=tmp,dn/=tmp;
cnt=;
bool jdg=;
while(jdg){
*(quo+cnt)=up/dn;
*(rem+cnt)=up%dn;
if(!*(rem+cnt)){
printf("0\n");
break;
}
for(i=;i<cnt;++i){
if(*(rem+i)==*(rem+cnt)&&*(quo+i)==*(quo+cnt)){
for(j=i;j<cnt;++j)printf("%ld",*(quo+j));
cout<<endl;
jdg=;
break;
}
}
up=rem[cnt++]*;
}
}
return ;
}
E2896 随机排序
First AC: 2017-11-24 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
string s;
int len,cnt;
int a[];
char c;
int i,j;
struct data{
string str,str0;
int len;
}b[];
bool cmp(data m,data n)
{
int lm=m.len,ln=n.len;
int tmp=m.len>n.len? n.len:m.len;
for(i=;i<tmp;++i){
if(a[m.str0[i]-'A']!=a[n.str0[i]-'A'])
return a[m.str0[i]-'A']<a[n.str0[i]-'A'];
}
return m.len<n.len;
}
int main()
{
while(cin>>s){
len=s.length();
memset(a,,sizeof(a));
for(i=;i<len;++i)a[s[i]-'A']=i;
for(i=cnt=;;++i){
cin>>b[i].str;
b[i].str0=b[i].str;
b[i].len=b[i].str.length();
for(j=;j<b[i].len;++j)if(b[i].str[j]>'Z')b[i].str0[j]-=;
++cnt;
c=getchar();
if(c=='\n')break;
}
sort(b,b+cnt,cmp);
cout<<b[].str;
for(i=;i<cnt;++i)cout<<' '<<b[i].str;
cout<<endl;
}
return ;
}
E2897 英文缩写词
First AC: 2017-11-30 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
string a;
int len;
int i,j;
int main()
{
while(getline(cin,a)){
len=a.length();
for(i=;i<len;++i){
if(a[i]=='-')a[i]=;
if(a[i]>&&a[i]<)a[i]-=;
}
if(a[]=='A'&&a[]==)
a[]=;
else if(a[]=='A'&&a[]=='N'&&a[]==)
a[]=a[]=;
else if(a[]=='O'&&a[]=='F'&&a[]==)
a[]=a[]=;
else if(a[]=='T'&&a[]=='H'&&a[]=='E'&&a[]==)
a[]=a[]=a[]=;
else if(a[]=='F'&&a[]=='O'&&a[]=='R'&&a[]==)
a[]=a[]=a[]=;
else if(a[]=='A'&&a[]=='N'&&a[]=='D'&&a[]==)
a[]=a[]=a[]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='A'&&a[i]==)
a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='A'&&a[i-]=='N'&&a[i]==)
a[i-]=a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='O'&&a[i-]=='F'&&a[i]==)
a[i-]=a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='T'&&a[i-]=='H'
&&a[i-]=='E'&&a[i]==)a[i-]=a[i-]=a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='F'&&a[i-]=='O'
&&a[i-]=='R'&&a[i]==)a[i-]=a[i-]=a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='A'&&a[i-]=='N'
&&a[i-]=='D'&&a[i]==)a[i-]=a[i-]=a[i-]=;
if(a[]!=)cout<<a[];
for(i=;i<len;++i)
if(a[i-]==&&a[i]!=)
cout<<a[i];cout<<endl;
}
return ;
}
E2902 Contest
First AC: 2018-05-23 Latest Modification: 2018-05-23
#include<bits/stdc++.h>
using namespace std;
int n;
bool mp[][];
int p[];
int i,j;
int main()
{
for(i=;i<;++i)
for(j=;j<=i;++j)
mp[i][(int)(j*100.0/i+0.5)]=;
cin>>n;
for(i=;i<n;++i)cin>>p[i];
for(i=;;++i){
bool jdg=;
for(j=;j<n;++j)if(!mp[i][p[j]]){
jdg=;
break;
}
if(jdg){
cout<<i;
break;
}
}
return ;
}
E2905 Finance
First AC: 2018-06-06 Latest Modification: 2018-06-06
Note: 利用贪心的思想可以证明最优解一定让每一段尽可能靠前(更可能因为前一年的休假而减少开支)
那么关键就在于如何分段与分几段,枚举所有可能计算取最小即可
#include<bits/stdc++.h>
using namespace std;
int w,tmp,cnt;
int a[];
string rst;
int i;
int main()
{
for(i=;i<;++i)cin>>w,a[w]=;
for(i=;i<;++i)a[i]+=a[i-];
cnt=*,rst="1 2 3 4";
if((tmp=*+*(-+a[]))<cnt)
cnt=tmp,rst="1 2 3 5";
if((tmp=*+*(-+a[]))<cnt)
cnt=tmp,rst="1 2 4 5";
if((tmp=*+*(-+a[]))<cnt)
cnt=tmp,rst="1 3 4 5";
if((tmp=*+*(-+a[])+*(-+a[]))<cnt)
cnt=tmp,rst="1 2 4 6";
if((tmp=*+*(-+a[])+*(-+a[]))<cnt)
cnt=tmp,rst="1 3 4 6";
if((tmp=*+*(-+a[])+*(-+a[]))<cnt)
cnt=tmp,rst="1 3 5 6";
if((tmp=*+*(+a[])+*(+a[])+*(+a[]))<cnt)
cnt=tmp,rst="1 3 5 7";
cout<<rst;
return ;
}
E2907 Hometask
First AC: 2018-04-17 Latest Modification: 2018-04-17
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
bool pri(ll n)
{
ll m=(ll)sqrt(n)+;
if(!(n&))return ;
for(ll i=;i<m;i+=)if(!(n%i))return ;
return ;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n){
if(n<){cout<<"YES\n";continue;}
if(n<){cout<<"NO\n";continue;}
pri(n)? cout<<"NO\n":cout<<"YES\n";
}
return ;
}
E2909 Joke
First AC: 2018-04-19 Latest Modification: 2018-04-19
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string s,t;
ll lens,lent,cnts,cntt,a,b;
ll i;
int main()
{
while(cin>>s>>t){
lens=s.length();
lent=t.length();
cnts=cntt=a=b=;
for(i=;i<lens;++i){
if(s[i]=='.')cnts=lens-i-;
else a=*a+s[i]-'';
}
for(i=;i<lent;++i){
if(t[i]=='.')cntt=lent-i-;
else b=*b+t[i]-'';
}
if(cnts>cntt)b*=pow(,cnts-cntt);
else a*=pow(,cntt-cnts);
cout<<a/b<<endl;
}
return ;
}
E2912 放书
First AC: 2018-05-14 Latest Modification: 2018-05-14
#include<bits/stdc++.h>
using namespace std;
int T,n;
bool jdg[];
int a[];
void dfs(int x)
{
int i;
if(x==n+){
for(i=;i<=n;++i)cout<<a[i];
cout<<endl;
return;
}
for(i=;i<=n;++i){
if(!jdg[i]&&i!=x){
a[x]=i;
jdg[i]=;
dfs(x+);
jdg[i]=;
}
}
}
int main()
{
cin>>T;
while(T--){
cin>>n;
if(n<){
dfs();
}
else{
a[]=,a[]=;
for(int i=;i<=n;++i)a[i]=(i-)*(a[i-]+a[i-]);
cout<<a[n]<<endl;
}
}
return ;
}
E2913 A+B(I)
First AC: 2017-10-21 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main()
{
while(cin>>a>>b)cout<<a+b<<endl;
return ;
}
E2914 A+B(II)
First AC: 2017-10-21 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main()
{
cin>>a;
while(cin>>a>>b)cout<<a+b<<endl;
return ;
}
E2915 A+B(III)
First AC: 2017-10-21 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main()
{
while(cin>>a>>b,a||b)cout<<a+b<<endl;
return ;
}
E2918 K-th Number
First AC: 2018-04-13 Latest Modification: 2018-04-13
#include<bits/stdc++.h>
using namespace std;
int n,m,l,r,k;
struct data{
int num,item;
}a[];
int i;
bool cmp(data a,data b)
{
return a.num<b.num;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(i=;i<=n;++i)cin>>a[i].num,a[i].item=i;
sort(a+,a+n+,cmp);
while(m--){
cin>>l>>r>>k;
--l,++r;
if(r-l>*k){
for(i=;;++i)
if(a[i].item>l&&a[i].item<r&&!(--k))break;
cout<<a[i].num<<endl;
}
else{
k=r-l-k;
for(i=n;;--i)
if(a[i].item>l&&a[i].item<r&&!(--k))break;
cout<<a[i].num<<endl;
}
}
return ;
}
E2920 加密2
First AC: 2017-11-15 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
long long n;
int a[],i,s,cnt;
int main()
{
while(cin>>n){
s=cnt=;
for(i=;i>=;--i)a[i]=n%,n/=;
for(i=;i<;++i){
if(a[i]==)a[i]=a[i+];
else {if(a[i+]==)a[i]=;
else a[i]=;}
if(a[i+]==)a[i+]=;
else a[i+]=;
}
for(i=;i<;i+=){
s=*a[i]+*a[i+]+*a[i+]+a[i+];
if(cnt==&&s!=)++cnt;
if(cnt){
if(s<)cout<<s;
else cout<<(char)(s-+'A');
}
}
for(i=;i<;i+=){
s=*a[i]+*a[i+]+*a[i+]+a[i+];
if(cnt==&&s!=)++cnt;
if(cnt){
if(s<)cout<<s;
else cout<<(char)(s-+'A');
}
}
cout<<endl;
}
return ;
}
E2936 查找病毒
First AC: 2017-11-01 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,lens,lent,del,cnt,num;
string s,t;
int i,j,k;
int main()
{
cin>>T;
getchar();
for(i=;i<T;++i){
getline(cin,s);getline(cin,t);
lens=s.length(),lent=t.length(),num=;
if(lens<lent){cout<<"No\n";continue;}
if(lens==lent==){
if(s[]==t[]){cout<<"Yes\n";continue;}
cout<<"No\n";continue;
}
del=lens-lent+;
for(j=;j<del;++j){
if(s[j]==t[]){
if(lent==){cout<<"Yes\n";num++;break;}
for(k=;k<lent;++k){
cnt=;
if(s[j+k]!=t[k]){cnt++;break;}
}
if(cnt==){cout<<"Yes\n";num++;break;}
}
}
if(num==)cout<<"No\n";
}
return ;
}
E2937 ZZZ vs MJQ 1
First AC: 2018-06-04 Latest Modification: 2018-06-04
#include<bits/stdc++.h>
using namespace std;
int T,a,b,c,d,e,f,g,h;
int main()
{
cin>>T;
while(T--){
cin>>a>>b>>c>>d>>e>>f>>g>>h;
if(abs(a+c-e-g)<c-a+g-e&&abs(b+d-f-h)<b-d+f-h)cout<<"Yes\n";
else cout<<"No\n";
}
return ;
}
E2938 ZZZ vs MJQ 2
First AC: 2018-01-03 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,n,i;
double p,rst;
int c(int n,int m)
{
int rst=;
for(int i=;i<=m;++i)rst*=n+-i,rst/=i;
return rst;
}
int main()
{
cin>>T;
while(T--){
cin>>n>>p;
rst=;
for(i=n/+;i<=n;++i)rst+=c(n,i)*pow(p,i)*pow(-p,n-i);
printf("%.3f\n",rst);
}
return ;
}
E2939 BG攒RP
First AC: 2018-01-19 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,n,ls;
int R,P,B,G;
string s;
int i,j;
struct data{
string t;
int lt;
long long r,p,b,g,RP;
}a[];
long long rp(long long r,long long p,long long b,long long g)
{
return (r*r%*r*r%+p*p%*p%+b*b%+g)%
*(r+p*p%+b*b%*b%+g*g%*g*g%)%;
}
bool cmp(data a,data b)
{
if(a.RP!=b.RP)return a.RP>b.RP;
int lt=a.lt<b.lt? a.lt:b.lt;
for(int i=;i<lt;++i)if(a.t[i]!=b.t[i])return a.t[i]<b.t[i];
return a.lt<b.lt;
}
int main()
{
cin>>T;
while(T--){
cin>>s>>n;
ls=s.length();
R=P=B=G=;
for(i=;i<ls;++i){
if(s[i]=='R')++R;
else if(s[i]=='P')++P;
else if(s[i]=='B')++B;
else if(s[i]=='G')++G;
}
for(i=;i<n;++i){
cin>>a[i].t;
a[i].lt=a[i].t.length();
a[i].r=R,a[i].p=P,a[i].b=B,a[i].g=G;
for(j=;j<a[i].lt;++j){
if(a[i].t[j]=='R')++a[i].r;
else if(a[i].t[j]=='P')++a[i].p;
else if(a[i].t[j]=='B')++a[i].b;
else if(a[i].t[j]=='G')++a[i].g;
}
a[i].RP=rp(a[i].r,a[i].p,a[i].b,a[i].g);
}
sort(a,a+n,cmp);
cout<<a[].t<<endl;
}
return ;
}
E2940 凤凰古城
First AC: 2018-01-06 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,n,k,res,tmp,cnt;
int a[];
int i,j;
int main()
{
cin>>T;
while(T--){
cin>>n>>k;
for(i=;i<n;++i)cin>>a[i];
sort(a,a+n);
cnt=,res=n;
while(res){
tmp=k;
for(i=n-cnt-;;--i){
if(a[i]<=tmp)break;
}
++cnt,--res,tmp-=a[i],a[i]=k+;
for(j=i-;j>=;--j){
if(a[j]<=tmp){
--res,a[j]=k+;
break;
}
}
}
cout<<cnt<<endl;
}
return ;
}
E2941 在线投票系统
First AC: 2018-01-07 Latest Modification: 2018-01-07
Note: 观察得到每个g[i][j]是其余所有奇数个g[i][j]亦或的结果
因此所有g[i][j]亦或的结果即为a
#include<iostream>
using namespace std;
int T,n,a,tmp,rst;
int g[][];
int i,j,k;
int main()
{
cin>>T;
while(T--){
cin>>n;
for(a=i=;i<n;++i)cin>>g[i][]>>g[i][],a^=g[i][],a^=g[i][];
for(i=;i<n;++i)for(j=;j<;++j){
tmp=,rst=;
for(k=;k<;++k){
if(tmp&g[i][j]){
if(!(tmp&a))rst|=tmp;
}
else rst|=tmp&a;
tmp<<=;
}
cout<<rst;
j? cout<<endl:cout<<' ';
}
}
return ;
}
E2944 四元一次方程
First AC: 2017-11-02 Latest Modification: 2017-11-02
#include<iostream>
using namespace std;
int T,n,maxp,p,q,sum;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
sum=;
cin>>n;
maxp=n/+;
for(j=;j<maxp;++j){
q=n-*j;
sum+=(j/+)*(q/+);
}
cout<<sum<<endl;
}
return ;
}
E2945 Search Paper Pages
First AC: 2017-12-30 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T;
struct data{
string s;
int rst,num;
}a[];
int i;
bool cmp(data a,data b)
{
if(a.rst^b.rst)return a.rst>b.rst;
return a.num<b.num;
}
int main()
{
cin>>T;
for(i=;i<T;++i)cin>>a[i].s>>a[i].rst,a[i].num=i;
sort(a,a+T,cmp);
cout<<a[].s<<endl;
for(i=;i<T;++i){
if(a[i].rst!=a[i-].rst)break;
cout<<a[i].s<<endl;
}
return ;
}
E2946 整数的质因子分解
First AC: 2017-12-02 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,a[],n,cnt,i,j;
int main()
{
for(i=;i<;++i)
if(a[i]==)
for(j=*i;j<;j+=i)
++a[j];
cin>>T;
while(T--){
cin>>n;
for(i=;n!=;++i){
if(a[i]==){
cnt=;
while(n%i==)++cnt,n/=i;
if(cnt)cout<<'('<<i<<','<<cnt<<')';
}
}
cout<<endl;
}
return ;
}
E2947 行数据的排序
First AC: 2017-12-29 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt;
struct data{
int num;
long long f[];
}a[];
int i,j,k;
bool cmp(data a,data b)
{
int len=a.num<b.num? a.num:b.num;
for(int i=;i<len;++i)if(a.f[i]!=b.f[i])return a.f[i]>b.f[i];
return a.num>b.num;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cnt=;
while(){
cin>>a[j].f[cnt];
if(a[j].f[cnt]==-)break;
++cnt;
}
a[j].num=cnt;
}
sort(a,a+n,cmp);
for(j=;j<n;++j){
cout<<a[j].f[];
for(k=;k<a[j].num;++k)cout<<' '<<a[j].f[k];
cout<<endl;
}
}
return ;
}
E2948 Phone Number
First AC: 2017-12-20 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,cnt,len;
long long base;
string s;
int i,j;
struct data{
long long n;
}a[];
bool cmp(data a,data b)
{
return a.n<b.n;
}
long long change(char c)
{
switch(c){
case 'A':case 'B':case 'C':return ;
case 'D':case 'E':case 'F':return ;
case 'G':case 'H':case 'I':return ;
case 'J':case 'K':case 'L':return ;
case 'M':case 'N':case 'O':return ;
case 'P':case 'Q':case 'R':case 'S':return ;
case 'T':case 'U':case 'V':return ;
case 'W':case 'X':case 'Y':case 'Z':return ;
default:return c-'';
}
}
void print(data a,int cnt)
{
cout<<a.n/<<'-'<<a.n%/<<a.n%/
<<a.n%/<<a.n%<<' '<<cnt<<endl;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
for(j=len-;j>=;--j){
if(s[j]!='-'){
if(!base)a[i].n=change(s[j]),base=;
else a[i].n+=base*change(s[j]),base*=;
}
}
base=;
}
sort(a,a+T,cmp);
for(cnt=i=;i<T;++i){
if(a[i].n^a[i-].n)print(a[i-],cnt),cnt=;
else ++cnt;
}
print(a[T-],cnt);
return ;
}
E2949 求x的y次方
First AC: 2017-10-16 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
double x,y;
int main()
{
cin>>x>>y;
printf("%.3f\n",pow(x,y));
return ;
}
E2950 求整数部分
First AC: 2017-10-16 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
char c;
int main()
{
while((c=getchar())!='.')cout<<c;
cout<<endl;
return ;
}
E2951 循环移位
First AC: 2017-11-17 Latest Modification: 2017-11-17
#include<iostream>
using namespace std;
long long a,n,s,m=;
int x[];
int i,j;
int main()
{
cin>>a>>n;
for(;i<;)x[i++]=a%,a/=;
for(i=n;i<;s+=m*x[i++],m*=);
for(i=;i<n;s+=m*x[i++],m*=);
cout<<s<<endl;
return ;
}
E2952 日期计算
First AC: 2017-10-16 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int y,m,d;
int a[]={,,,,,,,,,,,,};
int b[]={,,,,,,,,,,,,};
int main()
{
cin>>y>>m>>d;
if((y%==&&y%!=)||y%==)cout<<b[m]+d;
else cout<<a[m]+d;
return ;
}
E2953 字母含义
First AC: 2017-10-16 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
char c;
int main()
{
c=getchar();
if(c=='E')cout<<"Excellent\n";
else if(c=='C')cout<<"Cheer\n";
else if(c=='N')cout<<"Nice\n";
else if(c=='U')cout<<"Ultimate\n";
else if(c=='A')cout<<"Accept\n";
else cout<<"Error\n";
return ;
}
E2954 点名
First AC: 2017-10-16 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,m,n,i,j,cnt;
char c;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>m>>n;
for(j=;j<m*n;j++){
cin>>c;
if(c=='.')cnt++;
}
cout<<"case #"<<i<<":\n"<<cnt<<endl;
cnt=;
}
return ;
}
E2955 Familial Numbers
First AC: 2017-10-16 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,m,n,i,j,mini,cnt;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>m>>n;
if(m>n)mini=n;else mini=m;
for(j=;j<=mini;j++){
if(m%j==&&n%j==){
cnt=j;
break;
}
}
if(cnt>)cout<<"case #"<<i<<":\nYes "<<j<<endl;
else cout<<"case #"<<i<<":\nNo"<<endl;
cnt=;
}
return ;
}
E2956 Maya历日期的排序
First AC: 2017-11-26 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,N,len;
string d,m;
string mon[]={"pop","no","zip","zotz","tzec","xul","yoxkin",
"mol","chen","yax","zac","ceh","mac","kankin",\
"muan","pax","koyab","cumhu","uayet"};
int i,j;
struct data{
int d,m,y;
}a[];
bool cmp(data a,data b)
{
if(a.y!=b.y)return a.y<b.y;
else if(a.m!=b.m)return a.m<b.m;
else return a.d<b.d;
}
int main()
{
cin>>T;
for(j=;j<T;++j){
cin>>N;
for(i=;i<N;++i){
cin>>d>>m>>a[i].y;
len=d.length();
if(len==)a[i].d=d[]-'';
else a[i].d=*d[]+d[]-*'';
if(m=="pop")a[i].m=;
else if(m=="no")a[i].m=;
else if(m=="zip")a[i].m=;
else if(m=="zotz")a[i].m=;
else if(m=="tzec")a[i].m=;
else if(m=="xul")a[i].m=;
else if(m=="yoxkin")a[i].m=;
else if(m=="mol")a[i].m=;
else if(m=="chen")a[i].m=;
else if(m=="yax")a[i].m=;
else if(m=="zac")a[i].m=;
else if(m=="ceh")a[i].m=;
else if(m=="mac")a[i].m=;
else if(m=="kankin")a[i].m=;
else if(m=="muan")a[i].m=;
else if(m=="pax")a[i].m=;
else if(m=="koyab")a[i].m=;
else if(m=="cumhu")a[i].m=;
else a[i].m=;
}
sort(a,a+N,cmp);
cout<<"case #"<<j<<":\n";
for(i=;i<N;++i)
cout<<a[i].d<<". "<<mon[a[i].m]
<<' '<<a[i].y<<endl;
}
return ;
}
E2957 统计不同的最简真分数的个数
First AC: 2017-11-30 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt,u,v,w;
int a[];
long long num,i,j,k;
struct data{int q,p;}b[];
bool cmp(data a,data b)
{
if(a.p==b.p)return a.q<b.q;
return a.p<b.p;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=cnt=num=;j<n;++j)cin>>a[j];
sort(a,a+n);
for(j=;j<n;++j)for(k=j+;k<n;++k){
if(a[j]==a[k])continue;
u=a[k],v=a[j];
do{w=u%v,u=v,v=w;}while(w);
if(u!=)continue;
b[num].q=a[j],b[num++].p=a[k];
}
sort(b,b+num,cmp);
for(j=;j<num;++j)if(b[j].p==b[j-].p&&b[j].q==b[j-].q)++cnt;
cout<<"case #"<<i<<":\n"<<num-cnt<<endl;
}
return ;
}
E2958 求上升子序列和的最大值
First AC: 2017-12-13 Latest Modification: 2018-02-12
#include<bits/stdc++.h>
using namespace std;
int T,n,rst;
int a[],dp[];
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j)cin>>a[j];
memset(dp,,sizeof(dp)),rst=;
for(j=;j<n;++j){
for(k=;k<j;++k){
if(a[k]<a[j]&&dp[k]+a[j]>dp[j])dp[j]=dp[k]+a[j];
}
if(dp[j]<a[j])dp[j]=a[j];
if(dp[j]>rst)rst=dp[j];
}
cout<<"case #"<<i<<":\n"<<rst<<endl;
}
return ;
}
E2959 正则表达式简化版
First AC: 2018-06-06 Latest Modification: 2018-06-06
from re import match
flag = 1
pat = ''
while True:
try:
s = input()
if s =='':
flag=1
elif flag == 1:
pat = s
flag = 0
else:
try:
m = match(pat,s)
if m:
print('Regular Expression is Fun!')
else :
print('Boring String Matching...')
except:
print('Boring String Matching...')
except EOFError:
break
E2965 纪念品分组
First AC: 2018-05-15 Latest Modification: 2018-05-15
#include<bits/stdc++.h>
using namespace std;
int w,n,p,rst,tmp;
int a[];
int i,j,k;
int main()
{
ios::sync_with_stdio(false);
while(cin>>w){
cin>>n;
while(n--)cin>>p,++a[p];
rst=;
while(w>){
rst+=a[w];
a[w--]=;
i=,j=w;
while(i<j){
tmp=min(a[i],a[j]);
rst+=tmp;
a[i]-=tmp,a[j]-=tmp;
++i,--j;
}
if(i==j){
rst+=a[i]/;
a[i]%=;
}
}
rst+=(a[]+)/;
a[]=;
cout<<rst<<endl;
}
return ;
}
E2966 二进制和十六进制
First AC: 2017-10-30 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
long T,n,r,cnt,i,j,a[],m;
char c;
int main()
{
cin>>T;
for(j=;j<T;++j){
cin>>n;
cout<<"case #"<<j<<":\n";
m=n;
r=;
if(n==)cout<<"";
else{
for(i=;i>;--i)a[i]=n%r,n/=r;
for(i=;;++i)if(a[i]!=){cnt=i;break;}
for(i=cnt;i<;++i)
if(a[i]<)cout<<a[i];
else cout<<(char)(a[i]-+'A');
}
printf(" %X\n",m);
}
return ;
}
E2967 公共元素
First AC: 2017-12-08 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,cnt,num,flag;
int cmp[];
int i,j,k;
struct data{
int len;
int h[],f[];
}a[];
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>a[].len>>a[].len>>a[].len;
for(j=;j<;++j)for(k=;k<a[j].len;++k)cin>>a[j].h[k];
for(j=;j<;++j)sort(a[j].h,a[j].h+a[j].len);
for(j=;j<;++j){
a[j].f[]=a[j].h[];
for(k=cnt=;k<a[j].len;++k)
if(a[j].h[k]!=a[j].h[k-])
a[j].f[cnt++]=a[j].h[k];
a[j].len=cnt;
}
for(flag=num=cnt=j=;j<a[].len;++j)cmp[cnt++]=a[].f[j];
for(j=;j<a[].len;++j)cmp[cnt++]=a[].f[j];
for(j=;j<a[].len;++j)cmp[cnt++]=a[].f[j];
sort(cmp,cmp+cnt);
for(j=;j<cnt;++j)if(cmp[j]==cmp[j-]&&cmp[j]==cmp[j-]){
if(num)cout<<' '<<cmp[j];
else cout<<"case #"<<i<<":\n"<<cmp[j],flag=num=;
}
if(!flag)cout<<"case #"<<i<<":\n"<<-;
cout<<endl;
}
return ;
}
E2968 付钱
First AC: 2017-10-16 Latest Modification: 2018-03-22
#include<bits/stdc++.h>
using namespace std;
int n,i;
int main()
{
cin>>n;
while(cin>>n){
cout<<"case #"<<i<<":\n"<<n/<<" "<<n%/<<" "<<n%/
<<" "<<n%%/<<" "<<n%/<<" "<<n%<<endl;
++i;
}
return ;
}
E2969 分析字符
First AC: 2017-11-06 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,i,j,A,a,s,n,o,len;
string c;
int main()
{
cin>>T;
getchar();
for(i=;i<T;i++){
getline(cin,c);
len=c.length();
for(j=;j<len;++j){
if(c[j]>='A'&&c[j]<='Z'){A++;continue;}
if(c[j]>='a'&&c[j]<='z'){a++;continue;}
if(c[j]==' '){s++;continue;}
if(c[j]>=''&&c[j]<=''){n++;continue;}
o++;
}
cout<<"case #"<<i<<":\n"<<A<<" "<<a
<<" "<<s<<" "<<n<<" "<<o<<endl;
A=a=s=n=o=;
}
return ;
}
E2970 农场
First AC: 2017-10-17 Latest Modification: 2018-03-22
#include<bits/stdc++.h>
using namespace std;
long i,m,n;
int main()
{
cin>>n;
while(cin>>n>>m){
if(m>=*n&&m<=*n&&m%==)
cout<<"case #"<<i<<":\n"<<(*n-m)/<<" "<<(m-*n)/<<endl;
else cout<<"case #"<<i<<":\nImpossible\n";
++i;
}
return ;
}
E2971 3的倍数
First AC: 2017-10-17 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,C,W,i;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>C>>W;
cout<<"case #"<<i<<":\n"<<*pow(,C-)<<endl;
}
return ;
}
E2972 分数的精确值
First AC: 2017-11-08 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,n,up,dn;
int r[];
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>up>>dn;
cout<<"case #"<<i<<":\n"<<up/dn<<'.';
memset(r,-,sizeof(r));
r[up%dn]=;
for(j=;;++j){
cout<<(up*=)/dn;
if(!(up%dn)){
up%=dn;
cout<<endl;
break;
}
if(r[up%dn]<){
up%=dn;
r[up]=j;
}
else{
cout<<endl<<r[up%dn]+<<'-'<<j<<endl;
break;
}
}
}
return ;
}
E2973 卡片游戏
First AC: 2018-02-12 Latest Modification: 2018-02-12
#include<bits/stdc++.h>
using namespace std;
int T,n;
long m,tmp;
long a[];
bool f[];
int i,j,k,r;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n>>m;
memset(f,,sizeof(f));
for(j=;j<n;++j)cin>>a[j];
for(j=;j<n;++j)for(k=;k<j;++k){
tmp=a[j]+a[k];
for(r=;r<k;++r)f[tmp+a[r]]=;
}
for(j=m;j>=;--j)if(f[j])break;
cout<<"case #"<<i<<":\n"<<j<<endl;
}
return ;
}
E2974 统计单词个数
First AC: 2017-12-03 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
string a;
int T,len,num;
int i,j,k;
int main()
{
cin>>T;getchar();
for(k=;k<T;++k){
getline(cin,a);
len=a.length(),num=;
for(i=;i<len;++i)if(a[i]>&&a[i]<)a[i]-=;
if(a[]=='A'&&a[]==)a[]=;
else if(a[]=='A'&&a[]=='N'&&a[]==)
a[]=a[]=;
else if(a[]=='O'&&a[]=='F'&&a[]==)
a[]=a[]=;
else if(a[]=='T'&&a[]=='H'&&a[]=='E'&&a[]==)
a[]=a[]=a[]=;
else if(a[]=='F'&&a[]=='O'&&a[]=='R'&&a[]==)
a[]=a[]=a[]=;
else if(a[]=='A'&&a[]=='N'&&a[]=='D'&&a[]==)
a[]=a[]=a[]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='A'&&a[i]==)
a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='A'&&a[i-]=='N'&&a[i]==)
a[i-]=a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='O'&&a[i-]=='F'&&a[i]==)
a[i-]=a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='T'&&a[i-]=='H'
&&a[i-]=='E'&&a[i]==)
a[i-]=a[i-]=a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='F'&&a[i-]=='O'
&&a[i-]=='R'&&a[i]==)
a[i-]=a[i-]=a[i-]=;
for(i=;i<len;++i)
if(a[i-]==&&a[i-]=='A'&&a[i-]=='N'
&&a[i-]=='D'&&a[i]==)
a[i-]=a[i-]=a[i-]=;
if(a[]!=)++num;
for(i=;i<len;++i)if(a[i-]==&&a[i]!=)++num;
cout<<"case #"<<k<<":\n"<<num<<endl;
}
return ;
}
E2975 排序
First AC: 2017-10-17 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
int T,N,n,m,cnt;
int i,j,k;
int a[];
int main()
{
cin>>T;
for(i=;i<T;i++){
cout<<"case #"<<i<<":\n";
cin>>N;
for(j=;j<N;j++){cin>>n;a[n]=;}
for(j=;j<;j++){
for(k=;k<;k++){
m=j+*k;
if(a[m]==){
cnt++;
if(cnt==)cout<<m;
else cout<<" "<<m;
a[m]=;
}
}
}
cnt=;
cout<<"\n";
}
return ;
}
E2976 Light
First AC: 2017-10-17 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,p,q,c,d,e,f,g,h,s;
int i,j,k;
int a[][],b[][];
int main()
{
cin>>T;
for(i=;i<T;i++){
cout<<"case #"<<i<<":\n";
cin>>p>>q>>c>>d>>e>>f>>g>>h>>s;
a[][]=p;a[][]=q;a[][]=c;
a[][]=d;a[][]=e;a[][]=f;
a[][]=g;a[][]=h;a[][]=s;
for(j=;j<;j++)for(k=;k<;k++){
if(!((a[j][k]+a[j][k-]+a[j][k+]+a[j-][k]+a[j+][k])%))
b[j][k]=;
else b[j][k]=;
}
cout<<b[][]<<" "<<b[][]<<" "<<b[][]<<endl
<<b[][]<<" "<<b[][]<<" "<<b[][]<<endl
<<b[][]<<" "<<b[][]<<" "<<b[][]<<endl;
}
return ;
}
E2977 成绩排序
First AC: 2017-11-20 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
struct data{
long long num,score;
}stu[];
long long T,N,M,G,S,s,cnt,tmp;
long long a[];
int i,j,k;
bool cmp(data a,data b)
{
if(a.score==b.score)return a.num<b.num;
return a.score>b.score;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cnt=;
cin>>N>>M>>G;
for(j=;j<=M;++j)cin>>a[j];
for(j=;j<N;++j){
cin>>stu[j].num>>S;
stu[j].score=;
while(S--)cin>>s,stu[j].score+=a[s];
if(stu[j].score>=G)++cnt;
}
cout<<"case #"<<i<<":\n"<<cnt<<endl;
sort(stu,stu+N,cmp);
for(j=;j<cnt;++j){
if(stu[j].num==)cout<<"";
else for(tmp=;;tmp/=)
if(stu[j].num/tmp==)cout<<'';
else break;
cout<<stu[j].num<<' '<<stu[j].score<<endl;
}
}
return ;
}
E2978 计算数字之和
First AC: 2017-11-17 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
string s;
int T,len,a,b,c,d,sum;
long cnt,tmp,nums;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length(),sum=;
for(j=;j<len;++j)sum+=s[j]-'';
a=s[len-]-'',b=s[len-]-'',c=s[len-]-'';
tmp=,nums=;
for(j=len-;j>=;--j)nums+=tmp*(s[j]-''),tmp*=;
nums-=len;
for(j=;j<nums;++j){
d=(a+b)%*c%;
if(d==)break;
sum+=d,a=b,b=c,c=d;
}
cout<<"case #"<<i<<":\n"<<sum<<endl;
}
return ;
}
E2979 图形
First AC: 2017-10-17 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int a[][];
int i,j,k;
int T,n;
int main()
{
for(i=;i<;i+=)for(j=;j<=i;j++)a[i][j]=(i-)*(i-)+j;
for(i=;i<;i+=)for(j=;j<=i;j++)a[i][j]=i*i-j+;
for(j=;j>;j-=)for(i=j-;i>;i--)a[i][j]=a[i+][j]-;
for(j=;j>;j-=)for(i=j-;i>;i--)a[i][j]=a[i+][j]+;
cin>>T;
for(k=;k<T;k++){
cin>>n;
cout<<"case #"<<k<<":\n";
for(i=;i<=n;i++){
cout<<a[i][];
for(j=;j<=n;j++)cout<<" "<<a[i][j];
cout<<endl;
}
}
return ;
}
E2980 小数转化分数
First AC: 2017-12-10 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,len,p,l,r;
long long tmp,base,up,down;
string s;
int i,j,k;
void print(long long up,long long down)
{
long long m=up,n=down,q=;
while(q)q=m%n,m=n,n=q;
cout<<up/m<<'/'<<down/m<<endl;
}
int main()
{
cin>>T;
getchar();
for(i=;i<T;++i){
getline(cin,s);
len=s.length();
l=r=up=down=;
cout<<"case #"<<i<<":\n";
for(j=;;++j)if(s[j]=='.'){p=j;break;}
for(k=j+;k<len;++k){
if(s[k]=='['){
l=k,r=len-;
break;
}
}
if(l-r){
down=(long long)pow(,r-l-)-;
for(base=,j=r-;j>l;--j)
up+=base*(s[j]-''),base*=;
for(tmp=,base=,j=l-;j>p;--j)
tmp+=base*(s[j]-''),base*=;
up+=down*tmp;
down*=(long long)pow(,l-p-);
}
else{
down=(long long)pow(,len-p-);
for(base=,j=len-;j>p;--j)
up+=base*(s[j]-''),base*=;
}
for(tmp=,base=,j=p-;j>=;--j)
tmp+=base*(s[j]-''),base*=;
up+=tmp*down;
print(up,down);
}
return ;
}
E2981 零食
First AC: 2017-10-29 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,n,p,a[],i,j,low=;
string s;
int main()
{
cin>>T;
for(i=;i<T;++i){
memset(a,,sizeof(a));
cout<<"case #"<<i<<":\n";
cin>>n;
for(j=;j<n;++j){
cin>>s;
if(s=="B"){
cin>>p;
a[p]++;
if(p<low)low=p;
}
if(s=="G"){
while(a[low]==)++low;
if(a[low]==)cout<<low<<endl,a[low++]--;
else cout<<low<<endl,a[low]--;
}
}
}
return ;
}
E2982 三元斐波那契奇数列
First AC: 2017-10-05 Latest Modification: 2018-04-01
#include<bits/stdc++.h>
using namespace std;
int T,n,temp,cnt;
long a[];
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n,cnt=;
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j){
cin>>a[j];
if(a[j]==-)temp=j;
}
if(temp==)a[]=a[]-a[]-a[];
else if(temp==)a[]=a[]-a[]-a[];
else if(temp==)a[]=a[]-a[]-a[];
else a[temp]=a[temp-]+a[temp-]+a[temp-];
for(j=;j<n;++j)if(a[j]!=a[j-]+a[j-]+a[j-]){--cnt;break;}
if(cnt==&&a[temp]>)cout<<a[temp]<<endl;
else cout<<"-1\n";
}
return ;
}
E2983 蛇形图案
First AC: 2017-10-20 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,n;
int a[][],r,q,e;
int i,j,k;
int main()
{
ios::sync_with_stdio(false);
cin>>T;
for(i=;i<T;i++){
cout<<"case #"<<i<<":\n";
cin>>n;
for(j=n;j>;j-=){
for(k=;k<j;++k)a[r][++q]=++e;
for(k=;k<j;++k)a[++r][q]=++e;
for(k=;k<j;++k)a[r][--q]=++e;
for(k=;k<j;++k)a[--r][q]=++e;
}
if(n%!=)a[n/][n/+]=n*n;
r=q=e=;
for(j=;j<n;++j){
cout<<a[j][];
a[j][]=;
for(k=;k<=n;++k)cout<<" "<<a[j][k];
cout<<endl;
}
}
return ;
}
E2984 数学手稿
First AC: 2017-11-22 Latest Modification: 2018-03-22
#include<bits/stdc++.h>
using namespace std;
long T,N,K,m,minm,maxm;
int i;
int main()
{
for(cin>>T;i<T;++i){
cin>>N>>K;
cout<<"case #"<<i<<":\n";
K>*N? minm=(K-*N)/:minm=;
K>*N? maxm=K/+:maxm=(K-N)/+;
if(minm+>maxm){cout<<"-1\n";continue;}
for(m=minm;m<maxm;++m){
cout<<m<<' '<<K-N-*m<<' '<<*N-K+m<<endl;
}
}
return ;
}
E2985 圆和正方形
First AC: 2017-10-17 Latest Modification: 2018-03-22
#include<bits/stdc++.h>
using namespace std;
int T,N,K,i;
int main()
{
cin>>T;
for(i=;i<T;i++){
cout<<"case #"<<i<<":\n";
cin>>N>>K;
printf("%.6lf\n",N*N*(-1.0/pow(,K))*0.429203673205);
}
return ;
}
E2986 求整数部分计算2的n次方
First AC: 2017-10-17 Latest Modification: 2018-04-11
#include<bits/stdc++.h>
using namespace std;
int T,n,i;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>n;
cout<<"case #"<<i<<":\n"<<(long)pow(,n)<<endl;
}
return ;
}
E2987 判断IP
First AC: 2017-10-17 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,i,a,b,c,d,cnt;
int main()
{
cin>>T;
for(i=;i<T;i++){
scanf("%d.%d.%d.%d",&a,&b,&c,&d);
if(a<||a>)
cout<<"case #"<<i<<":\nNo 0 "<<a<<endl,cnt++;
if(b<||b>)
cout<<"case #"<<i<<":\nNo 1 "<<b<<endl,cnt++;
if(c<||c>)
cout<<"case #"<<i<<":\nNo 2 "<<c<<endl,cnt++;
if(d<||d>)
cout<<"case #"<<i<<":\nNo 3 "<<d<<endl,cnt++;
if(!cnt)
cout<<"case #"<<i<<":\nYes\n";
cnt=;
}
return ;
}
E2988 密码产生器
First AC: 2017-12-01 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,len;
long long tmp;
string s;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
cout<<"case #"<<i<<":\n";
for(j=;j<;++j){
for(k=tmp=;j+*k<len;++k)tmp+=s[j+*k];
cout<<tmp%;
}
cout<<endl;
}
return ;
}
E2989 字符串重排
First AC: 2017-12-20 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,len,N,B,A;
string s;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
cout<<"case #"<<i<<":\n";
for(N=B=A=j=;j<len;++j){
if(s[j]=='N')++N;
else if(s[j]=='B')++B;
else ++A;
}
while(N||B||A){
if(N)cout<<'N',--N;
if(B)cout<<'B',--B;
if(A)cout<<'A',--A;
}
cout<<endl;
}
return ;
}
E2990 文献排序
First AC: 2017-12-05 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
int T,n,tmp;
int i,j;
struct data{
string s,t;
int len;
}a[];
bool cmp(data a,data b)
{
if(a.len<b.len)tmp=a.len;
else tmp=b.len;
for(int i=;i<tmp;++i){
if(a.t[i]>='A'&&a.t[i]<='Z')a.t[i]+=;
if(b.t[i]>='A'&&b.t[i]<='Z')b.t[i]+=;
if(a.t[i]-b.t[i])return a.t[i]<b.t[i];
}
return a.len<b.len;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n,getchar();
for(j=;j<n;++j){
getline(cin,a[j].s);
a[j].t=a[j].s,a[j].len=a[j].s.length();
}
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j)cout<<a[j].s<<endl;
}
return ;
}
E2991 鞍点
First AC: 2017-10-20 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,i,j,k,r,m,cnt;
int a[][],b[][];
int main()
{
ios::sync_with_stdio(false);
cin>>T;
for(i=;i<T;++i){
cout<<"case #"<<i<<":\n";
for(j=;j<;++j)
for(k=;k<;++k){
cin>>m;
a[j][k]=m;
for(r=;r<k;++r)
if(a[j][k]<a[j][r])b[j][k]++;
else if(a[j][k]>a[j][r])b[j][r]++;
}
for(j=;j<;++j)
for(k=;k<;++k)
for(r=k+;r<;++r)
if(a[k][j]<a[r][j])b[r][j]++;
else if(a[k][j]>a[r][j])b[k][j]++;
for(j=;j<;++j)
for(k=;k<;++k)
if(b[j][k]==){
cout<<j<<" "<<k<<endl;
cnt++;
}
else b[j][k]=;
if(cnt==)cout<<"-1 -1\n";
cnt=;
}
return ;
}
E2992 卡片
First AC: 2017-10-31 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt,num;
int a[];
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cout<<"case #"<<i<<":\n";
cin>>n;
memset(a,,sizeof(a));
a[]=;
for(j=;;){
if(j==n+)j=;
if(a[j]==)j++;
else j++,num++;
if(num==)num=,a[j-]=,cnt++;
if(cnt==n-){cnt=;break;}
}
for(j=;j<=n;++j)if(a[j]!=)cout<<j<<endl;
}
return ;
}
E2993 替换
First AC: 2017-10-31 Latest Modification: 2018-03-22
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt,num;
int a[];
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j)cin>>a[j];
do{
cnt=;
for(j=;j<n-;++j){
num=;
for(k=j+;k<n;++k)if(a[j]>a[k])++num;
if(a[j]!=num)++cnt,a[j]=num;
}
a[n-]=;
}while(cnt!=);
cout<<a[];
for(j=;j<n;++j)cout<<" "<<a[j];
cout<<endl;
}
return ;
}
E2994 数组排序
First AC: 2017-12-19 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
int T,n,m;
int i,j,k;
struct data{
int num;
int cnt;
}a[];
bool cmp(data a,data b)
{
if(a.cnt==b.cnt)return a.num<b.num;
return a.cnt>b.cnt;
}
int main()
{
for(i=;i<;++i)a[i].num=i;
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cin>>m;
++a[m].cnt;
}
sort(a,a+,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<;++j){
for(k=;k<a[j].cnt;++k){
cout<<a[j].num;
--n? cout<<' ':cout<<'\n';
}
a[j].num=j,a[j].cnt=;
}
}
return ;
}
E2995 科学计数法
First AC: 2017-12-21 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,p,ls,cnt,point,flag,tmp;
string s;
int a[];
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s>>p;
ls=s.length();
memset(a,,sizeof(a));
cout<<"case #"<<i<<":\n";
for(point=-,j=cnt=;j<ls;++j){
if(s[j]=='.')point=cnt;
else a[++cnt]=s[j]-'';
}
if(point<)point=ls;
for(j=;j<;++j)if(a[j])break;
flag=a[p+j]>? :;
for(k=p+j;k<ls;++k)a[k]=;
tmp=p+j-;
while(flag){
if(^a[tmp])++a[tmp],--flag;
else a[tmp]=;
--tmp;
}
for(j=;j<;++j)if(a[j])break;
cout<<a[j];
if(p^)cout<<'.';
for(k=j+;k<j+p;++k)cout<<a[k];
if(point-j)cout<<'F'<<point-j;
cout<<endl;
}
return ;
}
E2996 计算a的n次方
First AC: 2017-10-15 Latest Modification: 2019-03-02
Note: 把数分为两段存储
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,a,b;
ll i;
void pow(ll a,ll b)
{
ll lft=,rgt=,mod=1e12;
while(b--){
rgt*=a;
lft=lft*a+rgt/mod;
rgt%=mod;
}
if(lft){
printf("%lld%013lld\n",lft,rgt);
}
else{
if(rgt)printf("%lld\n",rgt);
else printf("0\n");
}
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>a>>b;
cout<<"case #"<<i<<":\n";
pow(a,b);
}
return ;
}
E2997 泰波那契数列的前37项
First AC: 2017-09-28 Latest Modification: 2018-06-04
#include<bits/stdc++.h>
using namespace std;
long long tib[]={,,};
int T,n;
int i;
int main()
{
for(i=;i<;++i)tib[i]=tib[i-]+tib[i-]+tib[i-];
cin>>T;
for(i=;i<T;++i){
cin>>n;
cout<<"case #"<<i<<":\n"<<tib[n]<<endl;
}
return ;
}
E2998 质因数
First AC: 2017-10-17 Latest Modification: 2018-06-06
#include<bits/stdc++.h>
using namespace std;
int T,i,j,n;
int main()
{
cin>>T;
for(i=;i<T;i++){
cout<<"case #"<<i<<":\n";
cin>>n;
for(j=;;j++)if(n%j==){
cout<<n/j<<endl;
break;
}
}
return ;
}
E2999 计算多项式的系数
First AC: 2017-12-22 Latest Modification: 2018-04-01
#include<iostream>
#define p 10007
using namespace std;
typedef long long ll;
ll T,rst,i,j,a,b,k,n,m;
ll pow(ll a,ll b)
{
ll ans=,base=a%p;
while(b){
if(b&)ans*=base,ans%=p;
base*=base,base%=p;
b>>=;
}
return ans;
}
ll inv(ll n)
{
return pow(n,p-)%p;
}
ll comb(ll n,ll m)
{
if(m>n)return ;
ll ans=;
for(ll i=;i<=m;++i){
ll a=(n+i-m)%p,b=i%p;
ans*=a*pow(b,p-)%p;
ans%=p;
}
return ans;
}
ll Lucas(ll n,ll m)
{
if(m)return comb(n%p,m%p)*Lucas(n/p,m/p)%p;
return ;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>a>>b>>k>>n>>m;
//Lucas求Ckn
rst=Lucas(k,n);
//逆元模幂
rst*=pow(inv(a),n/(p-)),rst%=p;
rst*=pow(a,n%(p-)),rst%=p;
rst*=pow(inv(b),m/(p-)),rst%=p;
rst*=pow(b,m%(p-)),rst%=p;
cout<<"case #"<<i<<":\n"<<rst<<endl;
}
return ;
}
E3000 ROT13加密和解密
First AC: 2017-10-30 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
int T,i,j,len;
string s;
int main()
{
cin>>T;
getchar();
for(i=;i<T;i++){
cout<<"case #"<<i<<":\n";
getline(cin,s,'\n');
len=s.length();
for(j=;j<len;++j){
if(s[j]>='A'&&s[j]<'N')cout<<(char)(s[j]+);
else if(s[j]>'M'&&s[j]<='Z')cout<<(char)(s[j]-);
else if(s[j]>='a'&&s[j]<'n')cout<<(char)(s[j]+);
else if(s[j]>'m'&&s[j]<='z')cout<<(char)(s[j]-);
else cout<<s[j];
}
cout<<endl;
}
return ;
}
E3001 计算a的n次方的大整数
First AC: 2017-12-21 Latest Modification: 2018-04-11
n=input()
i=0
while True:
try:
a,b=input().split()
print('case #%d:'%i)
print((int)(a)**(int)(b))
i=i+1
except:
break
E3002 泰波那契数列的前74项
First AC: 2017-09-28 Latest Modification: 2018-06-04
#include<bits/stdc++.h>
using namespace std;
long long tib[]={,,};
int T,n;
int i;
int main()
{
for(i=;i<;++i)tib[i]=tib[i-]+tib[i-]+tib[i-];
cin>>T;
for(i=;i<T;++i){
cin>>n;
cout<<"case #"<<i<<":\n"<<tib[n]<<endl;
}
return ;
}
E3003 最小向量点积
First AC: 2017-12-14 Latest Modification: 2018-03-22
#include<bits/stdc++.h>
using namespace std;
int T,n;
long a[],b[],rst;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j)cin>>a[j];
for(j=;j<n;++j)cin>>b[j];
sort(a,a+n),sort(b,b+n);
for(j=rst=;j<n;++j)rst+=a[j]*b[n--j];
cout<<"case #"<<i<<":\n"<<rst<<endl;
}
return ;
}
E3004 生理高峰
First AC: 2017-10-22 Latest Modification: 2018-03-22
#include<bits/stdc++.h>
using namespace std;
int T,p,e,i,d,rst;
int j;
int main()
{
cin>>T;
for(j=;j<T;++j){
cin>>p>>e>>i>>d;
for(rst=d+;;++rst){
if((rst-p)%==&&(rst-e)%==&&(rst-i)%==){
printf("case #%d:\nthe next triple ",j);
printf("peak occurs in %d days.\n",rst-d);
break;
}
}
}
return ;
}
E3005 小型组合数
First AC: 2017-10-24 Latest Modification: 2018-04-11
#include<bits/stdc++.h>
using namespace std;
long long T,m,n,i,j,s=;
int main()
{
cin>>T;
for(i=;i<T;++i){
cout<<"case #"<<i<<":\n";
cin>>m>>n;
for(j=;j<=n;++j)s*=(m+-j),s/=j;
cout<<s<<endl;
s=;
}
return ;
}
E3007 A*B II
First AC: 2017-12-17 Latest Modification: 2017-12-17
n=(int)(input())
while n>0:
a,b=(input().split())
print((int)(a)*(int)(b))
n=n-1
E3008 Coins (I)
First AC: 2018-04-24 Latest Modification: 2018-06-06
Note: 因为每个硬币面额至少为1,所以K次的限制是多余的,那么就转化为完全背包
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M=1e8+;
ll T,n,K,m;
ll dp[];
ll i,j;
int main()
{
ios::sync_with_stdio(false);
cin>>T;
for(i=;i<=T;++i){
cin>>n>>K;
memset(dp,,sizeof(dp));
dp[]=;
while(n--){
cin>>m;
for(j=m;j<=K;++j)
(dp[j]+=dp[j-m])%=M;
}
cout<<"Case "<<i<<": "<<dp[K]<<endl;
}
return ;
}
E3009 Coins (II)
First AC: 2018-06-06 Latest Modification: 2018-06-06
Note: 多重背包
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M=1e8+;
ll T,n,K,m;
ll a[],dp[];
ll i,j,k,l;
int main()
{
ios::sync_with_stdio(false);
cin>>T;
for(i=;i<=T;++i){
cin>>n>>K;
for(j=;j<n;++j)cin>>a[j];
memset(dp,,sizeof(dp));
dp[]=;
for(j=;j<n;++j){
cin>>m;
for(k=K;k>=;--k)
for(l=;l<=m;++l)
if(k-l*a[j]>=)
(dp[k]+=dp[k-l*a[j]])%=M;
}
cout<<"Case "<<i<<": "<<dp[K]<<endl;
}
return ;
}
E3010 Coins (III)
First AC: 2018-06-07 Latest Modification: 2018-06-07
Note: 多重背包的二进制优化
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,n,K,m,cnt;
ll a[];
bool dp[];
ll i,j,k,l;
int main()
{
ios::sync_with_stdio(false);
cin>>T;
for(i=;i<=T;++i){
cin>>n>>K;
for(j=;j<n;++j)cin>>a[j];
memset(dp,,sizeof(dp));
dp[]=;
for(j=;j<n;++j){
cin>>m;
ll sum=,tmp;
bool jdg=;
for(k=;jdg;k*=){
if(sum+k<=m)sum+=k,tmp=k*a[j];
else jdg=,tmp=(m-sum)*a[j];
for(l=K-tmp;l>=;--l)
dp[l+tmp]|=dp[l];
}
}
cnt=;
for(j=;j<=K;++j)if(dp[j])++cnt;
cout<<"Case "<<i<<": "<<cnt<<endl;
}
return ;
}
E3012 Coins (V)
First AC: 2018-04-24 Latest Modification: 2018-04-24
#include<bits/stdc++.h>
using namespace std;
long long T,n,i;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n;
cout<<"Case "<<i<<": "<<(long long)(log(n)/log())+<<endl;
}
return ;
}
E3013 Coins (VI)
First AC: 2018-04-24 Latest Modification: 2018-04-24
#include<bits/stdc++.h>
using namespace std;
long long T,n,i;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n;
cout<<"Case "<<i<<": "<<ceil(log(*n+)/log())<<endl;
}
return ;
}
E3014 小高斯和小欧几里得(I)
First AC: 2018-06-08 Latest Modification: 2018-06-08
Note: 数据范围很大,很多方法都会超时
注意到(1+60000)×60000/2>2000000000
这表明符合条件的连续正整数个数不超过60000
而每个连续正整数的个数最多只能有一组解满足题设
所以对整数个数遍历即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,n,rst;
ll i,j;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>n;
rst=;
for(j=;j*(j+)<=*n;++j)
if(!((n-j*(j+)/)%j))
++rst;
cout<<"case #"<<i<<": "<<rst<<endl;
}
return ;
}
E3015 小高斯和小欧几里得(II)
First AC: 2018-06-08 Latest Modification: 2018-06-08
Note: 将所有程序按写的时间和调的贡献大小分为总体写bug和总体debug两类
注意到首尾两个程序计算时写bug和debug的时间都要计算在内
所以应该尽可能让第一个程序写的时间和最后一个程序调的时间尽可能少
同时中间的程序尽可能先写bug
因为写的时间里可以调前面程序的bug,贪心的做法是先写bug再调bug
所以可以排序,凡是写bug的都排在调bug前面
在写bug内部,让第一个程序的写bug时间最小
在debug内部,让最后一个程序不得不遗留的debug时间最小
但这个算法我没法证明正确性的一点是
在写bug和debug差异较大的数据中,是否最优解的首末程序仍是首写bug末debug
虽然测试数据中没有这种可能存在的反例
有大佬写的cmp函数是这样的:return min(a.bug,b.debug)<min(a.debug,b.bug);
试了一下可以过,但我也不知道怎么证明正确性
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T,n;
ll bug,debug,tmp,rst;
struct data{
ll bug,debug;
}a[],b;
int i,j;
bool cmp(data a,data b)
{
bool jdga=a.bug<a.debug? :;
bool jdgb=b.bug<b.debug? :;
if(jdga!=jdgb)return jdga>jdgb;
if(jdga){
if(a.bug!=b.bug)return a.bug<b.bug;
return a.bug-a.debug<b.bug-b.debug;
}
else{
if(a.debug!=b.debug)return a.debug>b.debug;
return a.bug-a.debug<b.bug-b.debug;
}
}
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>n;
for(j=;j<n;++j)cin>>a[j].bug>>a[j].debug;
sort(a,a+n,cmp);
bug=debug=rst=;
for(j=;j<n;++j){
rst+=a[j].bug;
if(debug<a[j].bug)debug=a[j].debug;
else debug=debug-a[j].bug+a[j].debug;
}
cout<<"case #"<<i<<": "<<rst+debug<<endl;
}
return ;
}
E3016 Blue Forehead
First AC: 2018-06-08 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n,sum;
bool a[];
int i;
int main()
{
cin>>T;
while(T--){
cin>>n;
sum=n;
for(i=;i<n;++i)cin>>a[i],sum-=a[i];
a[]? cout<<-:cout<<sum;
for(i=;i<n;++i)a[i]? cout<<" -1":cout<<' '<<sum;
cout<<endl;
}
return ;
}
E3017 计算n阶乘右端0的个数(I)
First AC: 2017-10-22 Latest Modification: 2018-04-11
#include<bits/stdc++.h>
using namespace std;
int n,T,i;
int main()
{
cin>>T;
for(;i<T;i++){
cin>>n;
cout<<"case #"<<i<<":\n"<<n/+n/+n/+n/<<endl;
}
return ;
}
E3018 查找单词
First AC: 2017-11-07 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
int T,i,j,k,ls,lt,mx,num,cnt;
string s,t;
int main()
{
cin>>T;getchar();
for(i=;i<T;++i){
getline(cin,t),getline(cin,s);
cout<<"case #"<<i<<":\n";
ls=s.length(),lt=t.length();
for(j=;j<ls;++j)if(s[j]>&&s[j]<)s[j]+=;
for(j=;j<lt;++j)if(t[j]>&&t[j]<)t[j]+=;
mx=ls-lt,cnt=;
for(j=;j<mx;++j){
if((j==||s[j-]==' ')&&s[j]==t[]
&&(lt+j==ls||s[j+lt]==' ')){
num=;
for(k=;k<lt;++k){
if(s[j+k]!=t[k]){
num++;
break;
}
}
if(num==){cout<<j+<<endl;cnt++;break;}
}
}
if(cnt==)cout<<"None\n";
}
return ;
}
E3020 数字猜想问题
First AC: 2017-11-05 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n,M,cnt,i;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
cout<<"case #"<<i<<":\n";
M=cnt=;
while(n!=){
++cnt;
if(n>M)M=n;
if(n%==)n/=;
else n=*n+;
}
cout<<M<<" "<<cnt<<endl;
}
return ;
}
E3021 字符排序
First AC: 2017-12-13 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
int T,n,M,cnt,i;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
cout<<"case #"<<i<<":\n";
M=cnt=;
while(n!=){
++cnt;
if(n>M)M=n;
if(n%==)n/=;
else n=*n+;
}
cout<<M<<" "<<cnt<<endl;
}
return ;
}
E3022 计算n阶乘右端0的个数(II)
First AC: 2017-10-15 Latest Modification: 2018-04-11
#include<bits/stdc++.h>
using namespace std;
int T,n,i;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>n;
cout<<"case #"<<i<<":\n"<<n/+n/+n/+n/<<endl;
}
return ;
}
E3023 字符组合
First AC: 2018-03-15 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
long T,len,tmp,cnt;
string s,t,r;
bool a[],b[];
string str[];
long i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
for(j=;j<len;++j)
if(s[j]>='A'&&s[j]<='Z')a[s[j]-'A']=;
else if(s[j]>='a'&&s[j]<='z')b[s[j]-'a']=;
t="";
for(j=;j<;++j)if(a[j])t+=(char)(j+'A'),a[j]=;
for(j=;j<;++j)if(b[j])t+=(char)(j+'a'),b[j]=;
k=;
for(j=(<<t.length())-;j;--j){
r="";
tmp=j,cnt=;
while(tmp){
if(tmp&)r+=t[cnt];
tmp>>=;
++cnt;
}
str[k++]=r;
}
sort(str,str+k);
cout<<"case #"<<i<<":\n";
for(j=;j<k;++j)cout<<str[j]<<endl;
}
return ;
}
E3024 八进制小数
First AC: 2018-03-10 Latest Modification: 2018-03-10
#include<bits/stdc++.h>
using namespace std;
int T,len;
string s,t;
int i,j;
void div()
{
string r="";
int tmp=,len=t.length();
for(int i=;i<len;++i){
tmp=*tmp+t[i]-'';
r+=(char)(tmp/+'');
tmp%=;
}
while(tmp){
tmp*=;
r+=(char)(tmp/+'');
tmp%=;
}
t=r.substr(,r.length());
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
t="";
for(j=len-;j>;--j)t=s[j]+t,div();
len=t.length();
while(t[len-]=='')t=t.substr(,--len);
cout<<"case #"<<i<<":\n0."<<t<<endl;
}
return ;
}
E3025 连续正整数之和
First AC: 2017-10-18 Latest Modification: 2018-09-14
Note: 对加数个数j分奇偶
奇数j可以是加数个数,当且仅当平均数n÷j为整数且最小数>0
偶数j可以是加数个数,当且仅当平均数n÷j小数部分为0.5且最小数>0
对给定的n,满足前述的j唯一确定一种连加方法,故j的种数cnt即为所求
#include<bits/stdc++.h>
using namespace std;
int T,m,n,cnt;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
cnt=;
for(j=;j<n;j+=)if(n%j==&&n/j-j/>)++cnt;
for(j=;j<n;j+=)if(*n%j==&&n%j!=&&n/j+-j/>)++cnt;
cout<<"case #"<<i<<":\n"<<cnt<<endl;
}
return ;
}
E3026 排版
First AC: 2018-02-17 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
int T,M,len,tmp,suml,cnt;
char c;
queue<string>q,r;
string s,t;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>M;
getchar();
getline(cin,s);
len=s.length();
for(j=;j<len;++j){
if(s[j]!=' ')t+=s[j];
else if(t.length())q.push(t),t="";
}
if(t.length())q.push(t),t="";
tmp=cnt=suml=;
cout<<"case #"<<i<<":\n";
while(){
if(q.empty()||tmp+q.front().length()>M){
if(q.empty()){
cout<<r.front(),r.pop();
while(!r.empty())cout<<' '<<r.front(),r.pop();
cout<<endl;
break;
}
else{
cout<<r.front(),r.pop();
--cnt,suml=M-suml;
int tmp1=suml/cnt,tmp2=suml%cnt,tmp3=cnt-tmp2;
for(j=;j<tmp3;++j){
for(k=;k<tmp1;++k)cout<<' ';
cout<<r.front(),r.pop();
}
for(j=;j<tmp2;++j){
for(k=;k<=tmp1;++k)cout<<' ';
cout<<r.front(),r.pop();
}
cout<<endl;
}
tmp=cnt=suml=;
}
else{
++cnt;
suml+=q.front().length();
tmp+=q.front().length()+;
r.push(q.front()),q.pop();
}
}
}
return ;
}
E3027 抽奖
First AC: 2017-12-09 Latest Modification: 2017-12-09
#include<iostream>
using namespace std;
int T,m,n,cnt;
int a[];
int i,j;
int gcdj(int m,int n)
{
int q=;
while(q)q=m%n,m=n,n=q;
if(m==)return ;
return ;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>m>>n;
for(cnt=,j=;j<=m;++j)if(gcdj(m,j))a[++cnt]=j;
a[]=a[cnt];
cout<<"case #"<<i<<":\n"<<a[n%cnt]+n/cnt*m<<endl;
}
return ;
}
E3028 构造多项式
First AC: 2017-11-14 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,m,cnt;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cnt=;
cout<<"case #"<<i<<":\n";
for(j=;j>;--j){
cin>>m;
if(m==)continue;
if(m==){
if(cnt!=)cout<<"+";
cout<<"x^"<<j;
}
else if(m==-)cout<<"-x^"<<j;
else if(m>){
if(cnt!=)cout<<"+";
cout<<m<<"x^"<<j;
}
else cout<<m<<"x^"<<j;
++cnt;
}
cin>>m;
if(m==){
if(cnt!=)cout<<"+";
cout<<"x",++cnt;
}
else if(m==-)cout<<"-x",++cnt;
else if(m>){
if(cnt!=)cout<<"+";
cout<<m<<"x",++cnt;
}
else if(m<)cout<<m<<"x",++cnt;
cin>>m;
if(m>){
if(cnt!=)cout<<"+";
cout<<m,++cnt;
}
else if(m<)cout<<m,++cnt;
if(cnt==)cout<<;
cout<<endl;
}
return ;
}
E3029 不重复正整数
First AC: 2018-04-12 Latest Modification: 2018-04-12
#include<bits/stdc++.h>
using namespace std;
int T,n,m;
int i;
int find(int n,int m)
{
if(n<)return ;
if(n<)return ;
if(n<&&m>)return ;
if(m<)return ;
int rst=;
for(int i=m;i;--i)rst+=find(n-i,i-);
return rst;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n>>m;
cout<<"case #"<<i<<":\n"<<find(n,m)<<endl;
}
return ;
}
E3030 天黑请闭眼
First AC: 2017-10-31 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,m,n,cnt,num;
int a[];
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cout<<"case #"<<i<<":\n";
cin>>n>>m;
memset(a,,sizeof(a));
for(j=;;){
if(j==n+)j=;
if(a[j]==)j++;
else j++,num++;
if(num==m)num=,a[j-]=,cnt++;
if(cnt==n-){cnt=;break;}
}
for(j=;j<=n;++j)if(a[j]!=){cout<<j<<endl;}
}
return ;
}
E3031 二进制倒置
First AC: 2018-03-09 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int T,len;
string s,t;
int i,j,k;
void div()
{
/* t/=2 */
string r="";
int tmp=,len=s.length();
for(int i=;i<len;++i){
tmp=*tmp+s[i]-'';
r+=(char)(tmp/+'');
tmp&=;
}
while(len=r.length(),len>&&r[]=='')
r=r.substr(,len);
s=r;
}
void mul()
{
/* s*=2 */
string r="";
int tmp=,len=s.length();
for(int i=len-;i>=;--i){
tmp=(s[i]-'')*+tmp;
r=(char)(tmp%+'')+r;
tmp/=;
}
if(tmp)r=""+r;
while(len=r.length(),len>&&r[]=='')
r=r.substr(,len);
s=r;
}
void plu()
{
/* s+=1 */
int len=s.length(),i;
for(i=len-;i>=;--i){
if(s[i]<''){++s[i];break;}
s[i]='';
}
if(i<)s=""+s;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
cout<<"case #"<<i<<":\n";
if(s==""){cout<<"0\n";continue;}
t="";
while(s!=""){
if((s[s.length()-]-'')&)t+='';
else t+='';
div();
}
t+="",s="";
while(len=t.length(),len>&&t[]=='')
t=t.substr(,len);
len=t.length();
for(j=;j<len;++j){
mul();
if(t[j]!='')plu();
}
cout<<s<<endl;
}
return ;
}
E3032 是坚挺数吗?
First AC: 2017-12-11 Latest Modification: 2017-12-11
#include<iostream>
using namespace std;
int a[];
int T,cnt,n;
int i;
void del(int n)
{
int cnt=;
for(int i=;i<;++i){
if(a[i]&&++cnt==n)cnt=,a[i]=;
}
}
int main()
{
for(i=;i<;++i)a[i]=i;
for(i=;i<;++i)del(i);
for(cnt=i=;i<;++i)if(a[i])a[i]=cnt++;
cin>>T;
for(i=;i<T;++i){
cin>>n;
cout<<"case #"<<i<<":\n";
if(a[n])cout<<"Yes "<<a[n]<<endl;
else cout<<"No\n";
}
return ;
}
E3033 删除子串
First AC: 2017-12-24 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,ls,lt,flag;
string s,t;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s>>t;
ls=s.length(),lt=t.length();
cout<<"case #"<<i<<":\n";
for(j=;j<ls;++j){
if(s[j]==t[]){
for(flag=k=;k<lt;++k)
if(s[j+k]!=t[k]){
flag=;
break;
}
if(flag)j+=lt-;
else cout<<s[j];
}
else cout<<s[j];
}
cout<<endl;
}
return ;
}
E3034 数字拆分
First AC: 2017-12-11 Latest Modification: 2017-12-11
#include<iostream>
using namespace std;
long long f[]={,,};
long long i,n;
int T;
int main()
{
for(i=;i<;++i){
if(i&)f[i]=f[i-];
else f[i]=(f[i-]+f[i>>])%;
}
cin>>T;
for(i=;i<T;++i)cin>>n,cout<<"case #"<<i<<":\n"<<f[n]<<endl;
return ;
}
E3035 次大黑区域
First AC: 2017-12-26 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,w,h,r,l,tmp,maxn,maxm,i,j,k;
char a[][];
int cnt(int l,int r)
{
if(a[l][r]==''||l==||r==||l>h||r>w)return ;
if(a[l-][r]-''||a[l][r-]-''||a[l][r+]-''||a[l+][r]-''){
a[l][r]='';
return cnt(l-,r)+cnt(l,r-)+cnt(l,r+)+cnt(l+,r)+;
}
a[l][r]='';
return ;
}
int main()
{
cin>>T;
for(k=;k<T;++k){
memset(a,'',sizeof(a));
maxn=maxm=;
cin>>h>>w;
for(i=;i<=h;++i)for(j=;j<=w;++j)cin>>a[i][j];
for(i=;i<=h;++i)for(j=;j<=w;++j){
tmp=cnt(i,j);
if(tmp>maxn)maxm=maxn,maxn=tmp;
else if(tmp>maxm&&tmp<maxn)maxm=tmp;
}
cout<<"case #"<<k<<":\n"<<maxm<<endl;
}
return ;
}
E3036 按数据中1的位数排序
First AC: 2017-12-06 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt,tmp,flag;
int i,j,k;
struct data{
long long sum,sum0;
int num;
}a[];
bool cmp(data a,data b)
{
if(a.num-b.num)return a.num>b.num;
return a.sum0<b.sum0;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cin>>a[j].sum;
a[j].sum0=a[j].sum;
if(a[j].sum>){
cnt=;
for(k=;k<;++k){
tmp=a[j].sum%;
if(tmp)++cnt;
a[j].sum/=;
}
a[j].num=cnt;
}
else if(a[j].sum<){
a[j].sum*=-,cnt=,flag=;
for(k=;k<;++k){
tmp=a[j].sum%;
if(flag){if(tmp)flag=,++cnt;}
else if(!tmp)++cnt;
a[j].sum/=;
}
a[j].num=cnt;
}
else if(a[j].sum==){a[j].num=;continue;}
}
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n-;++j)cout<<a[j].sum0<<' ';
cout<<a[n-].sum0<<endl;
}
return ;
}
E3037 十六进制加法
First AC: 2017-11-11 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int T,c[],d[],e[];
string a,b;
int lena,lenb,num;
int i,j,k;
int main()
{
cin>>T;
for(k=;k<T;++k){
cin>>a>>b;
if(a==""&&b==""){cout<<"case #"<<k<<":\n0\n";continue;}
cout<<"case #"<<k<<":\n";
for(i=;i<;++i)c[i]=d[i]=e[i]=;
num=,lena=a.length(),lenb=b.length();
for(i=lena-,j=;i>=;--i,--j)
if(a[i]>=''&&a[i]<='')c[j]=(int)(a[i]-'');
else c[j]=(int)(a[i]-'A'+);
for(i=lenb-,j=;i>=;--i,--j)
if(b[i]>=''&&b[i]<='')d[j]=(int)(b[i]-'');
else d[j]=(int)(b[i]-'A'+);
for(i=;i>=;--i)
if(c[i]+d[i]+num>)e[i+]=c[i]+d[i]+num-,num=;
else e[i+]=c[i]+d[i]+num,num=;
for(i=;i<;++i)if(e[i]!=)break;
for(j=i;j<;++j)
if(e[j]>=&&e[j]<=)cout<<e[j];
else cout<<(char)(e[j]-+'A');
cout<<endl;
}
return ;
}
E3038 构造字典序最小字符串
First AC: 2017-12-31 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
int T,n,l,r;
char a[],b[];
int i,j,k;
int main()
{
cin>>T;
for(k=;k<T;++k){
cin>>n;
scanf("%s",a);
j=l=,r=n-;
while(j<n){
if(a[l]<a[r])b[j]=a[l],++l;
else if(a[l]>a[r])b[j]=a[r],--r;
else for(i=;;++i){
if(l+i>=r-i){b[j]=a[l],++l;break;}
if(a[l+i]>a[r-i]){b[j]=a[r],--r;break;}
else if(a[l+i]<a[r-i]){b[j]=a[l],++l;break;}
}
++j;
}
cout<<"case #"<<k<<":\n";
for(i=;i<n;++i)cout<<b[i];
cout<<endl;
}
return ;
}
E3039 按整数最高位的值排序
First AC: 2017-12-05 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
int T,n,len;
long long tmp;
string s;
int i,j,k;
struct data{
int num;
long long sum;
}a[];
bool cmp(data a,data b)
{
if(a.num-b.num)return a.num>b.num;
return a.sum<b.sum;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cin>>s;
len=s.length();
if(s[]=='-'){
a[j].num=s[]-'',a[j].sum=;
for(k=len-,tmp=;k;--k)
a[j].sum+=tmp*(s[k]-''),tmp*=;
a[j].sum*=-;
}
else{
a[j].num=s[]-'',a[j].sum=;
for(k=len-,tmp=;k>=;--k)
a[j].sum+=tmp*(s[k]-''),tmp*=;
}
}
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n"<<a[].sum;
for(j=;j<n;++j)cout<<' '<<a[j].sum;
cout<<endl;
}
return ;
}
E3040 非重复二进制串
First AC: 2018-01-01 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt,tmp,rst;
int a[];
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
cnt=rst=;
while(n)a[cnt++]=n&,n>>=;
for(j=;j<cnt;++j){
for(tmp=k=;j+k<cnt;++k){
if(a[j+k]!=a[j+k-])++tmp;
else break;
}
if(tmp>rst)rst=tmp;
}
cout<<"case #"<<i<<":\n"<<rst<<endl;
}
return ;
}
E3041 分数的加减运算
First AC: 2018-02-19 Latest Modification: 2018-02-19
#include<bits/stdc++.h>
using namespace std;
int T,n,len;
string s;
long long tmp;
long long up,dn,flag;
long long tup,tdn,tflag;
int i,j;
long long gcd(long long a,long long b)
{
long long c=;
while(c)c=a%b,a=b,b=c;
return a;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n>>s;
s='+'+s;
len=s.length();
j=,up=,dn=,flag=;
while(n--){
tup=tdn=;
for(;;++j){
if(s[j]=='+')tflag=;
else if(s[j]=='-')tflag=;
else if(s[j]=='/'){++j;break;}
else tup=*tup+s[j]-'';
}
for(;;++j){
if(j==len)break;
else if(s[j]=='+')break;
else if(s[j]=='-')break;
else tdn=*tdn+s[j]-'';
}
tmp=gcd(tup,tdn);
tup/=tmp,tdn/=tmp;
if(flag){
if(tflag)up=up*tdn+dn*tup;
else{
up=up*tdn-dn*tup;
if(up<)up*=-,flag=;
}
}
else{
if(tflag){
up=tup*dn-tdn*up;
if(up<)up*=-;
else flag=;
}
else up=up*tdn+dn*tup;
}
dn*=tdn,tmp=gcd(up,dn),up/=tmp,dn/=tmp;
}
cout<<"case #"<<i<<":\n";
if(!flag)cout<<'-';
cout<<up;
if(dn!=)cout<<'/'<<dn;
cout<<endl;
}
return ;
}
E3042 4个值的和为0 (II)
First AC: 2018-07-17 Latest Modification: 2018-07-17
#include<bits/stdc++.h>
using namespace std;
int n,s,tmp,rst;
int a[];
vector< pair<int,int> >v[];
vector< pair<int,int> >::iterator it;
int i,j;
int main()
{
cin>>n>>s;
for(i=;i<n;++i)cin>>a[i];
for(i=;i<n;++i){
for(j=i+;j<n;++j)
v[a[i]+a[j]].push_back(pair<int,int>(i,j));
}
for(i=;i<n;++i){
for(j=i+;j<n;++j){
tmp=s-a[i]-a[j];
for(it=v[tmp].begin();it!=v[tmp].end();++it){
if(it->first>j)++rst;
}
}
}
cout<<rst;
return ;
}
E3043 最大公约数
First AC: 2017-10-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,a,b;
int i;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>a>>b;
cout<<"case #"<<i<<":\n"<<__gcd(a,b)<<endl;
}
return ;
}
E3044 字符串的幂
First AC: 2017-11-20 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n;
string s;
int i;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s>>n;
cout<<"case #"<<i<<":\n";
while(n--)cout<<s;
cout<<endl;
}
return ;
}
E3045 学生信息处理
First AC: 2017-12-05 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n;
int i,j;
struct data{
long long num;
string name;
int score1,score2,score3,score;
}a[];
bool cmp(data a,data b)
{
if(a.score-b.score)return a.score>b.score;
return a.num<b.num;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cin>>a[j].num>>a[j].name>>a[j].score1
>>a[j].score2>>a[j].score3;
a[j].score=a[j].score1+a[j].score2+a[j].score3;
}
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j)
cout<<a[j].num<<' '<<a[j].name<<' '<<a[j].score1
<<' '<<a[j].score2<<' '<<a[j].score3<<endl;
}
return ;
}
E3046 单向链表中的节点删除
First AC: 2018-04-05 Latest Modification: 2018-04-05
#include<bits/stdc++.h>
using namespace std;
int T,n,m;
struct node{
int n;
node *next;
};
node *head;
int i;
void ins(int n)
{
if(!head){
head=new node;
head->n=n;
head->next=;
return;
}
node *tmp=head;
while(tmp->next)tmp=tmp->next;
tmp->next=new node;
tmp=tmp->next;
tmp->n=n;
tmp->next=;
}
void del(int n)
{
while(head&&head->n==n)head=head->next;
}
void pri(int n)
{
if(head){
cout<<head->n;
head=head->next;
while(head){
if(head->n!=n)cout<<' '<<head->n;
head=head->next;
}
}
cout<<endl;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
head=;
while(n--)cin>>m,ins(m);
cin>>m;
del(m);
cout<<"case #"<<i<<":\n";
pri(m);
}
return ;
}
E3047 最小公倍数
First AC: 2017-11-11 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,a,b;
ll i;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>a>>b;
cout<<"case #"<<i<<":\n"<<a*b/__gcd(a,b)<<endl;
}
return ;
}
E3048 单词出现次数
First AC: 2017-12-03 Latest Modification: 2019-03-20
#include<bits/stdc++.h>
using namespace std;
int T,cnt;
string s,t;
int lens,lent;
int i,j;
int main()
{
cin>>T;
getchar();
for(i=;i<T;++i){
getline(cin,s);
getline(cin,t);
lens=s.length();
lent=t.length();
cnt=;
for(j=;j<lens;++j){
if(j!=&&s[j-]!=' ')continue;
if(j+lent<lens&&s[j+lent]!=' ')continue;
if(s.substr(j,lent)==t){
++cnt;
}
}
cout<<"case #"<<i<<":\n"<<cnt<<endl;
}
return ;
}
E3049 Hosts排序
First AC: 2018-01-02 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n;
struct data{
int a,b,c,d;
string s;
}f[];
int i,j,k;
bool cmp(data m,data n)
{
if(m.a!=n.a)return m.a>n.a;
if(m.b!=n.b)return m.b>n.b;
if(m.c!=n.c)return m.c>n.c;
if(m.d!=n.d)return m.d>n.d;
return m.s<n.s;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
scanf("%d.%d.%d.%d",&f[j].a,&f[j].b,&f[j].c,&f[j].d);
cin>>f[j].s;
}
sort(f,f+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j){
printf("%d.%d.%d.%d ",f[j].a,f[j].b,f[j].c,f[j].d);
cout<<f[j].s<<endl;
}
}
return ;
}
E3050 链表整理
First AC: 2018-04-05 Latest Modification: 2018-04-05
#include<bits/stdc++.h>
using namespace std;
int T,n1,n2,tmp;
int a[];
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n1>>n2;
for(j=;j<n1;++j)cin>>a[j];
for(j=;j<n2;++j){
cin>>tmp;
for(k=;k<n1;++k)if(a[k]==tmp)a[k]=;
}
cout<<"case #"<<i<<":\n";
for(j=;j<n1;++j)
if(a[j]!=){
cout<<a[j];
break;
}
for(k=j+;k<n1;++k)
if(a[k]!=)cout<<' '<<a[k];
cout<<endl;
}
return ;
}
E3051 台阶走法数
First AC: 2017-11-25 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
long long a[]={,,,,};
int T,n,i;
int main()
{
for(i=;i<;++i){
a[i]=a[i-]+a[i-]+a[i-]+a[i-];
}
for(cin>>T,i=;i<T;++i){
cin>>n;
cout<<"case #"<<i<<":\n"<<a[n]<<endl;
}
return ;
}
E3052 最小不重复数
First AC: 2017-11-23 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
string s;
char a[];
int T,len,cnt,flag,num,n,circle;
int i,j,k,r;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
cout<<"case #"<<i<<":\n";
memset(a,'',sizeof(a));
len=s.length(),cnt=,flag=,n=;
for(j=len;j>;)a[--cnt]=s[--j];
for(j=;flag;--j){
if(a[j]=='')a[j]='';
else ++a[j],--flag;
}
for(circle=;circle;){
circle=num=;
for(j=cnt;j<;++j){
if(a[j]==a[j+]){
++circle;
if(a[j]==''){
++n;
for(k=j,flag=-;k>cnt;--k){
if(a[k]+a[k-]-*''!=){flag=k;break;}
}
if(flag==-){
if(a[cnt]==''){
for(k=cnt-,num=;k<;++k){
if(num)a[k]='',--num;
else a[k]='',++num;
}
}
else{
a[cnt]='';
for(k=cnt+;k<;++k){
if(num)a[k]='',--num;
else a[k]='',++num;
}
}
}
else{
if(a[flag]=='')++a[flag-],k=flag;
else ++a[flag],k=flag+;
for(;k<;++k){
if(num)a[k]='',--num;
else a[k]='',++num;
}
}
}
else{
++n,++a[j+];
for(k=j+;k<;++k){
if(num)a[k]='',--num;
else a[k]='',++num;
}
}
break;
}
}
}
for(j=num=;j<;++j){
if(num)cout<<a[j];
else if(a[j]!='')cout<<a[j],++num;
}
cout<<endl;
}
return ;
}
E3053 神秘信息
First AC: 2017-12-24 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int T,len,cnt,num;
char tmp;
long long rst,base;
string s;
bool a[];
int i,j,k;
int jdg(char c)
{
if((c>=''&&c<='')||(c>='a'&&c<='z')||(c>='A'&&c<='Z'))return ;
return ;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
memset(a,,sizeof(a));
for(cnt=j=;j<len;++j)if(a[s[j]])a[s[j]]=,++cnt;
cout<<"case #"<<i<<":\n";
if(cnt==)cout<<pow(,len)-<<endl;
else{
memset(a,,sizeof(a));
tmp=s[],s[]=,a[tmp]=;
for(j=;j<len;++j)if(s[j]==tmp)s[j]=;
for(j=;j<len;++j){
if(a[s[j]]&&jdg(s[j])){
tmp=s[j],s[j]=,a[tmp]=;
for(k=j+;k<len;++k)if(s[k]==tmp)s[k]=;
break;
}
}
for(num=j=;j<len;++j){
if(a[s[j]]&&jdg(s[j])){
tmp=s[j],s[j]=num,a[tmp]=;
for(k=j+;k<len;++k)if(s[k]==tmp)s[k]=num;
++num;
}
}
for(j=len-,rst=,base=;j>=;--j){
rst+=base*s[j],base*=cnt;
}
cout<<rst-<<endl;
}
}
return ;
}
E3054 波兰表达式
First AC: 2017-12-16 Latest Modification: 2018-04-01
#include<bits/stdc++.h>
using namespace std;
int T,len,flag,i,cnt;
char a[],c;
double num,tmp1,tmp2;
int sig[];
stack<char>m;
stack<double>n;
int main()
{
cin>>T;
for(i=;i<T;++i){
while(){
cin>>a;
len=strlen(a);
flag=;
if(len==){
switch(a[]){
case '+':case '-': case '*':case '/':
m.push(a[]);flag=;++cnt;break;
}
}
if(flag){
num=atof(a);
n.push(num);
++sig[cnt];
}
while(sig[cnt]>&&m.size()&&n.size()>){
tmp2=n.top();
n.pop();
tmp1=n.top();
n.pop();
c=m.top();
m.pop();
switch(c){
case '+':n.push(tmp1+tmp2);break;
case '-':n.push(tmp1-tmp2);break;
case '*':n.push(tmp1*tmp2);break;
case '/':n.push(tmp1/tmp2);break;
}
sig[cnt]=,++sig[--cnt];
}
c=getchar();
if(c=='\n'){
num=n.top();
n.pop();
printf("case #%d:\n%.6f\n",i,num);
cnt=;
break;
}
}
}
return ;
}
E3055 字符频率
First AC: 2017-12-01 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len;
string s;
int i,j;
double p[];
struct data{
char c,d;
double pos;
int cap;
}a[];
bool cmp(data a,data b)
{
if(a.pos!=b.pos)return a.pos>b.pos;
if(a.d!=b.d)return a.d<b.d;
return a.c>b.c;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
for(j=;j<;++j)cin>>p[j];
cin>>s;
len=s.length();
for(j=;j<len;++j){
a[j].c=a[j].d=s[j];
if(s[j]>='a')a[j].pos=p[s[j]-'a'],a[j].cap=;
else a[j].pos=p[s[j]-'A'],a[j].cap=,a[j].d+=;
}
sort(a,a+len,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<len;++j)cout<<a[j].c;
cout<<endl;
}
return ;
}
E3056 链表查询
First AC: 2018-04-05 Latest Modification: 2018-04-05
#include<bits/stdc++.h>
using namespace std;
typedef struct Node
{
int value;
struct Node *next;
}NODE;
NODE *FindLastNthNode(NODE *h,int n)
{
NODE *tmp=h;
int cnt=;
while(tmp->next)tmp=tmp->next,++cnt;
if(cnt<n)return NULL;
cnt-=n;
tmp=h;
while(cnt--)tmp=tmp->next;
return tmp;
}
static unsigned long next=;
int RND()
{
next=next*+;
return (unsigned)(next/)%;
}
void SETSEED(unsigned seed)
{
next = seed;
}
void solve()
{
int i,s,n,m,t;
NODE *head=,*p,*tail;
scanf("%d%d%d%d",&s,&t,&m,&n);
SETSEED(s);
for(i=;i<t;i++){
p=(NODE*)malloc(sizeof(NODE));
p->value=RND()%m;
p->next=;
if(head==)head=p;
else tail->next=p;
tail=p;
}
if(p=FindLastNthNode(head,n))
printf("%d:",p->value);
else printf("NONE:");
n=;
while(head){
p=head;
head=head->next;
if(t<||t>&&n<){
printf("%d",p->value);
if(head)printf(" ");
}
n++;
free(p);
}
printf("\n");
}
int main()
{
int i,t;
scanf("%d\n",&t);
for(i=;i<t;i++){
printf("case #%d:\n",i);
solve();
}
return ;
}
E3057 素数进制A+B
First AC: 2018-07-11 Latest Modification: 2018-07-11
#include<bits/stdc++.h>
using namespace std;
int T,len1,len2,cnt;
char s1[],s2[];
int num1[],num2[];
int base[]={,,,,,,,,,,,,,,,,,,,,,,,,};
int cas,i,j;
int main()
{
cin>>T;
for(cas=;cas<T;++cas){
memset(num1,,sizeof num1);
memset(num2,,sizeof num2);
cin>>s1>>s2;
cnt=;
len1=strlen(s1);
len2=strlen(s2);
for(i=len1-,j=;i+;--i){
if(s1[i]==','){
j=;
++cnt;
}
else{
num1[cnt]+=(s1[i]-'')*j;
j*=;
}
}
cnt=;
for(i=len2-,j=;i+;--i){
if(s2[i]==','){
j=;
++cnt;
}
else{
num2[cnt]+=(s2[i]-'')*j;
j*=;
}
}
for(i=;i<;++i){
num1[i]+=num2[i];
if(num1[i]>=base[i]){
num1[i+]+=num1[i]/base[i];
num1[i]%=base[i];
}
}
cout<<"case #"<<cas<<":\n";
for(i=;i;--i)if(num1[i])break;
for(;i;--i)cout<<num1[i]<<",";
cout<<num1[]<<endl;
}
return ;
}
E3058 链表运算
First AC: 2017-12-16 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len,flag,i;
char a[],c;
double num,tmp1,tmp2;
stack<char>m;
stack<double>n;
int main()
{
cin>>T;
for(i=;i<T;++i){
while(){
cin>>a;
len=strlen(a);
flag=;
if(len==){
switch(a[]){
case '+':case '-': case '*':case '/':
m.push(a[]);flag=;break;
}
}
if(flag){
num=atof(a);
n.push(num);
}
while(m.size()&&n.size()>){
tmp1=n.top();
n.pop();
tmp2=n.top();
n.pop();
c=m.top();
m.pop();
switch(c){
case '+':n.push(tmp1+tmp2);break;
case '-':n.push(tmp1-tmp2);break;
case '*':n.push(tmp1*tmp2);break;
case '/':n.push(tmp1/tmp2);break;
}
}
c=getchar();
if(c=='\n'){
num=n.top();
n.pop();
printf("case #%d:\n%.2f\n",i,num);
break;
}
}
}
return ;
}
E3059 极坐标排序
First AC: 2017-12-26 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
struct data{
double p,r;
}a[];
int T,n,i,j;
double x,y,t;
bool cmp(data a,data b)
{
if(a.r!=b.r)return a.r<b.r;
return a.p>b.p;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cin>>x>>y;
a[j].p=sqrt(x*x+y*y);
t=atan2(y,x);
if(t<)t+=*3.14159265358979323846;
a[j].r=t;
}
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j)printf("(%.4f,%.4f)\n",a[j].p,a[j].r);
}
return ;
}
E3060 高次方数的尾数
First AC: 2017-10-27 Latest Modification: 2018-10-14
Note: 第七组评测数据有误,故加特判
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9;
int T,n;
ll a,b,tmp;
int rst[];
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>a>>b>>n;
cout<<"case #"<<i<<":\n";
if((a==||a==)&&b<)cout<<a<<endl;
else{
tmp=;
while(b--)(tmp*=a)%=mod;
for(j=;j<;++j)rst[j]=tmp%,tmp/=;
for(j=n;j;--j)cout<<rst[j];
cout<<endl;
}
}
return ;
}
E3061 莫尔斯电码
First AC: 2018-03-15 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
int T,len;
string s,t;
int i,j;
void jdg(string s)
{
if(s==".-")cout<<'A';
else if(s=="-...")cout<<'B';
else if(s=="-.-.")cout<<'C';
else if(s=="-..")cout<<'D';
else if(s==".")cout<<'E';
else if(s=="..-.")cout<<'F';
else if(s=="--.")cout<<'G';
else if(s=="....")cout<<'H';
else if(s=="..")cout<<'I';
else if(s==".---")cout<<'J';
else if(s=="-.-")cout<<'K';
else if(s==".-..")cout<<'L';
else if(s=="--")cout<<'M';
else if(s=="-.")cout<<'N';
else if(s=="---")cout<<'O';
else if(s==".--.")cout<<'P';
else if(s=="--.-")cout<<'Q';
else if(s==".-.")cout<<'R';
else if(s=="...")cout<<'S';
else if(s=="-")cout<<'T';
else if(s=="..-")cout<<'U';
else if(s=="...-")cout<<'V';
else if(s==".--")cout<<'W';
else if(s=="-..-")cout<<'X';
else if(s=="-.--")cout<<'Y';
else if(s=="--..")cout<<'Z';
else if(s=="-----")cout<<;
else if(s==".----")cout<<;
else if(s=="..---")cout<<;
else if(s=="...--")cout<<;
else if(s=="....-")cout<<;
else if(s==".....")cout<<;
else if(s=="-....")cout<<;
else if(s=="--...")cout<<;
else if(s=="---..")cout<<;
else if(s=="----.")cout<<;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length()+;
s+="/ ";
cout<<"case #"<<i<<":\n";
for(j=;j<len;++j)
if(s[j]!='/')t+=s[j];
else{
jdg(t);
if(s[j+]=='/'&&s[j+]=='/'){
if(s[j+]=='/'&&s[j+]=='/')cout<<'.',j+=;
else cout<<' ',j+=;
}
t="";
}
cout<<endl;
}
return ;
}
E3065 rxms写摘要
First AC: 2017-12-27 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,N,B,D;
int i,j,k;
struct data{
string s;
double b,d,tf,idf,rst;
}a[];
bool cmp(data a,data b)
{
if(a.rst-b.rst<0.000001&&b.rst-a.rst<0.000001){
int tmp=a.s.length()<b.s.length()?a.s.length():b.s.length();
for(int i=;i<tmp;++i)if(a.s[i]-b.s[i])return a.s[i]<b.s[i];
return a.s.length()<b.s.length();
}
return a.rst>b.rst;
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>N>>B>>D;
for(j=;j<N;++j){
cin>>a[j].s>>a[j].b>>a[j].d;
a[j].tf=a[j].b/B,a[j].idf=log(D/(a[j].d+));
a[j].rst=a[j].tf*a[j].idf;
}
int tmp=N>?:N;
sort(a,a+N,cmp);
cout<<"Case #"<<i<<":\n"<<a[].s;
for(j=;j<tmp;++j)cout<<' '<<a[j].s;
cout<<endl;
}
return ;
}
E3068 rxms换气球
First AC: 2018-02-09 Latest Modification: 2018-02-09
#include<bits/stdc++.h>
using namespace std;
int T,i,a,b,c,s;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>a>>b>>c;
s=a+b+c;
a%=,b%=,c%=;
if(a^b&&a+b+c==)cout<<"Case #"<<i<<":"<<s-<<endl;
else cout<<"Case #"<<i<<":"<<s<<endl;
}
return ;
}
E3069 简单的矩形计数
First AC: 2018-02-15 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,c,d;
long rst;
bool f[][];
int i,j;
int main()
{
cin>>n>>m;
for(i=;i<n;++i)for(j=;j<m;++j)cin>>f[i][j];
for(a=;a<n;++a)for(b=;b<a;++b)for(c=;c<m;++c)for(d=;d<c;++d)
if(f[a][c]&&f[a][d]&&f[b][c]&&f[b][d])++rst;
cout<<rst;
return ;
}
E3070 简单的数学问题
First AC: 2018-12-12 Latest Modification: 2018-12-12
Note: 我的想法是可以利用标准分解将$a^{b}$统一表示成$c^{d}$,使得$c$尽可能小
其他人的思路包括:计算$a^{b}%p$存$long long$、对$a^b$取对数存$double$、以及$python$啥都不做
本质上就是一一映射到可以存的结构中,然后去重计数
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
int n;
int a,b,c,d;
map<int,int>mp;
int i;
bool ok(int a,int b,int i)
{
c=pow((ld)a,(ld)1.0/i)+0.3;
d=i*b;
int r=;
for(int j=;j<i;++j)r*=c;
return r==a;
}
int main()
{
while(cin>>n){
mp.clear();
for(a=;a<=;++a){
for(b=;b<=n;++b){
bool flag=;
for(i=;i;--i){
if(ok(a,b,i)){
mp.insert(pair<int,int>(*c+d,));
flag=;
break;
}
}
}
}
cout<<mp.size()<<endl;
}
return ;
}
E3072 小巴菲特买股票
First AC: 2018-01-01 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n,low,m,rst;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n>>low;
rst=;
for(j=;j<n;++j){
cin>>m;
if(m<low)low=m;
else if(m>low)rst=m-low>rst?m-low:rst;
}
cout<<"case #"<<i<<":\n"<<rst<<endl;
}
return ;
}
E3073 道路排序
First AC: 2018-01-06 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
struct data{
int a,b,c;
}a[];
int T,m,n,i,j;
bool cmp(data a,data b)
{
if(a.c^b.c)return a.c>b.c;
if(a.a^b.a)return a.a<b.a;
return a.b<b.b;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>m>>n;
for(j=;j<n;++j)cin>>a[j].a>>a[j].b>>a[j].c;
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j)printf("(%d,%d,%d)\n",a[j].a,a[j].b,a[j].c);
}
return ;
}
E3074 特殊加密
First AC: 2017-11-15 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len;
int i,j;
string s,a[]={"..---/.----","..---/..---","..---/...--",
"...--/.----","...--/..---","...--/...--",
"....-/.----","....-/..---","....-/...--",
"...../.----","...../..---","...../...--",
"-..../.----","-..../..---","-..../...--",
"--.../.----","--.../..---","--.../...--",
"--.../....-","---../.----","---../..---",
"---../...--","----./.----","----./..---",
"----./...--","----./....-"};
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
cout<<"case #"<<i<<":\n"<<a[s[]-'A'];
for(j=;j<len;++j)cout<<"/"<<a[s[j]-'A'];
cout<<endl;
}
return ;
}
E3075 素进制链表
First AC: 2018-07-11 Latest Modification: 2018-07-11
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll base[]={,,,,,,,,,,,,,,,};
ll rst[];
ll T,n;
ll cas,i;
int main()
{
for(i=;i<;++i)base[i]*=base[i-];
cin>>T;
for(cas=;cas<T;++cas){
cin>>n;
memset(rst,,sizeof(rst));
for(i=;i;--i){
if(n>=base[i])rst[i]=n/base[i];
n%=base[i];
}
rst[]=n;
cout<<"case #"<<cas<<":\n";
for(i=;i;--i)if(rst[i])break;
for(;i+;--i)cout<<rst[i]<<";";
cout<<endl;
}
return ;
}
E3076 斐波那契数列
First AC: 2018-01-08 Latest Modification: 2018-01-08
#include<iostream>
using namespace std;
int T,n;
string f[]={"","","","","","","","",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
int main()
{
cin>>T;
for(int i=;i<T;++i){
cin>>n;
cout<<"case #"<<i<<":\n"<<f[n]<<endl;
}
return ;
}
E3081 购房还款
First AC: 2017-10-18 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
double d,p,r;
int main()
{
cin>>d>>p>>r;
cout<<(long)(0.5+log10(p/(p-d*r/))/log10(+r/));
return ;
}
E3082 公式计算
First AC: 2017-10-18 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int x;
int main()
{
cin>>x;
if(x<)cout<<x<<endl;
else if(x<)cout<<*x-<<endl;
else cout<<*x-<<endl;
return ;
}
E3084 最大公约数
First AC: 2017-10-17 Latest Modification: 2018-12-20
#include<bits/stdc++.h>
using namespace std;
int a,b,c;
int main()
{
cin>>a>>b;
c=;
while(c)c=a%b,a=b,b=c;
cout<<a;
return ;
}
E3085 统计1的个数
First AC: 2017-10-15 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int n,i,c;
int main()
{
cin>>n;
for(i=;i<;i++)if(n%==)c++,n/=;
cout<<c;
return ;
}
E3086 水仙花数
First AC: 2018-01-05 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int i;
int main()
{
for(i=;i<;++i)
if(pow(i/,)+pow(i%/,)+pow(i%,)==i)
cout<<i<<endl;
return ;
}
E3087 牛顿法求解方程
First AC: 2017-12-16 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int cnt;
double x=1.5;
double k(double x){return *x*x-*x+;}
double rst(double x){return (*x*x+)*(x-);}
int main()
{
do{
++cnt;
x-=rst(x)/k(x);
}while(rst(x)>1e-||-rst(x)>1e-);
printf("%.2f %d",x,cnt);
return ;
}
E3088 循环移位
First AC: 2017-11-16 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
string s;
int n,ls,m,cnt;
int i,j;
int main()
{
cin>>s>>n,ls=s.length(),m=n%ls;
cout<<ls<<' ';
for(i=ls-m;i<ls;++i){
if(cnt==&&s[i]!='')++cnt;
if(cnt==)cout<<s[i];
}
ls-=m;
for(i=;i<ls;++i){
if(cnt==&&s[i]!='')++cnt;
if(cnt==)cout<<s[i];
}
return ;
}
E3090 杨辉三角
First AC: 2017-10-22 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int a[][];
int n,i,j;
int main()
{
a[][]=;
a[][]=;
a[][]=;
for(i=;i<;i++){
a[i][]=;
a[i][i]=;
for(j=;j<i;j++){
a[i][j]=a[i-][j-]+a[i-][j];
}
}
cin>>n;
for(i=;i<=n;i++){
cout<<a[i][];
for(j=;j<=i;j++)cout<<" "<<a[i][j];
cout<<"\n";
}
cout<<"\n";
return ;
}
E3092 GDP
First AC: 2017-11-01 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int n,r;
int main()
{
cin>>n>>r;
printf("%.2f",pow(+r/100.0,n));
return ;
}
E3093 最大公约数
First AC: 2017-10-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int a,b,c;
int main()
{
cin>>a>>b>>c;
cout<<__gcd(__gcd(a,b),c);
return ;
}
E3094 反序输出
First AC: 2017-11-04 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int n,a[],sum;
int i;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i],sum+=a[i];
for(i=n-;i>;--i)cout<<a[i]<<" ";
cout<<a[]<<endl<<sum;
printf(" %.2f",sum*1.0/n);
return ;
}
E3096 字母数字对应
First AC: 2017-11-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
string s;
int i;
int main()
{
cin>>s;
for(i=;i<;++i)switch(s[i]){
case 'A':case 'B':case 'C':case 'a':case 'b':case 'c':
cout<<;break;
case 'D':case 'E':case 'F':case 'd':case 'e':case 'f':
cout<<;break;
case 'G':case 'H':case 'I':case 'g':case 'h':case 'i':
cout<<;break;
case 'J':case 'K':case 'L':case 'j':case 'k':case 'l':
cout<<;break;
case 'M':case 'N':case 'O':case 'm':case 'n':case 'o':
cout<<;break;
case 'T':case 'U':case 'V':case 't':case 'u':case 'v':
cout<<;break;
case 'P':case 'Q':case 'R':case 'S':case 'p':case 'q':
case 'r':case 's':
cout<<;break;
default:cout<<;
}
cout<<endl;
return ;
}
E3097 字符串排序
First AC: 2017-12-30 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int n;
struct data{
string s;
int len;
}a[];
bool cmp(data a,data b)
{
int tmp=a.len<b.len? a.len:b.len;
for(int i=;i<tmp;++i)if(a.s[i]-b.s[i])return a.s[i]>b.s[i];
return a.len>b.len;
}
int main()
{
cin>>n,getchar();
for(int i=;i<n;++i)getline(cin,a[i].s),a[i].len=a[i].s.length();
sort(a,a+n,cmp);
for(int i=;i<n;++i)cout<<a[i].s<<endl;
return ;
}
E3098 字符串的交织
First AC: 2017-11-20 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
string s,t;
int ls,lt;
int i;
int main()
{
cin>>s>>t;
ls=s.length(),lt=t.length();
if(ls==lt)for(i=;i<ls;++i)cout<<s[i]<<t[i];
else if(ls>lt){
for(i=;i<lt;++i)cout<<s[i]<<t[i];
for(;i<ls;)cout<<s[i++];
}
else{
for(i=;i<ls;++i)cout<<s[i]<<t[i];
for(;i<lt;)cout<<t[i++];
}
return ;
}
E3101 矩阵转置
First AC: 2017-12-06 Latest Modification: 2017-12-06
#include<iostream>
using namespace std;
int n,i,j;
int a[][];
int main()
{
cin>>n;
for(i=;i<n;++i)for(j=;j<n;++j)cin>>a[i][j];
for(i=;i<n;++i){
for(j=;j<n-;++j)cout<<a[j][i]<<' ';
cout<<a[n-][i]<<endl;
}
return ;
}
E3106 骰子概率
First AC: 2018-01-21 Latest Modification: 2018-01-21
#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[];
int succ,fail,jdg;
long i,j;
int main()
{
cin>>n>>m;
for(i=;i<;++i){
memset(a,,sizeof(a));
for(j=;j<n;++j)++a[rand()%];
for(j=jdg=;j<;++j){
if(a[j]>=m){jdg=;break;}
}
jdg? ++succ:++fail;
}
printf("%.2f",succ*1.0/(succ+fail));
return ;
}
E3107 数据交换
First AC: 2017-11-23 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int n,m,M,x,y;
int a[];
int i;
int main()
{
cin>>n;
x=,y=;
m=,M=n-;
for(i=;i<n;++i){
cin>>a[i];
if(a[i]<y)m=i,y=a[i];
if(a[i]>x)M=i,x=a[i];
}
if(m==n-&&M==)a[]=y,a[n-]=x;
else if(m==n-){
a[]^=a[m],a[m]^=a[],a[]^=a[m];
a[n-]^=a[M],a[M]^=a[n-],a[n-]^=a[M];
}
else if(M==){
a[n-]^=a[M],a[M]^=a[n-],a[n-]^=a[M];
a[]^=a[m],a[m]^=a[],a[]^=a[m];
}
else{
if(m-)a[]^=a[m],a[m]^=a[],a[]^=a[m];
if(M^(n-))a[n-]^=a[M],a[M]^=a[n-],a[n-]^=a[M];
}
cout<<a[];
for(i=;i<n;++i)cout<<' '<<a[i];
return ;
}
E3119 排序
First AC: 2017-10-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int a[],i,j,m,count;
int main()
{
for(i=;i<;i++){
cin>>m;
a[m]++;
}
for(i=;i<;i++){
if(a[i]!=){
count++;
if(count==){
for(j=;j<a[i];j++)cout<<i;
break;
}
else for(j=;j<a[i];j++)cout<<i<<" ";
}
}
return ;
}
E3120 整除
First AC: 2017-10-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
cin>>n;
if(n%==)cout<<"Yes";
else cout<<"No";
return ;
}
E3121 素数
First AC: 2017-10-17 Latest Modification: 2018-06-08
Note: 坑数据,最后一个数字后面有一个空格
#include<bits/stdc++.h>
using namespace std;
int n,i,count;
int main()
{
cout<<"101 ";
for(n=;n<;n+=){
for(i=;i<=sqrt(n);i++)if(n%i==){count++;break;}
if(count==)cout<<n<<" ";
count=;
}
return ;
}
E3122 最大公约数
First AC: 2017-10-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int m,n;
int main()
{
cin>>m>>n;
cout<<__gcd(m,n);
return ;
}
E3123 二次方程的根
First AC: 2017-10-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
double sqrtdelta,delta;
int a,b,c;
while(cin>>a>>b>>c){
if(a<)a=-a,b=-b,c=-c;
delta=b*b-*a*c;
if(delta==)printf("%.6f\n",-(float)b/(*a));
else{
sqrtdelta=sqrt(delta);
printf("%.6f %.6f\n",-(b+sqrtdelta)/(a<<)
,(sqrtdelta-b)/(*a));
}
}
return ;
}
E3124 单词表
First AC: 2018-03-15 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
int T,len,num;
string s,t;
struct data{
string s;
}a[];
int i,j;
bool cmp(data a,data b)
{
return a.s<b.s;
}
int main()
{
cin>>T;
getchar();
for(i=;i<T;i++){
getline(cin,s);
len=s.length();
num=;
for(j=;j<len;++j){
if(isalpha(s[j]))t+=s[j];
else if(t!="") a[num++].s=t,t="";
}
if(t!="") a[num++].s=t,t="";
sort(a,a+num,cmp);
cout<<"case #"<<i<<":\n"<<a[].s;
for(j=;j<num;++j)if(a[j].s!=a[j-].s)cout<<' '<<a[j].s;
cout<<endl;
}
return ;
}
E3125 巧克力
First AC: 2017-11-06 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt,num;
long long s;
int a[];
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
s=,num=,memset(a,,sizeof(a));
cin>>n;
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j){cin>>a[j];if(a[j]==)++num;}
if(num==){cout<<"0\n";continue;}
for(j=;j<n;++j){
cnt=;
if(a[j]==){
for(k=j+;k<n;++k){
if(a[j]==a[k])++cnt;
else break;
}
if(k==n)break;
if(j==){j=k-;continue;};
s*=cnt,j=k-;
}
}
cout<<s<<endl;
}
return ;
}
E3126 商品推荐
First AC: 2017-12-23 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt;
long samt[],mamt;
double spri[],mpri;
int i,j,k;
struct data{
string num;
long amt;
double pri;
bool flag;
int rank;
}a[];
bool cmp(data a,data b)
{
if(a.flag-b.flag)return a.flag>b.flag;
if(a.amt^b.amt)return a.amt>b.amt;
if(a.pri-b.pri)return a.pri<b.pri;
return a.rank<b.rank;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
a[j].rank=j;
cin>>a[j].num>>a[j].amt>>a[j].pri;
samt[j]=a[j].amt;
spri[j]=a[j].pri;
}
sort(samt,samt+n);
sort(spri,spri+n);
if(n&)mamt=samt[n/],mpri=spri[n/];
else{
mamt=(samt[n/]+samt[n/-])/2.0;
mpri=(spri[n/]+spri[n/-])/2.0;
}
for(cnt=j=;j<n;++j){
if(a[j].amt>mamt&&a[j].pri<mpri)
a[j].flag=,++cnt;
else a[j].flag=;
}
cout<<"case #"<<i<<":\n";
if(!cnt){
cout<<"no recommendation\n";
continue;
}
sort(a,a+n,cmp);
for(j=;j<cnt;++j)
cout<<a[j].num<<' '<<a[j].amt<<' '<<a[j].pri<<endl;
}
return ;
}
E3127 字串间距
First AC: 2017-12-22 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
int T,ls,lt,lr;
int ns[],nt[],cnts,cntt,rst;
string s,t,r;
bool flag,flags,flagt;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s>>t>>r;
ls=s.length(),lt=t.length(),lr=r.length();
memset(ns,,sizeof(ns)),memset(nt,,sizeof(nt));
flags=flagt=cnts=cntt=;
for(j=;j<=lr-ls;++j){
if(r[j]==s[]){
for(flag=k=;k<ls;++k)if(r[j+k]!=s[k]){flag=;break;}
if(flag)ns[cnts++]=j,flags=;
}
}
for(j=;j<=lr-lt;++j){
if(r[j]==t[]){
for(flag=k=;k<lt;++k)if(r[j+k]!=t[k]){flag=;break;}
if(flag)nt[cntt++]=j,flagt=;
}
}
cout<<"case #"<<i<<":\n";
if(flags&&flagt){
sort(ns,ns+cnts),sort(nt,nt+cntt);
if(ns[cnts-]<=nt[])rst=max(,nt[cntt-]-ns[]-ls);
else if(nt[cntt-]<=ns[])rst=max(,ns[cnts-]-nt[]-lt);
else rst=max(,max(ns[cnts-]-nt[]-lt,nt[cntt-]-ns[]-ls));
cout<<rst<<endl;
}
else cout<<"0\n";
}
return ;
}
E3128 十进制数列项
First AC: 2017-12-23 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len,cnt,flag;
string s;
int a[];
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length(),cnt=flag=;
for(j=len-;;--j){
a[cnt++]=s[j]-'';
if(j==){flag=;break;}
if(s[j]>s[j-])break;
}
cout<<"case #"<<i<<":\n";
if(flag){
sort(a,a+cnt);
for(j=;;++j)if(a[j]){
cout<<a[j]<<;
a[j]=;
break;
}
sort(a,a+cnt);
for(j=;j<cnt;++j)cout<<a[j];
cout<<endl;
continue;
}
sort(a,a+cnt);
for(j=;j<len-cnt-;++j)cout<<s[j];
for(j=;j<cnt;++j)if(a[j]>s[len-cnt-]-''){
cout<<a[j];a[j]=s[len-cnt-]-'';
break;
}
sort(a,a+cnt);
for(j=;j<cnt;++j)cout<<a[j];
cout<<endl;
}
return ;
}
E3129 最大最小之差
First AC: 2017-12-10 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len,cnt,flag,tmp,num;
string s;
int a[],M[],m[],r[];
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
memset(a,,),memset(M,,);
memset(m,,),memset(r,,);
for(j=;j<len;++j)a[s[j]-'']++;
for(cnt=,j=;j<;++j)for(k=;k<a[j];++k)M[cnt--]=j;
for(cnt=,j=;j>=;--j)for(k=;k<a[j];++k)m[cnt--]=j;
for(flag=num=,j=;j;--j){
tmp=M[j]-m[j]-flag;
if(tmp<)r[j]=+tmp,flag=;
else r[j]=tmp,flag=;
if(tmp)num=;
}
cout<<"case #"<<i<<":\n";
if(num==){cout<<<<endl;continue;}
for(j=;;++j)if(r[j])break;
for(k=j;k<;++k)cout<<r[k];
cout<<endl;
}
return ;
}
E3130 方形码
First AC: 2017-12-23 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len,cnt,tmp,num;
string s;
int a[];
int i,j,k;
int main()
{
cin>>T,getchar();
for(i=;i<T;++i){
getline(cin,s);
len=s.length();
for(cnt=j=;j<len;++j)if(s[j]-' ')a[cnt++]=s[j];
tmp=(int)sqrt(cnt);
if(tmp*tmp^cnt)++tmp;
cout<<"case #"<<i<<":\n";
for(num=j=;;++j){
for(k=;j+tmp*k<cnt;++k){
cout<<(char)a[j+tmp*k],++num;
}
if(num-cnt)cout<<' ';
else{cout<<endl;break;}
}
}
return ;
}
E3131 字母对的频率
First AC: 2017-12-23 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len,cnt,rst,maxa;
string s;
int a[][];
int i,j;
int main()
{
cin>>T,getchar();
for(i=;i<T;++i){
getline(cin,s);
len=s.length();
memset(a,,sizeof(a));
for(cnt=maxa=j=;j<len;++j)if(s[j]>='A'&&s[j]<='Z')s[j]+=;
for(j=;j<len;++j)
if(s[j-]>='a'&&s[j-]<='z'&&s[j]>='a'&&s[j]<='z'){
++cnt,a[s[j-]-'a'][s[j]-'a']++;
if(a[s[j-]-'a'][s[j]-'a']>maxa)
maxa=a[s[j-]-'a'][s[j]-'a'],rst=j;
}
cout<<"case #"<<i<<":\n"<<s[rst-]<<s[rst];
printf(" %.2f%%\n",a[s[rst-]-'a'][s[rst]-'a']*100.0/cnt);
}
return ;
}
E3132 寻宝
First AC: 2017-11-11 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int a[][],b[][],c[],r,q,tr,tq,cnt;
int T;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
memset(b,,sizeof(b));
memset(c,,sizeof(c));
for(j=;j<;++j)
cin>>a[j][]>>a[j][]>>a[j][]>>a[j][]>>a[j][];
cout<<"case #"<<i<<":\n";
r=q=,a[][]=,cnt=;
while(){
tr=a[r][q]/,tq=a[r][q]%;
if(r==tr&&q==tq){
cout<<a[][];
for(j=;j<cnt;++j)cout<<' '<<c[j];
break;
}
if(b[tr][tq]==){
++b[tr][tq];
c[cnt++]=a[tr][tq];
r=tr;
q=tq;
}
else{
cout<<"-1";
break;
}
}
cout<<endl;
}
return ;
}
E3133 最长回文子串
First AC: 2017-12-23 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len,tmp,rst1,rst2,cnt;
string s;
int i,j,k;
int main()
{
cin>>T,getchar();
for(i=;i<T;++i){
getline(cin,s);
len=s.length()-,rst1=rst2=;
for(j=;j<len;++j){
tmp=min(j,len-j)+;
for(cnt=k=;k<tmp;++k){
if(s[j-k]!=s[j+k])break;
++cnt;
}
if(cnt>rst1)rst1=cnt;
}
for(j=;j<=len;++j){
if(s[j]==s[j-]){
tmp=min(j,len-j+);
for(cnt=k=;k<tmp;++k){
if(s[j--k]!=s[j+k])break;
++cnt;
}
if(cnt>rst2)rst2=cnt;
}
}
cout<<"case #"<<i<<":\n"<<max(,max(*rst1-,*rst2))<<endl;
}
return ;
}
E3134 短信激活码
First AC: 2017-11-04 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n;
long long m;
string s;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
cout<<"case #"<<i<<":\n";
m=;
n=(s[]-'')*+(s[]-'')*
+(s[]-'')*+(s[]-'')*+s[]-'';
for(j=;j<;++j)m*=n,m%=;
if(m/==)cout<<"";
if(m/==)cout<<"";
if(m/==)cout<<"";
if(m/==)cout<<"";
cout<<m<<endl;
}
return ;
}
E3135 数据压缩
First AC: 2017-10-23 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len,cnt;
string s;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
cout<<"case #"<<i<<":\n";
len=s.length();
cnt=;
for(j=;j<=len-;++j){
if(s[j]==s[j-])cnt++;
else if(cnt>){
cout<<""<<s[j-]<<cnt-<<s[j-];
cnt=;
}
else cout<<cnt<<s[j-],cnt=;
}
if(cnt>)cout<<""<<s[len-]<<cnt-<<s[len-]<<endl;
else cout<<cnt<<s[len-]<<endl;
}
return ;
}
E3136 指数比例
First AC: 2018-01-06 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
#define e 2.71828
using namespace std;
int T,m,cnt;
double sumn;
struct data{
double n;
bool rst;
}a[];
int i,j,k;
bool cmp(data a,data b)
{
if(a.rst!=b.rst)return a.rst>b.rst;
return a.n>b.n;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>m;
for(sumn=cnt=j=;j<m;++j)cin>>a[j].n,sumn+=pow(e,a[j].n);
for(j=;j<m;++j){
if(pow(e,a[j].n)/sumn>0.5/m)a[j].rst=,++cnt;
else a[j].rst=;
}
sort(a,a+m,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<cnt;++j)printf("%.2f\n",a[j].n);
}
return ;
}
E3137 矩形个数
First AC: 2018-05-11 Latest Modification: 2018-05-11
#include<bits/stdc++.h>
using namespace std;
int T,r,c,n,k,x,y,rst;
int mp[][];
int i,j,l;
int cnt(int x,int y)
{
int ret=,i,j,l;
for(i=x;i<=r;++i){
int tmp=;
for(j=y;j<=c;++j){
for(l=x;l<=i;++l)
if(mp[l][j])
++tmp;
if(tmp>=k){
ret+=c-j+;
break;
}
}
}
return ret;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>r>>c>>n>>k;
memset(mp,,sizeof(mp));
while(n--){
cin>>x>>y;
mp[x][y]=;
}
rst=;
for(j=;j<=r;++j)
for(l=;l<=c;++l)
rst+=cnt(j,l);
cout<<"case #"<<i<<":\n"<<rst<<endl;
}
return ;
}
E3138 Base64 编码
First AC: 2018-06-08 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt;
char s[];
int rst[];
int cas,i,j;
int main()
{
cin>>T;
for(cas=;cas<T;++cas)
{
cin>>s;
int l=strlen(s);
cnt=;
for(i=j=;i<l;++i)
{
if(cnt==)
{
rst[j++]=s[i]/;
rst[j]=s[i]%*;
}
else if(cnt==)
{
rst[j++]+=s[i]/;
rst[j]=s[i]%*;
}
else
{
rst[j++]+=s[i]/;
rst[j++]=s[i]%;
}
cnt=++cnt%;
}
if(j%)for(j++;j%;j++)rst[j]=;
cout<<"case #"<<cas<<":\n";
for(i=;i<j;++i)
if(rst[i]<)cout<<(char)('A'+rst[i]);
else if(rst[i]<)cout<<(char)('a'+rst[i]-);
else if(rst[i]<)cout<<(char)(''+rst[i]-);
else if(rst[i]==)cout<<'+';
else if(rst[i]==)cout<<'/';
else cout<<'=';
cout<<endl;;
}
return ;
}
E3139 鸡兔同笼
First AC: 2017-10-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
long i,m,n;
int main()
{
cin>>i;i=;
while(cin>>n>>m){
if(m>=*n&&m<=*n&&m%==)
cout<<(*n-m)/<<" "<<(m-*n)/<<endl;
else cout<<"No answer\n";
i++;
}
return ;
}
E3140 另一种阶乘问题
First AC: 2017-10-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,s=,R;
int main()
{
cin>>i;
while(cin>>n){
for(j=;j<=n;j++){
for(k=;k<=j;k+=)s*=k;
R+=s;
s=;
}
cout<<R<<endl;
R=;
}
return ;
}
E3141 小学生算术
First AC: 2017-10-27 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
string m,n;
int a[],b[],cnt,num;
int i,j,k;
int main()
{
while(cin>>m>>n,m!=""||n!=""){
cnt=num=;
for(i=,j=m.length()-;j>=;--j)a[i--]=(int)(m[j]-'');
for(i=,j=n.length()-;j>=;--j)b[i--]=(int)(n[j]-'');
for(i=;i>=;--i){
if(a[i]+b[i]+num>)cnt++,num=;
else num=;
a[i]=b[i]=;
}
cout<<cnt<<endl;
}
return ;
}
E3142 九九乘法表
First AC: 2017-10-17 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,i,N,cnt;
int main()
{
cin>>T;
for(i=;i<T;i++){
cin>>N;
if(cnt>)cout<<endl;
++cnt;
cout<<"1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9\n";
if(N>)cout<<"2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18\n";
if(N>)cout<<"3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27\n";
if(N>)cout<<"4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36\n";
if(N>)cout<<"5*5=25 5*6=30 5*7=35 5*8=40 5*9=45\n";
if(N>)cout<<"6*6=36 6*7=42 6*8=48 6*9=54\n";
if(N>)cout<<"7*7=49 7*8=56 7*9=63\n";
if(N>)cout<<"8*8=64 8*9=72\n";
if(N>)cout<<"9*9=81\n";
}
return ;
}
E3143 纯虚数的幂
First AC: 2018-01-17 Latest Modification: 2018-01-17
n=int(input())
for i in range(0,n):
print('case #%d:'%(i))
s,t=input().split()
s=(int)(s[:-1])
t=(int)(t)
if t%4==0:
print(s**t)
elif t%4==1:
print('%dj'%(s**t))
elif t%4==2:
print('-%d'%(s**t))
else:
print('-%dj'%(s**t))
E3144 问候
First AC: 2017-10-31 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n,i,j;
int h,m;
double s,sum;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
sum=;
for(j=;j<n;++j)scanf("%d:%d:%f",&h,&m,&s),sum+=*h+*m+s;
while(sum>)sum-=;
cout<<"case #"<<i<<":\nGood ";
if(sum>=&&sum<)cout<<"morning!\n";
else if(sum>=&&sum<)cout<<"afternoon!\n";
else if(sum>=&&sum<)cout<<"evening!\n";
else if(sum<||sum>=)
cout<<"night!\n";
}
return ;
}
E3145 互素勾股数
First AC: 2018-02-09 Latest Modification: 2018-02-09
#include<bits/stdc++.h>
using namespace std;
int T;
long a,b,c;
long r,i,j,k;
long gcd(long a,long b)
{
long c=;
while(c)c=a%b,a=b,b=c;
return a;
}
struct data{
long a,b,c;
}num[];
bool cmp(data a,data b)
{
return a.a<b.a;
}
int main()
{
cin>>T;
for(r=;r<T;++r){
cin>>c;
long m=(long)(sqrt(c/2.0))+;
for(i=,k=;i<m;++i){
j=(long)(sqrt(c-i*i));
if(i*i+j*j==c){
a=abs(i*i-j*j),b=*i*j;
if(a>b)a^=b,b^=a,a^=b;
if(gcd(a,b)==&&gcd(a,c)==&&gcd(b,c)==)
num[k].a=a,num[k].b=b,num[k++].c=c;
}
}
sort(num,num+k,cmp);
cout<<"case #"<<r<<":\n"<<k<<endl;
for(i=;i<k;++i)
cout<<num[i].a<<"^2+"<<num[i].b<<"^2="<<num[i].c<<"^2\n";
}
return ;
}
E3147 维吉尼亚密码
First AC: 2017-11-03 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,lenp,lens,cnt;
string pro,scrt;
int i,j;
int main()
{
cin>>T;
getchar();
for(i=;i<T;++i){
getline(cin,pro);getline(cin,scrt);
cout<<"case #"<<i<<":\n";
lenp=pro.length(),lens=scrt.length(),cnt=;
for(j=;j<lenp;++j)
if(pro[j]==' ')cout<<' ',++cnt;
else{
cout<<(char)(((pro[j]+scrt[(j-cnt)%
lens]-'A')-'A')%+'A');
}
puts("");
}
return ;
}
E3148 线性表比较
First AC: 2018-04-14 Latest Modification: 2018-04-14
#include<bits/stdc++.h>
using namespace std;
int n,m,M;
int a[],b[];
int i;
int main()
{
cin>>m>>n;
for(i=;i<m;++i)cin>>a[i];
for(i=;i<n;++i)cin>>b[i];
M=min(m,n);
bool jdg=;
for(i=;i<M;++i){
if(a[i]<b[i]){
cout<<"-1";
jdg=;
break;
}
else if(a[i]>b[i]){
cout<<"";
jdg=;
break;
}
}
if(jdg){
if(m<n)cout<<"-1";
else if(m<n)cout<<"";
else cout<<;
}
return ;
}
E3149 倒置顺序表
First AC: 2017-11-03 Latest Modification: 2017-11-03
#include<iostream>
using namespace std;
int N,i;
int main()
{
cin>>N;
int a[N];
for(i=;i<N;++i)cin>>a[i];
for(i=N-;i>;i--)cout<<a[i]<<" ";
cout<<a[];
return ;
}
E3150 线性表去重
First AC: 2017-11-22 Latest Modification: 2017-11-22
#include<iostream>
using namespace std;
int N,n,cnt;
int a[];
int i;
int main()
{
cin>>N;
while(N--){
cin>>n;
if(a[n]==)++a[n];
}
for(i=;i<;++i)
if(a[i]>){
if(cnt==)cout<<i,++cnt;
else cout<<' '<<i;
}
return ;
}
E3151 循环打印
First AC: 2017-12-30 Latest Modification: 2017-12-30
#include<iostream>
using namespace std;
int num,bgn,step,cnt;
int a[],b[];
int i,j,k,n;
int main()
{
cin>>num>>bgn>>step;
for(i=;i<;++i)a[i]=i;
for(i=bgn;;++i){
if(i==num+)i=;
if(a[i]){
if(++cnt==step)b[n++]=a[i],a[i]=,cnt=;
}
if(n==num)break;
}
cout<<b[];
for(i=;i<num;++i)cout<<' '<<b[i];
return ;
}
E3158 特殊计算
First AC: 2017-11-08 Latest Modification: 2017-11-08
#include<iostream>
using namespace std;
long a,b,temp;
int main()
{
cin>>a>>b;
temp=a&&b;
cout<<temp<<endl;
temp=a&b;
cout<<temp;
return ;
}
E3160 统计字符及行数
First AC: 2017-12-25 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
string s;
int len,maxl,numl,rst;
int main()
{
while(getline(cin,s)){
len=s.length();
if(len>maxl)maxl=len;
++numl,rst+=len;
}
cout<<rst<<','<<numl<<','<<maxl;
return ;
}
E3161 位运算
First AC: 2017-11-25 Latest Modification: 2017-11-25
#include<iostream>
using namespace std;
long long N,a[]={};
int p,n,i;
int main()
{
for(i=;i<;++i)a[i]=*a[i-];
cin>>N>>p>>n;
for(i=p;n--;--i)N&a[i]? N-=a[i]:N+=a[i];
cout<<N;
return ;
}
E3162 逻辑或、位或、位亦或
First AC: 2017-11-08 Latest Modification: 2017-11-08
#include<iostream>
using namespace std;
long a,b,t;
int main()
{
cin>>a>>b;
t=a||b;
cout<<t<<endl;
t=a|b;
cout<<t<<endl;
t=a^b;
cout<<t<<endl;
return ;
}
E3163 二进制中1的占比
First AC: 2017-11-03 Latest Modification: 2017-12-04
#include<iostream>
using namespace std;
long n,i,a[],s;
int main()
{
cin>>n;
for(i=;n!=;++i){
a[i]=n%,n/=;
if(a[i]==)++s;
}
if(s==)cout<<"0,0:32";
else if(s==)cout<<"32,1:1";
else {
cout<<s<<",";
for(i=;i>=;i/=)if(s%i==)break;
cout<<s/i<<":"<</i;
}
return ;
}
E3167 去掉多余的空白符
First AC: 2017-12-09 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
string s;
int len,cnt,i;
int isspace(char c)
{
if(c!='\n'&&c!=' '&&c!='\t')return ;
return ;
}
int main()
{
while(getline(cin,s)){
len=s.length();
for(cnt=,i=len-;i>=;--i){
if(cnt){
if(isspace(s[i])==)s[i]='\n';
else{cnt=;break;}
}
}
if(cnt)continue;
for(cnt=,i=;i<len;++i){
if(cnt){
if(isspace(s[i])==)s[i]='\n';
else{cnt=;break;}
}
}
for(i=;i<len;++i){
if(isspace(s[i])==||(s[i]!='\n'&&isspace(s[i-])==))
cout<<s[i];
}
cout<<endl;
}
return ;
}
E3169 字符串出现次数
First AC: 2017-11-09 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
#define N 80
using namespace std;
int count(char s[],char t[])
{
int lens=strlen(s),lent=strlen(t),i,j,cnt=,num=,M=lens-lent;
for(i=;i<=M;++i){
if(s[i]==t[]){
for(j=;j<lent;++j){
if(s[i+j]!=t[j]){num++;break;}
}
if(num==)cnt++,--i+=lent;
else num=;
}
}
return cnt;
}
int main()
{ char s[N+],t[N+];
scanf("%s%s",s,t);
printf("%d\n",count(s,t));
return ;
}
E3170 二进制转十进制
First AC: 2017-11-07 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
#define N 32
using namespace std;
unsigned b2i(char s[])
{
int len=strlen(s);
int i;
unsigned int m=,n=;
for(i=len-;i>=;--i){
if(s[i]=='')m+=n;
n*=;
}
return m;
}
int main()
{
char s[N+];
scanf("%s",s);
printf("%u\n",b2i(s));
return ;
}
E3171 波兰表达式
First AC: 2017-12-16 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int len,flag,i,cnt;
char a[],c;
double num,tmp1,tmp2;
int sig[];
stack<char>m;
stack<double>n;
int main()
{
while(){
cin>>a;
len=strlen(a);
flag=;
if(len==){
switch(a[]){
case '+':case '-': case '*':case '/':
m.push(a[]);flag=;++cnt;break;
}
}
if(flag){
num=atof(a);
n.push(num);
++sig[cnt];
}
while(sig[cnt]>&&m.size()&&n.size()>){
tmp2=n.top();
n.pop();
tmp1=n.top();
n.pop();
c=m.top();
m.pop();
switch(c){
case '+':n.push(tmp1+tmp2);break;
case '-':n.push(tmp1-tmp2);break;
case '*':n.push(tmp1*tmp2);break;
case '/':n.push(tmp1/tmp2);break;
}
sig[cnt]=,++sig[--cnt];
}
c=getchar();
if(c!=' '){
num=n.top();
n.pop();
printf("%.6f",num);
cnt=;
return ;
}
}
}
E3172 整数转化为字符串
First AC: 2017-11-20 Latest Modification: 2017-11-20
#include<iostream>
using namespace std;
string s;
int main()
{
cin>>s;
cout<<s<<endl;
return ;
}
E3182 字符串排序
First AC: 2018-01-03 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n;
struct data{
int num,len;
string s;
}a[];
int i,j,k;
bool cmp(data m,data n)
{
if(m.num^n.num)return m.num<n.num;
int tmp=m.len<n.len?m.len:n.len;
for(int i=;i<tmp;++i)if(m.s[i]-n.s[i])return m.s[i]<n.s[i];
return m.len<n.len;
}
int main()
{
cin>>T;
for(k=;k<T;++k){
cin>>n;
for(i=;i<n;++i){
cin>>a[i].s;
a[i].num=-,a[i].len=a[i].s.length();
for(j=;j<a[i].len;++j)
if(a[i].s[j]>=''&&a[i].s[j]<=''){
a[i].num=a[i].s[j]-'';
break;
}
}
sort(a,a+n,cmp);
cout<<"case #"<<k<<":\n"<<a[].s;
for(i=;i<n;++i)cout<<' '<<a[i].s;
cout<<endl;
}
return ;
}
E3183 坐标排序
First AC: 2017-12-06 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n;
int i,j;
struct data{
long long dis;
int x,y,qua,absx;
}a[];
bool cmp(data a,data b)
{
if(a.dis-b.dis)return a.dis<b.dis;
if(a.qua-b.qua)return a.qua<b.qua;
return a.absx<b.absx;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cin>>a[j].x>>a[j].y;
a[j].dis=a[j].x*a[j].x+a[j].y*a[j].y;
if(a[j].y>=){
if(a[j].x>=)a[j].qua=,a[j].absx=a[j].x;
else a[j].qua=,a[j].absx=-a[j].x;
}
else{
if(a[j].x>)a[j].qua=,a[j].absx=a[j].x;
else a[j].qua=,a[j].absx=-a[j].x;
}
}
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n-;++j)cout<<'('<<a[j].x<<','<<a[j].y<<')'<<' ';
cout<<'('<<a[n-].x<<','<<a[n-].y<<')'<<endl;
}
return ;
}
E3184 二维数组排序
First AC: 2017-12-21 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
struct data{int n[],sumn;}a[];
int T,n,m,cnt;
int i,j,k;
bool cmp(data a,data b)
{
if(a.sumn^b.sumn)return a.sumn<b.sumn;
for(int i=;i<m;++i)if(a.n[i]^b.n[i])return a.n[i]<b.n[i];
return a.n[]<b.n[];
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n>>m;
for(j=;j<n;++j){
for(cnt=k=;k<m;++k)cin>>a[j].n[k],cnt+=a[j].n[k];
a[j].sumn=cnt;
}
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j){
cout<<a[j].n[];
for(k=;k<m;++k)cout<<' '<<a[j].n[k];
cout<<endl;
}
}
return ;
}
E3185 双阶乘的质因数个数
First AC: 2018-01-05 Latest Modification: 2019-04-14
#include<bits/stdc++.h>
using namespace std;
int T,n,m;
int tmp,ans;
int i;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n>>m;
ans=;
while(n>){
tmp=n;
while(tmp%m==){
++ans;
tmp/=m;
}
n-=;
}
cout<<"case #"<<i<<":\n"<<ans<<endl;
}
return ;
}
E3186 A+B
First AC: 2017-11-13 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,num,rst1,rst2;
string s,t[];
int i;
int main()
{
cin>>T;
for(i=;i<T;++i){
cout<<"case #"<<i<<":\n";
while(cin>>s){
if(s.length()!=){
t[num++]=s;
}
else if(s=="+"){
if(t[]=="one")rst1=;
else if(t[]=="two")rst1=;
else if(t[]=="three")rst1=;
else if(t[]=="four")rst1=;
else if(t[]=="five")rst1=;
else if(t[]=="six")rst1=;
else if(t[]=="seven")rst1=;
else if(t[]=="eight")rst1=;
else if(t[]=="nine")rst1=;
else rst1=;
if(num==){
rst1*=;
if(t[]=="one")rst1+=;
else if(t[]=="two")rst1+=;
else if(t[]=="three")rst1+=;
else if(t[]=="four")rst1+=;
else if(t[]=="five")rst1+=;
else if(t[]=="six")rst1+=;
else if(t[]=="seven")rst1+=;
else if(t[]=="eight")rst1+=;
else if(t[]=="nine")rst1+=;
}
num=;
}
else{
if(t[]=="one")rst2=;
else if(t[]=="two")rst2=;
else if(t[]=="three")rst2=;
else if(t[]=="four")rst2=;
else if(t[]=="five")rst2=;
else if(t[]=="six")rst2=;
else if(t[]=="seven")rst2=;
else if(t[]=="eight")rst2=;
else if(t[]=="nine")rst2=;
else rst2=;
if(num==){
rst2*=;
if(t[]=="one")rst2+=;
else if(t[]=="two")rst2+=;
else if(t[]=="three")rst2+=;
else if(t[]=="four")rst2+=;
else if(t[]=="five")rst2+=;
else if(t[]=="six")rst2+=;
else if(t[]=="seven")rst2+=;
else if(t[]=="eight")rst2+=;
else if(t[]=="nine")rst2+=;
}
num=,cout<<rst1+rst2<<endl;
break;
}
}
}
return ;
}
E3187 凹数
First AC: 2018-02-21 Latest Modification: 2018-02-21
#include<bits/stdc++.h>
using namespace std;
long cnt;
int tmp1,tmp2,tmp3;
long d[];
int T,a,b;
long i,j,k;
bool jdg(long n)
{
int cnt=;
tmp1=n%,n/=;
tmp2=n%,n/=;
if(tmp1==tmp2)return ;
while(n){
tmp3=n%,n/=;
if(tmp2==tmp3)return ;
if(tmp2<tmp1&&tmp2<tmp3)++cnt;
else if(tmp2>tmp1&&tmp2>tmp3)return ;
tmp1=tmp2,tmp2=tmp3;
}
if(cnt!=)return ;
return ;
}
int main()
{
for(i=;i<;++i){
if(jdg(i))++cnt;
d[i]=cnt;
}
cin>>T;
for(i=;i<T;++i){
cin>>a>>b;
cout<<"case #"<<i<<":\n"<<d[b]-d[a-]<<endl;
}
return ;
}
E3188 坏掉的彩灯
First AC: 2017-11-08 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,len,M,m,R,B,Y,G,temp,cnt;
string s;
int i,j,k;
int main()
{
cin>>T,getchar();
for(i=;i<T;++i){
getline(cin,s);
cout<<"case #"<<i<<":\n";
len=s.length();
R=B=Y=G=M=len/,m=len%,temp=len-m,cnt=;
for(j=;j<temp;++j)
if(s[j]=='R')R--;
else if(s[j]=='B')B--;
else if(s[j]=='G')G--;
else if(s[j]=='Y')Y--;
if(m==){
cout<<R<<' '<<B<<' '<<Y<<' '<<G<<endl;
continue;
}
for(j=;j<=m;++j)
if(s[len-j]=='!'){
for(k=;k<=len;k+=)
if(s[len-j-k]!='!'){
cnt++;
s[len-j]=s[len-j-k];
if(s[len-j-k]=='R')R++;
else if(s[len-j-k]=='B')B++;
else if(s[len-j-k]=='G')G++;
else if(s[len-j-k]=='Y')Y++;
break;
}
}
else cnt++;
if(cnt==m)cout<<R<<' '<<B<<' '<<Y<<' '<<G<<endl;
else{
++R,++B,++Y,++G;
for(j=;j>m;--j)
for(k=-;j+k<len;k+=)
if(s[j+k]!='!'){
if(s[j+k]=='R')R--;
else if(s[j+k]=='B')B--;
else if(s[j+k]=='G')G--;
else if(s[j+k]=='Y')Y--;
break;
}
for(j=;j<=m;++j)
if(s[len-j]=='R')R--;
else if(s[len-j]=='B')B--;
else if(s[len-j]=='G')G--;
else if(s[len-j]=='Y')Y--;
cout<<R<<' '<<B<<' '<<Y<<' '<<G<<endl;
} }
return ;
}
E3189 求和
First AC: 2018-05-11 Latest Modification: 2018-05-11
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,n,m,num,l,r;
ll a[],b[];
ll i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n>>m;
for(j=;j<=n;++j)cin>>a[j],a[j]+=a[j-];
num=;
for(j=;j<n;++j)for(k=j+;k<=n;++k)b[num++]=a[k]-a[j];
sort(b+,b+num);
for(j=;j<num;++j)b[j]+=b[j-];
cout<<"case #"<<i<<":\n";
while(m--){
cin>>l>>r;
cout<<b[r]-b[l-]<<endl;
}
}
return ;
}
E3190 平衡三进制
First AC: 2017-11-25 Latest Modification: 2018-03-09
#include<bits/stdc++.h>
using namespace std;
int a[]={};
string s;
int T,len,sum;
int i,j,k; int main()
{
for(i=;i<;++i)a[i]=*a[i-];
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length()-;
for(j=len,sum=k=;j+;--j,++k)
if(s[j]=='-')sum-=a[k];
else if(s[j]=='')sum+=a[k];
cout<<"case #"<<i<<":\n"<<sum<<endl;
}
return ;
}
E3191 闰年问题
First AC: 2017-11-05 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,d;
long long y,n1,n2;
string m;
int i;
int main()
{
cin>>T;
for(i=;i<T;++i){
cout<<"case #"<<i<<":\n";
cin>>m,scanf("%d,%ld",&d,&y);
n1=y/-y/+y/;
if((y%==&&y%!=||y%==)&&
(m=="February"||m=="January"))--n1;
cin>>m,scanf("%d,%ld",&d,&y);
n2=y/-y/+y/;
if((y%==&&y%!=||y%==)&&
((m=="February"&&d<)||m=="January"))--n2;
cout<<n2-n1<<endl;
}
return ;
}
E3193键盘
First AC: 2017-12-21 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,a,b,c,m,pri,cntu,cntp,sumn,sump,tmpu,tmpp,cnt;
int u[],p[],tmp[];
string s;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>a>>b>>c>>m;
for(cntu=cntp=j=;j<m;++j){
cin>>pri>>s;
if(s[]=='U')u[cntu++]=pri;
else p[cntp++]=pri;
}
cout<<"case #"<<i<<":\n";
sort(u,u+cntu),sort(p,p+cntp);
sumn=sump=cnt=;
if(cntu>=a){
sumn=a;
for(j=;j<a;++j)sump+=u[j];
for(j=a;j<cntu;++j)tmp[cnt++]=u[j];
}
else{
sumn=cntu;
for(j=;j<cntu;++j)sump+=u[j];
}
if(cntp>=b){
sumn+=b;
for(j=;j<b;++j)sump+=p[j];
for(j=b;j<cntp;++j)tmp[cnt++]=p[j];
}
else{
sumn+=cntp;
for(j=;j<cntp;++j)sump+=p[j];
}
if(c){
sort(tmp,tmp+cnt);
if(c>cnt){
sumn+=cnt;
for(j=;j<cnt;++j)sump+=tmp[j];
}
else{
sumn+=c;
for(j=;j<c;++j)sump+=tmp[j];
}
}
cout<<sumn<<' '<<sump<<endl;
}
return ;
}
E3194 字符串消除
First AC: 2018-03-15 Latest Modification: 2018-03-15
#include<bits/stdc++.h>
using namespace std;
int T,len,rst,tmp;
string s,t;
int i,j;
int cnt(string s)
{
int num=s.length();
while(){
s=" "+s+" ";
int len=s.length()-,i;
string t="";
for(i=;i<len;++i){
if(s[i]!=s[i-]&&s[i]!=s[i+])t+=s[i];
}
if(t.length()==len-)return num-len+;
s=t;
}
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
rst=;
for(j=;j<len;++j){
tmp=cnt(s.substr(,j)+'A'+s.substr(j,len));
if(tmp>rst)rst=tmp;
tmp=cnt(s.substr(,j)+'B'+s.substr(j,len));
if(tmp>rst)rst=tmp;
tmp=cnt(s.substr(,j)+'C'+s.substr(j,len));
if(tmp>rst)rst=tmp;
}
tmp=cnt(s+'A');
if(tmp>rst)rst=tmp;
tmp=cnt(s+'B');
if(tmp>rst)rst=tmp;
tmp=cnt(s+'C');
if(tmp>rst)rst=tmp;
cout<<"case #"<<i<<":\n"<<rst<<endl;
}
return ;
}
E3195 Ubiquitous Religions
First AC: 2018-06-08 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,rst,cas;
int pre[];
int i;
int find(int x)
{
int r=x,i=x,j;
while(pre[r]!=r)r=pre[r];
while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)pre[fx]=fy;
}
int main()
{
while(cin>>n>>m,n){
for(i=;i<=n;++i)pre[i]=i;
while(m--){
cin>>x>>y;
join(x,y);
}
rst=;
for(i=;i<=n;++i){
if(pre[i]==i)++rst;
}
cout<<"Case "<<++cas<<": "<<rst<<endl;
}
return ;
}
E3196 WormHoles
First AC: 2019-02-09 Latest Modification: 2019-02-09
#include<bits/stdc++.h>
using namespace std;
const int maxn=,maxm=;
int F,n,m,w,ne;
int u,v,val;
int h[maxn],dis[maxn],vis[maxn],cnt[maxn];
queue<int>q,emp;
struct edge{
int to,val,nxt;
}e[maxm];
void insert(int u,int v,int val)
{
e[++ne].to=v;
e[ne].val=val;
e[ne].nxt=h[u];
h[u]=ne;
}
bool spfa(int ask)
{
memset(dis,0x3f,sizeof dis);
dis[ask]=;
memset(vis,,sizeof vis);
vis[ask]=;
memset(cnt,,sizeof cnt);
++cnt[ask];
q=emp;
q.push(ask);
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=;
for(int i=h[x];i;i=e[i].nxt){
int to=e[i].to;
if(dis[to]>dis[x]+e[i].val){
dis[to]=dis[x]+e[i].val;
if(!vis[to]){
vis[to]=;
if(++cnt[to]>=n)return ;
q.push(to);
}
}
}
}
return ;
}
int main()
{
cin>>F;
while(F--){
cin>>n>>m>>w;
ne=;
memset(h,,sizeof h);
while(m--){
cin>>u>>v>>val;
insert(u,v,val);
insert(v,u,val);
}
while(w--){
cin>>u>>v>>val;
insert(u,v,-val);
}
spfa()? cout<<"NO\n":cout<<"YES\n";
}
return ;
}
E3200 Six Degrees of Cowvin Bacon
First AC: 2019-02-07 Latest Modification: 2019-02-07
#include<bits/stdc++.h>
using namespace std;
int n,m,num,rst,tmp;
int dis[][];
int movie[];
int sumdegree[];
int i,j,k;
int main()
{
cin>>n>>m;
memset(dis,0x3f,sizeof dis);
for(i=;i<=n;++i)dis[i][i]=;
while(m--){
cin>>num;
for(i=;i<num;++i)cin>>movie[i];
for(i=;i<num;++i){
for(j=i+;j<num;++j){
dis[movie[i]][movie[j]]=;
dis[movie[j]][movie[i]]=;
}
}
}
for(k=;k<=n;++k){
for(i=;i<=n;++i){
for(j=;j<=n;++j){
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
rst=1e6;
for(i=;i<=n;++i){
tmp=;
for(j=;j<=n;++j){
if(i!=j)tmp+=dis[i][j];
}
rst=min(rst,tmp);
}
cout<<(int)(rst*100.0/(n-));
return ;
}
E3202 RoadBlocks
First AC: 2019-02-13 Latest Modification: 2019-02-13
Note: 添加一个数组dis2[]存次短路,注意更新时要避免||的短路操作
#include<bits/stdc++.h>
using namespace std;
const int maxn=,maxm=;
int n,m,ne;
int u,v,val;
int h[maxn],dis[maxn],dis2[maxn],vis[maxn],cnt[maxn];
struct edge{
int to,val,nxt;
}e[maxm];
queue<int> q,emp;
void insert(int u,int v,int val)
{
e[++ne].to=v;
e[ne].val=val;
e[ne].nxt=h[u];
h[u]=ne;
}
bool update(int index,int val)
{
if(dis[index]==val||dis2[index]<=val)return ;
if(dis[index]>val){
dis2[index]=dis[index];
dis[index]=val;
}
else{
dis2[index]=val;
}
return ;
}
bool spfa(int ask)
{
memset(dis,0x3f,sizeof dis);
dis[ask]=;
memset(dis2,0x3f,sizeof dis2);
memset(vis,,sizeof vis);
vis[ask]=;
memset(cnt,,sizeof cnt);
++cnt[ask];
q=emp;
q.push(ask);
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=;
for(int i=h[x];i;i=e[i].nxt){
int to=e[i].to;
if(update(to,dis[x]+e[i].val)|update(to,dis2[x]+e[i].val)){
if(!vis[to]){
if(++cnt[to]>=n)return ;
vis[to]=;
q.push(to);
}
}
}
}
return ;
}
int main()
{
cin>>n>>m;
while(m--){
cin>>u>>v>>val;
insert(u,v,val);
insert(v,u,val);
}
spfa();
cout<<dis2[n];
return ;
}
E3205 Prime Gap
First AC: 2018-05-05 Latest Modification: 2018-05-05
#include<bits/stdc++.h>
using namespace std;
bool pri[];
int n;
int i,j;
int main()
{
ios::sync_with_stdio(false);
for(i=;i<;++i)
if(!pri[i])
for(j=i+i;j<;j+=i)
pri[j]=;
while(cin>>n,n){
i=j=n;
while(pri[i])--i;
while(pri[j])++j;
cout<<j-i<<endl;
}
return ;
}
E3206 Aggressive Cows
First AC: 2018-05-06 Latest Modification: 2018-05-06
#include<bits/stdc++.h>
using namespace std;
int n,c;
int x[];
int i;
bool jdg(int dis)
{
int tmp=,num=;
for(int i=;i<n;++i){
tmp+=x[i]-x[i-];
if(tmp>=dis)tmp=,++num;
}
if(num>=c)return ;
return ;
}
void find(int lft,int rgt)
{
if(lft+>=rgt){
if(jdg(rgt))cout<<rgt;
else cout<<lft;
exit();
}
int mid=(lft+rgt)/;
if(jdg(mid))find(mid,rgt);
else find(lft,mid);
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>c;
for(i=;i<n;++i)cin>>x[i];
sort(x,x+n);
find(,x[n-]-x[]);
return ;
}
E3209 分数加法
First AC: 2018-05-16 Latest Modification: 2018-05-16
from fractions import Fraction
T=int(input())
a=0
for i in range(T):
a+=Fraction(input())
print(a)
E3210 Maximum Element
First AC: 2018-02-13 Latest Modification: 2018-02-13
#include<bits/stdc++.h>
using namespace std;
int n,q,x,tmp;
stack<int>team,maxu;
int main()
{
cin>>n;
while(n--)
{
scanf("%d",&q);
if(q==){
cin>>x;
team.push(x);
if(maxu.empty())maxu.push(x);
else if(x>=maxu.top())maxu.push(x);
}
else if(q==){
tmp=team.top();
team.pop();
if(tmp==maxu.top())maxu.pop();
}
else cout<<maxu.top()<<endl;
}
}
E3211 Rails
First AC: 2018-04-13 Latest Modification: 2018-04-13
#include<bits/stdc++.h>
using namespace std;
int n,m;
stack<int>s,t,emps;
queue<int>q,empq;
int i;
bool op()
{
if(!t.empty()&&t.top()==q.front()){
t.pop();
q.pop();
return ;
}
while(!s.empty()&&s.top()!=q.front()){
t.push(s.top());
s.pop();
}
if(!s.empty()&&s.top()==q.front()){
s.pop();
q.pop();
return ;
}
else return ;
}
int main()
{
while(cin>>n,n){
while(cin>>m){
if(!m){
cout<<endl;
break;
}
s=emps,t=emps,q=empq;
q.push(m);
for(i=;i<n;++i)cin>>m,q.push(m);
for(i=n;i;--i)s.push(i);
bool jdg=;
for(i=;i<n;++i){
if(!op()){
jdg=;
break;
}
}
jdg? cout<<"Yes\n":cout<<"No\n";
}
}
return ;
}
E3212 Balances Brackets
First AC: 2018-04-13 Latest Modification: 2018-04-13
#include<bits/stdc++.h>
using namespace std;
int n,len;
string s;
stack<char>stk,emp;
int i;
int main()
{
cin>>n;
while(n--){
cin>>s;
len=s.length();
stk=emp;
bool jdg=;
for(i=;i<len;++i){
if(s[i]==')'){
if(stk.empty()||stk.top()!='('){
jdg=;
break;
}
else stk.pop();
}
else if(s[i]==']'){
if(stk.empty()||stk.top()!='['){
jdg=;
break;
}
else stk.pop();
}
else if(s[i]=='}'){
if(stk.empty()||stk.top()!='{'){
jdg=;
break;
}
else stk.pop();
}
else stk.push(s[i]);
}
if(jdg&&stk.empty())cout<<"YES\n";
else cout<<"NO\n";
}
return ;
}
E3213 向右看齐
First AC: 2018-06-04 Latest Modification: 2018-06-04
#include<bits/stdc++.h>
using namespace std;
int n,h;
int a[];
stack< pair<int,int> >stk;
int i;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(i=;i<=n;++i){
cin>>h;
while(!stk.empty()&&stk.top().first<h){
a[stk.top().second]=i;
stk.pop();
}
stk.push(pair<int,int>(h,i));
}
for(i=;i<=n;++i)cout<<a[i]<<endl;
return ;
}
E3221 北京记者跑得最快
First AC: 2017-11-01 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
long L,v,a,i,s,b;
int main()
{
cin>>L>>v>>a;
s=v*v,b=a*;
for(i=;i<L;++i){
s+=b;printf("%.7f\n",(sqrt(s)-v)/a);
}
return ;
}
E3222 六六六
First AC: 2018-06-08 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,x,tmp,sum;
bool jdg[];
int i,j;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>x;
memset(jdg,,sizeof(jdg));
cout<<"Case "<<i<<": ";
sum=tmp=%x;
bool rst=;
for(j=;j<=x;++j){
if(!sum){
cout<<j<<endl;
rst=;
break;
}
tmp=*tmp%x;
(sum+=tmp)%=x;
}
if(rst)cout<<"-1\n";
}
return ;
}
E3226 声控开关(Easy)
First AC: 2018-12-17 Latest Modification: 2018-12-17
#include<bits/stdc++.h>
using namespace std;
int T,n,k;
int a[];
int i,j;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n>>k;
for(j=;j<;++j){
a[j]=k&;
k>>=;
}
cout<<"Case "<<i<<": O";
bool flag=;
for(j=;j<n;++j){
if(!a[j]){
cout<<"FF\n";
flag=;
break;
}
}
if(flag)cout<<"N\n";
}
return ;
}
E3227 声控开关(Hard)
First AC: 2018-12-17 Latest Modification: 2018-12-17
#include<bits/stdc++.h>
using namespace std;
int T,n,k;
int a[];
int i,j;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n>>k;
for(j=;j<;++j){
a[j]=k&;
k>>=;
}
cout<<"Case "<<i<<": O";
bool flag=;
for(j=;j<n;++j){
if(!a[j]){
cout<<"FF\n";
flag=;
break;
}
}
if(flag)cout<<"N\n";
}
return ;
}
E3233 N!
First AC: 2017-10-27 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
long n;
int main()
{
while(cin>>n)
cout<<n/+n/+n/+n/+n/+n/
+n/+n/+n/+n/
+n/+n/<<endl;
return ;
}
E3234 Sort
First AC: 2018-04-27 Latest Modification: 2018-04-27
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M=;
ll a[M],b[M];
ll n,rst;
ll i,j,k;
void Merge(ll a[],ll lft,ll mid,ll rgt)
{
i=lft,j=mid+,k=lft;
while(i<=mid&&j<=rgt){
if(a[i]<=a[j])b[k++]=a[i++];
else rst+=j-k,b[k++]=a[j++];
}
while(i<=mid)b[k++]=a[i++];
while(j<=rgt)b[k++]=a[j++];
for(i=lft;i<=rgt;++i)a[i]=b[i];
}
void MergeSort(ll a[],ll lft,ll rgt)
{
if(lft<rgt){
ll mid=(lft+rgt)/;
MergeSort(a,lft,mid);
MergeSort(a,mid+,rgt);
Merge(a,lft,mid,rgt);
}
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n){
for(i=;i<n;++i)cin>>a[i];
rst=;
MergeSort(a,,n-);
cout<<rst<<endl;
}
return ;
}
E3236 因子平方和
First AC: 2017-10-30 Latest Modification: 2017-10-30
#include<iostream>
using namespace std;
int T,s,n;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
s=;
cin>>n;
for(j=;j<n;++j)if(n%j==)s+=j*j;
cout<<"case #"<<i<<":\n"<<s<<endl;
}
return ;
}
E3237 n!进制
First AC: 2017-12-11 Latest Modification: 2017-12-11
#include<iostream>
using namespace std;
long a[]={,};
int T,i,j,cnt;
long n,tmp;
int main()
{
for(i=;i<;++i)a[i]=a[i-]*i;
cin>>T;
for(i=;i<T;++i){
cin>>n;
cout<<"case #"<<i<<":\n";
for(cnt=,j=;j;--j){
if(tmp=n/a[j])cnt=;
if(cnt)cout<<tmp;
n%=a[j];
}
cout<<endl;
}
return ;
}
E3238 字串非重复字符数排序
First AC: 2017-11-24 Latest Modification: 2018-03-01
#include<bits/stdc++.h>
using namespace std;
int T,n,len;
struct data{
string s;
int len,num;
}a[];
bool jdg[];
int i,j,k;
bool cmp(data a,data b)
{
if(a.num!=b.num)return a.num>b.num;
return a.s<b.s;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cin>>a[j].s;
a[j].len=a[j].s.length();
a[j].num=;
memset(jdg,,sizeof(jdg));
for(k=;k<a[j].len;++k){
if(!jdg[a[j].s[k]-'A'])++a[j].num,jdg[a[j].s[k]-'A']=;
}
}
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j)cout<<a[j].s<<endl;
}
return ;
}
E3239 最长的等差数列
First AC: 2017-12-23 Latest Modification: 2018-06-08
#include<bits/stdc++.h>
using namespace std;
int T,n,rst,cnt,tmp;
int a[];
int i,j,k,r;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(rst=j=;j<n;++j)cin>>a[j];
cout<<"case #"<<i<<":\n";
if(n==){cout<<<<endl;continue;}
sort(a,a+n);
for(j=;j<n;++j){
for(k=j+;k<n;++k){
tmp=a[k]-a[j];
for(cnt=,r=k+;r<n;++r){
if(a[r]==a[j]+cnt*tmp)++cnt;continue;
if(a[r]>a[j]+cnt*tmp&&a[r-]<a[j]+cnt*tmp)break;
}
if(cnt>rst)rst=cnt;
}
}
cout<<rst<<endl;
}
return ;
}
E3240 小香农范诺编码
First AC: 2018-03-07 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
int T,n;
int suma[];
struct data{
int n;
string s;
}a[];
int i,j;
void code(int lft,int rgt)
{
int m=suma[lft]+suma[rgt];
int i,j;
for(i=lft+;;++i){
if((abs(*suma[i+]-m)>=abs(*suma[i]-m)))break;
}
for(j=lft;j<i;++j)a[j].s+='';
for(j=i;j<rgt;++j)a[j].s+='';
if(i-lft>)code(lft,i);
if(rgt-i>)code(i,rgt);
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j)
cin>>a[j].n,suma[j+]=suma[j]+a[j].n,a[j].s="";
code(,n);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j)cout<<a[j].n<<':'<<a[j].s<<endl;
}
return ;
}
E3241 字母替换
First AC: 2017-10-27 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,i,j,l;
string s;
int main()
{
cin>>T;
for(i=;i<T;++i){
cout<<"case #"<<i<<":\n";
cin>>s;
l=s.length();
for(j=;j<l;++j)
if(j%==&&s[j]>='A'&&s[j]<='Z')
cout<<(char)(s[j]-'A'+'a');
else cout<<s[j];
cout<<endl;
}
return ;
}
E3242 重复数
First AC: 2017-11-04 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,n;
int a[],b[],M;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
memset(a,,sizeof(a)),memset(b,,sizeof(b)),M=;
for(j=;j<n;++j)cin>>a[j];
for(j=;j<n-;++j){
for(k=j+;k<n;++k)if(a[j]==a[k])++b[j];
if(b[j]>M)M=b[j];
}
cout<<"case #"<<i<<":\n"<<M+<<endl;
}
return ;
}
E3243 搜索联系人
First AC: 2017-12-22 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
long T,n,ls,cnt;
struct data{
string name,tel;
long lt;
bool flag;
}a[];
string s;
long i,j,k,l;
bool cmp(data a,data b)
{
if(a.flag!=b.flag)return a.flag>b.flag;
if(a.name!=b.name){
int len1=a.name.length(),len2=b.name.length();
int tmp=len1<len2? len1:len2;
for(int i=;i<tmp;++i)
if(a.name[i]-b.name[i])
return a.name[i]<b.name[i];
return a.name.length()<b.name.length();
}
int len1=a.tel.length(),len2=b.tel.length();
int tmp=len1<len2? len1:len2;
for(int i=;i<tmp;++i)if(a.tel[i]-b.tel[i])
return a.tel[i]<b.tel[i];
return a.tel.length()<b.tel.length();
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cin>>a[j].name>>a[j].tel;
a[j].lt=a[j].tel.length();
a[j].flag=;
}
cin>>s,ls=s.length();
for(cnt=j=;j<n;++j){
for(k=;k<=a[j].lt-ls;++k){
if(a[j].tel[k]==s[]){
for(a[j].flag=l=;l<ls;++l)
if(a[j].tel[k+l]-s[l]){
a[j].flag=;
break;
}
if(a[j].flag){
++cnt;
break;
}
}
}
}
sort(a,a+n,cmp);
cout<<"case #"<<i<<":\n"<<cnt<<endl;
for(j=;j<cnt;++j)
cout<<a[j].name<<' '<<a[j].tel<<endl;
}
return ;
}
E3244 KL排序
First AC: 2018-03-07 Latest Modification: 2018-03-07
#include<bits/stdc++.h>
using namespace std;
int T,K,n,m;
int timex[];
double px[];
struct data{
int num;
double timey[];
double kl;
}y[];
int i,j,k;
bool cmp(data a,data b)
{
if(fabs(a.kl-b.kl)>1e-)return a.kl<b.kl;
return a.num<b.num;
}
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>K>>n;
for(m=j=;j<K;++j)
cin>>timex[j],m+=timex[j];
for(j=;j<K;++j)
px[j]=(timex[j]+1.0/K)/(m+);
for(j=;j<n;++j){
y[j].num=j+;
for(m=k=;k<K;++k)
cin>>y[j].timey[k],m+=y[j].timey[k];
for(y[j].kl=k=;k<K;++k){
y[j].kl+=px[k]*log(px[k]/((y[j].timey[k]+1.0/K)/(m+)));
if(fabs(y[j].kl)<1e-)y[j].kl=0.0000;
}
}
sort(y,y+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j)printf("%d %.4f\n",y[j].num,y[j].kl);
}
return ;
}
E3245 找数
First AC: 2018-02-19 Latest Modification: 2018-02-19
#include<bits/stdc++.h>
using namespace std;
int T,len,jdg1,jdg2;
string s;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
cout<<"case #"<<i<<":\n";
if(len==){cout<<s<<endl;continue;}
jdg1=;
while(jdg1){
jdg2=;
for(j=;j<len;++j){
if(s[j]<s[j-]){
jdg2=;
//s[j-1]!='0'
--s[j-];
for(k=j;k<len;++k)s[k]='';
break;
}
}
if(jdg2)continue;
jdg1=;
}
for(j=;;++j)if(s[j]!=''){cout<<s[j];break;}
for(k=j+;k<len;++k)cout<<s[k];
cout<<endl;
}
return ;
}
E3250 计软联谊
First AC: 2018-05-10 Latest Modification: 2018-05-10
#include<bits/stdc++.h>
using namespace std;
vector<int>all_fac[];
vector<int>::iterator it;
int T,n,K;
int a[];
int i,j,k,cas;
int main()
{
ios::sync_with_stdio(false);
for(i=;i<;++i)
for(j=i;j<;j+=i)
all_fac[j].insert(all_fac[j].begin(),i);
cin>>T;
for(cas=;cas<=T;++cas){
cin>>n>>K;
for(i=;i<n;++i)cin>>a[i];
cout<<"Case "<<cas<<":";
for(j=;j<n;++j){
int tmp=__gcd(a[j],a[j-]);
if(all_fac[tmp].size()+<K)cout<<" -1";
else if(all_fac[tmp].size()+==K)cout<<"";
else cout<<' '<<*(all_fac[tmp].begin()+K-);
}
cout<<endl;
}
return ;
}
E3251 数青蛙
First AC: 2018-05-11 Latest Modification: 2018-05-11
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M=1e18;
int n;
ll a[],tmp,up,dn;
int i;
bool ok(ll up1,ll dn1,ll up2,ll dn2)
{
tmp=__gcd(up1,dn1);
up1/=tmp,dn1/=tmp;
tmp=__gcd(up2,dn2);
up2/=tmp,dn2/=tmp;
if(up1!=up2||dn1!=dn2)return ;
return ;
}
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
if(n==){
if(a[]==M)a[]=;
else ++a[];
}
else if(n==){
if(!a[])a[]=;
else if(!a[])a[]=;
else if(a[]!=)a[]=;
else if(a[]!=)a[]=;
else a[]=;
}
else if(n==){
if(!a[]){
up=a[],dn=a[];
tmp=__gcd(up,dn);
up/=tmp,dn/=tmp;
a[]=a[]/dn*up;//必改0,必整除
}
else if(!a[]){
tmp=__gcd(a[],a[]);
up=a[]/tmp,dn=a[]/tmp;
if((ll)sqrt(up)*(ll)sqrt(up)==up)
a[]=(ll)sqrt(up)*(ll)sqrt(dn)*tmp;
else a[]=tmp*sqrt(up*dn);//必改0
}
else if(!a[]){
up=a[],dn=a[];
tmp=__gcd(up,dn);
up/=tmp,dn/=tmp;
a[]=a[]/dn*up;//必改0,必整除
}
else{
bool jdg=;
if(jdg){//试改a[0]
up=a[],dn=a[];
tmp=__gcd(up,dn);
up/=tmp,dn/=tmp;
if(!(a[]%dn)&&a[]*1.0/dn<=M*1.0/up){
a[]=a[]/dn*up;
jdg=;
}
}
if(jdg){//试改a[2]
up=a[],dn=a[];
tmp=__gcd(up,dn);
up/=tmp,dn/=tmp;
if(!(a[]%dn)&&a[]*1.0/dn<=M*1.0/up){
a[]=a[]/dn*up;
jdg=;
}
}
if(jdg){//必改a[1]
tmp=__gcd(a[],a[]);
up=a[]/tmp,dn=a[]/tmp;
if((ll)sqrt(up)*(ll)sqrt(up)==up)
a[]=(ll)sqrt(up)*(ll)sqrt(dn)*tmp;
else a[]=tmp*sqrt(up*dn);
}
}
}
else{//n>3,前2元与末两元必有一组无误
bool jdg=;
for(i=;i<n;++i){//有0必改
if(!a[i]){
if(i==){
up=a[],dn=a[];
tmp=__gcd(up,dn);
up/=tmp,dn/=tmp;
a[]=a[]/dn*up;
}
else if(i==){
tmp=__gcd(a[],a[]);
up=a[]/tmp,dn=a[]/tmp;
if((ll)sqrt(up)*(ll)sqrt(up)==up)
a[]=(ll)sqrt(up)*(ll)sqrt(dn)*tmp;
else a[]=tmp*sqrt(up*dn);
}
else{
up=a[i-],dn=a[i-];
tmp=__gcd(up,dn);
up/=tmp,dn/=tmp;
a[i]=a[i-]/dn*up;
}
jdg=;
}
}
if(jdg){//试验证前2元
ll b[];
for(i=;i<n;++i)b[i]=a[i];
int cnt=;
for(i=;i<n;++i){
if(!ok(b[],b[],b[i-],b[i])){//试改
up=b[i-],dn=b[i-];
tmp=__gcd(up,dn);
up/=tmp,dn/=tmp;
if(b[i-]%dn||b[i-]*1.0/dn>M*1.0/up){
cnt=;
break;
}
b[i]=b[i-]/dn*up;
++cnt;
}
}
if(cnt==){
jdg=;
for(i=;i<n;++i)a[i]=b[i];
}
}
if(jdg){//必为后2元
ll b[];
for(i=;i<n;++i)b[i]=a[n--i];
for(i=;i<n;++i){
if(!ok(b[],b[],b[i-],b[i])){
up=b[i-],dn=b[i-];
tmp=__gcd(up,dn);
up/=tmp,dn/=tmp;//必改必整除
b[i]=b[i-]/dn*up;
}
}
for(i=;i<n;++i)a[i]=b[n--i];
}
}
cout<<a[];
for(i=;i<n;++i)cout<<' '<<a[i];
return ;
}
E3252 语言辨别
First AC: 2017-12-19 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
string s;
int len,n,c,e;
int main()
{
while(cin>>s){
++n;
len=s.length();
if(len>)++c;
else if(len==&&(s=="the"||s=="and"))++e;
}
if(*c<n)cout<<"Chinese";
else if(*e>n)cout<<"English";
else cout<<"Japanese";
return ;
}
E3253 玉米和葡萄
First AC: 2018-02-17 Latest Modification: 2018-02-17
#include<bits/stdc++.h>
using namespace std;
double f[][];
int n,m,sum,cnt;
double tmp;
int i,j;
int main()
{
for(i=;i<;++i)f[][i]=;
for(i=;i<;++i)for(j=;j<;++j){
cnt=tmp=;
if(i>=&&j>=)++cnt,tmp+=f[i-][j];
if(i>=&&j>=)++cnt,tmp+=f[i-][j-];
if(i>=&&j>=)++cnt,tmp+=f[i-][j-];
if(i>=&&j>=)++cnt,tmp+=f[i-][j-];
if(i>=&&j>=)++cnt,tmp+=f[i][j-];
f[i][j]=tmp*1.0/cnt;
}
while(cin>>n>>m)printf("%.7f\n",f[*n][*m]);
return ;
}
E3256 拼音魔法
First AC: 2018-12-18 Latest Modification: 2018-12-18
#include<bits/stdc++.h>
using namespace std;
string aa[]={"ā","á","ǎ","à","a"};
string ee[]={"ē","é","ě","è","e"};
string ii[]={"ī","í","ǐ","ì","i"};
string oo[]={"ō","ó","ǒ","ò","o"};
string uu[]={"ū","ú","ǔ","ù","u"};
string vv[]={"ǖ","ǘ","ǚ","ǜ","ü"};
int T,len,tmp1,tmp2;
string s;
int i;
int find(string s,int len,char c)
{
for(int i=;i<len;++i)if(s[i]==c)return i;
return -;
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>s;
len=s.length();
if(s[len-]>='a')s+='',++len;
cout<<"Case "<<i<<": ";
tmp1=find(s,len,'v');
if(tmp1!=-){
cout<<s.substr(,tmp1)<<vv[s[len-]-'']
<<s.substr(tmp1+,len-tmp1-)<<endl;
continue;
}
tmp1=find(s,len,'a');
if(tmp1!=-){
cout<<s.substr(,tmp1)<<aa[s[len-]-'']
<<s.substr(tmp1+,len-tmp1-)<<endl;
continue;
}
tmp1=find(s,len,'o');
if(tmp1!=-){
cout<<s.substr(,tmp1)<<oo[s[len-]-'']
<<s.substr(tmp1+,len-tmp1-)<<endl;
continue;
}
tmp1=find(s,len,'e');
if(tmp1!=-){
cout<<s.substr(,tmp1)<<ee[s[len-]-'']
<<s.substr(tmp1+,len-tmp1-)<<endl;
continue;
}
tmp1=find(s,len,'i');
tmp2=find(s,len,'u');
if(tmp1>tmp2){
cout<<s.substr(,tmp1)<<ii[s[len-]-'']
<<s.substr(tmp1+,len-tmp1-)<<endl;
}
else{
cout<<s.substr(,tmp2)<<uu[s[len-]-'']
<<s.substr(tmp2+,len-tmp2-)<<endl;
}
}
return ;
}
E3257 七减一
First AC: 2017-11-12 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[],b[];
ll l,r;
int i,j;
void dp(ll t,ll *c,ll d){
ll n=t/,m=t%;
ll tmp=n;
for(i=;i<=m;i++)c[i]+=d;
for(j=;j<=;j++)c[j]+=d*n;
c[]-=d;
while(tmp){
c[tmp%]+=(m+)*d;
tmp/=;
}
if(n)dp(n-,c,d*);
}
int main(){
while(cin>>l>>r){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
dp(l-,a,);
dp(r,b,);
cout<<b[]-a[]<<endl;
}
return ;
}
E3258 平方俱乐部
First AC: 2018-02-10 Latest Modification: 2018-02-10
#include<bits/stdc++.h>
using namespace std;
int p,q;
int i,j;
/* let b=d=q,a*a+c*c=pq */
void find(int m,int n)
{
//let a*a+c*c=m and b=d=n
for(i=;;++i){
j=sqrt(m-i*i);
if(i*i+j*j==m){
cout<<i<<' '<<n<<' '<<j<<' '<<n;
return;
}
}
}
int main()
{
cin>>p>>q;
find(p*q,q);
return ;
}
E3262 黑心啤酒厂
First AC: 2017-09-19 Latest Modification: 2017-11-22
#include<iostream>
using namespace std;
int x,n,a,b,c;
int i;
int main()
{
cin>>x>>n;
for(i=;i<=n;++i){
a=x,b=i,c=;
while(c){
c=a%b,a=b,b=c;
}
cout<<i/a<<endl;
}
return ;
}
E3263 丽娃河的狼人传说
First AC: 2019-01-02 Latest Modification: 2019-01-02
#include<bits/stdc++.h>
using namespace std;
int T,n,m,k,x;
int l,r,cnt,tmp;
int a[],s[];
struct data{
int l,r,t;
}b[];
int i,j;
bool cmp(data a,data b)
{
return a.r<b.r;
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n>>m>>k;
cnt=;
memset(a,,sizeof a);
memset(s,,sizeof s);
while(k--)cin>>x,a[x]=;
for(j=;j<;++j)s[j]=s[j-]+a[j];
for(j=;j<m;++j)cin>>b[j].l>>b[j].r>>b[j].t;
sort(b,b+m,cmp);
for(j=;j<m;++j){
l=b[j].l,r=b[j].r;
tmp=b[j].t-s[r]+s[l-];
if(tmp<)continue;
for(k=r;k>=l;--k){
if(!tmp)break;
if(!a[k]){
--tmp;
a[k]=;
++cnt;
for(int q=k;q<;++q)++s[q];
}
}
if(tmp){
cnt=-;
break;
}
}
cout<<"Case "<<i<<": "<<cnt<<endl;
}
return ;
}
E3264 蚂蚁
First AC: 2018-02-14 Latest Modification: 2018-03-12
#include<bits/stdc++.h>
using namespace std;
long n,lft,tmp;
long a[];
bool b[];
stack<long>rgt;
long i;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i]>>b[i];
for(i=;i<n;++i)
if(b[i])rgt.push(a[i]);
else
while(){
if(rgt.empty()){++lft;break;}
tmp=rgt.top();
if(a[i]>tmp)rgt.pop();
else break;
}
cout<<lft+rgt.size();
return ;
}
E3267 足球锦标赛
First AC: 2017-10-18 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int a[],T,A,B,K,power,most=;
int i,j;
int main()
{
for(i=;i<;i++)a[i]=i/*+i%/*+i%;
cin>>T;
for(i=;i<=T;i++){
cin>>A>>B>>K;
for(j=;j<=K;j++){
power=a[A+j]+a[B+K-j];
if(power>most)most=power;
}
most-=(a[A]+a[B]);
cout<<"Case "<<i<<": "<<most<<endl;
most=;
}
return ;
}
E3268 神奇怪兽在哪里
First AC: 2018-02-14 Latest Modification: 2018-02-14
#include<bits/stdc++.h>
using namespace std;
int n,m;
char a[][];
int i,j,k;
// U D L R
int dx[]={-,, ,};
int dy[]={ ,,-,};
char d[]={'U','D','L','R'};//for convenience
char e[]={'D','U','R','L'};
list<char>dir;//store each direction
void dfs(int x,int y)//find a path from (l,r) to point 'P'
{
if(a[x][y]=='P'){
while(!dir.empty())cout<<dir.front(),dir.pop_front();
exit();
}
a[x][y]='*';
for(int i=;i<;++i){
int nx=x+dx[i];
int ny=y+dy[i];
if(a[nx][ny]!='*'){
dir.push_back(d[i]);
dir.push_front(e[i]);
dfs(nx,ny);
dir.pop_front(),dir.pop_back();
}
}
}
int main()
{
cin>>n>>m;
memset(a,'*',sizeof(a));
for(i=;i<=n;++i)for(j=;j<=m;++j)cin>>a[i][j];
for(i=;i<m;++i)dir.push_back('R');
for(i=;i<n;++i)dir.push_back('D');
for(i=;i<m;++i)dir.push_back('L');
for(i=;i<n;++i)dir.push_back('U');
dfs(,);
return ;
}
E3269 爱吃糖果的Pokemon
First AC: 2018-06-14 Latest Modification: 2018-06-14
#include<bits/stdc++.h>
using namespace std;
int n,r,m,maxi;
int cnt[];
int rst[];
int i,j;
int main()
{
cin>>n>>r>>m;
for(i=;i<=m;++i){
for(j=;j<=n;++j){
if(!cnt[j]){
rst[i]=j;
++maxi;
++cnt[j];
break;
}
}
if(!rst[i])rst[i]=rst[i-],++cnt[rst[i]];
if(i-r>)--cnt[rst[i-r]];
}
cout<<maxi<<endl<<rst[];
for(i=;i<=m;++i)cout<<' '<<rst[i];
return ;
}
E3270 切西瓜
First AC: 2018-05-02 Latest Modification: 2018-05-02
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,tmp,rst;
ll x[],y[],z[];
ll tmp1[],tmp2[];
ll a,b,c,d;
int i,j,k,l; int main()
{
cin>>n;
for(i=;i<n;++i)cin>>x[i]>>y[i]>>z[i];
for(i=;i<n;++i)for(j=i+;j<n;++j)for(k=j+;k<n;++k){
tmp1[]=x[i]-x[j],tmp1[]=y[i]-y[j],tmp1[]=z[i]-z[j];
tmp2[]=x[i]-x[k],tmp2[]=y[i]-y[k],tmp2[]=z[i]-z[k];
a=tmp1[]*tmp2[]-tmp1[]*tmp2[];
b=tmp1[]*tmp2[]-tmp1[]*tmp2[];
c=tmp1[]*tmp2[]-tmp1[]*tmp2[];
d=-a*x[i]-b*y[i]-c*z[i];
for(tmp=l=;l<n;++l)if(a*x[l]+b*y[l]+c*z[l]+d==)++tmp;
rst=max(rst,tmp);
}
cout<<rst;
return ;
}
E3271 电话送报
First AC: 2018-04-19 Latest Modification: 2018-04-19
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,b,rst;
ll pos[];
ll i;
int main()
{
scanf("%lld%lld",&n,&b);
for(i=;i<n;++i)scanf("%lld",pos+i);
sort(pos,pos+n);
rst=n*b;
for(i=;i<n;++i)
rst=min(rst,*pos[i]+(n-i-)*b);
cout<<rst;
return ;
}
E3274 灌水
First AC: 2017-11-12 Latest Modification: 2017-11-12
#include<iostream>
using namespace std;
int T,i,s[],count,num;
int main()
{
while(cin>>T){
num=count=;
for(i=;i<T;++i){
cin>>s[i];
if(s[i]!=&&s[i]!=){
if(count==)num=i;
count++;
}
}
if(count==)cout<<"Deep Dark Fantasy of ECNU\n";
else{
for(i=;i<num;++i)cout<<s[i]<<" ";
cout<<"1 1 ";
for(i=num+;i<T;++i)cout<<s[i]<<" ";
cout<<endl;
}
}
return ;
}
E3275 章鱼哥没有日历
First AC: 2017-10-15 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
char c,d,e;
int M,D,i;
int main()
{
while(c=getchar()){
if(c=='-'){
d=getchar();
e=getchar();
break;
}
}
M=*(d-'')+e-'';
while(c=getchar()){
if(c=='-'){
d=getchar();
e=getchar();
break;
}
}
D=*(d-'')+e-'';
if(M==&&D>&&D<)cout<<D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else if(M==&&D>&&D<)cout<<+D;
else cout<<-;
return ;
}
E3276 连续正整数之和
First AC: 2017-10-28 Latest Modification: 2018-09-14
Note: 对加数个数j分奇偶
奇数j可以是加数个数,当且仅当平均数n÷j为整数且最小数>0
偶数j可以是加数个数,当且仅当平均数n÷j小数部分为0.5且最小数>0
对给定的n,满足前述的j唯一确定一种连加方法,故j的种数cnt即为所求
#include<bits/stdc++.h>
using namespace std;
int T,m,n,cnt;
int i,j;
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n;
cnt=;
for(j=;j<n;j+=)if(n%j==&&n/j-j/>)++cnt;
for(j=;j<n;j+=)if(*n%j==&&n%j!=&&n/j+-j/>)++cnt;
cout<<"Case "<<i<<": "<<cnt<<endl;
}
return ;
}
E3277 今天不是愚人节
First AC: 2017-10-27 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int i;
int main()
{
cin>>i;
if(i==||i==||i==||i==||i==||i==||i==)
cout<<"NO";
else cout<<"YES";
return ;
}
E3278 hcy226 的蜜月计划
First AC: 2018-05-28 Latest Modification: 2018-05-28
#include<bits/stdc++.h>
using namespace std;
int n,s,t,l,rst;
int main()
{
cin>>n;
while(--n)cin>>s>>t>>l,rst+=l;
cout<<*rst;
return ;
}
E3279 爱狗狗的两个dalao
First AC: 2018-01-06 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,M,res,ans,status,cnt;
ll W[];
ll i,j,k;
int dfs(ll k,ll w,ll status,ll Q)
{
if(w>M)return ;
if(k==n){
if(w>ans)res=status,ans=w;
}
else{
if((<<k)&Q)dfs(k+,w,status,Q),dfs(k+,w+W[k],status|(<<k),Q);
else dfs(k+,w,status,Q);
}
}
int main()
{
cin>>n>>M;
ll Q=pow(,n)-;
for(i=;i<n;++i)cin>>W[i];
while(Q){
res=ans=;
dfs(,,,Q);
Q^=res;
++cnt;
}
cout<<cnt;
return ;
}
E3281 找不到路的ultmaster
First AC: 2018-04-19 Latest Modification: 2018-04-19
#include<bits/stdc++.h>
using namespace std;
int m,n,h;
int mp[][];
int dp[][];
int i,j;
int main()
{
cin>>m>>n>>h;
memset(dp,0xff,sizeof(dp));
dp[][]=dp[][]=h;
for(i=;i<=m;++i)for(j=;j<=n;++j){
cin>>mp[i][j];
if(dp[i][j-]>=)
dp[i][j]=max(dp[i][j],dp[i][j-]+mp[i][j]);
if(dp[i-][j]>=)
dp[i][j]=max(dp[i][j],dp[i-][j]+mp[i][j]);
}
cout<<max(dp[m][n],-);
return ;
}
E3283 食堂的座位
First AC: 2017-09-21 Latest Modification: 2017-11-22
#include<iostream>
using namespace std;
int N,M;
int main()
{
cin>>N>>M;
cout<<(+N*M)/;
return ;
}
E3288 成绩计算
First AC: 2017-11-01 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,n,M,m,s,cnt;
int i;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
s=M=m=n,cnt=;
while(cin>>n,n!=-){
++cnt,s+=n;
if(n>M)M=n;
if(n<m)m=n;
}
cout<<"case #"<<i<<":\n"<<M<<endl
<<m<<endl<<(int)(s/cnt)<<endl;
}
return ;
}
E3289 N个整数中1的个数
First AC: 2017-10-29 Latest Modification: 2017-10-29
#include<iostream>
using namespace std;
int T,n,i,s;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>n;
s=++n/+n/*+n/*+n/*+n/*;
if(n%>)++s;
if(n%>)n%<?s+=n%-:s+=;
if(n%>)n%<?s+=n%-:s+=;
if(n%>)n%<?s+=n%-:s+=;
if(n%>)n%<?s+=n%-:s+=;
if(n==)++s;
cout<<"case #"<<i<<":\n"<<s<<endl;
}
return ;
}
E3290 找数(II)
First AC: 2018-02-19 Latest Modification: 2018-02-19
#include<bits/stdc++.h>
using namespace std;
int T,len,jdg1,jdg2;
string s;
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
s=""+s;
len=s.length();
cout<<"case #"<<i<<":\n";
if(len==){cout<<s[]<<endl;continue;}
jdg1=;
while(jdg1){
jdg2=;
for(j=;j<len;++j){
if(s[j]>s[j-]){
jdg2=;
//consider case s[j-1]=='9'
for(k=j-;;--k)if(s[k]!=''){++s[k];break;}
for(j=k+;j<len;++j)s[j]='';
break;
}
}
if(jdg2)continue;
jdg1=;
}
for(j=;;++j)if(s[j]!=''){cout<<s[j];break;}
for(k=j+;k<len;++k)cout<<s[k];
cout<<endl;
}
return ;
}
E3291 素数个数排序
First AC: 2017-11-21 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int a[]={,},b[];
int T,n,num;
int i,j;
struct data{
int nump,x,y;
}c[];
bool cmp(data a,data b)
{
if(a.nump==b.nump){
if(a.x==b.x)return a.y<b.y;
else return a.x<b.x;
}
else return a.nump<b.nump;
}
int main()
{
for(i=;i<;++i)
if(a[i]==)for(j=*i;j<;j+=i)++a[j];
cin>>T;
for(i=;i<;++i){
if(a[i]==)++num;
b[i]=num;
}
for(i=;i<T;++i){
cin>>n;
for(j=;j<n;++j){
cin>>c[j].x>>c[j].y;
c[j].nump=b[c[j].y]-b[c[j].x-];
}
sort(c,c+n,cmp);
cout<<"case #"<<i<<":\n";
for(j=;j<n;++j)cout<<c[j].x<<' '<<c[j].y<<endl;
}
return ;
}
E3292 解密字符串
First AC: 2017-12-23 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,len;
string s;
int a[],b[];
int i,j,k;
int main()
{
cin>>T;
for(i=;i<T;++i){
cin>>s;
len=s.length();
memset(a,,sizeof(a));
for(j=;j<len;++j)++a[s[j]-'A'+'a'];
b[]=a['z'];
b[]=a['o']-a['w']-a['r']+a['h']-a['g'];
b[]=a['w'];
b[]=a['h']-a['g'];
b[]=a['r']-a['h']+a['g']-a['z'];
b[]=a['f']-a['r']+a['h']-a['g']+a['z'];
b[]=a['x'];
b[]=a['v']-a['f']+a['r']-a['h']+a['g']-a['z'];
b[]=a['g'];
b[]=a['i']-a['f']+a['r']-a['h']-a['z']-a['x'];
cout<<"case #"<<i<<":\n";
for(j=;j<;++j)for(k=;k<b[j];++k)cout<<j;
cout<<endl;
}
return ;
}
E3293 至尊
First AC: 2017-10-27 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
string n;
int l,a[],b[],num,i,j;
int main()
{
while(cin>>n){
l=n.length();
num=a[l]=;
for(i=;i<l;++i)a[i]=(int)(n[i]-'');
for(i=l;i>;--i)
if(a[i]-num-a[i-]<)b[i]=a[i]-num-a[i-]+,num=;
else b[i]=a[i]-num-a[i-],num=;
b[]=a[]-num;
if(b[l]>)b[l]-=;
else{
b[l]+=;
for(i=l-;;--i)
if(b[i]!=){b[i]--;break;}
else b[i]=;
}
for(i=;;++i)if(b[i]!=)break;
for(j=i;j<=l;++j)cout<<b[j];
cout<<endl;
}
return ;
}
E3296 2333
First AC: 2017-09-18 Latest Modification: 2017-11-22
#include<iostream>
using namespace std;
int T,n;
int main()
{
cin>>T;
while(T--){
cin>>n;
cout<<n/<<endl;
}
return ;
}
E3297 铺瓷砖
First AC: 2018-06-17 Latest Modification: 2018-06-17
#include<bits/stdc++.h>
using namespace std;
int T,n;
int dp[][],rst[][];
int i;
int main()
{
dp[][]= dp[][]= dp[][]=;
dp[][]= dp[][]=;
rst[][]=rst[][]=rst[][]=;
for(i=;i<;++i){
dp[i][]= dp[i-][]+ dp[i-][];
rst[i][]=rst[i-][]+rst[i-][]+dp[i][];
dp[i][]= dp[i-][]+ dp[i-][];
rst[i][]=rst[i-][]+rst[i-][];
dp[i][]= dp[i-][]+ dp[i-][];
rst[i][]=rst[i-][]+rst[i-][];
}
cin>>T;
while(T--){
cin>>n;
cout<< dp[n][]+ dp[n][]+ dp[n][]<<endl;
cout<<rst[n][]+rst[n][]+rst[n][]<<endl;
}
return ;
}
E3298 排队买夜宵
First AC: 2017-10-28 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,len,num,cnt;
int i;
string s;
int main()
{
cin>>T;
while(cin>>s){
len=s.length();
cnt=,num=s[];
for(i=;i<len;++i){
if(cnt==){
num=s[i];
++cnt;
continue;
}
if(s[i]==num)++cnt;
else --cnt;
}
cout<<cnt<<endl;
}
return ;
}
E3299 主色调
First AC: 2019-04-21 Latest Modification: 2019-04-21
Note: 将所有区间按左端点分类,这样每类区间就能一次遍历答案
#include<bits/stdc++.h>
using namespace std;
int n;
int color[];
int cnt[],ans[];
int maxcnt,maxcolor;
int i,j;
int main()
{
while(cin>>n){
for(i=;i<=n;++i)cin>>color[i];
memset(ans,,sizeof ans);
for(i=;i<=n;++i){
maxcnt=;
memset(cnt,,sizeof cnt);
for(j=i;j<=n;++j){
int t=++cnt[color[j]];
if(t>maxcnt){
maxcolor=color[j];
maxcnt=cnt[color[j]];
}
else if(t==maxcnt){
maxcolor=min(maxcolor,color[j]);
}
++ans[maxcolor];
}
}
cout<<ans[];
for(i=;i<=n;++i)cout<<' '<<ans[i];
cout<<endl;
}
return ;
}
E3302 打印
First AC: 2018-04-19 Latest Modification: 2018-04-19
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,y;
ll dp[];
ll i,j;
int main()
{
cin>>n>>x>>y;
dp[]=x;
for(i=;i<=n;++i){
if(i&){
dp[i]=min(dp[i-]+x,min(dp[i/],dp[(i+)/])+x+y);
}
else{
dp[i]=min(dp[i-]+x,dp[i/]+y);
}
}
cout<<dp[n];
return ;
}
E3304 不等式
First AC: 2018-05-09 Latest Modification: 2018-05-09
#include<bits/stdc++.h>
using namespace std;
struct data{
string cmp;
int num;
}a[];
int n,rst;
string s;
int b[];
int i;
void jdg(int m)
{
int tmp=;
for(int i=;i<n;++i){
if(a[i].cmp=="<"&&m<a[i].num)++tmp;
else if(a[i].cmp==">"&&m>a[i].num)++tmp;
else if(a[i].cmp=="="&&m==a[i].num)++tmp;
else if(a[i].cmp=="<="&&m<=a[i].num)++tmp;
else if(a[i].cmp==">="&&m>=a[i].num)++tmp;
}
rst=max(rst,tmp);
}
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>s>>a[i].cmp>>a[i].num;
b[i]=a[i].num;
}
sort(b,b+n);
jdg(b[]-),jdg(b[n-]+);
for(i=;i<n;++i)jdg(b[i]);
for(i=;i<n;++i)
if(b[i-]+<b[i])jdg(b[i-]+);
cout<<rst;
return ;
}
E3305 十亿分考
First AC: 2018-07-10 Latest Modification: 2018-07-10
Note: 随机数瞎暴力
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int mod=1e9+;
const ld eps=4e-;
ld val;
ll up,dn,mid;
int main()
{
ios::sync_with_stdio(false);
cin>>val;
srand();
while(){
dn=rand()%mod;
mid=val*dn;
for(up=mid-;up<=mid+;++up){
if(fabs((ld)up/dn-val)<eps){
cout<<up/__gcd(up,dn)<<' '<<dn/__gcd(up,dn);
return ;
}
}
}
return ;
}
E3309 SBT
First AC: 2017-09-30 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int n,a,maxa,mina,i;
int main()
{
while(cin>>n){
cin>>a;
maxa=mina=a;
for(i=;i<n;i++){
cin>>a;
if(a>maxa)maxa=a;
else if(a<mina)mina=a;
}
if(maxa<&&mina>&&maxa-mina<)cout<<"Is SBT\n";
else cout<<"Is not SBT\n";
}
return ;
}
E3312 TTT
First AC: 2018-05-24 Latest Modification: 2018-05-24
#include<bits/stdc++.h>
using namespace std;
int n,m,rst,tmp;
int a[][];
void cal(int n)
{
int jdg=,tmp=;
while(n){
tmp+=n%;
if(n%==)jdg=;
n/=;
}
++a[jdg][tmp%];
}
int main()
{
while(cin>>n){
memset(a,,sizeof(a));
while(n--){
cin>>m;
cal(m);
}
rst=a[][];
tmp=min(a[][],a[][]);
rst+=tmp,a[][]-=tmp,a[][]-=tmp;
tmp=min(a[][],a[][]);
rst+=tmp,a[][]-=tmp,a[][]-=tmp;
tmp=min(a[][],a[][]/);
rst+=tmp,a[][]-=tmp,a[][]-=*tmp;
tmp=min(a[][],a[][]/);
rst+=tmp,a[][]-=tmp,a[][]-=*tmp;
tmp=min(a[][],a[][]);
rst+=tmp,a[][]-=tmp,a[][]-=tmp;
tmp=min(a[][]/,a[][]);
rst+=tmp,a[][]-=*tmp,a[][]-=tmp;
tmp=min(a[][]/,a[][]);
rst+=tmp,a[][]-=*tmp,a[][]-=tmp;
rst+=a[][]/+a[][]/;
cout<<rst<<endl;
}
return ;
}
E3313 Git
First AC: 2018-07-08 Latest Modification: 2018-07-08
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,w,l,r;
string s;
char ch;
int in[],out[];
int dp[][];
int main()
{
cin>>m>>n>>s;
while(m--){
cin>>ch>>x>>y;
ch-='a';
in[ch]=x;
out[ch]=y;
}
for(w=;w<=n;++w){
for(r=w-;r<n;++r){
l=r-w+;
if(s[l]==s[r])dp[l][r]=dp[l+][r-];
else{
int tmp1=min(in[s[l]-'a'],out[s[l]-'a'])+dp[l+][r];
int tmp2=min(in[s[r]-'a'],out[s[r]-'a'])+dp[l][r-];
dp[l][r]=min(tmp1,tmp2);
}
}
}
cout<<dp[][n-];
return ;
}
E3316 和和和
First AC: 2019-02-17 Latest Modification: 2019-02-17
Note: fread快速读入模板,对着网上代码改了好久才琢磨出判断EOF办法
#include<bits/stdc++.h>
using namespace std;
const int mod=;
int a,rst;
namespace io
{
const int maxn=<<;
static char buf[maxn],*pbuf=buf,*end;
void init()
{
int c=fread(buf,,maxn,stdin);
end=buf+c;
}
bool readint()
{
a=;
if(pbuf==end)return ;
while(pbuf!=end&&!isdigit(*pbuf))++pbuf;
while(pbuf!=end&&isdigit(*pbuf)){
(a*=)+=*pbuf-'';
++pbuf;
}
return ;
}
}
using namespace io;
int main()
{
io::init();
while(io::readint())(rst+=a)%=mod;
cout<<rst;
return ;
}
E3321 坏掉的里程表
First AC: 2018-04-18 Latest Modification: 2018-04-18
#include<bits/stdc++.h>
using namespace std;
long n,t,minc;
long d[],v[];
long i;
double sum(double c)
{
double rst=;
for(i=;i<n;++i){
rst+=*(d+i)/(c+*(v+i));
if(rst>t)return rst;
}
return rst;
}
double bi(double lft,double rgt)
{
if(fabs(lft-rgt)<0.0000001)return lft;
double mid=(lft+rgt)/,tmp=sum(mid);
if(fabs(tmp-t)<0.00000001)return mid;
if(tmp<t)return bi(lft,mid);
else return bi(mid,rgt);
}
int main()
{
scanf("%ld%ld",&n,&t);
minc=;
for(i=;i<n;++i){
scanf("%ld%ld",d+i,v+i);
if(v[i]<minc)minc=*(v+i);
}
printf("%.7f",bi(-1.0*minc,));
return ;
}
E3322 万年历
First AC: 2017-10-16 Latest Modification: 2018-06-09
Note: 判断y-m-d是0001-01-01开始的第几天,用n表示
对n模7,同一剩余类里的星期数相同,用b[]描述剩余类
尽管我们不知道0001-01-01星期几,但可以通过样例加加减减来调整
#include<bits/stdc++.h>
using namespace std;
int y,m,d,n,i;
int a[]={,,,,,,,,,,,,};
string b[]={"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"};
int main()
{
scanf("%d-%d-%d",&y,&m,&d);
for(i=;i<=y;i++)if((i%==&&i%!=)||i%==)n++;
//累加0001-y年的2.29天数
n+=y,n+=a[m],n+=d;
//与+=365*(y-1)同质,不改变星期数周期性
if(((y%==&&y%!=)||y%==)&&m<)n--;
//减当年可能多算的2.29
cout<<b[(n+)%];
//n%7,调整剩余类,0是根据样例得到的调整数
return ;
}
E3323 罗塞塔石碑
First AC: 2019-02-07 Latest Modification: 2019-02-07
#include<bits/stdc++.h>
using namespace std;
int m,n,len;
int dis[][];
char x,y;
string s,t;
int i,j,k;
int main()
{
cin>>m>>n;
memset(dis,0x3f,sizeof dis);
for(i=;i<;++i)dis[i][i]=;
while(m--){
cin>>x>>y;
dis[x-'a'][y-'a']=;
}
for(k=;k<;++k){
for(i=;i<;++i){
for(j=;j<;++j){
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
while(n--){
cin>>s>>t;
if((len=s.length())!=t.length()){
cout<<"no\n";
continue;
}
bool flag=;
for(i=;i<len;++i){
if(dis[s[i]-'a'][t[i]-'a']>1e8){
flag=;
break;
}
}
flag? cout<<"yes\n":cout<<"no\n";
}
return ;
}
E3325 今天是个特殊的日子
First AC: 2018-05-18 Latest Modification: 2018-05-18
#include<bits/stdc++.h>
using namespace std;
int a[]={,,,,,,,,,,,,};
int b[]={,,,,,,,,,,,,};
int np,y,rst;
int pri[];
int i;
bool ispri(int n)
{
for(int i=;i*i<=n;++i)if(!(n%i))return ;
return ;
}
int cal(int y,int n)
{
int i;
if((!(y%)&&(y%))||!(y%)){
for(i=;;--i)if(b[i]<n)break;
return y*+(i+)*+n-b[i];
}
else{
for(i=;;--i)if(a[i]<n)break;
return y*+(i+)*+n-a[i];
}
}
int main()
{
for(i=;i<;++i)if(ispri(i))pri[np++]=i;
cin>>y;
for(i=;i<np;++i)if(ispri(cal(y,pri[i])))++rst;
if((!(y%)&&(y%))||!(y%))printf("%.9lf",rst/366.0);
else printf("%.9lf",rst/365.0);
return ;
}
E3329 奇异收缩的剪影
First AC: 2018-06-09 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
#define M 1000000007
using namespace std;
typedef long long ll;
ll n,m,rst;
ll cnt[],dp[][];
bool pri[];
ll i,j;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
while(n--)cin>>m,++cnt[m];
dp[][]=;
for(i=;i<;++i){
if(cnt[i+])for(j=;j<;++j)if(dp[i][j])
(dp[i+][j^(i+)]+=(cnt[i+]+)/*dp[i][j])%=M;
for(j=;j<;++j)if(dp[i][j])
(dp[i+][j]+=(cnt[i+]/+)*dp[i][j])%=M;
}
for(i=;i<;++i)if(!pri[i])for(j=i+i;j<;j+=i)pri[j]=;
for(i=;i<;++i)if(!pri[i])rst=(rst+dp[][i])%M;
cout<<rst;
return ;
}
E3330 沉陷过往的幻灭
First AC: 2018-05-04 Latest Modification: 2018-05-04
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll M=,N=,P=1e9+;
string s;
ll len,num,base=,rst;
bool a[M],b[M];
ll i,j;
int main()
{
ios::sync_with_stdio(false);
cin>>s;
len=s.length();
for(i=j=;i<len;++i)a[i]=s[len--i]-'';
cin>>s;
len=s.length();
for(i=j=;i<len;++i)b[i]=s[len--i]-'';
for(i=;i<N;++i){
if(b[i])++num;
if(a[i])rst=(rst+(N-num)*base)%P;
else rst=(rst+num*base)%P;
base=base*%P;
}
for(i=N;i<M;++i){
if(b[i-N])--num;
if(a[i])rst=(rst+(N-num)*base)%P;
else rst=(rst+num*base)%P;
base=base*%P;
}
cout<<rst;
return ;
}
E3331 凝滞逆转的神谕
First AC: 2018-05-04 Latest Modification: 2018-05-04
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,rst;
ll a[];
ll i;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(i=;i<;++i)a[i]=i;
bool jdg=;
for(i=;i<=n;++i){
cin>>m;
if(a[i]==m)continue;
if(a[i+]==m){
a[i+]=a[i];
++rst;
continue;
}
if(a[i+]==m){
a[i+]=a[i+];
a[i+]=a[i];
rst+=;
continue;
}
jdg=;
break;
}
jdg? cout<<rst:cout<<"Too chaotic";
return ;
}
E3332 无限剑制的终焉
First AC: 2018-05-01 Latest Modification: 2018-05-01
#include<bits/stdc++.h>
#define M 10000
using namespace std;
typedef unsigned long long ll;
ll T,n,k,rst;
ll base[]={};
bool jdg[];
ll mod[];
ll i;
int main()
{
cin>>T;
for(i=;i<;++i)base[i]=*base[i-];
while(T--){
cin>>n>>k;
k*=;
memset(jdg,,sizeof(jdg));
for(i=;n;--i)
if(n>=base[i])
jdg[i]=,n-=base[i];
mod[]=%k;
for(i=;i<;++i){
ll hgh=mod[i-]/M,low=mod[i-]%M;
mod[i]=(mod[i-]*M%k*hgh%k+mod[i-]*low%k)%k;
}
rst=;
for(i=;i<;++i)
if(jdg[i]){
ll hgh=mod[i]/M,low=mod[i]%M;
rst=(rst*M%k*hgh%k+rst*low%k)%k;
}
rst=(rst+k-)%k;
cout<<rst/<<endl;
}
return ;
}
E3333 渴求真实的天使
First AC: 2018-05-04 Latest Modification: 2018-05-04
#include<bits/stdc++.h>
using namespace std;
long n,k;
long a[];
long i;
bool ispri(long n)
{
for(long i=;i*i<=n;++i)
if(!(n%i))return ;
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>k;
for(i=;i<n;++i)cin>>a[i];
for(i=n-;i;--i)a[i/]=__gcd(a[i/],a[i]);
if(a[]==)cout<<;
else if(ispri(a[]))cout<<k/a[]*a[];
else for(i=k;;--i)if(__gcd(a[],i)>){cout<<i;break;}
return ;
}
E3334 绝望恸哭的愚者
First AC: 2018-06-09 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,n;
int main()
{
cin>>T;
while(T--){
cin>>n;
if(n<)cout<<"yes\n";
else n%? cout<<"no\n":cout<<"yes\n";
}
return ;
}
E3336 Beautiful Numbers
First AC: 2018-06-09 Latest Modification: 2018-06-09
Note: 一个很显然而有用的事实是,进制数和给定数在该进制下表示位数是单调减的
所以只要找到一个满足题设的进制数base,就不需要再检索比base大的进制数了
另一个很显然的事实是,给定数n(n>2),base=n-1是它的一个解(事实上也是最大解)
因为暴力求解base的复杂度是O(nlgn),所以对于10^6以内的进制数暴力检索
而对于超过10^6的进制数,不难发现符合条件的进制数表示不能超过3位
这是因为当base>10^6时,base*base*base>=10^18+1>n
恰等于三位时,只要判断二次方程base*base+base+1=n是否有正整数解
恰等于两位时,解正好就是前面提到的base=n-1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll n,base;
ll i;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(i=;i<;++i){
ll tmp=n;
bool jdg=;
while(tmp){
if(tmp%i!=){
jdg=;
break;
}
tmp/=i;
}
if(jdg){
base=i;
break;
}
}
if(!base){
ll tmp=pow((ld)(*n-),(ld)0.5)+0.5;
if(tmp*tmp==*n-)base=(tmp-)/;
else base=n-;
}
cout<<base;
return ;
}
E3338 双塔问题
First AC: 2018-05-02 Latest Modification: 2018-05-02
#include<bits/stdc++.h>
using namespace std;
int n;
long a;
long dp[][];//前i个积木差为j的矮塔最大高度
int i,j;
int main()
{
cin>>n;
for(i=;i<;++i)dp[][i]=-;
for(i=;i<=n;++i){
cin>>a;
for(j=;j>=;--j){
dp[i][j]=dp[i-][j];//不放
if(j>=a)dp[i][j]=max(dp[i][j],dp[i-][j-a]);//放高
if(j+a<)dp[i][j]=max(dp[i][j],dp[i-][j+a]+a);//放矮仍矮
if(j<=a)dp[i][j]=max(dp[i][j],dp[i-][a-j]+a-j);//放矮为高
}
}
cout<<max(dp[n][],0L);
return ;
}
E3342 经典的猜数游戏
First AC: 2018-05-08 Latest Modification: 2018-05-08
#include<bits/stdc++.h>
using namespace std;
long long lft,rgt,mid;
string s;
int main()
{
lft=-1e9;
rgt=1e9;
while(){
mid=(lft+rgt)/;
cout<<mid<<endl;
cin>>s;
if(s=="big")rgt=mid-;
else if(s=="small")lft=mid+;
else break;
}
return ;
}
E3343 丽娃河
First AC: 2017-10-14 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int n;
string s=" HACKING";
int main()
{
cin>>n;
cout<<s[n];
return ;
}
E3344 送命题
First AC: 2017-09-19 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int a[]={,,,,,,,,,,,,,,,,,
,,,,,,,,
,,,,,,};
int n;
int main()
{
cin>>n;
cout<<a[n];
return ;
}
E3345 猜猜看
First AC: 2018-04-15 Latest Modification: 2018-04-15
#include<bits/stdc++.h>
using namespace std;
long long lft,rgt,num;
int main()
{
lft=-1e9;
rgt=1e9;
while(){
cin>>num;
if(lft==rgt){
cout<<"equal\n";
fflush(stdout);
break;
}
if(num*>rgt+lft){
cout<<"big\n";
fflush(stdout);
rgt=num-;
}
else{
cout<<"small\n";
fflush(stdout);
lft=num+;
}
}
return ;
}
E3346 皇后问题
First AC: 2018-04-04 Latest Modification: 2018-04-04
#include<bits/stdc++.h>
#define N 100001
using namespace std;
long n,x,y,rst;
long a[N],b[N],c[*N],d[*N];
long i;
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>x>>y;
++a[x],++b[y],++c[x+y],++d[x-y+n];
}
++n;
for(i=;i<n;++i){
if(a[i]>)rst+=a[i]*(a[i]-)/;
if(b[i]>)rst+=b[i]*(b[i]-)/;
}
n=(n-)*;
for(i=;i<n;++i){
if(c[i]>)rst+=c[i]*(c[i]-)/;
if(d[i]>)rst+=d[i]*(d[i]-)/;
}
cout<<rst;
return ;
}
E3348 树的顺序存储结构
First AC: 2018-06-29 Latest Modification: 2018-06-29
#include<bits/stdc++.h>
using namespace std;
int n,m;
struct node{
stack<int>nxt;
}a[];
int i;
void post(int m)
{
while(!a[m].nxt.empty()){
post(a[m].nxt.top());
a[m].nxt.pop();
}
cout<<m<<' ';
}
int main()
{
cin>>n>>m;
for(i=;i<n;++i)cin>>m,a[m].nxt.push(i);
post();
return ;
}
E3350 林吉吉的野望
First AC: 2017-11-03 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,n,k,a[],b[],m,cnt;
int i,j;
int main()
{
cin>>T;
for(i=;i<T;++i){
memset(a,,sizeof(a)),memset(b,,sizeof(b)),cnt=;
cin>>n>>k;
for(j=;j<=n;++j)cin>>m,a[m]=j;
for(j=;cnt<k;--j)
if(a[j]!=)++b[a[j]],++cnt;
cnt=;
for(j=;j<=;++j)
if(b[j]==)
if(cnt==)cout<<j,cnt++;
else cout<<" "<<j;
cout<<endl;
}
return ;
}
E3351 泡咖啡
First AC: 2017-09-25 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,a,b,c;
int main()
{
cin>>T;
while(cin>>a>>b){
c=a+b;
if(a==)cout<<"LBB made no coffee today.\n";
else{
if(a==)cout<<"LBB made a cup of coffee today.\n";
else cout<<"LBB made "<<a<<" cups of coffee today.\n";
}
if(b==)cout<<"JDG made no coffee today.\n";
else{
if(b==)cout<<"JDG made a cup of coffee today.\n";
else cout<<"JDG made "<<b<<" cups of coffee today.\n";
}
if(c==)cout<<"BOSS had no coffee today.\n";
else{
if(c==)cout<<"BOSS had a cup of coffee today.\n";
else cout<<"BOSS had "<<c<<" cups of coffee today.\n";
}
}
return ;
}
E3352 密集数
First AC: 2017-09-25 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,n,a,sum;
int i;
int main()
{
cin>>T;
while(cin>>n){
sum=;
for(i=;i<n;i++)cin>>a,sum+=a;
for(i=;i<n;i++)sum*=;
cout<<sum<<"\n";
}
return ;
}
E3354 领外卖
First AC: 2017-12-15 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T;
bool a[];
long b[];
long i,j,k,n;
int main()
{
for(i=;i<;++i)b[i]=i*i,a[b[i]]=;
b[]=;
for(i=;i<;++i){
for(j=b[i-]+;j<b[i];++j){
for(k=;k<i;++k){
if(!a[j-b[k]]){a[j]=;break;}
}
}
}
cin>>T;
while(T--){
cin>>n;
if(a[n])cout<<"JDG";
else cout<<"LJJ";
cout<<" will get takeaways!\n";
}
return ;
}
E3357 吉吉木的野望
First AC: 2018-05-10 Latest Modification: 2018-05-10
#include<bits/stdc++.h>
using namespace std;
int d,k;
double rst;
int i;
double cal(int n)
{
if(n>k)return ;
return 1.0/(+(n-)*d+cal(n+));
}
int main()
{
cin>>d>>k;
k=min(k,);
printf("%.12lf",cal());
return ;
}
E3358 种水稻
First AC: 2017-09-25 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int T,n,a,b;
int i;
int main()
{
cin>>T;
while(cin>>n){
b=n;
for(i=;i<n;i++){
cin>>a;
if(a>)b--;
}
cout<<b<<"\n";
}
return ;
}
E3359 吉吉木和五彩方块
First AC: 2018-04-20 Latest Modification: 2018-04-20
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,num;
ll cnt[];
struct data{
ll color,time;
}a[];
queue<ll>rst[];
ll i,j;
bool cmp(data a,data b)
{
return a.time>b.time;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
if(n<){
cin>>m;
cout<<m;
return ;
}
for(i=;i<n;++i)cin>>m,++cnt[m];
for(i=;i<;++i)if(cnt[i]){
a[num].color=i;
a[num].time=cnt[i];
++num;
}
sort(a,a+num,cmp);
if(a[].time>(n+)/){cout<<-;return ;}
for(i=a[].time-;i>=;--i)rst[i].push(a[].color);
int pos=;
j=;
while(){
if(a[j].time){
rst[pos].push(a[j].color);
if(++pos==a[].time)pos=;
--a[j].time;
}
else{
if(++j==num)break;
}
}
cout<<rst[].front();
rst[].pop();
for(i=;i<a[].time;++i)
while(!rst[i].empty()){
cout<<' '<<rst[i].front();
rst[i].pop();
}
return ;
}
E3362 推锅大战
First AC: 2017-09-28 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int i,n,a,b,c;
long Fx,Fy,Fz;
int main()
{
cin>>n;
for(i=;i<n;i++){
cin>>a>>b>>c;
Fx+=a;Fy+=b;Fz+=c;
}
if(Fx==&&Fy==&&Fz==)cout<<"NO";else cout<<"YES";
return ;
}
E3363 吉吉木和木木吉
First AC: 2017-10-04 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
double win,eq,lose;
double p;
int i,j,jjm,mmj,a[];
int main()
{
for(i=;i<;i++){
cin>>jjm;
a[i]=jjm;
}
for(i=;i<;i++){
cin>>mmj;
for(j=;j<;j++){
if(a[j]>mmj)win++;
else{if(a[j]<mmj)lose++;
else eq++;
}
}
}
win/=,lose/=,eq/=;
p=(-eq*eq*eq*eq*eq*eq*eq*eq*eq*eq)/(-eq)*win;
printf("%.5f",p);
}
E3364 吉吉木坐地铁
First AC: 2019-02-11 Latest Modification: 2019-02-11
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int n,q,num;
int u,v;
queue<int>e[maxn];
int city_num[maxn];
int city_len[maxn];
int dis[maxn];
int i,j;
void dfs(int head,int u,int pre,int d)
{
while(!e[u].empty()){
city_num[u]=num;
dis[u]=d+;
int v=e[u].front();
e[u].pop();
if(v==pre)continue;
if(v==head){
city_len[num]=d+;
continue;
}
dfs(head,v,u,d+);
}
}
int main()
{
cin>>n>>q;
for(i=;i<=n;++i){
cin>>u>>v;
e[u].push(v);
e[v].push(u);
}
for(i=;i<=n;++i){
if(!city_num[i]){
++num;
dfs(i,i,,);
}
}
while(q--){
cin>>u>>v;
if(city_num[u]!=city_num[v])cout<<"-1\n";
else{
num=city_num[u];
int len=city_len[num];
int tmp=abs(dis[u]-dis[v]);
cout<<min(tmp,len-tmp)<<endl;
}
}
return ;
}
E3366 这题没验过啊?
First AC: 2017-10-16 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
cin>>n;
if(n==)cout<<"infiniteee";else{
if(n%==||n%==)cout<<"ultmaster";
else cout<<"infiniteee";
}
return ;
}
E3367 咸鱼翻身
First AC: 2018-04-21 Latest Modification: 2018-04-21
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
int a[],dp[];
int i;
int main()
{
cin>>n;
for(i=;i<=n;++i){
cin>>m;
if(m)++cnt;
a[i]=(m? -:);
}
for(i=;i<=n;++i)dp[i]=max(dp[i-],)+a[i];
for(i=;i<n;++i)if(dp[i]>dp[n])dp[n]=dp[i];
cout<<dp[n]+cnt;
return ;
}
E3368 逃课吃鸡
First AC: 2018-04-19 Latest Modification: 2018-04-19
#include<bits/stdc++.h>
using namespace std;
int n,rst;
struct data{
string s;
int t;
}a[];
bool cmp(data a,data b)
{
if(a.s!=b.s)return a.s<b.s;
return a.t>b.t;
}
int i;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i].s>>a[i].t;
sort(a,a+n,cmp);
if(n<)rst=a[].t;
else{
rst=a[].t+a[].t;
for(i=;i<n;++i)
if(a[i].s!=a[i-].s)
rst+=a[i].t;
}
cout<<rst;
return ;
}
E3369 三千米健身步道
First AC: 2018-07-02 Latest Modification: 2018-07-02
#include<bits/stdc++.h>
using namespace std;
int n,x,y,rst;
int cnt[],fr[],to[];
int i;
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>x>>y;
++cnt[x],++cnt[y];
fr[i]=x,to[i]=y;
}
for(i=;i<n;++i){
rst+=(cnt[fr[i]]-)*(cnt[to[i]]-);
}
cout<<rst;
return ;
}
E3380 Ants
First AC: 2018-06-09 Latest Modification: 2018-06-09
Note: 蚂蚁的编号对题目没有影响,每只蚂蚁速度相同
所以蚂蚁相遇的反向实际上可以忽略,默认直接穿过对方
#include<bits/stdc++.h>
using namespace std;
int T,len,n,maxn,minn,m;
int main()
{
cin>>T;
while(T--){
cin>>len>>n;
maxn=minn=;
while(n--){
cin>>m;
maxn=max(maxn,max(m,len-m));
minn=max(minn,min(m,len-m));
}
cout<<minn<<' '<<maxn<<endl;
}
return ;
}
E3383 Saruman’s Army
First AC: 2017-12-30 Latest Modification: 2017-12-30
#include<iostream>
using namespace std;
int r,n,m,cnt,lft,rgt;
int a[];
int i,j;
int main()
{
while(cin>>r>>n,r+){
for(cnt=i=;i<n;++i)cin>>m,++a[m];
rgt=;
while(n){
for(i=rgt;i>=;--i)if(a[i])break;
lft=i-r;
if(lft<)lft=;
for(j=lft;;++j){
if(a[j]){
n-=a[j],a[j]=;
rgt=j-r;
if(rgt<)rgt=;
break;
}
}
for(++cnt,j=rgt;j<=i;++j){
if(a[j])n-=a[j],a[j]=;
}
--rgt;
if(rgt<)rgt=;
}
cout<<cnt<<endl;
}
return ;
}
E3384 食物链
First AC: 2019-02-27 Latest Modification: 2019-03-03
Note: 差分维护的带标记并查集,a吃(被吃)a的祖先就把a的标记置为1(-1)
这样a到根节点的路径标记和模3就能反映a与根节点的关系
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e4+;
int n,k,ans;
int pre[maxn],val[maxn];
int d,x,y;
int i;
int find(int x)
{
int r=x,s=,i;
while(pre[r]!=r)s+=val[r],r=pre[r];
s+=val[r];
while(x!=r){
i=val[x];
val[x]=s%;
s=(s-i+)%;
i=pre[x];
pre[x]=r;
x=i;
}
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
pre[fx]=fy;
val[fx]=(val[y]-val[x]+d-+)%;
}
else{
if((val[x]-val[y]-(d-)+)%)++ans;
}
}
int main()
{
cin>>n>>k;
for(i=;i<=n;++i)pre[i]=i;
while(k--){
cin>>d>>x>>y;
if(x>n||y>n||(d==&&x==y))++ans;
else join(x,y);
}
cout<<ans;
return ;
}
E3416 摄氏华氏转换
First AC: 2017-10-27 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
string s;
int n;
int main()
{
cin>>s;
n=*(int)(s[]-'')+(int)(s[]-'');
if(s[]=='c')printf("%.2ff",9.0*n/+);
else printf("%.2fc",(n-)*5.0/);
return ;
}
E3417 求幂
First AC: 2017-10-27 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int a,b,i;
long long c=;
int main()
{
cin>>a>>b;
for(i=;i<b;i++)c*=a;
cout<<c;
return ;
}
E3418 数字字符个数
First AC: 2017-11-07 Latest Modification: 2017-11-07
#include<stdio.h>
int main()
{
int count=;
char c;
while((c=getchar())!='\n')
if(c>=''&&c<='')
count++;
printf("%d",count);
return ;
}
E3419 假水仙花数
First AC: 2017-10-27 Latest Modification: 2017-10-27
main(){puts("0\n1\n1634\n8208\n9474");}
E3430 Urban Design
First AC: 2018-05-10 Latest Modification: 2018-05-10
#include<bits/stdc++.h>
using namespace std;
int S,T,xl,xr,yl,yr,cntl,cntr;
struct line{
double k,b;
bool vert;
}a[];
int i;
int cnt(int x,int y)
{
int rst=;
for(i=;i<S;++i){
if(a[i].vert){
if(x<a[i].b)++rst;
}
else{
if(y>a[i].k*x+a[i].b)++rst;
}
}
return rst;
}
int main()
{
cin>>S;
for(i=;i<S;++i){
cin>>xl>>yl>>xr>>yr;
if(xl==xr){
a[i].vert=;
a[i].b=xl;
}
else{
a[i].k=(yr-yl)*1.0/(xr-xl);
a[i].b=yl-a[i].k*xl;
}
}
cin>>T;
while(T--){
cin>>xl>>yl>>xr>>yr;
cntl=cnt(xl,yl);
cntr=cnt(xr,yr);
if((cntl-cntr)%)cout<<"different\n";
else cout<<"same\n";
}
return ;
}
E3441 唐纳德与子串(Easy)
First AC: 2017-12-08 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
string s,t;
long lens,lent,T,lft,rgt,most,tmp,rst;
struct data{
long num[];
long cnt;
}a[];
bool flag;
long i,j;
int main()
{
cin>>s;
lens=s.length();
for(i=;i<lens;++i)a[s[i]-'a'].num[a[s[i]-'a'].cnt++]=i;
cin>>T;
while(T--){
cin>>lft>>rgt>>t;
lent=t.length();
--lft,rgt=rgt-lent+;
most=a[t[]-'a'].cnt;
for(i=rst=;i<most;++i){
tmp=a[t[]-'a'].num[i];
if(tmp>lft&&tmp<rgt){
for(j=flag=;j<lent;++j){
if(t[j]!=s[tmp+j]){
flag=;
break;
}
}
if(flag)++rst;
}
}
cout<<rst<<endl;
}
return ;
}
E3443 Sec2HMS
First AC: 2017-12-08 Latest Modification: 2017-12-08
#include<iostream>
using namespace std;
int second,h,m,s,rst;
int main()
{
cin>>second;
h=second/,m=second%/,s=second%;
rst=*h+*m+s;
cout<<rst<<endl;
return ;
}
E3444 四位分数加法
First AC: 2017-12-08 Latest Modification: 2017-12-08
#include<iostream>
using namespace std;
long long a,b,c,d,tmp1=,tmp2,tmp3,m,n;
int main()
{
cin>>a>>b>>c>>d;
cout<<a<<'/'<<b<<'+'<<c<<'/'<<d<<'=';
n=tmp2=b*d,m=tmp3=a*d+b*c;
while(tmp1)tmp1=tmp2%tmp3,tmp2=tmp3,tmp3=tmp1;
cout<<m/tmp2<<'/'<<n/tmp2<<'.';
return ;
}
E3445 字符串替换
First AC: 2017-12-08 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
string s,t,r;
int lens,lent,tmp,cnt,flag;
int i,j;
int main()
{
cin>>s>>t>>r;
lens=s.length(),lent=t.length();
if(lent>lens){cout<<s<<endl;return ;}
tmp=lens-lent+;
for(i=;i<tmp;++i){
if(s[i]==t[]){
for(cnt=j=;j<lent;++j){
if(s[i+j]!=t[j]){cnt=;break;}
}
if(cnt)cout<<r,i+=lent-,flag=;
else cout<<s[i],flag=;
}
else cout<<s[i],flag=;
}
if(flag)for(j=i;j<lens;++j)cout<<s[j];
else for(j=tmp;j<lens;++j)cout<<s[j];
cout<<endl;
return ;
}
E3446 骰子点数和问题
First AC: 2017-12-08 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int n,m;
double pro[][];
int i,j,k;
int main()
{
cin>>n>>m;
if(m>*n){
cout<<<<endl;
return ;
}
for(i=;i<;++i)pro[][i]=1.0/;
for(i=;i<=n;++i)
for(j=i;j<=m;++j)
for(k=;k<j&&k<;++k)
pro[i][j]+=pro[i-][j-k]/6.0;
printf("%.6f\n",pro[n][m]);
return ;
}
E3451 易位构词
First AC: 2017-12-08 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
char c;
long long cnt,i,len;
int b[];
struct data{
char v,t;
long num;
}a[];
bool cmp1(data m,data n)
{
if(b[m.v-'a']-b[n.v-'a'])return b[m.v-'a']>b[n.v-'a'];
if(m.v-n.v)return m.v<n.v;
return m.num<n.num;
}
bool cmp2(data m,data n)
{
return m.num<n.num;
}
int main()
{
while((c=getchar())!='\n')a[cnt].num=cnt,a[cnt++].v=c,++b[c-'a'];
sort(a,a+cnt,cmp1);
len=b[a[].v-'a'];
if(len>cnt/){cout<<"impossible";return ;}
for(i=;i<cnt;++i)a[i].t=a[(i+len)%cnt].v;
sort(a,a+cnt,cmp2);
for(i=;i<cnt;++i)cout<<a[i].t;
return ;
}
E3452 唐纳德先生和假骰子
First AC: 2017-12-08 Latest Modification: 2017-12-08
#include<iostream>
using namespace std;
int a[],b[],c[];
int i,j,p;
int main()
{
cin>>p;
for(i=;i<;++i)cin>>b[i];
for(i=;i<;++i)cin>>c[i];
for(i=;i<;++i){
for(j=;j<;++j)a[(b[i]+c[j])%p]++;
}
for(i=;i<p;++i)if(a[i]!=a[i-]){cout<<"NO";return ;}
cout<<"YES";
return ;
}
E3455 Huge Numbers(Small)
First AC: 2017-12-10 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,a,n,p,rst;
ll i,j;
ll pow(ll a,ll b,ll p)
{
ll rst=,base=a;
while(b){
if(b&)(rst*=base)%=p;
(base*=base)%=p;
b>>=;
}
return rst;
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>a>>n>>p;
rst=a%p;
for(j=;j<=n;++j)rst=pow(rst,j,p);
cout<<"Case #"<<i<<": "<<rst<<endl;
}
return ;
}
E3456 Huge Numbers(Large)
First AC: 2018-06-09 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,a,n,p,rst;
ll i,j;
ll pow(ll a,ll b,ll p)
{
ll rst=,base=a;
while(b){
if(b&)(rst*=base)%=p;
(base*=base)%=p;
b>>=;
}
return rst;
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>a>>n>>p;
rst=a%p;
for(j=;j<=n;++j)rst=pow(rst,j,p);
cout<<"Case #"<<i<<": "<<rst<<endl;
}
return ;
}
E3457 Cards Game(Small)
First AC: 2019-05-16 Latest Modification: 2019-05-16
#include<bits/stdc++.h>
using namespace std;
const int maxn=,maxm=;
int T,n,m;
long long ans;
int red[maxn],blue[maxn];
struct edge{
int u,v,w;
}e[maxm];
int pre[maxn];
int i,j,k;
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int find(int x)
{
int r=x,i;
while(pre[r]!=r)r=pre[r];
while(pre[x]!=r)i=pre[x],pre[x]=r,x=i;
return r;
}
void join(int x,int y,int z)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
pre[fx]=fy;
ans+=z;
}
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n;
for(j=;j<=n;++j)cin>>red[j];
for(j=;j<=n;++j)cin>>blue[j];
m=;
for(j=;j<n;++j){
for(k=j+;k<=n;++k){
e[m].u=j;
e[m].v=k;
e[m].w=min(red[j]^blue[k],red[k]^blue[j]);
++m;
}
}
sort(e,e+m,cmp);
ans=;
for(j=;j<=n;++j)pre[j]=j;
for(j=;j<m;++j)join(e[j].u,e[j].v,e[j].w);
cout<<"Case #"<<i<<": "<<ans<<endl;
}
return ;
}
E3458 Cards Game(Large)
First AC: 2019-05-16 Latest Modification: 2019-05-16
#include<bits/stdc++.h>
using namespace std;
const int maxn=,maxm=;
int T,n,m;
long long ans;
int red[maxn],blue[maxn];
struct edge{
int u,v,w;
}e[maxm];
int pre[maxn];
int i,j,k;
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int find(int x)
{
int r=x,i;
while(pre[r]!=r)r=pre[r];
while(pre[x]!=r)i=pre[x],pre[x]=r,x=i;
return r;
}
void join(int x,int y,int z)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
pre[fx]=fy;
ans+=z;
}
}
int main()
{
cin>>T;
for(i=;i<=T;++i){
cin>>n;
for(j=;j<=n;++j)cin>>red[j];
for(j=;j<=n;++j)cin>>blue[j];
m=;
for(j=;j<n;++j){
for(k=j+;k<=n;++k){
e[m].u=j;
e[m].v=k;
e[m].w=min(red[j]^blue[k],red[k]^blue[j]);
++m;
}
}
sort(e,e+m,cmp);
ans=;
for(j=;j<=n;++j)pre[j]=j;
for(j=;j<m;++j)join(e[j].u,e[j].v,e[j].w);
cout<<"Case #"<<i<<": "<<ans<<endl;
}
return ;
}
E3461 在哈尔滨的寒风中
First AC: 2017-12-08 Latest Modification: 2017-12-08
#include<iostream>
using namespace std;
long long m,n,t;
int main()
{
cin>>m>>n;
if(m>n)t=m,m=n,n=t;
if(m==)cout<<;
else if(m==){
if(n==)cout<<;
else cout<<(n-)/*((n-)/+)+(n-)/*((n-)/+);
}
else if(m==&&n==)cout<<;
else cout<<m*n*(m*n-)/;
return ;
}
E3466 Baza
First AC: 2017-12-13 Latest Modification: 2018-06-09
#include<bits/stdc++.h>
using namespace std;
int n,m,q,cnt;
long a[][],b[];
int i,j;
int main()
{
cin>>n>>m;
for(i=;i<n;++i)for(j=;j<m;++j)cin>>a[i][j];
cin>>q;
while(q--){
for(i=;i<m;++i)cin>>b[i];
for(cnt=n,i=;i<n;++i){
for(j=;j<m;++j)if(b[j]>&&a[i][j]!=b[j]){--cnt;break;}
}
cout<<cnt<<endl;
}
return ;
}
E3467 Telefoni
First AC: 2017-12-14 Latest Modification: 2017-12-14
#include<cstdio>
int main()
{
long n,d,cnt,tmp;
long a[]={,};
long i,j;
scanf("%ld%ld%ld",&n,&d,&tmp);
for(i=;i<d;++i){
scanf("%ld",&tmp);
for(cnt=i,j=;j<i;++j)if(a[j]<cnt)cnt=a[j];
a[i]=cnt;
if(!tmp)++a[i];
}
for(i=d;i<n;++i){
scanf("%ld",&tmp);
for(cnt=i,j=i-d;j<i;++j)if(a[j]<cnt)cnt=a[j];
a[i]=cnt;
if(!tmp)++a[i];
}
printf("%ld",a[n-]);
return ;
}
E3468 Igra
First AC: 2018-06-09 Latest Modification: 2018-06-09
Note: 贪心+dfs剪枝(好像可以证明剪枝判定恰能得到最优解,如果能证明就可以不用dfs直接贪心)
#include<bits/stdc++.h>
using namespace std;
int n,len;
string s,t;
char rst[];
int sums[],sumt[];
int i;
void dfs(int i)
{
if(i==len){
for(int j=;j<len;++j)
cout<<rst[j];
exit();
}
if(t[i]!='a'&&sums[]){
--sums[];
--sumt[t[i]-'a'];
if(sumt[]<=sums[]+sums[]&&
sumt[]<=sums[]+sums[]&&
sumt[]<=sums[]+sums[]){
rst[i]='a';
dfs(i+);
}
++sums[];
++sumt[t[i]-'a'];
}
if(t[i]!='b'&&sums[]){
--sums[];
--sumt[t[i]-'a'];
if(sumt[]<=sums[]+sums[]&&
sumt[]<=sums[]+sums[]&&
sumt[]<=sums[]+sums[]){
rst[i]='b';
dfs(i+);
}
++sums[];
++sumt[t[i]-'a'];
}
if(t[i]!='c'&&sums[]){
--sums[];
--sumt[t[i]-'a'];
if(sumt[]<=sums[]+sums[]&&
sumt[]<=sums[]+sums[]&&
sumt[]<=sums[]+sums[]){
rst[i]='c';
dfs(i+);
}
++sums[];
++sumt[t[i]-'a'];
}
}
int main()
{
cin>>n>>s>>t;
len=s.length();
for(i=;i<len;++i){
++sums[s[i]-'a'];
++sumt[t[i]-'a'];
}
dfs();
return ;
}
E3469 Savrsen
First AC: 2017-12-14 Latest Modification: 2017-12-14
#include<iostream>
using namespace std;
long long a[]={,,,};
long long i,j,m,rst,A,B;
int main()
{
ios::sync_with_stdio(false);
for(i=;i<;++i){
a[i*i]+=i;
m=/i+;
for(j=i+;j<m;++j)a[i*j]+=i+j;
}
cin>>A>>B;
for(i=A;i<=B;++i){
m=a[i]+-i;
m>? rst+=m:rst-=m;
}
cout<<rst;
return ;
}
E3486 最大的子串
First AC: 2018-01-21 Latest Modification: 2018-01-21
#include<iostream>
using namespace std;
string s;
int len,n,cnt;
int i,j;
int main()
{
cin>>s;
len=s.length();
for(i=len-;i>=;--i){
if(s[i]^'')break;
--len;
}
if(len==){cout<<;return ;}
n=;
for(i=;i<len;++i){
cnt=len-i;
for(j=;j<cnt;++j){
if(s[i+j]<s[n+j])break;
if(s[i+j]>s[n+j]){n=i;break;}
}
}
for(i=n;i<len;++i)cout<<s[i];
return ;
}
E3487 石头剪刀布的套路
First AC: 2018-01-21 Latest Modification: 2018-01-21
#include<iostream>
using namespace std;
int p,q,r,a,b,c;
int main()
{
cin>>p>>q>>r;
for(int i=;i<;++i){
if(a>b&&a>c)cout<<,++b;
else if(b>a&&b>c)cout<<,++c;
else if(c>a&&c>b)cout<<,++a;
else if(a==b){
if(c==a){
if(p==)cout<<,++c;
else if(q==)cout<<,++a;
else cout<<,++b;
}
else if(p>q)cout<<,++c;
else cout<<,++a;
}
else if(a==c){
if(p>r)cout<<,++c;
else cout<<,++b;
}
else if(q>r)cout<<,++a;
else cout<<,++b;
cout<<endl;
}
return ;
}
E3489 坑爹的售票机
First AC: 2019-03-28 Latest Modification: 2019-03-28
Note: 最后直接枚举优先级是不正确的,道理类似5,10,25元纸币凑n元最少张数不能贪心
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,k,p,ans;
ll cost[]={,,,,,};
ll now[];
ll dp[];
ll i,j;
void check(ll sumx,ll pri)
{
ll tmp=max(0LL,sumx-)/pri*pri;
ans=min(ans,tmp/pri*now[pri]+dp[sumx-tmp]);
}
int main()
{
cin>>n>>k>>p;
for(i=;i<=k;++i){
ll tmp=i*p;
for(j=;j>=;--j){
now[i]+=tmp/cost[j];
tmp%=cost[j];
}
}
memset(dp,0x3f,sizeof dp);
dp[]=;
for(i=;i<=k;++i){
for(j=i;j<;++j){
dp[j]=min(dp[j],dp[j-i]+now[i]);
}
}
ans=1e18;
for(i=;i<=k;++i)check(n,i);
cout<<ans;
return ;
}
E3492 简单排序题
First AC: 2018-04-29 Latest Modification: 2018-04-29
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,tmp,rst;
ll a[];
ll i;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
cin>>a[];
for(i=;i<=n;++i)tmp+=i,cin>>a[i],a[i]-=tmp;
sort(a+,a+n+);
tmp=a[(n+)/];
for(i=;i<=n;++i)rst+=abs(tmp-a[i]);
cout<<rst;
return ;
}
E3493 你们要的与质数与偶数相关的送分题
First AC: 2018-04-29 Latest Modification: 2018-04-29
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
bool ispri(ll n)
{
for(ll i=;i*i<=n;++i)
if(!(n%i))return ;
return ;
}
int main()
{
cin>>n;
if(ispri(n))cout<<;
else if(!(n%)||ispri(n-))cout<<;
else cout<<;
return ;
}
E3494 归零
First AC: 2018-02-21 Latest Modification: 2018-02-21
#include<bits/stdc++.h>
using namespace std;
long n,p,s,a;
int main()
{
cin>>n>>p;
if(p<n){
while(n--)cin>>a,s=(s+a)%p;
s? cout<<"NO":cout<<"YES";
}
else{
cin>>s;
while(--n){
cin>>a;
if(a!=s)p=;
}
p? cout<<"YES":cout<<"NO"; }
return ;
}
E3496 贪吃的xjj和贪心的oxx
First AC: 2018-04-18 Latest Modification: 2018-04-18
#include<bits/stdc++.h>
using namespace std;
long long n,oxx,xjj;
struct data{
long long num,item;
}a[];
stack<long long>s,t;
long long i;
bool cmp(data a,data b)
{
return a.num>b.num;
}
int main()
{
cin>>n;
for(i=;i<=n;++i)cin>>a[i].num,a[i].item=i;
sort(a+,a+n+,cmp);
for(i=;i<=n;++i){
if(oxx<xjj)oxx+=a[i].num,s.push(a[i].item);
else xjj+=a[i].num,t.push(a[i].item);
}
cout<<"Yes\n";
if(oxx<xjj){
cout<<s.size()<<endl<<s.top();
s.pop();
while(!s.empty())cout<<' '<<s.top(),s.pop();
}
else{
cout<<t.size()<<endl<<t.top();
t.pop();
while(!t.empty())cout<<' '<<t.top(),t.pop();
}
return ;
}
E3499 回家咯
First AC: 2018-02-13 Latest Modification: 2018-02-13
#include<bits/stdc++.h>
using namespace std;
long x,y,z;
int main()
{
cin>>x>>y>>z;
if(x+*y>z&&z>x&&x+z>*y)cout<<(x+*y-z)/2.0;
else cout<<"Wrong";
}
E3502 密码碰撞
First AC: 2018-06-28 Latest Modification: 2018-06-28
Note: 字符串s是t的子串的必要条件是s长度小于等于t
而此时若s和t长度相等,则必有s和t完全相同
一种做法是利用trie树,先对输入字符串相同合并后按长度降序排序
对个数为cnt的每种字符串,其内部对答案的贡献为cnt×(cnt-1)÷2
遍历排序后的字符串,先匹配其在trie树出现的次数,乘上cnt便是答案
再把字符串的所有子串去重后按权值cnt插入trie树,为后面更短的字符串提供匹配源
#include<bits/stdc++.h>
using namespace std;
int n,num,tmp,rst;
string s,t;
map<string,int>fd,mp;
map<string,int>::iterator it;
struct data{
string s;
int cnt,len;
}a[];
int i,j,k;
bool cmp(data a,data b)
{
return a.len>b.len;
}
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>s;
if((it=fd.find(s))!=fd.end())++a[it->second].cnt;
else{
a[num].s=s;
a[num].cnt=;
a[num].len=s.length();
fd.insert(pair<string,int>(s,num++));
}
}
fd.clear();
sort(a,a+num,cmp);
for(i=;i<num;++i){
rst+=a[i].cnt*(a[i].cnt-);
if((it=fd.find(a[i].s))!=fd.end())rst+=a[i].cnt*it->second;
mp.clear();
for(j=;j<a[i].len;++j){
for(k=j;k<a[i].len;++k){
t=a[i].s.substr(j,k-j+);
if((it=mp.find(t))==mp.end()){
mp.insert(pair<string,int>(t,));
if((it=fd.find(t))!=fd.end())it->second+=a[i].cnt;
else fd.insert(pair<string,int>(t,a[i].cnt));
}
}
}
}
cout<<rst;
return ;
}
E3504 打工是不可能打工的
First AC: 2018-03-30 Latest Modification: 2018-03-30
#include<bits/stdc++.h>
using namespace std;
long n,tmp,debt,cnt;
long a[];
long i;
int main()
{
cin>>n;
for(i=;i<n;++i)scanf("%ld",a+i);
for(i=;i<n;++i){
scanf("%ld",&tmp);
debt+=tmp;
}
sort(a,a+n);
for(i=n-;i>=;--i){
if(debt<=)break;
debt-=*(a+i);
++cnt;
}
if(debt>)cout<<"Game Over!";
else cout<<cnt;
return ;
}
E3505 这也是一道排序题
First AC: 2018-04-29 Latest Modification: 2018-04-29
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,rst;
ll a[];
ll i;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(i=;i<n;++i)cin>>a[i];
for(i=n-;i;--i)a[i]-=a[i-];
sort(a+,a+n);
rst=a[];
for(i=;i<n;++i)a[i]+=a[i-],rst+=a[i];
cout<<rst;
return ;
}
E3506 斐波那契数列
First AC: 2018-05-07 Latest Modification: 2018-05-07
#include<bits/stdc++.h>
using namespace std;
const int M=;
int fib[]={,,};
int hsh[M];
int i;
int main()
{
for(i=;i<;++i)fib[i]=(fib[i-]+fib[i-])%M;
for(i=;i<;++i)hsh[fib[i]]=i;
char c;
int tmp=;
while((c=getchar())!=EOF){
if(c!='\n')tmp=(tmp*+c-'')%M;
else{
cout<<hsh[tmp]<<endl;
tmp=;
}
}
return ;
}
E3507 坑爹的售票机(Easy)
First AC: 2019-03-27 Latest Modification: 2019-03-27
Note: 枚举同时买1至k张的最小取票数,然后dp答案
#include<bits/stdc++.h>
using namespace std;
int n,k,p;
int cost[]={,,,,,};
int now[];
int dp[];
int i,j;
int main()
{
cin>>n>>k>>p;
for(i=;i<=k;++i){
int tmp=i*p;
for(j=;j>=;--j){
now[i]+=tmp/cost[j];
tmp%=cost[j];
}
}
memset(dp,0x3f,sizeof dp);
dp[]=;
for(i=;i<=k;++i){
for(j=i;j<=n;++j){
dp[j]=min(dp[j],dp[j-i]+now[i]);
}
}
cout<<dp[n]<<endl;
return ;
}
E3528 西班牙馅饼
First AC: 2018-06-28 Latest Modification: 2018-06-28
#include<bits/stdc++.h>
using namespace std;
int n,m,tmp1,tmp2,rst;
int i;
int main()
{
cin>>n>>m;
while(n--){
tmp1=;
for(i=;i<m;++i){
cin>>tmp2;
if(tmp2>tmp1)tmp1=tmp2;
}
rst+=tmp1;
}
cout<<rst;
return ;
}
E3529 梵高先生
First AC: 2018-04-13 Latest Modification: 2018-04-13
#include<bits/stdc++.h>
using namespace std;
long a[][];
int n;
int i,j;
int main()
{
a[][]=;
a[][]=;
a[][]=;
for(i=;i<;++i){
a[i][]=;
for(j=;j<i;++j)a[i][j]=a[i-][j-]+a[i-][j];
a[i][i]=;
}
cin>>n;
for(i=;i<=n;++i){
for(j=;j<i;++j)cout<<a[i][j]<<' ';
cout<<a[i][i]<<endl;
}
return ;
}
E3530 和你在一起
First AC: 2018-04-13 Latest Modification: 2018-06-28
#include<bits/stdc++.h>
using namespace std;
int n;
string s[];
int i;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>s[i];
sort(s,s+n);
for(i=n-;i>=;--i)cout<<s[i];
return ;
}
E3531 定西
First AC: 2018-04-13 Latest Modification: 2018-04-13
#include<bits/stdc++.h>
using namespace std;
int n,k,m;
bool dmg[];
int dp[];
int i;
int main()
{
cin>>n>>k;
while(k--)cin>>m,dmg[m]=;
if(!dmg[])dp[]=;
if(!dmg[])dp[]=dp[]+;
if(!dmg[])dp[]=dp[]+dp[]+;
for(i=;i<=n;++i){
if(dmg[i])continue;
if(!dmg[i-])dp[i]+=dp[i-];
if(!dmg[i-])dp[i]+=dp[i-];
if(!dmg[i-])dp[i]+=dp[i-];
}
cout<<dp[n];
return ;
}
E3532 热河路
First AC: 2018-04-13 Latest Modification: 2018-04-13
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,a,tmp;
int main()
{
scanf("%lld",&n);
while(n--){
scanf("%lld",&tmp);
a=*tmp-;
ll b=sqrt(a);
double c=sqrt(a);
if(fabs(b*1.0-c)!=)puts("");
else puts("");
}
return ;
}
E3533 庙会
First AC: 2018-04-13 Latest Modification: 2018-04-13
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
int i;
int main()
{
cin>>n>>m>>k;
for(i=;i<k;++i)cout<<i%n+<<' '<<i%m+<<endl;
return ;
}
E3540 ultmaster的小迷妹们
First AC: 2018-04-21 Latest Modification: 2018-04-21
#include<bits/stdc++.h>
using namespace std;
int n,x,y;
int main()
{
cin>>n>>x>>y;
n%__gcd(x,y)? cout<<"No":cout<<"Yes";
return ;
}
E3541 代码查重
First AC: 2018-07-04 Latest Modification: 2018-07-04
#include<bits/stdc++.h>
using namespace std;
int n,m,k,x,y;
int a[],b[];
map< pair<int,int> ,bool>mp;
map< pair<int,int> ,bool>::iterator it;
int i;
int main()
{
cin>>n>>m>>k;
if(n!=m){
cout<<"No";
return ;
}
for(i=;i<n;++i)cin>>a[i];
for(i=;i<m;++i)cin>>b[i];
for(i=;i<k;++i){
cin>>x>>y;
mp.insert(pair< pair<int,int> ,bool>(pair<int,int>(x,y),));
mp.insert(pair< pair<int,int> ,bool>(pair<int,int>(y,x),));
}
for(i=;i<n;++i){
it=mp.find(pair<int,int>(a[i],b[i]));
if(a[i]!=b[i]&&it==mp.end()){
cout<<"No";
return ;
}
}
cout<<"Yes";
return ;
}
E3542 神奇的魔术
First AC: 2018-04-21 Latest Modification: 2018-04-21
#include<bits/stdc++.h>
using namespace std;
int n,num,jdg,pos;
int tmp[],rst[];
int i,j;
void print()
{
int i;
cout<<tmp[];
for(i=;i<=num;++i)cout<<' '<<tmp[i];
cout<<endl;
}
void find(int lft,int rgt,int n)
{
if(lft+==rgt){
rst[lft]=n;
return;
}
if(lft+==rgt){
if(rst[lft]){
rst[lft+]=n;
return;
}
else if(rst[lft+]){
rst[lft]=n;
return;
}
}
int mid=(lft+rgt)/,i;
for(i=;i<=num;++i)tmp[i]=;
for(i=mid;i<rgt;++i)
if(i!=pos)tmp[i]=n;
print();
cin>>jdg;
if(jdg==num)exit();
if(jdg!=)find(lft,mid,n);
else find(mid,rgt,n);
}
int main()
{
cin>>n;
num=pow(,n);
for(i=;i<=num;++i){
for(j=;j<=num;++j)tmp[j]=;
tmp[i]=;
print();
cin>>jdg;
if(jdg==num)exit();
if(jdg==){
rst[i]=;
pos=i;
break;
}
}
for(i=;i<=num;++i)find(,num+,i);
cout<<rst[];
for(i=;i<=num;++i)cout<<' '<<rst[i];
cout<<endl;
cin>>jdg;
return ;
}
E3544 小迷妹在哪儿
First AC: 2018-04-21 Latest Modification: 2018-04-21
#include<bits/stdc++.h>
using namespace std;
int n,T,rst;
struct data{
int a,t;
}girl[];
int dp[][];
bool jdg[][];
int i,j;
bool cmp(data a,data b)
{
return a.a*b.t>b.a*a.t;
}
int main()
{
cin>>n>>T;
for(i=;i<=n;++i)
cin>>girl[i].a>>girl[i].t;
sort(girl+,girl+n+,cmp);
jdg[][T]=;
for(i=;i<n;++i)for(j=;j<=T;++j){
if(jdg[i][j]){
dp[i+][j]=max(dp[i+][j],dp[i][j]);
jdg[i+][j]=;
if(j>=girl[i+].t){
dp[i+][j-girl[i+].t]=
max(dp[i][j]+(j-girl[i+].t)*
girl[i+].a,dp[i+][j-girl[i+].t]);
jdg[i+][j-girl[i+].t]=;
}
}
}
for(i=;i<=T;++i)if(dp[n][i]>rst)rst=dp[n][i];
cout<<rst;
return ;
}
E3560 浮点数模运算
First AC: 2018-05-12 Latest Modification: 2018-05-12
#include<bits/stdc++.h>
using namespace std;
long double a,b;
long long c,d;
int main()
{
cin>>a>>b;
c=(long double)(a*1e9+0.5);
d=(long double)(b*1e9+0.5);
printf("%.15f",c%d*1.0/1e9);
return ;
}
E3561 数螃蟹
First AC: 2018-05-12 Latest Modification: 2018-05-12
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M=1e18;
ll n,d,cnt,tmp;
ll a[],b[];
ll i,j,k;
int main()
{
cin>>n;
for(i=;i<n;++i)cin>>a[i];
if(n>){
tmp=;
for(j=;j<n;++j){
tmp=a[j]-a[j-];
if(tmp==a[j-]-a[j-]&&tmp==a[j-]-a[j-]){
b[j]=a[j];
for(k=j+;k<n;++k)b[k]=b[k-]+tmp;
for(k=j-;k>=;--k)b[k]=b[k+]-tmp;
cout<<b[];
for(k=;k<n;++k)cout<<' '<<b[k];
return ;
}
}
}
else{
for(i=;i<n;++i)for(j=i+;j<n;++j){
d=(a[j]-a[i])/(j-i);
if(a[j]+(n-j-)*d>1e18||a[j]+(n-j-)*d<-1e18)continue;
if(a[j]-j*d>1e18||a[j]-j*d<-1e18)continue;
b[j]=a[j];
for(k=j+;k<n;++k)b[k]=b[k-]+d;
for(k=j-;k>=;--k)b[k]=b[k+]-d;
cnt=;
for(k=;k<n;++k)if(a[k]!=b[k])++cnt;
if(cnt<){
cout<<b[];
for(k=;k<n;++k)cout<<' '<<b[k];
return ;
}
}
}
return ;
}
E3564 无聊的数学题
First AC: 2018-05-12 Latest Modification: 2018-05-12
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,k,p,rst;
ll power(ll a,ll b,ll p)
{
ll base=a,rst=;
while(b){
if(b&)rst=(rst*base)%p;
base=(base*base)%p;
b>>=;
}
return rst;
}
int main()
{
cin>>n>>k>>p;
rst=power(,n,p-);
rst=power(,(rst+p--n)%(p-)+p-,p);
k? cout<<rst:cout<<(rst+p-)%p;
return ;
}
E3591 GPS数据处理
First AC: 2018-07-11 Latest Modification: 2018-07-11
import java.util.*; class Result
{
public static int ret = 0;
public static final int mod = 65536;
public static int result = 0;
} public class Main
{
public static void cal(String s)
{
int len = s.length();
for(int i = 0; i < len; ++i) {
Result.ret ^= s.charAt(i);
}
} public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String s;
while(true) {
s = in.nextLine();
if(s.equals("END"))break;
String[] part = s.split(",");
if(part.length != 13)continue;
if(part[0].equals("$GPRMC") == false)continue;
if(part[2].equals("A") == false)continue;
if((part[4].equals("N")||part[4].equals("S"))==false)continue;
if((part[6].equals("W")||part[6].equals("E"))==false)continue;
if(part[12].contains("*") == false)continue;
Result.ret = 0;
Main.cal("$");
for(int i = 0; i < 12; ++i)Main.cal(part[i]);
int len = part[12].length();
for(int i = 0; i < len; ++i) {
if(part[12].substring(i, i+1).equals("*")) {
Main.cal(part[12].substring(0, i));
Result.ret %= Result.mod;
if(Result.ret == Integer.parseInt(part[12].substring(i+1,len),16)) {
Result.result = Integer.parseInt(part[1].substring(0, 6));
}
break;
}
}
}
Result.result = (Result.result + 80000) % 240000;
int hh = Result.result / 10000;
int mm = Result.result % 10000/100;
int ss = Result.result % 100;
System.out.printf("%02d:%02d:%02d", hh, mm, ss);
}
}
E3601 恢复古诗
First AC: 2018-07-05 Latest Modification: 2018-07-05
#include<bits/stdc++.h>
using namespace std;
int P,L;
string s;
struct data{
int item;
string s[];
}a[];
map<string,int>mp;
map<string,int>::iterator it;
int nxt[];
int i,j,k;
int main()
{
cin>>P>>L;
getline(cin,s);
for(i=;i<P;++i){
a[i].item=i;
for(j=;j<L;++j)getline(cin,a[i].s[j]);
mp.insert(pair<string,int>(a[i].s[L-],i));
}
for(i=;i<P;++i){
if((it=mp.find(a[i].s[]))!=mp.end()){
a[i].item=it->second;
nxt[it->second]=i;
}
}
for(i=;i<P;++i){
if(a[i].item==i){
cout<<a[i].s[]<<endl;
for(j=;j<P;++j){
for(k=;k<L;++k)cout<<a[i].s[k]<<endl;
i=nxt[i];
}
break;
}
}
return ;
}
E3619 锐角三角形
First AC: 2018-07-11 Latest Modification: 2018-07-11
#include<bits/stdc++.h>
using namespace std;
long long s;
int main()
{
cin>>s;
if(s<)cout<<"No";
else if(s&)cout<<"Yes\n1 0\n0 1\n"<<(s+)/<<' '<<(s+)/;
else cout<<"Yes\n0 0\n2 0\n1 "<<s/;
return ;
}
E3620 大鱼吃小鱼
First AC: 2018-07-11 Latest Modification: 2018-07-11
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,a,tmp,rst=;
struct data{
ll w,a;
bool add;
}fish[];
int i;
bool cmp(data a,data b)
{
if(a.add!=b.add)return a.add>b.add;
if(a.add){
if(a.w!=b.w)return a.w<b.w;
return a.a>b.a;
}
else{
if(a.w+a.a!=b.w+b.a)return a.w+a.a>b.w+b.a;
return a.a>b.a;
}
}
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>fish[i].w>>fish[i].a;
if(fish[i].a<)fish[i].add=;
else fish[i].add=;
}
sort(fish,fish+n,cmp);
for(i=;i<n;++i){
if(fish[i].add)rst=max(rst,fish[i].w-tmp);
else rst=max(rst,max(fish[i].w-tmp,-fish[i].a-tmp));
tmp+=fish[i].a;
}
cout<<rst;
return ;
}
E3622 异常的葡萄
First AC: 2018-07-09 Latest Modification: 2018-07-09
#include<bits/stdc++.h>
using namespace std;
int n,rst;
struct data{
int item;
double b[];
}a[];
double cnt[];
int i,j,k;
int main()
{
cin>>n;
for(i=;i<n;++i){
cin>>a[i].item;
for(j=;j<;++j)cin>>a[i].b[j];
}
for(i=;i<n;++i){
for(j=;j<n;++j){
if(i==j)continue;
double tmp=;
for(k=;k<;++k){
tmp+=pow(a[i].b[k]-a[j].b[k],);
}
cnt[i]+=sqrt(tmp);
}
}
for(i=;i<n;++i)if(cnt[i]>cnt[rst])rst=i;
cout<<a[rst].item;
return ;
}
E3628 A Simple Convolution
First AC: 2018-08-13 Latest Modification: 2018-08-13
#include<bits/stdc++.h>
using namespace std;
int n,m,h,w,r,c;
int a[][],b[][],t[][];
int i,j,k,l;
int main()
{
cin>>n>>m;
for(i=;i<n;++i)for(j=;j<m;++j)cin>>a[i][j];
cin>>h>>w;
for(i=;i<h;++i)for(j=;j<w;++j)cin>>b[i][j];
r=n-h+,c=m-w+;
memset(t,,sizeof(t));
for(i=;i<r;++i)for(j=;j<c;++j)
for(k=;k<h;++k)for(l=;l<w;++l)
t[i][j]+=a[i+k][j+l]*b[k][l];
for(i=;i<r;++i){
cout<<t[i][];
for(j=;j<c;++j)cout<<' '<<t[i][j];
cout<<endl;
}
return ;
}
E3630 Bad Queen
First AC: 2018-08-13 Latest Modification: 2018-08-13
Note: 可以先跳到(1,y),然后跳到(1,1),最后蛇形走完
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y;
bool jdg[][];
int i,j;
int main()
{
cin>>n>>m>>x>>y;
if(x!=)cout<<<<' '<<y<<endl;
jdg[][y]=jdg[x][y]=;
for(i=;i<=n;++i){
if(i&){
for(j=;j<=m;++j)
if(!jdg[i][j])
cout<<i<<' '<<j<<endl;
}
else{
for(j=m;j;--j)
if(!jdg[i][j])
cout<<i<<' '<<j<<endl;
}
}
return ;
}
E3637 解密信件
First AC: 2018-12-10 Latest Modification: 2018-12-10
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
ll n,x;
ll cal(ll l,ll r,ll x)
{
if(l==r)return l;
ll m=(r+l+)/;
if(x<m)x=cal(l,m-,x);
else x=cal(m,r,x);
return l+r-x;
}
int main()
{
cin>>T;
while(T--){
cin>>n>>x;
cout<<cal(,n,x)<<endl;
}
return ;
}
E3639 圆
First AC: 2018-09-15 Latest Modification: 2018-09-15
#include<bits/stdc++.h>
using namespace std;
int r;
int main()
{
cin>>r;
printf("%.6lf\n%.6lf",acos(-)*r*r,2.0*r*r);
return ;
}
E3640 素数子序列
First AC: 2019-02-20 Latest Modification: 2019-02-20
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,a,b,c;
bool jdg(ll n)
{
if(n<)return ;
for(ll i=;i*i<=n;++i){
if(n%i==)return ;
}
return ;
}
int main()
{
cin>>n;
if(n>)cout<<"Impossible";
else if(n==){
cin>>a;
if(a){
if(jdg(a))cout<<"Impossible";
else cout<<a;
}
else cout<<;
}
else if(n==){
cin>>a>>b;
if(a==){
if(b){
if(!jdg(b)&&!jdg(b+))cout<<"2 "<<b;
else cout<<"Impossible";
}
else cout<<"2 3";
}
else if(b==){
if(a){
if(!jdg(a)&&!jdg(a+))cout<<a<<"";
else cout<<"Impossible";
}
else cout<<"3 2";
}
else if(a||b)cout<<"Impossible";
else cout<<"2 3";
}
else if(n==){
cin>>a>>b>>c;
if(!a)a=;
if(!b)b=;
if(!c)c=;
if(a==&&b==&&c==)cout<<"2 3 2";
else cout<<"Impossible";
}
return ;
}
E3641 整数划分
First AC: 2018-09-15 Latest Modification: 2018-09-15
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
int i;
int main()
{
cin>>n;
if(n<||n%==){
cout<<"Impossible";
return ;
}
if(n%==)cout<<"1 2 3 3 2 1",cnt=(n-)/;
else if(n%==)cout<<"1 1 1 2 3 1 3 2",cnt=(n-)/;
else if(n%==)cout<<"1 1 1 1 1 2 3 3 2",cnt=(n-)/;
else if(n%==)cout<<"1 2 2 1 3",cnt=(n-)/;
while(cnt--)cout<<" 1 2 3 3 2 1";
return ;
}
E3642 oxx的小姐姐们
First AC: 2019-01-30 Latest Modification: 2019-01-30
Note: 由p|nm知p|n或p|m
#include<bits/stdc++.h>
using namespace std;
int n,m,p,cnt;
int i,j;
int main()
{
cin>>n>>m>>p;
if(!(n%p)){
cout<<"Yes\n";
for(i=;i<n;++i){
cout<<i/p*m+;
for(j=;j<m;++j){
cout<<' '<<i/p*m+j+;
}
cout<<endl;
}
}
else if(!(m%p)){
cout<<"Yes\n";
for(i=;i<n;++i){
cout<<m/p*i+;
for(j=;j<m;++j){
cout<<' '<<m/p*i+j/p+;
}
cout<<endl;
}
}
else cout<<"No";
return ;
}
E3646 猜价格
First AC: 2018-11-19 Latest Modification: 2018-11-19
Note: 注意到二分确定答案的最大次数是n,说明剩下的n次是用来纠错的
的如果k≤n,可以先试错k-1个1,以后每次判断的正确性都确定了
的如果k>n,最多碰到一次错误,只要每个数试两遍,如果判断不同则表明出错
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,k;
ll l,r,m;
char op,op1,op2;
int cnt,err;
int i;
void print(int n)
{
if(n)cout<<n<<endl;
else cout<<m<<endl;
fflush(stdout);
++cnt;
}
int main()
{
cin>>n>>k;
l=r=;
for(i=;i<n;++i)r*=;
if(k<=n){
for(i=;i<k;++i){
print();
cin>>op;
if(op=='=')return ;
if(op=='>'){
err=cnt;
break;
}
}
while(){
m=(l+r)/;
print();
cin>>op;
if(op=='=')return ;
if(cnt%k==err){
if(op=='>')l=m+;
else r=m;
}
else{
if(op=='<')l=m+;
else r=m;
}
}
}
else{
while(){
m=(l+r)/;
print();
cin>>op1;
if(op1=='=')return ;
print();
cin>>op2;
if(op2=='=')return ;
if(op1==op2){
if(op1=='<')l=m+;
else r=m;
}
else{
print();
cin>>op;
if(op=='=')return ;
if(op=='<')l=m+;
else r=m;
break;
}
}
while(){
m=(l+r)/;
print();
cin>>op;
if(op=='=')return ;
if(op=='<')l=m+;
else r=m;
}
}
return ;
}
E3649 租房
First AC: 2018-11-19 Latest Modification: 2018-11-19
Note: 观察到两点坐标公式,于是把所有数据扩大两倍便均为整数,然后对中点坐标的奇偶性讨论
的我们最终的目的是把中点移动到横纵坐标都为偶数
的横纵坐标变化的最小幅度分别是△y/gcd(△x,△y)和△x/gcd(△x,△y)
的添加偶数倍的最小幅度是没有意义的,因为它不仅没改变坐标奇偶性,还把租房距离变大了
的再对输入的两点连线斜率分四类(0、正、负、不存在)讨论并在必要时加还是减一倍的最小幅度即可
1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 ll xl,yl,xr,yr;
5 ll xm,ym;
6 ll delx,dely,gcd;
7 int main()
8 {
9 cin>>xl>>yl>>xr>>yr;
10 xm=xl+xr;
11 ym=yl+yr;
12 if(xm%2==0&&ym%2==0)cout<<xm/2<<' '<<ym/2;
13 else{
14 delx=abs(yl-yr);
15 dely=abs(xl-xr);
16 gcd=__gcd(delx,dely);
17 delx/=gcd;
18 dely/=gcd;
19 if(xl==xr){
20 if(ym%2)cout<<"NO";
21 else cout<<xm/2<<' '<<ym/2;
22 return 0;
23 }
24 if(xl>xr){
25 xl^=xr,xr^=xl,xl^=xr;
26 yl^=yr,yr^=yl,yl^=yr;
27 }
28 if(xm%2&&ym%2){
29 if(delx%2&&dely%2){
30 if(yl>yr)cout<<(xm-delx)/2<<' '<<(ym-dely)/2;
31 else cout<<(xm-delx)/2<<' '<<(ym+dely)/2;
32 }
33 else cout<<"NO";
34 }
35 else if(xm%2){
36 if(delx%2&&dely%2==0){
37 if(yl>yr)cout<<(xm-delx)/2<<' '<<(ym-dely)/2;
38 else cout<<(xm-delx)/2<<' '<<(ym+dely)/2;
39 }
40 else cout<<"NO";
41 }
42 else if(ym%2){
43 if(delx%2==0&&dely%2){
44 if(yl>yr)cout<<(xm-delx)/2<<' '<<(ym-dely)/2;
45 else cout<<(xm-delx)/2<<' '<<(ym+dely)/2;
46 }
47 else cout<<"NO";
48 }
49 }
50 return 0;
51 }
E3650 转机折扣
First AC: 2018-11-19 Latest Modification: 2018-11-19
1 #include<bits/stdc++.h>
2 using namespace std;
3 string a[2];
4 string s;
5 int len;
6 int i;
7 int main()
8 {
9 cin>>a[0]>>a[1];
10 sort(a,a+2);
11 s=a[0];
12 len=a[0].length();
13 for(i=len-1;i>=0;--i){
14 if(s[i]<'Z'){
15 ++s[i];
16 break;
17 }
18 s[i]='A';
19 }
20 cout<<s;
21 return 0;
22 }
E3651 仰望星空
First AC: 2018-12-09 Latest Modification: 2018-12-09
#include<bits/stdc++.h>
using namespace std;
long long N,A,B;
int main()
{
cin>>N>>A>>B;
cout<<(N-)*(B-A)+;
return ;
}
E3652 乘法还原
First AC: 2018-12-10 Latest Modification: 2018-12-10
Note: 记$A=\{ a_{1},a_{2},\cdots,a_{m}\},B=\{ b_{1},b_{2},\cdots,b_{m}\}$
设输入的$n$个无序二元组组成的集合为$W$,则$W$中元素可分为如下四类:
①$(a,a)$:表明$a\in A\cap B$,记所有这样的$a$组成的集合为$both$,即$both=\{ a\mid a\in A\cap B\}$
②$(a,b)$,其中$(b,a)\in W$:则$a,b\in both$,而这个性质可由①完全刻画,故可以忽略这样的二元组
③$(a,b)$,其中$a\in both$且$(b,a)\notin W$
④$(c,d)$,其中$c,d\notin both$
如果③④均为空,则$A=B=both$,只要遍历①
如果④非空,只要找到一组$(c,d)$然后遍历$W$找到所有$(c,a_{i}),(a_{i},c),(d,b_{i}),(b_{i},d)$
如果③非空且④为空,则$A=both$,只要遍历$W$找到所有出现过的数字,共同组成$B$
最后再对$A,B$字典序排序即可
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> Pr;
const int MAX=;
int n,x,y,cnt,tmp;
struct data{
int x,y;
}a[MAX],b[MAX];
map<int,int>both,all;
map<int,int>::iterator ed;
int A[MAX],B[MAX];
int cntA,cntB;
int i;
bool cmp(data a,data b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
void out(bool flag)
{
if(flag){
cout<<cntA<<' '<<cntB<<endl<<A[];
for(i=;i<cntA;++i)cout<<' '<<A[i];
cout<<endl<<B[];
for(i=;i<cntB;++i)cout<<' '<<B[i];
}
else{
cout<<cntB<<' '<<cntA<<endl<<B[];
for(i=;i<cntB;++i)cout<<' '<<B[i];
cout<<endl<<A[];
for(i=;i<cntA;++i)cout<<' '<<A[i];
}
exit();
}
void print()
{
sort(A,A+cntA);
sort(B,B+cntB);
if(cntA<cntB)out();
if(cntA>cntB)out();
for(i=;i<cntA;++i){
if(A[i]>B[i])out();
}
out();
}
int main()
{
cin>>n;
for(i=;i<=n;++i){
cin>>x>>y;
a[i].x=min(x,y);
a[i].y=max(x,y);
}
sort(a+,a+n+,cmp);
for(i=;i<=n;++i){
if(Pr(a[i].x,a[i].y)==Pr(a[i-].x,a[i-].y))continue;
if(Pr(a[i].x,a[i].y)==Pr(a[i+].x,a[i+].y))continue;
b[cnt].x=a[i].x;
b[cnt].y=a[i].y;
++cnt;
if(a[i].x==a[i].y)both.insert(Pr(a[i].x,));
}
if(cnt==both.size()){
cout<<cnt<<' '<<cnt<<endl<<b[].x;
for(i=;i<cnt;++i)cout<<' '<<b[i].x;
cout<<endl<<b[].x;
for(i=;i<cnt;++i)cout<<' '<<b[i].x;
return ;
}
ed=both.end();
for(i=;i<cnt;++i){
x=b[i].x;
y=b[i].y;
if(both.find(x)==ed&&both.find(y)==ed){
for(i=;i<cnt;++i){
if(b[i].x==x)A[cntA++]=b[i].y;
if(b[i].y==x)A[cntA++]=b[i].x;
}
for(i=;i<cnt;++i){
if(b[i].x==y)B[cntB++]=b[i].y;
if(b[i].y==y)B[cntB++]=b[i].x;
}
print();
}
}
for(i=;i<=n;++i){
all.insert(Pr(a[i].x,));
all.insert(Pr(a[i].y,));
}
for(ed=both.begin();ed!=both.end();++ed){
A[cntA++]=ed->first;
}
for(ed=all.begin();ed!=all.end();++ed){
B[cntB++]=ed->first;
}
print();
return ;
}
E3656 心与心的距离
First AC: 2018-11-19 Latest Modification: 2018-11-19
1 #include<bits/stdc++.h>
2 using namespace std;
3 int T,n;
4 int i;
5 int main()
6 {
7 cin>>T;
8 for(i=1;i<=T;++i){
9 cin>>n;
10 cout<<"Case #"<<i<<": "<<n<<" 0 0 0 0 "<<n<<endl;
11 }
12 return 0;
13 }
E3674 唐纳德先生与.DOC
First AC: 2019-01-19 Latest Modification: 2019-01-19
Note: 枚举发现,不论交换的两个字符同属同级DOC,还是分属主从句,都不能交换两个句子成分
#include<bits/stdc++.h>
using namespace std;
int T,len;
string s;
int i;
int main()
{
cin>>T;
while(T--){
cin>>s;
len=s.length();
for(i=;i<len;++i){
if(s[i]==s[i-])cout<<"C";
else cout<<"D";
}
cout<<endl;
}
return ;
}
E3675 唐纳德先生与假骰子
First AC: 2019-01-19 Latest Modification: 2019-01-19
Note: 类似离散分配的贪心策略,连续分配中按p/1-p概率分配的方案也总能无损转化为0/1分配方案
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,x,up,dn;
ll i;
int main()
{
cin>>T;
while(T--){
up=dn=;
for(i=;i<=;++i){
cin>>x;
up=max(up,i*x);
dn+=x;
}
printf("%.9lf\n",up*1.0/dn);
}
return ;
}
E3678 方差
First AC: 2019-02-22 Latest Modification: 2019-02-22
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,x;
ll a[];
ll b[];
ll i,j;
int main()
{
cin>>n>>m;
for(i=;i<=n;++i)cin>>a[i];
sort(a+,a+n+);
for(i=;i<=n;++i){
x=a[i];
a[i]=a[i-]+x;
b[i]=b[i-]+x*x;
}
ll rst=1e18;
for(i=m;i<=n;++i){
j=i-m;
rst=min(rst,m*(b[i]-b[j])-(a[i]-a[j])*(a[i]-a[j]));
}
cout<<rst;
return ;
}
E3679 进制转换
First AC: 2019-02-22 Latest Modification: 2019-02-22
Note: 判断乘法溢出用1e18+5.0会WA,因为5.0/ans值特别小超出了double的精度范围
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
ll l,r,k,m,tmp,tmp1,tmp2;
ll cal(ll k,ll m)
{
ll ans=,tmp;
while(m--){
if(2e18*1.0/ans<k)return 2e18;
tmp=ans*k;
ans=tmp;
}
return ans;
}
int main()
{
cin>>T;
while(T--){
cin>>l>>r>>k>>m;
tmp=cal(k,m);
tmp1=r/tmp,tmp2=(l-)/tmp;
tmp1-=tmp1/k;
tmp2-=tmp2/k;
cout<<tmp1-tmp2<<endl;
}
return ;
}
E3682 宇恒棋
First AC: 2019-03-23 Latest Modification: 2019-03-23
Note: 由奇偶性必然有一方不可能获胜,可以证明另一方总能把这一方往边界逼近
#include<bits/stdc++.h>
using namespace std;
int n,m;
int xa,ya,xb,yb;
int main()
{
cin>>n>>m>>xa>>ya>>xb>>yb;
if((xa-xb+ya-yb+)%)cout<<"Lose";
else cout<<"Win";
return ;
}
E3684 钝角三角形
First AC: 2019-03-23 Latest Modification: 2019-03-23
Note: 给出一种构造方法,把最小的n个数先分配给这n组
记这n个数中偶数个数为a,奇数个数为n-a
再把剩余2n个数中较小的2a个数分配给前面偶数的a组
其余2(n-1)个数分配给奇数的(n-a)组,嵌套分配,以下图为例
证明考虑论证奇数组、偶数组第一组满足能推出之后的组也满足
#include<bits/stdc++.h>
using namespace std;
int n,l1,r1,l2,r2;
int i;
int main()
{
cin>>n;
++n;
l1=n+;
r1=n+n/*;
l2=r1+;
r2=*n-;
for(i=n;i>;--i){
if(i&){
cout<<i<<' '<<l2<<' '<<r2<<endl;
++l2;
--r2;
}
else{
cout<<i<<' '<<l1<<' '<<r1<<endl;
++l1;--r1;
}
}
return ;
}
E3685 线段树
First AC: 2019-03-27 Latest Modification: 2019-03-27
Note: 一个很关键的剪枝是当前[l,r]不可能在初始[1,b>=r]的右半子树时直接剪掉
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,l,r,ans;
void dfs(ll l ,ll r)
{
if(l<||r>=ans)return;
if(l==){
ans=min(ans,r);
return;
}
if(*l-r<)return;
ll len=r-l+;
dfs(l,r+len);
dfs(l-len,r);
dfs(l-len-,r);
if(len!=){
dfs(l,r+len-);
}
}
int main()
{
scanf("%lld",&T);
while(T--){
scanf("%lld%lld",&l,&r);
if(l==||l==r){
cout<<r<<endl;
continue;
}
ans=2e9+;
dfs(l,r);
if(ans>2e9)cout<<"-1\n";
else cout<<ans<<endl;
}
return ;
}
E3686 回文串
First AC: 2019-03-23 Latest Modification: 2019-03-23
Note: 可以看出最终的每个数都是初始连续一段数之和
因此任何合并方法总能调整次序,使得靠近两端的比处于中间的先合并
而最终两端必然相等,所以只要依次选取两端,把小的向内合并即可
注意有些编译器在deque为空时仍有dq.front()==dq.back(),这种情况特判处理
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,y,ans;
deque<ll>dq;
int main()
{
cin>>n;
while(n--){
cin>>x;
dq.push_back(x);
}
while(dq.size()>){
x=dq.front();
y=dq.back();
if(x<y){
dq.pop_front();
x+=dq.front();
dq.pop_front();
dq.push_front(x);
++ans;
}
else if(x>y){
dq.pop_back();
y+=dq.back();
dq.pop_back();
dq.push_back(y);
++ans;
}
else{
dq.pop_front();
dq.pop_back();
}
}
if(!dq.empty()&&dq.front()!=dq.back())++ans;
cout<<ans;
return ;
}
到底辣!
OJ题解记录计划的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 洛谷 P3285 / loj 2212 [SCOI2014] 方伯伯的 OJ 题解【平衡树】【线段树】
平衡树分裂钛好玩辣! 题目描述 方伯伯正在做他的 OJ.现在他在处理 OJ 上的用户排名问题. OJ 上注册了 \(n\) 个用户,编号为 \(1\sim n\),一开始他们按照编号排名.方伯伯会按照 ...
- [leetcode] 题解记录 11-20
博客园markdown太烂, 题解详情https://github.com/TangliziGit/leetcode/blob/master/solution/11-20.md Leetcode So ...
- [leetcode] 题解记录 1-10
博客园markdown太烂, 题解详见https://github.com/TangliziGit/leetcode/blob/master/solution/1-10.md Leetcode Sol ...
- 2019 IEEEXtreme 13.0 题解记录
比赛时间 2019.10.19 8:00 - 2019.10.20 8:00 比赛网站 https://csacademy.com/ieeextreme13 // 连续24小时做题真的是极限体验 // ...
- 冬令营前的一些计划&记录
冬令营前的一些计划&记录 计划 yyb发现自己很多以前学过的东西完完全全不记得了,所以在接下来的时间里可能会留下多篇复习向的博客,当然也可能因为觉得没有必要复习而到处乱做题. 现在先大概归类一 ...
- OJ大集合、
转载自:传送门 什么是OJ Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交程序源代码,系统对源代码进行编译和执行,并通过预先设计的测试数据来检验程序源代码的正确性. 一 ...
- 软工案例分析之OJ
项目 内容 这个作业属于哪个课程 2021春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 案例分析作业要求 我在这个课程的目标是 和我的团队开发一个真正的软件,一起提升开发与合作的能力 这 ...
- Swustoj题目征集计划
SWUST OJ题目征集计划 鉴于SWUST OJ长时间没有新题添加,题目数量和类型有限,同时也为加强同学们之间的算法交流,享受互相出题AC的乐趣,提高算法水平,现在启动题目征集计划啦~ 当你遇到 ...
随机推荐
- 接口测试总结分享(http与rpc)
接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑依赖关系等. 一.了 ...
- Eclipse 上传 删除 下载 分析 hdfs 上的文件
本篇讲解如何通过Eclipse 编写代码去操作分析hdfs 上的文件. 1.在eclipse 下新建Map/Reduce Project项目.如图: 项目建好后,会默认加载一系列相应的jar包. 下 ...
- (Python基础)字典的使用
以下代码是字典的查,增,改,删的基本使用方法. #-*-coding:utf-8-*- _author_: Keep #字典是无序的 info = { ':'张飞', ':'刘备', ':'关羽' ...
- 域名动态解析到动态IP
一般宽带用户的IP都是动态IP,重连之后IP可能会发生变化. 如果想在其他地方连接家里的设备,或者在家中搭建服务器,就会受到影响. 现在提供一种动态解析域名的方式,只要检测到IP的变化,那么就调用阿里 ...
- request.getRealPath为什么会被代替
以及前两天在网上看到的“不是工程的物理路径封装在Session里 是工程的路径被封装在了ServletContext中的问题” 很抱歉没有找到答案. 只能怪鄙人才识短浅. 在通过这次学习的过程中使我懂 ...
- Mac 端配置 Lua 环境
一.设计目的 Lua 是一种轻量级的脚本语言,其目的是为了嵌入到程序中,从而为程序提供灵活的扩展和定制功能. 二.特性 轻量级:编译后仅仅 100 余K,可以很方便的嵌入到程序中. 可扩展:Lua 提 ...
- mongoDB(2)--mongoDB的常用命令
默认设置后台启动: vi mongodb.cfg 创建配置文件,配置启动信息 dbpath=/root/mongodb/data logpath=/root/mongodb/log/mongodb.l ...
- 【转】【JAVA资料免费下载】158个JAVA免豆精品资料汇总——下载目录
附件完整版下载地址: http://down.51cto.com/data/431561 附件部分预览~ java中国移动收费系统[源代码] http://down.51cto.com/data/70 ...
- eclipse启动tomcat访问http://localhost:8080 报404错误
eclipse正常启动tomcat,但是 访问http://localhost:8080 却报404错误 修改下配置 就好操作如下图 打开eclipse的server视图,双击配置好的那个tomcat ...
- zookeeper、solrcloud、rediscluster集群解决方案
集群解决方案 课程目标 目标1:说出什么是集群以及与分布式的区别 目标2:能够搭建Zookeeper集群 目标3:能够搭建SolrCloud集群 目标4:能够搭建RedisCluster集群 ...