USACO 之 Section 1.4 More Search Techniques (已解决)

Arithmetic Progressions:

/*
需要注意两个可以优化的地方:
1)预处理,找到all integers of the form p^2 + q^2
  (where p and q are non-negative integers)
2)判断以a为等差数列的第一个值,b为等差数列的公差时,会不会超过(p^2 + q^2)的上界

代码中相应位置做了说明。

*/

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #define HOME
  3 #define _CRTDBG_MAP_ALLOC
  4 #include <crtdbg.h>
  5
  6 /*
  7 ID: Jming
  8 PROG: ariprog
  9 LANG: C++
 10 */
 11 #include <iostream>
 12 #include <fstream>
 13 #include <sstream>
 14 #include <cstdlib>
 15 #include <cstdio>
 16 #include <cstddef>
 17 #include <iterator>
 18 #include <algorithm>
 19 #include <string>
 20 #include <locale>
 21 #include <cmath>
 22 #include <vector>
 23 #include <cstring>
 24 #include <map>
 25 #include <utility>
 26 #include <queue>
 27 #include <stack>
 28 #include <set>
 29 #include <functional>
 30 using namespace std;
 31 typedef pair<int, int> PII;
 32 typedef long long int64;
 33 const int INF = 0x3f3f3f3f;
 34 const int modPrime = 3046721;
 35 const double eps = 1e-9;
 36 const int MaxA = 250 * 250 + 250 * 250 + 10;
 37 const int MaxB = 10010;
 38
 39 int N, M;
 40
 41 priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pair_ans;
 42 bool isLegal[MaxA];
 43
 44 int limVal;
 45
 46
 47 void ini()
 48 {
 49     /*
 50     预处理,找到all integers of the form p^2 + q^2
 51     (where p and q are non-negative integers)
 52     */
 53     fill(isLegal, isLegal + MaxA + 1, false);
 54     int doubleVal[255];
 55     for (int i = 0; i <= M; ++i)
 56     {
 57         doubleVal[i] = i*i;
 58     }
 59     for (int i = 0; i <= M; ++i)
 60     {
 61         for (int j = 0; j <= M; ++j)
 62         {
 63             isLegal[doubleVal[i] + doubleVal[j]] = true;
 64         }
 65     }
 66
 67 }
 68
 69
 70 void Solve()
 71 {
 72     for (int a = 0; a <= limVal; ++a)
 73     {
 74         for (int b = 1; b <= limVal; ++b)
 75         {
 76             if (a + (N - 1)*b > limVal)
 77             {
 78                 /*
 79                 判断以a为等差数列的第一个值,b为等差数列的公差时,
 80                 会不会超过(p^2 + q^2)的上界
 81                 */
 82                 break;
 83             }
 84             int nowVal = a;
 85             int cnt = 0;
 86             while (true)
 87             {
 88                 if (isLegal[nowVal])
 89                 {
 90                     ++cnt;
 91                 }
 92                 else
 93                 {
 94                     break;
 95                 }
 96                 if (cnt == N)
 97                 {
 98                     pair<int, int> pII;
 99                     pII.first = b;
100                     pII.second = a;
101                     pair_ans.push(pII);
102                     break;
103                 }
104                 nowVal += b;
105             }
106         }
107     }
108 }
109
110
111 void printAns()
112 {
113     if (pair_ans.empty())
114     {
115         puts("NONE");
116     }
117     else
118     {
119         while (!pair_ans.empty())
120         {
121             printf("%d %d\n", pair_ans.top().second, pair_ans.top().first);
122             pair_ans.pop();
123         }
124     }
125 }
126
127 int main()
128 {
129 #ifdef HOME
130     freopen("in", "r", stdin);
131     //freopen("out", "w", stdout);
132 #endif
133
134     freopen("ariprog.in", "r", stdin);
135     freopen("ariprog.out", "w", stdout);
136
137     scanf("%d %d", &N, &M);
138     limVal = M*M + M*M;
139     ini();
140     Solve();
141     printAns();
142
143
144 #ifdef HOME
145     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
146     _CrtDumpMemoryLeaks();
147 #endif
148     return 0;
149 }

Mother‘s Milk:

