The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)

http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=391

A     Thanks, TuSimple!


Time Limit: 1 Second      Memory Limit: 65536 KB

题意:有t组数据,n个男生,m个女生,每个人身高不同,每个人都期望和比自己高或者比自己低的人跳舞,问最多有多少对舞伴

题解:把期待比自己高的男生身高和期待比自己低的男生身高分别存在两个数组a[n],b[n]里,同理对女生也进行这样的操作c[n],d[n],分别对这四个数组进行排序,对于期望比自己的高的男生来说应该找到期望比自己低的女生,这时就可以从a[1]开始找对应的满足条件的d[i](也就是比自己高的女生),同理可以从c[1]开始找对应的满足条件的b[i],(由于排序之后的单调性,所以二者的期望单调性也完全吻合所以正确性也很显然)统计个数即可

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<stack>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 ll a[100005],b[100005],q[100005],w[100005],e[100005],r[100005];
10 int main(){
11     int t;
12     scanf("%d",&t);
13     while(t--){
14         int n,m;
15         scanf("%d%d",&n,&m);
16         for(int i=1;i<=n;i++){
17             scanf("%lld",&a[i]);
18         }
19         for(int i=1;i<=m;i++){
20             scanf("%lld",&b[i]);
21         }
22         int tot1,tot2,tot3,tot4;
23         tot1=tot2=tot3=tot4=0;
24         for(int i=1;i<=n;i++){
25             int c;
26             scanf("%d",&c);
27             if(c){
28                 q[++tot1]=a[i];
29             }
30             else{
31                 w[++tot2]=a[i];
32             }
33         }
34         for(int i=1;i<=m;i++){
35             int c;
36             scanf("%d",&c);
37             if(c){
38                 e[++tot3]=b[i];
39             }
40             else{
41                 r[++tot4]=b[i];
42             }
43         }
44         sort(q+1,q+1+tot1);
45         sort(w+1,w+1+tot2);
46         sort(e+1,e+1+tot3);
47         sort(r+1,r+1+tot4);
48         int pos1=1;
49         int ans=0;
50         for(int i=1;i<=tot1;i++){
51             while(pos1<=tot4&&r[pos1]<q[i])pos1++;
52             if(pos1<=tot4){
53                 ans++;
54                 pos1++;
55             }
56             else{
57                 break;
58             }
59         }
60         pos1=1;
61         for(int i=1;i<=tot3;i++){
62             while(pos1<=tot2&&w[pos1]<e[i])pos1++;
63             if(pos1<=tot2){
64                 ans++;
65                 pos1++;
66             }
67             else{
68                 break;
69             }
70         }
71         printf("%d
",ans);
72     }
73     return 0;
74 }
View Code
B       Even Number Theory

Time Limit: 1 Second      Memory Limit: 65536 KB

题意:给了一堆定义,就是求  e/2+e/4+e/8+e/16.....+0 的值

题解:大数除法+大数加法(模板要用对..板子不对超时一万年..当然也可以使用java,但是我不会)

  1 #include<bits/stdc++.h>
  2 #define ll long long
  3 using namespace std;
  4 #define MAXN 9999
  5 #define MAXSIZE 1010
  6 #define DLEN 4
  7 class BigNum{
  8 private:
  9     int a[1010]; //可以控制大数的位数
 10     int len;
 11 public:
 12     BigNum(){len=1;memset(a,0,sizeof(a));} //构造函数
 13     BigNum(const int); //将一个 int 类型的变量转化成大数
 14     BigNum(const char*); //将一个字符串类型的变量转化为大数
 15     BigNum(const BigNum &); //拷贝构造函数
 16     BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算
 17     friend istream& operator>>(istream&,BigNum&); //重载输入运算符
 18     friend ostream& operator<<(ostream&,BigNum&); //重载输出运算符
 19     BigNum operator+(const BigNum &)const; //重载加法运算符,两个大数之间的相加运算
 20     BigNum operator-(const BigNum &)const; //重载减法运算符,两个大数之间的相减运算
 21     BigNum operator*(const BigNum &)const; //重载乘法运算符,两个大数之间的相乘运算
 22     BigNum operator/(const int &)const; //重载除法运算符,大数对一个整数进行相除运算
 23     BigNum operator^(const int &)const; //大数的 n 次方运算
 24     int operator%(const int &)const; //大数对一个类型的变量进行取模运算int
 25     bool operator>(const BigNum &T)const; //大数和另一个大数的大小比较
 26     bool operator>(const int &t)const; //大数和一个 int 类型的变量的大小比较
 27     void print(); //输出大数
 28 };
 29 BigNum::BigNum(const int b){
 30     int c, d = b; len=0;
 31     memset(a,0,sizeof(a));
 32     while(d>MAXN){
 33         c=d-(d/(MAXN+1))*(MAXN+1);
 34         d=d/(MAXN+1);
 35         a[len++]=c;
 36     }
 37     a[len++]=d;
 38 }
 39 BigNum::BigNum(const BigNum &T):len(T.len){
 40     int i;
 41     memset(a,0,sizeof(a));
 42     for(i=0;i<len;i++)
 43         a[i]=T.a[i];
 44 }
 45 
 46 BigNum & BigNum::operator=(const BigNum &n){
 47     int i;
 48     len=n.len;
 49     memset(a,0,sizeof(a));
 50     for(i=0;i<len;i++)
 51         a[i]=n.a[i];
 52     return *this;
 53 }
 54 istream& operator>>(istream &in,BigNum &b){
 55     char ch[MAXSIZE*4];
 56     int i=-1;
 57     in>>ch;
 58     int L=strlen(ch);
 59     int count=0,sum=0;
 60     for(i=L-1;i>=0;){
 61         sum=0;
 62         int t=1;
 63         for(int j=0;j<4&&i>=0;j++,i--,t*=10){
 64             sum+=(ch[i]-'0')*t;
 65         }
 66         b.a[count]=sum;
 67         count++;
 68     }
 69     b.len=count++;
 70     return in;
 71 }
 72 ostream& operator<<(ostream& out,BigNum& b){
 73     int i;
 74     cout<<b.a[b.len-1];
 75     for(i=b.len-2;i>=0;i--){
 76         printf("%04d",b.a[i]);
 77     }
 78     return out;
 79 }
 80 BigNum BigNum::operator/(const int &b)const{
 81     BigNum ret;
 82     int i,down=0;
 83     for(i=len-1;i>=0;i--){
 84         ret.a[i]=(a[i]+down*(MAXN+1))/b;
 85         down=a[i]+down*(MAXN+1)-ret.a[i]*b;
 86     }
 87     ret.len=len;
 88     while(ret.a[ret.len-1]==0 && ret.len>1)
 89         ret.len--;
 90     return ret;
 91 }
 92 BigNum BigNum::operator+(const BigNum &T)const{
 93     BigNum t(*this);
 94     int i,big;
 95     big=T.len>len?T.len:len;
 96     for(i=0;i<big;i++){
 97         t.a[i]+=T.a[i];
 98         if(t.a[i]>MAXN){
 99             t.a[i+1]++;
100             t.a[i]-=MAXN+1;
101         }
102     }
103     if(t.a[big]!=0)
104         t.len=big+1;
105     else t.len=big;
106     return t;
107 }
108 
109 bool BigNum::operator>(const BigNum &T)const{
110     int ln;
111     if(len>T.len)return true;
112     else if(len==T.len){
113         ln=len-1;
114         while(a[ln]==T.a[ln]&&ln>=0)
115         ln--;
116         if(ln>=0 && a[ln]>T.a[ln])
117         return true;
118         else return false;
119     }
120     else return false;
121 }
122 bool BigNum::operator>(const int &t)const{
123     BigNum b(t);
124     return *this>b;
125 }
126 int main(){
127     int t;
128     cin>>t;
129     while(t--){
130         BigNum a;
131         cin>>a;
132         BigNum ans;
133         ans=0;
134         while(a>1){
135             a=a/2;
136             ans=ans+a;
137         }
138         cout<<ans<<endl;
139     }
140     return 0;
141 }
View Code
C   Robot Cleaner I

Time Limit: 1 Second      Memory Limit: 65536 K


 题意:给出n*m的格子地图,0表示空地,1表示墙壁,2表示有垃圾的空地,然后给出一段包含L,R,U,D,P(清理垃圾),I的操作序列(长度恒为243),然后定义一个t(i,j)=mp[i]*3^4+mp[i-1]*3^3+mp[i+1][j]*3^2+mp[i][j-1]*3+mp[i][j+1],其中i,j表示所在位置的坐标,每次执行那段操作序列的第t(i,j)+1个字符表示的操作(不能移动到墙的位置,每个垃圾只能被清理一次),一共执行k次操作(k<=10^18),问最终能清理多少垃圾

题解:如果这道题没有清理垃圾之后mp[i][j]从2变为0的步骤,那么直接记录每个位置是否被访问到,如果被访问就退出即可,因为走过一次的路再走还是那一条路,但是这里的mp[i][j]会随着清扫工作而改变,所以需要记录每个位置被走过的次数,如果>n*m就退出,由于n*m<=2e4,所以大概4e8的复杂度..可以通过

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 #include<stack>
 6 using namespace std;
 7 typedef long long ll;
 8 int vis[2005][4];
 9 char ch[305];
10 char q[2005][2005];
11 int main(){
12     int t;
13     scanf("%d",&t);
14     while(t--){
15         int n,m;
16         scanf("%d%d",&n,&m);
17         ll a,b,k;
18         scanf("%lld%lld%lld",&a,&b,&k);
19         scanf("%s",ch+1);
20         for(int i=1;i<=n;i++){
21             scanf("%s",q[i]+1);
22         }
23         for(int i=1;i<=n;i++){
24             for(int j=1;j<=m;j++){
25                 for(int s=0;s<3;s++){
26                     vis[(i-1)*m+j][s]=0;
27                 }
28             }
29         }
30         vis[(a-1)*m+b][q[a][b]-'0']=1;
31         int ans=0;
32         while(k){
33             k--;
34             int sum=3*3*3*3*(q[a][b]-'0');
35             sum++;
36             if(a>1){
37                 sum+=(q[a-1][b]-'0')*3*3*3;
38             }
39             if(a<n){
40                 sum+=(q[a+1][b]-'0')*3*3;
41             }
42             if(b>1){
43                 sum+=(q[a][b-1]-'0')*3;
44             }
45             if(b<m){
46                 sum+=(q[a][b+1]-'0');
47             }
48             if(sum>243)break;
49             if(ch[sum]=='L'){
50                 b--;
51                 if((q[a][b]-'0')==1)b++;
52             }
53             if(ch[sum]=='R'){
54                 b++;
55                 if((q[a][b]-'0')==1)b--;
56             }
57             if(ch[sum]=='U'){
58                 a--;
59                 if((q[a][b]-'0')==1)a++;
60             }
61             if(ch[sum]=='D'){
62                 a++;
63                 if((q[a][b]-'0')==1)a--;
64             }
65             if(ch[sum]=='P'){
66                 if((q[a][b]-'0')==2){
67                     q[a][b]='0';
68                     ans++;
69                 }
70             }
71             if(vis[(a-1)*m+b][(q[a][b]-'0')]>n*m)break;
72             vis[(a-1)*m+b][(q[a][b]-'0')]++;
73         }
74         printf("%d
",ans);
75     }
76     return 0;
77 }
View Code
E     Potion

Time Limit: 1 Second      Memory Limit: 65536 KB

题意:给两个数组a[n],b[n],如果b[n]可以高位向低位移动物品,问能否通过若干次操作使对于 1<=i<=n 都可以a[i]<=b[i]

题解:由于可以从高位向低位移动,所以只需要倒着做,如果b[i]>a[i],就把多出的部分移动到b[i-1],如果b[i]<a[i]就证明不能通过若干次操作完成题目要求

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<stack>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 ll a[105],b[105];
10 int main(){
11     int t;
12     scanf("%d",&t);
13     while(t--){
14         int n;
15         scanf("%d",&n);
16         int f=1;
17         for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
18         for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
19         for(int i=n;i>=1;i--){
20             if(b[i]>=a[i]){
21                 b[i-1]+=b[i]-a[i];
22             }
23             else{
24                 f=0;
25                 break;
26             }
27         }
28         if(f)printf("Yes
");
29         else printf("No
");
30     }
31     return 0;
32 }
View Code
G     Postman

Time Limit: 1 Second      Memory Limit: 65536 KB

题意:给一个序列,表示n个邮箱,邮局在0处,小明每次可以最多携带k封信,最终结束地点可以不在邮局,问小明走的最短的距离

题解:贪心地想把尽量远的封信放在一起送,最后由于可以不在邮局结束,所以结果要减去最远的那次距离

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<stack>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 ll a[100005];
10 int main(){
11     int t;
12     scanf("%d",&t);
13     while(t--){
14         int n,k;
15         scanf("%d%d",&n,&k);
16         for(int i=1;i<=n;i++){
17             scanf("%lld",&a[i]);
18         }
19         sort(a+1,a+1+n);
20         int pos=upper_bound(a+1,a+1+n,0)-a;
21         ll sum=0;
22         int i;
23         ll maxx=0;
24         for(i=n;i>=pos;i-=k){
25             sum+=2*a[i];
26         }
27         maxx=max(abs(a[n]),abs(a[1]));
28         pos=min(pos,n);
29         while(pos>=1&&a[pos]>=0)pos--;
30         for(i=1;i<=pos;i+=k){
31             sum+=2*abs(a[i]);
32         }
33         printf("%lld
",sum-maxx);
34     }
35     return 0;
36 }
View Code
J      Extended Twin Composite Number

Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge

题意:给出一个n,求 满足x+n=y并且x,y是非质数的一组解

题解:x=8*n  y=9*n,x y 就一定不是质数且满足等式

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<stack>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 ll a[105],b[105];
10 int main(){
11     int t;
12     scanf("%d",&t);
13     while(t--){
14         ll n;
15         scanf("%lld",&n);
16         printf("%lld %lld
",n*8,n*9);
17     }
18     return 0;
19 }
View Code

 

原文地址:https://www.cnblogs.com/MekakuCityActor/p/10709965.html