problem 1042. Flower Planting With No Adjacent 参考 1. Leetcode_easy_1042. Flower Planting With No Adjacent; 完…
题目如下: You have N gardens, labelled 1 to N.  In each garden, you want to plant one of 4 types of flowers. paths[i] = [x, y] describes the existence of a bidirectional path from garden x to garden y. Also, there is no garden that has more than 3 paths…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 图 日期 题目地址:https://leetcode.com/problems/flower-planting-with-no-adjacent/ 题目描述 You have N gardens, labelled 1 to N. In each garden, you want to plant one of 4 types of flowers.…
题意: 本题题意为: 寻找一个花园的涂色方案,要求 1.花园和花园之间,不能有路径连接的,不能涂成相同颜色的 一共有4中颜色,花园和花园之间,至多有三条路径 我菜了 - - ,又没做出来.. 看答案 使用贪心: idea: 对每一个花园依次进行染色时,必定可以直接染色,因为相临接的最多最多就是三个,一共4中颜色,所以一定可以直接染色了 具体想法: 每次对节点染色时,新建一个colors数组,colors[j] , 代表节点i旁边临接的节点赋值的颜色是否有,如果有,就赋值为1 然后遍历,color…
1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3307  Solved: 2075[Submit][Status][Discuss] Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,…
http://www.lydsy.com/JudgeOnline/problem.php?id=1042 一开始写了个O(nv)的背包,果断tle... 看了题解,,好神..用了组合数学中的多重集合方案的容斥原理. 设$A_i$表示i超过d[i]的性质 则我们要求: $$| \overline{A_1} \cap \overline{A_2} \cap ... \cap \overline{A_n} |$$ 而我们可以根据容斥求出这个值,即: $$| \overline{A_1} \cap \o…
肯定是大叔,本来以为用加法做乘法,后来想想这样麻烦,还是可以使用乘法的,按位乘,ov可以看成不止一位,程序如下所示: #include <stdio.h> #define MAX 40000 int main() { unsigned int res[MAX]; int n; int ov, i, j, k; int pos; while (scanf("%d", &n) != EOF) { res[] = ; pos = ; ; i<=n; i++) { o…
problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完…
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binary Numbers; 完…
problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完…
problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完…
problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Order; 完…
problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecutive; 完…
problem 1037. Valid Boomerang 参考 1. Leetcode_easy_1037. Valid Boomerang; 完…
problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完…
problem 1047. Remove All Adjacent Duplicates In String 参考 1. Leetcode_easy_1047. Remove All Adjacent Duplicates In String; 完…
problem 1051. Height Checker solution class Solution { public: int heightChecker(vector<int>& heights) { vector<int> orders = heights; sort(orders.begin(), orders.end()); ; ; i<heights.size(); i++) { if(heights[i]!=orders[i]) res++; } r…
problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrings(string str1, string str2) { return (str1+str2==str2+str1) ? (str1.substr(, gcd(str1.size(), str2.size()))) : ""; } }; 参考 1. Leetcode_easy_1071…
problem 1154. Day of the Year solution class Solution { public: int dayOfYear(string date) { // 平年 闰年 ] = {, , , , , , , , , , , }; , )), m = stoi(date.substr(, )), d = stoi(date.substr()); && y % == && (y % != || y % == )) d++;//leap year…
problem 1170. Compare Strings by Frequency of the Smallest Character 参考 1. Leetcode_easy_1170. Compare Strings by Frequency of the Smallest Character; 完…
problem 1160. Find Words That Can Be Formed by Characters solution class Solution { public: int countCharacters(vector<string>& words, string chars) { ; unordered_map<char, int> charmap; for(auto ch:chars) charmap[ch]++; for(auto word:word…
problem 997. Find the Town Judge solution: class Solution { public: int findJudge(int N, vector<vector<int>>& trust) { vector<); ]]--; balance[t[]]++; } ; i<=N; i++) { ) return i; } ; } }; 参考 1. Leetcode_easy_997. Find the Town Judge…
problem 994. Rotting Oranges 参考 1. Leetcode_easy_994. Rotting Oranges; 完…
problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完…
problem 989. Add to Array-Form of Integer 参考 1. Leetcode_easy_989. Add to Array-Form of Integer; 完…
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) { vector<int> res; ; ==) sum +=a; for(auto query:queries) { ]]%== )…
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSquares(vector<int>& A) { for(auto &a:A) a *=a; sort(A.begin(), A.end()); return A; } }; 参考1. Leetcode_easy_977. Squares of a Sorted Array; 完…
problem 976. Largest Perimeter Triangle solution: class Solution { public: int largestPerimeter(vector<int>& A) { sort(A.begin(), A.end());//decrease. ; i>; i--) { ]+A[i-]) ]+A[i-]; } ; } }; 参考 1. Leetcode_easy_976. Largest Perimeter Triangle…
problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers(int x, int y, int bound) { unordered_set<int> tmp; ; a<bound; a*=x)// { ; a+b<=bound; b*=y) { tmp.insert(a+b);// ) break;// } ) break; } retur…
problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完…