Codeforces Beta Round #18 (Div. 2 Only)

http://codeforces.com/contest/18

A

暴力

 #include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define maxn 500005
typedef long long ll;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ int a[]; void Check(char *s){
int aa=sqr(a[]-a[])+sqr(a[]-a[]);
int bb=sqr(a[]-a[])+sqr(a[]-a[]);
int cc=sqr(a[]-a[])+sqr(a[]-a[]);
if((aa&&bb&&cc)==) return;
// cout<<aa<<" "<<bb<<" "<<cc<<endl;
if(aa+bb==cc||aa+cc==bb||bb+cc==aa){
cout<<s<<endl;
exit();
}
} int main(){
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
#endif
// std::ios::sync_with_stdio(false);
for(int i=;i<;i++) cin>>a[i];
Check("RIGHT");
for(int i=;i<;i++){
// cout<<a[i]<<"g"<<endl;
a[i]--;
Check("ALMOST");
a[i]+=;
Check("ALMOST");
a[i]--;
// cout<<a[i]<<"h"<<endl; }
cout<<"NEITHER"<<endl; }

B

水题

 #include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define maxn 500005
typedef long long ll;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ int main(){
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
#endif
std::ios::sync_with_stdio(false);
int n,m,l,d;
cin>>n>>d>>m>>l;
ll x=,L=-m,R=-m+l;
for(int i=;i<n;i++){
L+=m;
R+=m;
if(x+d<L) break;
ll o=(R-x)/d;
x+=d*o;
}
cout<<x+d<<endl;
}

C

水题

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll; int n;
ll a[]; int main(){
cin>>n;
ll ans=;
int b;
for(int i=;i<=n;i++){
cin>>b;
a[i]=a[i-]+b;
}
for(int i=;i<=n-;i++){
if(a[n]-a[i]==a[i]) ans++;
}
cout<<ans<<endl;
}

D

贪心,因为有2^n>2^(n-1)+2^(n-2)+...+1的规律,所以可以优先挑选大的。挑选的时候是从后往前选比较好写

 #include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define maxn 500005
typedef long long ll;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ #define MAXN 9999
#define MAXSIZE 5007//位数
#define DLEN 4 class BigNum
{
private:
int a[]; //可以控制大数的位数
int len; //大数长度
public:
BigNum()
{
len = ; //构造函数
memset(a,,sizeof(a));
}
BigNum(const int); //将一个int类型的变量转化为大数
BigNum(const char*); //将一个字符串类型的变量转化为大数
BigNum(const BigNum &); //拷贝构造函数
BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算 friend istream& operator>>(istream&, BigNum&); //重载输入运算符
friend ostream& operator<<(ostream&, BigNum&); //重载输出运算符 BigNum operator+(const BigNum &) const; //重载加法运算符,两个大数之间的相加运算
BigNum operator-(const BigNum &) const; //重载减法运算符,两个大数之间的相减运算
BigNum operator*(const BigNum &) const; //重载乘法运算符,两个大数之间的相乘运算
BigNum operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算 BigNum operator^(const int &) const; //大数的n次方运算
int operator%(const int &) const; //大数对一个int类型的变量进行取模运算
bool operator>(const BigNum & T)const; //大数和另一个大数的大小比较
bool operator>(const int & t)const; //大数和一个int类型的变量的大小比较 void print(); //输出大数
};
BigNum::BigNum(const int b) //将一个int类型的变量转化为大数
{
int c,d = b;
len = ;
memset(a,,sizeof(a));
while(d > MAXN)
{
c = d - (d / (MAXN + )) * (MAXN + );
d = d / (MAXN + );
a[len++] = c;
}
a[len++] = d;
}
BigNum::BigNum(const char*s) //将一个字符串类型的变量转化为大数
{
int t,k,index,l,i;
memset(a,,sizeof(a));
l=strlen(s);
len=l/DLEN;
if(l%DLEN)
len++;
index=;
for(i=l-; i>=; i-=DLEN)
{
t=;
k=i-DLEN+;
if(k<)
k=;
for(int j=k; j<=i; j++)
t=t*+s[j]-'';
a[index++]=t;
}
}
BigNum::BigNum(const BigNum & T) : len(T.len) //拷贝构造函数
{
int i;
memset(a,,sizeof(a));
for(i = ; i < len ; i++)
a[i] = T.a[i];
}
BigNum & BigNum::operator=(const BigNum & n) //重载赋值运算符,大数之间进行赋值运算
{
int i;
len = n.len;
memset(a,,sizeof(a));
for(i = ; i < len ; i++)
a[i] = n.a[i];
return *this;
}
istream& operator>>(istream & in, BigNum & b) //重载输入运算符
{
char ch[MAXSIZE*];
int i = -;
in>>ch;
int l=strlen(ch);
int count=,sum=;
for(i=l-; i>=;)
{
sum = ;
int t=;
for(int j=; j<&&i>=; j++,i--,t*=)
{
sum+=(ch[i]-'')*t;
}
b.a[count]=sum;
count++;
}
b.len =count++;
return in; }
ostream& operator<<(ostream& out, BigNum& b) //重载输出运算符
{
int i;
cout << b.a[b.len - ];
for(i = b.len - ; i >= ; i--)
{
cout.width(DLEN);
cout.fill('');
cout << b.a[i];
}
return out;
} BigNum BigNum::operator+(const BigNum & T) const //两个大数之间的相加运算
{
BigNum t(*this);
int i,big; //位数
big = T.len > len ? T.len : len;
for(i = ; i < big ; i++)
{
t.a[i] +=T.a[i];
if(t.a[i] > MAXN)
{
t.a[i + ]++;
t.a[i] -=MAXN+;
}
}
if(t.a[big] != )
t.len = big + ;
else
t.len = big;
return t;
}
BigNum BigNum::operator-(const BigNum & T) const //两个大数之间的相减运算
{
int i,j,big;
bool flag;
BigNum t1,t2;
if(*this>T)
{
t1=*this;
t2=T;
flag=;
}
else
{
t1=T;
t2=*this;
flag=;
}
big=t1.len;
for(i = ; i < big ; i++)
{
if(t1.a[i] < t2.a[i])
{
j = i + ;
while(t1.a[j] == )
j++;
t1.a[j--]--;
while(j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[t1.len - ] == && t1.len > )
{
t1.len--;
big--;
}
if(flag)
t1.a[big-]=-t1.a[big-];
return t1;
} BigNum BigNum::operator*(const BigNum & T) const //两个大数之间的相乘运算
{
BigNum ret;
int i,j,up;
int temp,temp1;
for(i = ; i < len ; i++)
{
up = ;
for(j = ; j < T.len ; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN)
{
temp1 = temp - temp / (MAXN + ) * (MAXN + );
up = temp / (MAXN + );
ret.a[i + j] = temp1;
}
else
{
up = ;
ret.a[i + j] = temp;
}
}
if(up != )
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
BigNum BigNum::operator/(const int & b) const //大数对一个整数进行相除运算
{
BigNum ret;
int i,down = ;
for(i = len - ; i >= ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + )) / b;
down = a[i] + down * (MAXN + ) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
int BigNum::operator %(const int & b) const //大数对一个int类型的变量进行取模运算
{
int i,d=;
for (i = len-; i>=; i--)
{
d = ((d * (MAXN+))% b + a[i])% b;
}
return d;
}
BigNum BigNum::operator^(const int & n) const //大数的n次方运算
{
BigNum t,ret();
int i;
if(n<)
exit(-);
if(n==)
return ;
if(n==)
return *this;
int m=n;
while(m>)
{
t=*this;
for( i=; i<<<=m; i<<=)
{
t=t*t;
}
m-=i;
ret=ret*t;
if(m==)
ret=ret*(*this);
}
return ret;
}
bool BigNum::operator>(const BigNum & T) const //大数和另一个大数的大小比较
{
int ln;
if(len > T.len)
return true;
else if(len == T.len)
{
ln = len - ;
while(a[ln] == T.a[ln] && ln >= )
ln--;
if(ln >= && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigNum::operator >(const int & t) const //大数和一个int类型的变量的大小比较
{
BigNum b(t);
return *this>b;
} void BigNum::print() //输出大数
{
int i;
cout << a[len - ];
for(i = len - ; i >= ; i--)
{
cout.width(DLEN);
cout.fill('');
cout << a[i];
}
cout << endl;
}
BigNum num1[];
char str[][];
int num2[],num3[];
BigNum Jud(int n,int m)
{
BigNum sum,mul;
sum=BigNum();
mul=BigNum();
while(m)
{
if(m&)
sum=sum*mul;
mul=mul*mul;
m>>=;
}
return sum;
} int n;
struct sair{
string str;
int num;
int pos;
}a[]; int book[]; bool cmp(sair a,sair b){
return a.num==b.num?a.pos>b.pos:a.num>b.num;
} bool Check(int x,int y){
for(int i=x;i<=y;i++){
if(book[i]) return false;
}
for(int i=x;i<=y;i++){
book[i]=;
}
return true;
} int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif
//std::ios::sync_with_stdio(false);
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i].str>>a[i].num;
a[i].pos=i;
}
sort(a+,a+n+,cmp);
vector<int>ve;
for(int i=;i<=n;i++){
if(a[i].str=="sell"){
for(int j=i+;j<=n;j++){
if(a[j].str=="win"&&a[j].num==a[i].num){
if(Check(a[j].pos,a[i].pos)){
ve.push_back(a[i].num);
}
}
}
}
}
BigNum ans=;
for(int i=;i<ve.size();i++){
ans=ans+Jud(,ve[i]);
}
ans.print(); }

E

dp

先预处理出每个格子各个颜色的花费,然后开始DP

参考博客:https://blog.csdn.net/c20190102/article/details/81775186

 #include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define maxn 505
#define maxc 35
typedef long long ll;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ int n,m;
int dp[maxn][maxc][maxc];
char color[maxn][maxn];
int cost[maxn][maxc][maxc];
pair<int,int>pre[maxn][maxc][maxc]; void print(int floor,int ii,int jj){
if(floor>){
print(floor-,pre[floor][ii][jj].first,pre[floor][ii][jj].second);
}
for(int i=;i<=m;i++){
if(i&) printf("%c",'a'-+ii);
else printf("%c",'a'-+jj);
}
printf("\n");
} int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif
//std::ios::sync_with_stdio(false);
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
scanf("%s",color[i]+);
}
memset(dp,0x3f,sizeof(dp));
memset(cost,0x3f,sizeof(dp));
for(int i=;i<=n;i++){
for(int j=;j<=;j++){
for(int k=;k<=;k++){
if(j!=k){
cost[i][j][k]=;
for(int l=;l<=m;l+=){
cost[i][j][k]+=(color[i][l]!=('a'-+j));
}
for(int l=;l<=m;l+=){
cost[i][j][k]+=(color[i][l]!=('a'-+k));
}
}
}
}
}
for(int i=;i<=;i++){
for(int j=;j<=;j++){
dp[][i][j]=cost[][i][j];
}
}
for(int i=;i<=n;i++){
for(int j=;j<=;j++){
for(int k=;k<=;k++){
for(int p=;p<=;p++){
for(int q=;q<=;q++){
if(p!=j&&q!=k){
if(dp[i][j][k]>dp[i-][p][q]+cost[i][j][k]){
pre[i][j][k]=make_pair(p,q);
dp[i][j][k]=dp[i-][p][q]+cost[i][j][k];
}
}
}
}
}
}
}
int ans=0x3f3f3f3f;
int ansi,ansj;
for(int i=;i<=;i++){
for(int j=;j<=;j++){
if(ans>dp[n][i][j]){
ans=dp[n][ansi=i][ansj=j];
}
}
}
printf("%d\n",ans);
print(n,ansi,ansj);
}

Codeforces Beta Round #18 (Div. 2 Only)的更多相关文章

  1. Codeforces Beta Round #18 (Div. 2 Only) C. Stripe 前缀和

    C. Stripe Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/18/C ...

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  4. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  5. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  6. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  8. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

  9. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

随机推荐

  1. nginx 和 php超时设置

    nginx.conf ---  http节: keepalive_timeout 600; #客户端浏览器超时时间fastcgi_connect_timeout 600; #php-fpm连接超时时间 ...

  2. docker之手动构建新的镜像

    转自:https://www.cnblogs.com/jsonhc/p/7766561.html 查看本地现有镜像: [root@docker ~]# docker images REPOSITORY ...

  3. upupw

    https://sourceforge.net/projects/upupw/files/ANK/

  4. 正则表达式(Python)

    课题 使用正则表达式匹配字符串 使用正则表达式 "\d{3}-(\d{4})-\d{2}" 匹配字符串 "123-4567-89" 返回匹配结果:'" ...

  5. 如何使用JBDC修改数据

    1.JDBC取得数据库Connection连接对象conn, Connection conn=null;   //数据库连接对象 String strSql=null;    //sql语句对象 // ...

  6. 创建一个Maven Web应用程序

    1 在Eclipes创建maven,首先File new ,在other 中找到Maven,Maven Project,下一步选择WebApp,创建Maven工程名字,完成 2 在新建的Maven工程 ...

  7. [jQ]使用jQuery将多条数据插入模态框的方法

    ---------------------------------------------------------------------------------------------------- ...

  8. Matlab实现BP网络识别字母

    训练样本空间   每个样本使用5×5的二值矩阵表征一个字母.一共10个字母类型,分别是N,I,L,H,T,C,E,F,Z,V.每个字母9个样本.共90个. N1=[1,0,0,0,1; 1,0,0,0 ...

  9. 吴裕雄 python oracle检索数据(1)

    import cx_Oracle conn = cx_Oracle.connect("scott/admin@localhost:1521/ORCL")cursor = conn. ...

  10. Oracle数据库操作总是显示运行中无法成功,删除表时报错 resource busy and acquire with NOWAIT specified

    1.直接运行以下语句: select t2.username,t2.sid,t2.serial#,t2.logon_timefrom v$locked_object t1,v$session t2wh ...