/*
题意关键:
  FJ pours milk from one bucket to another until
  the second bucket is filled or the first bucket is empty.
  (每一次倒牛奶都遵守这个原则)

深搜,判重
*/

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #define HOME
  3 #define _CRTDBG_MAP_ALLOC
  4 #include <crtdbg.h>
  5 /*
  6 ID: Jming
  7 PROG: milk3
  8 LANG: C++
  9 */
 10 #include <iostream>
 11 #include <fstream>
 12 #include <sstream>
 13 #include <cstdlib>
 14 #include <cstdio>
 15 #include <cstddef>
 16 #include <iterator>
 17 #include <algorithm>
 18 #include <string>
 19 #include <locale>
 20 #include <cmath>
 21 #include <vector>
 22 #include <cstring>
 23 #include <map>
 24 #include <utility>
 25 #include <queue>
 26 #include <stack>
 27 #include <set>
 28 #include <functional>
 29 using namespace std;
 30 typedef pair<int, int> PII;
 31 typedef long long int64;
 32 const int INF = 0x3f3f3f3f;
 33 const int modPrime = 3046721;
 34 const double eps = 1e-9;
 35 const int MaxN = 25;
 36 const int MaxM = 10010;
 37
 38 int A, B, C;
 39 bool isVisited[25][25][25];
 40 set<int> ansSet;
 41
 42 void Solve(int a, int b, int c)
 43 {
 44     if (!isVisited[a][b][c])
 45     {
 46         isVisited[a][b][c] = true;
 47         if (0 == a)
 48         {
 49             ansSet.insert(c);
 50         }
 51
 52         // C->A
 53         if (c >= (A - a))
 54         {
 55             Solve(A, b, c - (A - a));
 56         }
 57         else
 58         {
 59             Solve(a + c, b, 0);
 60         }
 61
 62         // C->B
 63         if (c >= (B - b))
 64         {
 65             Solve(a, B, c - (B - b));
 66         }
 67         else
 68         {
 69             Solve(a, b + c, 0);
 70         }
 71
 72         // B->A
 73         if (b >= (A - a))
 74         {
 75             Solve(A, b - (A - a), c);
 76         }
 77         else
 78         {
 79             Solve(a + b, 0, c);
 80         }
 81
 82         // B->C
 83         if (b >= (C - c))
 84         {
 85             Solve(a, b - (C - c), C);
 86         }
 87         else
 88         {
 89             Solve(a, 0, c + b);
 90         }
 91
 92         // A->B
 93         if (a >= (B - b))
 94         {
 95             Solve(a - (B - b), B, c);
 96         }
 97         else
 98         {
 99             Solve(0, b + a, c);
100         }
101
102         // A->C
103         if (a >= (C - c))
104         {
105             Solve(a - (C - c), b, C);
106         }
107         else
108         {
109             Solve(0, b, c + a);
110         }
111     }
112 }
113
114
115 int main()
116 {
117 #ifdef HOME
118     freopen("in", "r", stdin);
119     //freopen("out", "w", stdout);
120 #endif
121
122     freopen("milk3.in", "r", stdin);
123     freopen("milk3.out", "w", stdout);
124
125     scanf("%d %d %d", &A, &B, &C);
126     memset(isVisited, false, sizeof(isVisited));
127     Solve(0, 0, C);
128     for (set<int>::iterator it = ansSet.begin(); it != ansSet.end(); )
129     {
130         printf("%d", *it);
131         ++it;
132         if (it != ansSet.end())
133         {
134             printf(" ");
135         }
136     }
137     printf("\n");
138
139 #ifdef HOME
140     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
141     _CrtDumpMemoryLeaks();
142 #endif
143     return 0;
144 }

 

时间: 2024-08-01 10:32:11

USACO 之 Section 1.4 More Search Techniques (已解决)的相关文章

USACO Training Section 3.1 Contact

P2724 联系 Contact 题目背景 奶牛们开始对用射电望远镜扫描牧场外的宇宙感兴趣.最近,他们注意到了一种非常奇怪的脉冲调制微波从星系的中央发射出来.他们希望知道电波是否是被某些地外生命发射出来的,还是仅仅是普通的的星星发出的 题目描述 帮助奶牛们用一个能够分析他们在文件中记下的记录的工具来找到真相.他们在寻找长度在A到B之间(包含A和B本身)在每天的数据文件中重复得最多的比特序列 (1 <= A <= B <= 12).他们在找那些重复得最多的比特序列.一个输入限制告诉你应输出

USACO 之 Section 2.1 (已解决)

The Castle: /* 搜索 1A*/ 1 /* 2 ID: Jming 3 PROG: castle 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <ite

LuoGu 1200 你的飞碟在这儿 &amp;&amp; USACO Training Section 1.1_1

描述 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支持者.因此,他们要用 一种聪明的方案让这些小组提前知道谁会被彗星带走.他们为每个彗星起了一个名字,通过这些名字来决定这个小组是不是被带走的那个特定的小组(你认为是谁给 这些彗星取的名字呢?).关于如何搭配的细节会在下面告诉你:你的任务是写一个程序,通过小组名和彗星名来决定这个小组是否能被那颗彗星后面的UFO带 走. 小组名和彗星名都以下列方式转换成一个数字:最终的数字

USACO 之 Section 1.5 (已解决)

Number Triangles: /* DP 题 dp[j] = max(dp[j - 1] + tgl[i-1][j], dp[j] + tgl[i-1][j]); */ 1 #define _CRT_SECURE_NO_WARNINGS 2 #define HOME 3 #define _CRTDBG_MAP_ALLOC 4 #include <crtdbg.h> 5 6 /* 7 ID: Jming 8 PROG: numtri 9 LANG: C++ 10 */ 11 #includ

LuoGu 1201 贪婪的送礼者 &amp;&amp; USACO Training Section 1.1_2

描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人. 然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱. 给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表, 请确定每个人收到的比送出的钱多的数目. 格式 PROGRAM NAME: gift1 INPUT FORMAT: (f

LuoGu 1202 黑色星期五 &amp;&amp; USACO Training Section 1.1_3

描述 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的 一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400. 注意,开始今年是一千九百年,不是1990 这里有一些你要知道的: 1.1900年1月1日是星期一. 2.4,6,11和9月有30天.其他月份除了2月都有31天.闰年2月有29天,平年2月有28天. 3.年份可以被4整除的为闰年(

最小生成树基础模板题(USACO Training Section 3.1 最短网络 Agri-Net)

农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场.为了用最小的消费,他想铺设最短的光纤去连接所有的农场. 你将得到一份各农场之间连接费用的列表,你必须找出能连接所有农场并所用光纤最短的方案.每两个农场间的距离不会超过100000 输入格式: 第一行: 农场的个数,N(3<=N<=100). 第二行..结尾: 后来的行包含了一个N*N的矩阵,表示每个农场之间的

USACO Training Section 3.3 Shopping Offers

拿给出的每种方案作为一种物品其他的单卖的物品也作为一种物品 拿它们去跑背包就行 注意编号对应上就行 代码: #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cctype> #include <cstdio> #include <locale> #include <map> using

[LeetCode] 35. Search Insert Position 解决思路

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →