acmsguru 101 - Domino

要求每两个相邻的多尼诺骨牌相对的数字相同,即做一个一笔画

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+;
const ll mod = 1e9 + ;
#define afdafafafdafaf y1;
int ar[maxn], n; vector<int> ans1, ans2;
int b[maxn], to[maxn], c[maxn], index[maxn];
int head[maxn], cnt;
void add(int u, int v, int index_int, int val){
b[cnt] = v;
c[cnt] = val;
index[cnt] = index_int;
to[cnt] = head[u];
head[u] = cnt++;
}
int vis[maxn], in[maxn], vis_edge[maxn], flag = -, ans_flag = , ins = ;
void dfs(int u,int pre){
vis[u] = ;
for(int i = head[u]; i != -; i = to[i]){
if(vis_edge[i])continue;
int v = b[i];
vis_edge[i] = vis_edge[i ^ ] = ;
dfs(v, u);
ans1.push_back(index[i]);
ans2.push_back(c[i]);
}
}
int inx[maxn], iny[maxn];
int main()
{
cnt = ;
scanf("%d", &n);
for(int i=;i<=;i++)head[i] = -;
for(int i=;i<n;i++){
int x,y;scanf("%d%d", &x, &y);
add(x, y, i+, );
add(y, x, i+, );
in[x]++;
in[y]++;
inx[i+] = x;
iny[i+] = y;
}
for(int i=;i<;i++){
if(in[i] % == ){
flag = i;
break;
}
}
if(flag == -){
for(int i=;i<=;i++){
if(in[i]){
flag = i;
break;
}
}
}
dfs(flag, );
if(ans1.size() != n){
printf("No solution\n");
return ;
}
int pre = -;
for(int i=;i<ans1.size();i++){
int a = inx[ans1[i]], b = iny[ans1[i]];
if(ans2[i] == )swap(a, b);
if(i > && a != pre){
printf("No solution\n");
return ;
}
pre = b;
}
for(int i=ans1.size() - ;i >= ;i--){
int a = ans1[i], b = ans2[i];
printf("%d %c\n", a, b == ? '+' : '-');
}
return ;
}

acmsguru 102 - Coprimes

暴力完事

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+;
const ll mod = 1e9 + ;
#define afdafafafdafaf y1;
int ar[maxn], n; int main()
{
scanf("%d", &n);
int ins = ;
for(int i=; i<=n; i++){
if(__gcd(i, n) == )ins++;
}
printf("%d\n", ins);
return ;
}

sguru 103. Traffic Lights

