Codeforces Round #363
http://codeforces.com/contest/699
题意:n个球,每个球向左或右,速度都为1米每秒,问第一次碰撞的时间,否则输出-1
贪心最短时间一定在RL中,R右边一定有L,L左边一定有R
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 1e5+10; 22 const int M = 1e6+10; 23 const int MOD = 1e9+7; 24 #define LL long long 25 #define mi() (l+r)>>1 26 double const pi = acos(-1); 27 const double eps = 1e-8; 28 void fre() { 29 freopen("in.txt","r",stdin); 30 } 31 32 // inline int r() { 33 // int x=0,f=1;char ch=getchar(); 34 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 35 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 36 // } 37 38 int a[200010]; 39 int main(){ 40 int n; 41 string s; 42 scanf("%d",&n); 43 cin>>s; 44 for(int i=0;i<n;i++){ 45 scanf("%d",&a[i]); 46 } 47 int st=s.find('R'); 48 if(st==-1){ 49 printf("-1\n"); 50 exit(0); 51 } 52 int ans=inf; 53 bool flag=false; 54 for(int i=st+1;i<(int)s.size();i++){ 55 if(s[i]=='L'){ 56 ans=min(ans,(a[i]-a[st])/2); 57 flag=true; 58 } 59 else 60 st=i; 61 } 62 if(!flag) printf("-1\n"); 63 else printf("%d\n",ans); 64 return 0; 65 }
题意:给你一个n*m的图 问你能不能站在一个点上就把所有*覆盖完
思路:注意全是.的情况
开两个数组一个记录每行的*,另一个记录每列的*
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 1e5+10; 22 const int M = 1e6+10; 23 const int MOD = 1e9+7; 24 #define LL long long 25 #define mi() (l+r)>>1 26 double const pi = acos(-1); 27 const double eps = 1e-8; 28 void fre() { 29 freopen("in.txt","r",stdin); 30 } 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 char g[1005][1005]; 37 int x[1005],y[1005]; 38 int main(){ 39 // fre(); 40 int n,m; 41 int sum=0; 42 scanf("%d%d",&n,&m); 43 for(int i=1;i<=n;i++) { 44 scanf("%s",g[i]+1); 45 for(int j=1;j<=m;j++){ 46 if(g[i][j]=='*'){ 47 x[i]++,y[j]++,sum++; 48 } 49 } 50 } 51 int ansx,ansy; 52 bool flag=false; 53 for(int i=1;i<=n;i++){ 54 for(int j=1;j<=m;j++){ 55 if((x[i]+y[j]-(g[i][j]=='*'))==sum){ 56 ansx=i,ansy=j; 57 flag=true; 58 break; 59 } 60 } 61 if(flag) break; 62 } 63 if(flag){ 64 puts("YES"); 65 printf("%d %d\n",ansx,ansy); 66 } 67 else printf("NO\n"); 68 return 0; 69 }
题意:0代表必须休息,1代表只能做运动,2代表只能写作业,3代表两种都可以,然后不能连续两天做同样的事情,除了休息,问你最少休息多少天
思路:贪心。。。。
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 1e5+10; 22 const int M = 1e6+10; 23 const int MOD = 1e9+7; 24 #define LL long long 25 #define mi() (l+r)>>1 26 double const pi = acos(-1); 27 const double eps = 1e-8; 28 void fre() { 29 freopen("in.txt","r",stdin); 30 } 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 int a[105]; 37 int main(){ 38 int n; 39 int ans=0; 40 scanf("%d",&n); 41 for(int i=1;i<=n;i++){ 42 scanf("%d",&a[i]); 43 if(a[i]==0) ans++; 44 } 45 for(int i=1;i<=n;i++){ 46 if(a[i]==0) continue; 47 else if((a[i]==2&&a[i-1]==1)||(a[i]==2&&a[i-1]==1)) continue; 48 else if(a[i]==1&&a[i-1]==1){ a[i]=0;ans++;continue;} 49 else if(a[i]==2&&a[i-1]==2){a[i]=0;ans++;continue;} 50 else if(a[i]==3&&a[i-1]==1){a[i]=2;continue;} 51 else if(a[i]==3&&a[i-1]==2){a[i]=1;continue;} 52 } 53 printf("%d\n",ans); 54 return 0; 55 }
题意:n个数,ai是i的祖先,问最小的步数把图变成一棵树
思路:
并查集判断
首先孤点可以形成根;对于环,我们把最后一条边连到根上;全是环,任选一个环上点当作根;最小步数一定是cnt-1
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 2e5+10; 22 const int M = 1e6+10; 23 const int MOD = 1e9+7; 24 #define LL long long 25 #define mi() (l+r)>>1 26 double const pi = acos(-1); 27 const double eps = 1e-8; 28 void fre() { 29 freopen("in.txt","r",stdin); 30 } 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 int fa[N]; 37 int a[N]; 38 int find(int x){ return fa[x]==x?x:fa[x]=find(fa[x]);} 39 int main(){ 40 int n; 41 int cnt=0,root=-1; 42 scanf("%d",&n); 43 for(int i=1;i<=n;i++) fa[i]=i; 44 for(int i=1;i<=n;i++){ 45 scanf("%d",&a[i]); 46 if(a[i]==i){root=i;cnt++;} 47 else{ 48 int x=find(i),y=find(a[i]); 49 if(x==y){ 50 cnt++; 51 a[i]=i; 52 } 53 else 54 fa[x]=y; 55 } 56 } 57 if(root==-1){ 58 for(int i=1;i<=n;i++){ 59 if(a[i]==i){ 60 root=i; 61 cnt++; 62 break; 63 } 64 } 65 } 66 printf("%d\n",cnt-1); 67 for(int i=1;i<=n;i++){ 68 if(a[i]==i) a[i]=root; 69 printf("%d%c",a[i],i==n?'\n':' '); 70 } 71 return 0; 72 }