A. Codeforces 92A Chips
签到题。。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 10007 int a[55]; int main() { int n,m,i; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) a[i] = i; i = 1; while(1) { if(m>=a[i]) m-=a[i]; else break; i++; if(i == n+1) i = 1; } cout<<m<<endl; } return 0; }
B.Codeforces 217A Ice Skating
dfs或者并查集。
dfs:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 107 struct node { int x,y; }a[N]; int vis[N]; int n; void joint(int k) { if(!vis[k]) { vis[k] = 1; for(int i=0;i<n;i++) { if(i != k && (a[k].x == a[i].x || a[k].y == a[i].y)) joint(i); } } } int main() { int i; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) scanf("%d%d",&a[i].x,&a[i].y); memset(vis,0,sizeof(vis)); int cnt = 0; for(i=0;i<n;i++) { if(!vis[i]) { cnt++; joint(i); } } cout<<cnt-1<<endl; } return 0; }
C.UVA 12592 Slogan Learning of Princess
hash,用map做就可以了,也可以用字符串数组
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> using namespace std; #define N 10007 map<string,string> mp; int main() { int n,i,q; string s1,s2; while(scanf("%d",&n)!=EOF) { getchar(); for(i=0;i<n;i++) { getline(cin,s1); cin.clear(); getline(cin,s2); cin.clear(); mp[s1] = s2; } scanf("%d",&q); getchar(); while(q--) { getline(cin,s1); cout<<mp[s1]<<endl; } } return 0; }
D.HDU 4027 Can you answer these queries?
线段树,单点更新,用一个标记表示区间内是否全为1,全为1则不用更新,以节省操作时间。注意:要用_int64类型,不要忘记每组数据后打一个空行
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define lll __int64 using namespace std; #define N 100007 struct node { lll sum; int mark; }tree[4*N]; void pushup(int rt) { tree[rt].sum = tree[2*rt].sum + tree[2*rt+1].sum; tree[rt].mark = tree[2*rt].mark && tree[2*rt+1].mark; } void build(int l,int r,int rt) { tree[rt].mark = 0; if(l == r) { scanf("%I64d",&tree[rt].sum); if(tree[rt].sum == 1) tree[rt].mark = 1; return; } int mid = (l+r)/2; build(l,mid,2*rt); build(mid+1,r,2*rt+1); pushup(rt); } void update(int l,int r,int aa,int bb,int rt) { if(aa<=l && bb>=r) { if(tree[rt].mark) return; if(!tree[rt].mark && l == r) { tree[rt].sum = (lll)sqrt(tree[rt].sum); if(tree[rt].sum <= 1) tree[rt].mark = 1; return; } } int mid = (l+r)/2; if(aa<=mid) update(l,mid,aa,bb,2*rt); if(bb>mid) update(mid+1,r,aa,bb,2*rt+1); pushup(rt); } lll query(int l,int r,int aa,int bb,int rt) { if(aa>r || bb<l) return 0; if(aa<=l && bb>=r) return tree[rt].sum; int mid = (l+r)/2; return query(l,mid,aa,bb,2*rt)+query(mid+1,r,aa,bb,2*rt+1); } int main() { int n,i,q,op,aa,bb,cs = 1; while(scanf("%d",&n)!=EOF) { build(1,n,1); scanf("%d",&q); printf("Case #%d: ",cs++); while(q--) { scanf("%d%d%d",&op,&aa,&bb); if(aa>bb) swap(aa,bb); if(op) printf("%I64d ",query(1,n,aa,bb,1)); else update(1,n,aa,bb,1); } printf(" "); } return 0; }
E.UVA 11488 Hyper Prefix Sets
字典树,结构node维护两个值: count 和 deep ,结果即为节点的count * deep 的最大值。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 struct node { int count,deep; node *next[2]; }*root; char ss[N]; int maxi; node *create() { node *p; p = (node *)malloc(sizeof(node)); p->count = 0; p->deep = 0; for(int i=0;i<2;i++) p->next[i] = NULL; return p; } void release(node *p) { for(int i=0;i<2;i++) { if(p->next[i] != NULL) release(p->next[i]); } free(p); } void insert(char *ss) { node *p = root; int i = 0,k; while(ss[i]) { k = ss[i++] - '0'; if(p->next[k] == NULL) p->next[k] = create(); p->next[k]->deep = p->deep + 1; p = p->next[k]; p->count++; maxi = max(maxi,p->count*p->deep); } } int main() { int t,n,i; scanf("%d",&t); while(t--) { root = create(); scanf("%d",&n); maxi = -1000000; for(i=0;i<n;i++) { scanf("%s",ss); insert(ss); } cout<<maxi<<endl; release(root); } return 0; }
F.UVALive 6655 Two Points Revisited
构造法。