路可以走通的条件是开始走的时候两个路口的颜色一致,dijkstra,每次判断下什么时候可以走,就完事了

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e4+;
const ll mod = 1e9 + ;
#define afdafafafdafaf y1;
int ar[maxn], n, m; char ch[maxn][];
vector<pair<int,int> > g[maxn];
int main()
{
int s, t;
scanf("%d%d", &s, &t);
s--;
t--;
scanf("%d%d", &n, &m);
//cout << n << ' ' << m << '\n';
vector<int> remain(n, ), am(n, ), bm(n, ), vis(n, ), pre(n, -), dis(n, -);
for(int i=; i<n; i++){
scanf("%s", ch[i]);
scanf("%d%d%d", &remain[i], &am[i], &bm[i]);
//cout << ch[i] << ' ' << remain[i] << ' ' << am[i] << ' ' << bm[i] << '\n';
}
for(int i=; i<m; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
a--;
b--;
//cout << a << ' ' << b << ' ' << c << '\n';
g[a].push_back(make_pair(b, c));
g[b].push_back(make_pair(a, c));
}
auto next_time = [&] (int i, int time, vector<int> & ans){
int re = ;
ans.resize();
if(ch[i][] == 'B'){
re = (am[i] - remain[i] + time) % (am[i] + bm[i]);
}
else{
re = (bm[i] - remain[i] + am[i] + time) % (am[i] + bm[i]);
}
int k = ;
if(re < am[i]){
ans.push_back(am[i] - re);
for(int index = ; index < k; index++){
//cout << "index = " << index << '\n';
ans.push_back(bm[i]);
ans.push_back(am[i]);
}
}
else{
ans.push_back(am[i] + bm[i] - re);
for(int index = ; index < k; index++){
//cout << "index = " << index << '\n';
ans.push_back(am[i]);
ans.push_back(bm[i]);
}
}
return re < am[i];
};
priority_queue<pair<int,int>> qu;
qu.push(make_pair(, s));
int ans_time = , ans_flag = ;
dis[s] = ;
for(; !qu.empty(); ){
int u = qu.top().second;
int now_time = -qu.top().first;
qu.pop();
if(vis[u])continue;
vis[u] = ;
if(u == t){
ans_time = now_time;
ans_flag = ;
break;
}
//cout << "u = " << u << ' ' << u << ' ' << now_time << '\n';
for(int in = ; in < g[u].size(); in++){
int v = g[u][in].first, time = g[u][in].second;
if(vis[v])continue;
//cout << "v = " << v << '\n';
vector<int> v_u, v_v;
bool time_u = next_time(u, now_time, v_u);
bool time_v = next_time(v, now_time, v_v);
//for(auto x : v_u)cout << x << ' '; cout << '\n';
//for(auto x : v_v)cout << x << ' '; cout << '\n';
int ins = time_u == time_v;
int cost = , i = , j = , x = , y = , flag = ;
for(;;){
//cout << i << ' ' << j << '\n';
if(x == ){
if(i < v_u.size()){
x = v_u[i++];
}
else{
break;
}
ins ^= ;
}
if(y == ){
if(j < v_v.size()){
y = v_v[j++];
}
else{
break;
}
ins ^= ;
}
int mn = min(x, y);
if(ins){
cost += time;
flag = ;
break;
}
x -= mn;
y -= mn;
cost += mn;
}
//cout << "mid\n";
if(flag && (dis[v] == - || dis[v] > now_time + cost)){
qu.push(make_pair(-now_time - cost, v));
dis[v] = now_time + cost;
pre[v] = u;
}
}
}
if(!ans_flag){
printf("0\n");
return ;
}
vector<int> ans;
int x = t;
for(;;){
ans.push_back(x);
if(x == s)break;
x = pre[x];
}
reverse(ans.begin(), ans.end());
printf("%d\n", ans_time);
for(int i = ; i < ans.size(); i++){
printf("%d%c", ans[i] + , i == ans.size() - ? '\n' : ' ');
}
return ;
}

acmsguru 104 - Little Shop of Flowers

经典dp

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e2+;
const ll mod = 1e9 + ;
#define afdafafafdafaf y1;
int ar[maxn], n, m; int arr[maxn][maxn], dp[maxn][maxn];
int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++)scanf("%d", &arr[i][j]);
}
int mn = -5e6;
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++)dp[i][j] = mn;
}
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
int mx = arr[i][j];
if(dp[i - ][j - ] != mn)dp[i][j] = max(dp[i][j], dp[i-][j-] + mx);
if(dp[i][j-] != mn)dp[i][j] = max(dp[i][j], dp[i][j-]);
}
}
/*
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++)cout << dp[i][j] << ' ';
cout << "\n";
}
*/
vector<int> ans;
int j = m;
for(int i = n; i >= ; i--){
for(;;){
if(dp[i][j] > dp[i][j-]){
ans.push_back(j);
j--;
break;
}
j--;
}
}
reverse(ans.begin(), ans.end());
printf("%d\n", dp[n][m]);
for(int i = ; i < ans.size(); i++){
printf("%d%c", ans[i], i == ans.size() - ? '\n' : ' ');
}
return ;
}

acmsguru 105 - Div 3

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+;
const ll mod = 1e9 + ;
#define afdafafafdafaf y1;
int ar[maxn], n; int main()
{
ll n;
while(scanf("%lld", &n) != EOF){
ll ans = n / * ;
ans += (n % ) == ;
printf("%lld\n", ans);
}
return ;
}

acmsguru  106 - The Equation

debug了无数次,坑比较多,慢慢填

