1071
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e3+5;
typedef long long ll;
#define MP(x, y) make_pair(x, y);
char a[1048580];
map<string, int> mp;
map<string, int> ::iterator it;
int main() {
gets(a);
int len = strlen(a);
string tt;
for(int i = 0; i <= len; ++i) {
if( (a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z') || (a[i] >= '0' && a[i] <= '9') ) {
if(a[i] >= 'A' && a[i] <= 'Z') a[i] = a[i]-'A'+'a';
tt += a[i];
}
else {
if(tt.size() > 0) mp[tt] ++;
tt.clear();
}
}
string a1; int a2 = -1;
for(it = mp.begin(); it != mp.end(); ++it) {
if(a2 < it->second) {
a1 = it->first;
a2 = it->second;
}
}
printf("%s %d
", a1.c_str(), a2);
return 0;
}
1072 一道非常麻烦的最短路问题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e3+50;
typedef long long ll;
#define MP(x, y) make_pair(x, y);
int n,m,k,d;
map<string, int> mp;
double ans1, ans2; string ans3;
char nam[N][10];
int tol;
struct Node{
int to, nx, di;
}E[N*N*2];
int head[N], tot;
void add(int fr, int to, int di) {
E[tot].to = to; E[tot].di = di; E[tot].nx = head[fr]; head[fr] = tot ++;
}
struct Hode{
int po, di;
Hode(int a=0, int b=0):po(a), di(b) {}
bool operator< (const Hode & T) const {
return di > T.di;
}
};
int dis[N];
int vis[N];
string change(int x, int ty) {
vector<int> vc;
while(x) {
vc.push_back(x % 10);
x /= 10;
}
string tmp;
if(ty) tmp += 'G';
for(int i = vc.size()-1; i >= 0; --i) {
tmp += vc[i]+'0';
}
return tmp;
}
void dij(int x) {
memset(vis, 0, sizeof(vis));
memset(dis, INF, sizeof(dis));
dis[x] = 0;
priority_queue<Hode> Q;
Q.push(Hode(x, dis[x]));
while(!Q.empty()) {
int po = Q.top().po; Q.pop();
if(vis[po]) continue;
vis[po] = 1;
for(int i = head[po]; ~i; i = E[i].nx) {
int to = E[i].to;
if(dis[to] > dis[po] + E[i].di) {
dis[to] = dis[po] + E[i].di;
Q.push(Hode(to, dis[to]));
}
}
}
int fl = 1; int tmp = INF; int all = 0;
for(int i = 1; i <= n; ++i) {
string tt = change(i, 0);
int id = mp[tt];
if( dis[id] > d) {
fl = 0; break;
}
if(dis[id] < tmp) {
tmp = dis[id];
}
all += dis[id];
// printf("%d ", dis[id]);
}
// printf("%d %.2f %s
", fl, all*1.0/n, nam[x]);
if(fl && tmp*1.0 > ans1) {
ans3 = nam[x];
ans1 = tmp*1.0;
ans2 = all*1.0 / n;
}else if(fl && tmp*1.0 == ans1 && all*1.0/n < ans2) {
ans2 = all*1.0 / n;
ans3 = nam[x];
}
}
int main() {
while(~scanf("%d %d %d %d",&n, &m, &k, &d)) {
ans1 = -1.0;
mp.clear(); tot = 0;
memset(head, -1, sizeof(head));
tol = 0;
for(int i = 0; i < k; ++i) {
char s1[10]; char s2[10]; int a;
scanf("%s %s %d", s1, s2, &a);
if(mp.find(s1) == mp.end()) {
++tol;
for(int j = 0; j <= strlen(s1); ++j) nam[tol][j] = s1[j];
mp[s1] = tol;
}
if(mp.find(s2) == mp.end()) {
++tol;
for(int j = 0; j <= strlen(s2); ++j) nam[tol][j] = s2[j];
mp[s2] = tol;
}
int t1 = mp[s1]; int t2 = mp[s2];
// if(s1[0] == 'G' && s2[0] == 'G') continue;
add(t1, t2, a); add(t2, t1, a);
}
for(int i = 1; i <= m; ++i) {
string tt = change(i, 1);
// printf("%s %d
", tt.c_str(), mp[tt]);
int id = mp[tt];
dij(id);
}
if(ans1 == -1) printf("No Solution
");
else {
printf("%s
%.1f %.1f
", ans3.c_str(), round(ans1*10) /10, round(ans2*10)/10 );
}
}
return 0;
}
1073
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e3+50;
typedef long long ll;
#define MP(x, y) make_pair(x, y);
char s[10000];
char t[10000];
int main() {
while(~scanf("%s", s)) {
int len = strlen(s);
int cnt = 0;
if(s[0] == '-') printf("-");
int edpoi = -1;
int tag = 0;
for(int i = 1; i < len; ++i) {
if(s[i] == '.') edpoi = cnt-1;
else if(s[i] == 'E') {
tag = i; break;
}else t[cnt++] = s[i];
}
if(edpoi == -1) edpoi = cnt-1;
int tmp = 0;
for(int i = tag+2; i < len; ++i) {
tmp = tmp*10 + s[i]-'0';
}
if(s[tag+1] == '-') tmp *= -1;
// printf("%d %d
", edpoi, tmp);
int nwpoi = edpoi + tmp;
if(nwpoi < 0) {
printf("0.");
for(int i = 0; i < abs(nwpoi+1); ++i) printf("0");
}
for(int i = 0; i < cnt; ++i) {
printf("%c", t[i]);
if(i == nwpoi && i != cnt-1) printf(".");
}
if(nwpoi > cnt-1) {
for(int i = 0; i < nwpoi-cnt+1; ++i) printf("0");
}
printf("
");
}
return 0;
}
1074
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e6+5;
typedef long long ll;
#define MP(x, y) make_pair(x, y);
int key[N]; int nx[N];
vector<int> tmp;
vector<int> ans;
int head, n, k;
void dfs(int x) {
if(x == -1) return;
tmp.push_back(x);
if(tmp.size() == k) {
for(int i = k-1; i >= 0; --i) {
ans.push_back(tmp[i]);
}
tmp.clear();
}
dfs(nx[x]);
}
int main() {
while(~scanf("%d %d %d", &head, &n, &k)) {
for(int i = 0; i < n; ++i) {
int a, b, c; scanf("%d %d %d", &a, &b, &c);
key[a] = b; nx[a] = c;
}
dfs(head);
for(int i = 0; i < tmp.size(); ++i) ans.push_back(tmp[i]);
for(int i = 0; i < ans.size(); ++i) {
printf("%05d %d ", ans[i], key[ans[i]]);
if(i == ans.size()-1) printf("-1
");
else printf("%05d
", ans[i+1]);
}
}
return 0;
}
1075
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e4+5;
typedef long long ll;
#define MP(x, y) make_pair(x, y);
int n,m,k;
int mark[6];
map<int, int> mp;
struct Node{
int num;
int grade[6];
}E[N];
int tot;
int cmp(Node a, Node b) {
int a1 = 0; int b1 = 0;
int a2 = 0; int b2 = 0;
for(int j = 1; j <= k; ++j) {
a1 += a.grade[j], b1 += b.grade[j];
if(a.grade[j] == -1) a1 ++; else if(a.grade[j] == -2) a1 += 2;
if(b.grade[j] == -1) b1 ++; else if(b.grade[j] == -2) b1 += 2;
if(a.grade[j] == mark[j]) a2 ++;
if(b.grade[j] == mark[j]) b2 ++;
}
if(a1 != b1) return a1 > b1;
else if(a2 != b2) return a2 > b2;
else return a.num < b.num;
}
int main() {
while(~scanf("%d %d %d", &n, &k, &m)) {
tot = 0;
mp.clear();
for(int i = 1; i <= k; ++i) scanf("%d", &mark[i]);
for(int i = 0; i < m; ++i) {
int a, b, c; scanf("%d %d %d", &a, &b, &c);
if(mp.find(a) == mp.end()) {
mp[a] = ++tot;
for(int j = 1; j <= k; ++j) E[tot].grade[j] = -2;
}
int id = mp[a];
E[id].num = a;
E[id].grade[b] = max(E[id].grade[b], c);
}
sort(E+1, E+tot+1, cmp);
int pre = -1, prerank;
int cnt = 0;
for(int i = 1; i <= tot; ++i) {
int no = 0; int all = 0; int nwrank;
for(int j = 1; j <= k; ++j) {
if(E[i].grade[j] <= -1) no ++;
all += E[i].grade[j];
if(E[i].grade[j] == -1) all ++;
else if(E[i].grade[j] == -2) all += 2;
}
if(no == k) { cnt ++; continue; }
if(pre == all) nwrank = prerank;
else nwrank = i-cnt;
printf("%d %05d %d", nwrank, E[i].num, all);
for(int j = 1; j <= k; ++j) {
if(E[i].grade[j] == -2) printf(" -");
else if(E[i].grade[j] == -1) printf(" 0");
else printf(" %d", E[i].grade[j]);
}
printf("
");
pre = all; prerank = nwrank;
}
}
return 0;
}
1076
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e3+5;
typedef long long ll;
#define MP(x, y) make_pair(x, y)
int n,l;
struct Node{
int to, nx;
}E[N*N*2];
int head[N], tot;
void add(int fr, int to) {
E[tot].to = to; E[tot].nx = head[fr]; head[fr] = tot++;
}
int vis[N];
void bfs(int x) {
for(int i = 1; i <= n; ++i) vis[i] = 0;
vis[x] = 1;
queue<pair<int, int> > Q;
Q.push(MP(x, 0));
int ans = 0;
while(!Q.empty()) {
int po = Q.front().first; int dep = Q.front().second; Q.pop();
if(dep > l) break;
ans ++;
for(int i = head[po]; ~i; i = E[i].nx) {
int to = E[i].to;
if(!vis[to]) {
vis[to] = 1;
Q.push(MP(to, dep+1));
}
}
}
printf("%d
",ans-1);
}
int main() {
while(~scanf("%d %d", &n, &l)) {
memset(head, -1, sizeof(head));
tot = 0;
for(int i = 1; i <= n; ++i) {
int a; scanf("%d", &a);
for(int j = 0; j < a; ++j) {
int b; scanf("%d", &b);
add(b, i);
}
}
int k; scanf("%d", &k);
for(int i = 1; i <= k; ++i) {
int a; scanf("%d", &a);
bfs(a);
}
}
return 0;
}
1077
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e3+5;
typedef long long ll;
#define MP(x, y) make_pair(x, y)
char s[105][300];
int main() {
int n;
while(~scanf("%d", &n)) {
getchar();
for(int i = 0; i < n; ++i) {
gets(s[i]);
// printf("tt %s
", s[i]);
for(int j = 0; j < strlen(s[i])/2; ++j) {
swap(s[i][j], s[i][strlen(s[i])-j-1]);
}
// printf("%s
", s[i]);
}
vector<char> ans;
int fl = 0;
while(1) {
int nw = 1;
for(int i = 1; i < n; ++i) {
if(s[i][fl] != s[i-1][fl] || s[i][fl] == 0) {
nw = 0; break;
}
}
if(!nw) break;
else ans.push_back(s[0][fl]);
fl ++;
}
if(ans.size() == 0) printf("nai
");
else {
for(int i = ans.size()-1; i >= 0; --i) printf("%c", ans[i]); printf("
");
}
}
return 0;
}
1078 这道题提醒了我得把 数据结构学好,等题目刷完我要开始看数据结构啦
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e4+5;
typedef long long ll;
#define MP(x, y) make_pair(x, y)
int has[N];
int prime(int x) {
if(x == 1) return 0;
if(x == 2) return 1;
for(int i = 2; i <= sqrt(x); ++i) {
if(x % i == 0) return 0;
}
return 1;
}
int main() {
int msize, n;
while(~scanf("%d %d", &msize, &n)) {
memset(has, 0, sizeof(has));
while(1) {
if(prime(msize)) break;
msize ++;
}
for(int i = 0; i < n; ++i) {
int a; scanf("%d", &a);
if(i) printf(" ");
if(!has[a%msize]) {
has[a%msize] = a;
printf("%d", a%msize);
}else{
int fl = 0;
for(int j = 1; j < msize; ++j) {
int tmp = (a + j*j) % msize;
// if(tmp >= msize) break;
if(!has[tmp]) {
has[tmp] = a;
fl = 1;
printf("%d", tmp);
break;
}
}
if(!fl) printf("-");
}
}
printf("
");
}
return 0;
}
1079
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+5;
typedef long long ll;
#define MP(x, y) make_pair(x, y)
int n; double p, r;
struct Node{
int to, nx;
}E[N*1000];
int head[N]; int tot;
void add(int fr, int to) {
E[tot].to = to; E[tot].nx = head[fr]; head[fr] = tot++;
}
int product[N]; int has[N];
double ans;
void dfs(int x, double price) {
if(has[x]) {
ans += product[x] * price;
return;
}
for(int i = head[x]; ~i; i = E[i].nx) {
int to = E[i].to;
dfs(to, price*(1+ r/100));
}
}
int main() {
while(~scanf("%d %lf %lf", &n, &p, &r)) {
memset(head, -1, sizeof(head));
memset(has, 0, sizeof(has));
tot = 0; ans = 0;
for(int i = 0; i < n; ++i) {
int a; scanf("%d", &a);
for(int j = 0; j < a; ++j) {
int b; scanf("%d", &b);
add(i, b);
}
if(!a) {
scanf("%d", &a);
product[i] = a; has[i] = 1;
}
}
dfs(0, p);
printf("%.1f
", ans);
}
return 0;
}
1080
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 4e4+5;
const int INF = 0x3f3f3f3f;
const double eps = 1e-7;
const int MOD = 1e9+7;
int quota[105];
int Ge[N], Gi[N], Ch[N][6];
int Id[N];
int cmp(int a, int b) {
if(Ge[a]+Gi[a] != Ge[b]+Gi[b]) return Ge[a]+Gi[a] > Ge[b]+Gi[b];
else return Ge[a] > Ge[b];
}
vector<int> ans[105];
int has[N];
int n,m,k;
void solve(int fr, int to) {
int temp[105];
for(int i = 0; i < k; ++i) {
int cnt = 0;
for(int j = 0; j < m; ++j) temp[j] = ans[j].size();
for(int j = fr; j <= to; ++j) {
int tt = Ch[Id[j]][i];
if(has[Id[j]]) continue;
cnt ++;
if(temp[tt] < quota[tt]) {
ans[tt].push_back(Id[j]);
has[Id[j]] = 1;
}
}
if(!cnt) break;
}
}
int main() {
while(~scanf("%d %d %d", &n, &m, &k)) {
memset(has, 0, sizeof(has));
for(int i = 0; i < m; ++i) ans[i].clear();
for(int i = 0; i < m; ++i) scanf("%d", "a[i]);
for(int i = 0; i < n; ++i) {
scanf("%d %d", &Ge[i], &Gi[i]);
for(int j = 0; j < k; ++j) {
scanf("%d", &Ch[i][j]);
}
}
for(int i = 0; i <= n; ++i) Id[i] = i;
sort(Id, Id+n, cmp);
Ge[n] = INF;
int fr = 0; int to = -1;
for(int i = 0; i < n; ++i) {
to ++;
if(Ge[Id[i]]+Gi[Id[i]] != Ge[Id[i+1]]+Gi[Id[i+1]] || Ge[Id[i]] != Ge[Id[i+1]]) solve(fr, to), fr = to+1;
}
for(int i = 0; i < m; ++i) {
sort(ans[i].begin(), ans[i].end());
for(int j = 0; j < ans[i].size(); ++j) {
if(j) printf(" "); printf("%d",ans[i][j]);
}
printf("
");
}
}
return 0;
}