除数不要为0,逻辑太复杂

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+;
const ll mod = 1e9 + ;
#define afdafafafdafaf y1;
int ar[maxn], n; void exgcd(ll a,ll b,ll& d,ll &x,ll &y)
{
if(!b){
d=a,x=,y=;
}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
int main()
{
int aa,bb,cc,xx1,xx2,yy1,yy2;
scanf("%d%d%d", &aa, &bb, &cc);
scanf("%d%d", &xx1, &xx2);
scanf("%d%d", &yy1, &yy2);
double a = aa,b = bb, c = cc, x1 = xx1, x2 = xx2, y1 = yy1, y2 = yy2;
double xy1, xy2;
assert(xx1 <= xx2);
assert(xx1 <= xx2);
ll ans = ;
if(aa == && bb == ){
if(cc == ){
ans = 1LL * (yy2 - yy1 + ) * (xx2 - xx1 + );
}
else{
ans = ;
}
}
else if(aa == ){
if(cc % bb == && - cc / bb <= y2 && - cc / bb >= y1)ans = x2 - x1 + ;
else ans = ;
}
else if(b == ){
if(cc % aa == && - cc / aa <= x2 && - cc / aa >= x1)ans = y2 - y1 + ;
else ans = ;
}
else{
ll dd, xx, yy;
exgcd(aa, bb, dd, xx, yy);
dd = abs(dd);
if(cc % dd == ){
if(aa < )dd *= -;
aa /= dd;
bb /= dd;
cc /= dd;
a = aa;
b = bb;
c = cc;
xx *= -cc;
yy *= -cc;
//cout << aa << ' ' << bb << ' ' << cc << '\n';
//cout << xx << ' ' << yy << '\n';
assert(aa * xx + bb * yy + cc == );
xy1 = (-c - a * x1) / b;
xy2 = (-c - a * x2) / b;
y1 = max(y1, min(xy1, xy2));
y2 = min(y2, max(xy1, xy2));
//cout<<y1<<" "<<y2<<"\n";
y1 = ceil((y1 - yy) / aa);
y2 = floor((y2 - yy) / aa);
ans = y2 - y1 + ;
ans = max(ans, 0LL);
}
else{
ans = ;
}
}
printf("%lld\n", ans);
return ;
}

107 - 987654321 problem

有8个数的平方符合规则

x                x * x

所以枚举就完事了

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+;
const ll mod = 1e9 + ;
#define afdafafafdafaf y1;
int ar[maxn], n; void exgcd(ll a,ll b,ll& d,ll &x,ll &y)
{
if(!b){
d=a,x=,y=;
}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
vector<int> v;
ll s = ;
void dfs(ll x, ll last, ll pre, ll base = ){
if(x == ){
//cout << "s = " << pre << " " << pre * pre << "\n";
return ;
}
if(x <= ){
return ;
}
for(int i=;i<;i++){
ll mid = i * pre * + i * i * base + last;
if(mid % == x % ){
dfs(x / , mid / , pre + base * i, base * );
}
}
}
int main()
{
ll x = ;
//dfs(x, 0, 0, 1);
scanf("%lld\n", &x);
if(x < ){
printf("0\n");
}
else if(x == )printf("8\n");
else{
printf("");
for(int i = ; i < x - ; i++)printf("");
printf("\n");
}
return ;
}

108 - Self-numbers II

暴力

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e7+;
const int maxm = 5e3+;
const ll mod = 1e9 + ;
#define afdafafafdafaf y1;
int ar[], n, m;
bool is[maxn];
int check(int x){
int pre = x;
int ins = ;
while(x > ){
ins += x % ;
x /= ;
}
return ins + pre;
}
pair<int, int> p[maxm];
int main()
{
scanf("%d%d", &n, &m);
for(int i=;i<=m;i++){
scanf("%d", &p[i].first);
p[i].second = i;
}
sort(p+, p+m+);
int ins = , pre = ;
ll ans = ;
for(int i=;i<=n;i++){
if(i % != )pre += ;
else pre = check(i);
if(pre < maxn)is[pre] = ;
if(!is[i]){
ans++;
while(ins <=m && p[ins].first == ans){
ar[p[ins].second] = i;
ins++;
}
}
}
printf("%lld\n", ans);
for(int i=;i<=m;i++){
printf("%d%c", ar[i], i == m ? '\n' : ' ');
}
return ;
}

109 -

看不懂

110 -

疑似题意:给你n个球体和一条射线,射线到达球体表面会反射;问射线折射到达的前10个球体ID,不足十个全部输出

窝太菜了

acmsguru 552 - Database Optimization

充分利用STL即可

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+;
const ll mod = ;
const int sz = ;
#define afdafafafdafaf y1;
int n,m;
int ar[maxn];
map<string, int> mp;
int ins=;
map<vector<int>, int> ms;
int decode(string s){
if(mp.count(s) == ){
mp[s] = ins++;
}
return mp[s];
}
vector<int> res, in;
void dfs(int x){
if(x==in.size()){
if(res.size() == )return ;
vector<int> mid = res;
sort(res.begin(), res.end());
ms[res]++;
res = mid;
}
else{
dfs(x+);
res.push_back(in[x]);
dfs(x+);
res.pop_back();
}
}
char ch[maxn];
int main()
{
scanf("%d", &n);
for(int i=; i<=n; i++){
int k;scanf("%d", &k);
in.resize();
while(k--){
scanf("%s", ch);
in.push_back(decode(string(ch)));
}
dfs();
assert(res.size() == );
}
scanf("%d", &m);
for(int i=; i<=m; i++){
int k;scanf("%d", &k);
in.resize();
while(k--){
scanf("%s", ch);
in.push_back(decode(string(ch)));
}
sort(in.begin(), in.end());
printf("%d\n", ms[in]);
}
return ;
}

acmsguru的更多相关文章

  1. SGU---107 水题

    题目链接: http://codeforces.com/problemsets/acmsguru/problem/99999/107 题目大意: 求n位数有多少个的平方末尾是987654321 思路: ...

  2. The equation SGU - 106

    题目链接:https://codeforces.com/problemsets/acmsguru/problem/99999/106 这个题是关于EXGCD特别好的一个题目.题目大意:有一个等式ax+ ...

  3. SGU140. Integer Sequences

    https://codeforces.com/problemsets/acmsguru/problem/99999/140 n元同余方程的求解 对于任意二元我们可以替换成kgcd(a,b),不断迭代下 ...

随机推荐

  1. Python中很少见的用法

    print(*range(10)) # 自动解开可迭代的对象

  2. hive 源码笔记(1):命令行执行的主流程。

    1. 'hive'命令是(默认为hive跟目录)./bin下的一个shell脚本,依次加载 ./bin/hive-config.sh, ./conf/hive-env.sh, 设置与hadoop.sp ...

  3. JDK压缩指针

    https://www.cnblogs.com/iceAeterNa/p/4877549.html

  4. CentOS linux7 重置root密码

    1.启动linux,进入grub,就是选择系统界面,选中系统按e进入编辑界面 2.找到linux16那一行,在末尾加上init=/bin/sh.按Ctrl+x,使用单用户模式启动4.mount -o ...

  5. OGG-01201

    OGG-01201 Table of Contents 1. OGG-01201 1.1. 案例1 1.2. 案例2 1 OGG-01201 这种错误,出现的场景之一是 direct load 加载数 ...

  6. NDK的环境配置

    http://www.androiddevtools.cn/ 下载NDK, 最新版本. 解压压缩包,如解压后文件夹名为如android-ndk-r13,放在指定的位置 配置环境变量: 系统环境path ...

  7. mysql|full join 多表联查,系统报错,无法解答!

    查询语句: select 分数 from cfull join don c.姓名=d.姓名 报错: [Err] 1054 - Unknown column 'c.姓名' in 'on clause' ...

  8. Spring学习之==>IoC

    一.概述 Spring的三大核心思想:IoC(控制反转),DI(依赖注入),AOP(面向切面编程).本问讲着重介绍一下控制反转. 何谓控制反转:Spring 通过一种称作控制反转(IoC)的技术促进了 ...

  9. SqlServer:SqlServer(xpath,字段约束,时间查询,数据文件查询及还原,压缩备份)

    1.xpath --1.文档 select @data --2.任意级别是否存在price节点 select @data.exist('//price') --3.获取所有book节点 select ...

  10. ubuntu下自动获取ip设置

    vi /etc/network/interfaces文件为如下内容 wq保存 重启网卡:sudo /etc/init.d/networking restart